Helper scripts
This commit is contained in:
58
tests/test_drill_synsensor.py
Normal file
58
tests/test_drill_synsensor.py
Normal file
@@ -0,0 +1,58 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Offline unit tests for the drill injector builders (pure local)."""
|
||||
import importlib.util
|
||||
import struct
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
spec = importlib.util.spec_from_file_location(
|
||||
'drill_synsensor', Path(__file__).resolve().parent.parent
|
||||
/ 'tests' / 'drill_synsensor.py')
|
||||
ds = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(ds)
|
||||
|
||||
|
||||
def onesc(buf):
|
||||
if len(buf) % 2:
|
||||
buf += b'\x00'
|
||||
t = sum(struct.unpack('!%dH' % (len(buf) // 2), buf))
|
||||
t = (t & 0xFFFF) + (t >> 16)
|
||||
t = (t & 0xFFFF) + (t >> 16)
|
||||
return t
|
||||
|
||||
|
||||
class Builders(unittest.TestCase):
|
||||
def test_icmp_checksums(self):
|
||||
p = ds.icmp_packet('192.168.100.84', '192.0.2.44', 7,
|
||||
b'\x11\x22\x33\x44\x55\x66', b'\x66\x55\x44\x33\x22\x11')
|
||||
self.assertEqual(p[12:14], b'\x08\x00')
|
||||
ip = p[14:34]
|
||||
self.assertEqual(onesc(ip) & 0xFFFF, 0xFFFF) # hdr sum incl cksum
|
||||
icmp = p[34:]
|
||||
self.assertEqual(icmp[0], 8)
|
||||
z = icmp[:2] + b'\x00\x00' + icmp[4:]
|
||||
t = onesc(z)
|
||||
self.assertEqual((~t) & 0xFFFF, struct.unpack('!H', icmp[2:4])[0])
|
||||
|
||||
def test_syn_pseudo_checksum(self):
|
||||
p = ds.syn_packet('203.0.113.5', '192.0.2.5', 22, 3,
|
||||
b'\x00' * 6, b'\x00' * 6)
|
||||
ip = p[14:34]
|
||||
self.assertEqual(onesc(ip) & 0xFFFF, 0xFFFF)
|
||||
tcp = p[34:]
|
||||
self.assertEqual(tcp[13] & 0x02, 0x02) # SYN flag
|
||||
a, b = ip[12:16], ip[16:20]
|
||||
ph = a + b + struct.pack('!BBH', 0, 6, len(tcp))
|
||||
z = tcp[:16] + b'\x00\x00' + tcp[18:]
|
||||
t = onesc(ph + z)
|
||||
self.assertEqual((~t) & 0xFFFF, struct.unpack('!H', tcp[16:18])[0])
|
||||
|
||||
def test_testnet_guard(self):
|
||||
self.assertTrue(ds.in_testnet('192.0.2.5'))
|
||||
self.assertTrue(ds.in_testnet('203.0.113.12'))
|
||||
self.assertFalse(ds.in_testnet('172.16.0.1'))
|
||||
self.assertFalse(ds.in_testnet('192.168.100.84'))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user