Skip to content

Commit

Permalink
Merge pull request #540 from infinnovation-dev/main2unit36
Browse files Browse the repository at this point in the history
Convert __main__ tests into unit tests (bis)
  • Loading branch information
asolino authored Dec 7, 2018
2 parents f34153f + 0041cdd commit a43e434
Show file tree
Hide file tree
Showing 15 changed files with 878 additions and 669 deletions.
29 changes: 0 additions & 29 deletions impacket/IP6_Address.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,32 +247,3 @@ def is_a_valid_text_representation(cls, text_representation):

def __is_a_scoped_address(self, text_representation):
return text_representation.count(self.SCOPE_SEPARATOR) == 1

#############################################################################################################
# Informal tests
if __name__ == '__main__':
print((IP6_Address("A:B:C:D:E:F:1:2").as_string()))
# print IP6_Address("A:B:C:D:E:F:0:2").as_bytes()
print((IP6_Address("A:B:0:D:E:F:0:2").as_string()))
# print IP6_Address("A::BC:E:D").as_string(False)
print((IP6_Address("A::BC:E:D").as_string()))
print((IP6_Address("A::BCD:EFFF:D").as_string()))
print((IP6_Address("FE80:0000:0000:0000:020C:29FF:FE26:E251").as_string()))

# print IP6_Address("A::BCD:EFFF:D").as_bytes()
print((IP6_Address("::").as_string()))
print((IP6_Address("1::").as_string()))
print((IP6_Address("::2").as_string()))
# bin = [
# 0x01, 0x02, 0x03, 0x04,
# 0x01, 0x02, 0x03, 0x04,
# 0x01, 0x02, 0x03, 0x04,
# 0x01, 0x02, 0x03, 0x04]
# a = IP6_Address(bin)
# print a.as_string()
# print a

# Malformed addresses
# print IP6_Address("ABCD:EFAB:1234:1234:1234:1234:1234:12345").as_string()
# print IP6_Address(":::").as_string()
# print IP6_Address("::::").as_string()
123 changes: 0 additions & 123 deletions impacket/crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,126 +344,3 @@ def SamEncryptNTLMHash(encryptedHash, key):
plain2 = Crypt2.encrypt(Block2)

return plain1 + plain2



if __name__ == '__main__':
# Test Vectors
# --------------------------------------------------
# Subkey Generation
# K 2b7e1516 28aed2a6 abf71588 09cf4f3c
# AES-128(key,0) 7df76b0c 1ab899b3 3e42f047 b91b546f
# K1 fbeed618 35713366 7c85e08f 7236a8de
# K2 f7ddac30 6ae266cc f90bc11e e46d513b
# --------------------------------------------------
#
# --------------------------------------------------
# Example 1: len = 0
# M <empty string>
# AES-CMAC bb1d6929 e9593728 7fa37d12 9b756746
# --------------------------------------------------
#
# Example 2: len = 16
# M 6bc1bee2 2e409f96 e93d7e11 7393172a
# AES-CMAC 070a16b4 6b4d4144 f79bdd9d d04a287c
# --------------------------------------------------
#
# Example 3: len = 40
# M 6bc1bee2 2e409f96 e93d7e11 7393172a
# ae2d8a57 1e03ac9c 9eb76fac 45af8e51
# 30c81c46 a35ce411
# AES-CMAC dfa66747 de9ae630 30ca3261 1497c827
# --------------------------------------------------
#
# Example 4: len = 64
# M 6bc1bee2 2e409f96 e93d7e11 7393172a
# ae2d8a57 1e03ac9c 9eb76fac 45af8e51
# 30c81c46 a35ce411 e5fbc119 1a0a52ef
# f69f2445 df4f9b17 ad2b417b e66c3710
# AES-CMAC 51f0bebf 7e3b9d92 fc497417 79363cfe
# --------------------------------------------------
def pp(prev ,s):
print (prev, end= ' ')
for i in range((len(s)//8)):
print("%s" % (s[:8]), end = ' ')
s = s[8:]

print()
return ''

from binascii import hexlify, unhexlify

K = b'2b7e151628aed2a6abf7158809cf4f3c'
M = b'6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710'

K1, K2 = Generate_Subkey(unhexlify(K))
print("Subkey Generation")
pp("K ", (K))
pp("K1 ", (hexlify(K1)))
pp("K2 ", (hexlify(K2)))
print()
print("Example 1: len = 0")
print("M <empty string>")
pp("AES-CMAC " , (hexlify(AES_CMAC(unhexlify(K),unhexlify(M),0))))
print()
print("Example 2: len = 16")
pp("M " , (M[:16*2]))
pp("AES-CMAC " , (hexlify(AES_CMAC(unhexlify(K),unhexlify(M),16))))
print()
print("Example 3: len = 40")
pp("M " , (M[:40*2]))
pp("AES-CMAC " , (hexlify(AES_CMAC(unhexlify(K),unhexlify(M),40))))
print()
print("Example 3: len = 64")
pp("M " , (M[:64*2]))
pp("AES-CMAC " , (hexlify(AES_CMAC(unhexlify(K),unhexlify(M),64))))
print()
M = "eeab9ac8fb19cb012849536168b5d6c7a5e6c5b2fcdc32bc29b0e3654078a5129f6be2562046766f93eebf146b"
K = "6c3473624099e17ff3a39ff6bdf6cc38"
# Mac = dbf63fd93c4296609e2d66bf79251cb5
print("Example 4: len = 45")
pp("M " , (M[:45*2]))
pp("AES-CMAC " , (hexlify(AES_CMAC(unhexlify(K),unhexlify(M),45))))

# ------------------------------------------------------------
#
# Test Case AES-CMAC-PRF-128 with 20-octet input
# Key : 00010203 04050607 08090a0b 0c0d0e0f edcb
# Key Length : 18
# Message : 00010203 04050607 08090a0b 0c0d0e0f 10111213
# PRF Output : 84a348a4 a45d235b abfffc0d 2b4da09a
#
# Test Case AES-CMAC-PRF-128 with 20-octet input
# Key : 00010203 04050607 08090a0b 0c0d0e0f
# Key Length : 16
# Message : 00010203 04050607 08090a0b 0c0d0e0f 10111213
# PRF Output : 980ae87b 5f4c9c52 14f5b6a8 455e4c2d
#
# Test Case AES-CMAC-PRF-128 with 20-octet input
# Key : 00010203 04050607 0809
# Key Length : 10
# Message : 00010203 04050607 08090a0b 0c0d0e0f 10111213
# PRF Output : 290d9e11 2edb09ee 141fcf64 c0b72f3d
#
# ------------------------------------------------------------

K = b"000102030405060708090a0b0c0d0e0fedcb"
M = b"000102030405060708090a0b0c0d0e0f10111213"

print("AES-CMAC-PRF-128 Test Vectors")
print()
print("Example 1: len = 0")
pp("M " , (K))
print("Key Length 18 ")
pp("AES-CMAC " , (hexlify(AES_CMAC_PRF_128(unhexlify(K),unhexlify(M),18,len(unhexlify(M))))))
print()
print("Example 1: len = 0")
pp("M " , (K))
print("Key Length 16 ")
pp("AES-CMAC " , (hexlify(AES_CMAC_PRF_128(unhexlify(K)[:16],unhexlify(M),16,len(unhexlify(M))))))
print()
print("Example 1: len = 0")
pp("M " , (K))
print("Key Length 10 ")
pp("AES-CMAC " , (hexlify(AES_CMAC_PRF_128(unhexlify(K)[:10],unhexlify(M),10,len(unhexlify(M))))))
print()
132 changes: 0 additions & 132 deletions impacket/dcerpc/v5/ndr.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
from six import with_metaclass, PY3

from impacket import LOG
from impacket.structure import hexdump
from impacket.dcerpc.v5.enum import Enum
from impacket.uuid import uuidtup_to_bin

Expand Down Expand Up @@ -1751,134 +1750,3 @@ class UNKNOWNDATA(NDR):
structure = (
('Data', ':'),
)

################################################################################
# Tests

class NDRTest:
def create(self,data = None, isNDR64 = False):
if data is not None:
return self.theClass(data, isNDR64 = isNDR64)
else:
return self.theClass(isNDR64 = isNDR64)

def test(self, isNDR64 = False):
print()
print("-"*70)
testName = self.__class__.__name__
print("starting test: %s (NDR64 = %s)....." % (testName, isNDR64))
a = self.create(isNDR64 = isNDR64)
self.populate(a)
a.dumpRaw("packing.....")
print()
a_str = a.getData()
print("packed:")
hexdump(a_str)
print("unpacking.....")
b = self.create(a_str, isNDR64 = isNDR64)
b.dump("unpacked.....")
print("\nrepacking.....")
b_str = b.getData()
if b_str != a_str:
print("ERROR: original packed and repacked don't match")
print("packed: ")
hexdump(b_str)
raise Exception('repack and pack don\'t match')

def run(self):
self.test(False)
# Now the same tests but with NDR64
self.test(True)

class TestUniFixedArray(NDRTest):
class theClass(NDRSTRUCT):
structure = (
('Array', NDRUniFixedArray),
)
def populate(self, a):
a['Array'] = b'12345678'

class TestStructWithPad(NDRTest):
class theClass(NDRSTRUCT):
structure = (
('long', NDRLONG),
('short', NDRSHORT),
)
def populate(self, a):
a['long'] = 0xaa
a['short'] = 0xbb

#class TestUniConformantArray(NDRTest):
# class theClass(NDRCall):
# structure = (
# ('Array', PNDRUniConformantArray),
# ('Array2', PNDRUniConformantArray),
# )
# def __init__(self, data = None,isNDR64 = False):
# NDRCall.__init__(self, None, isNDR64)
# self.fields['Array'].fields['Data'].item = RPC_UNICODE_STRING
# self.fields['Array2'].fields['Data'].item = RPC_UNICODE_STRING
# if data is not None:
# self.fromString(data)
#
# def populate(self, a):
# array = []
# strstr = RPC_UNICODE_STRING()
# strstr['Data'] = 'ThisIsMe'
# array.append(strstr)
# strstr = RPC_UNICODE_STRING()
# strstr['Data'] = 'ThisIsYou'
# array.append(strstr)
# a['Array'] = array
# a['Array2'] = array

class TestUniVaryingArray(NDRTest):
class theClass(NDRSTRUCT):
structure = (
('Array', NDRUniVaryingArray),
)
def populate(self, a):
a['Array'] = b'12345678'

class TestUniConformantVaryingArray(NDRTest):
class theClass(NDRSTRUCT):
structure = (
('Array', NDRUniConformantVaryingArray),
)
def populate(self, a):
a['Array'] = b'12345678'

class TestVaryingString(NDRTest):
class theClass(NDRSTRUCT):
structure = (
('Array', NDRVaryingString),
)
def populate(self, a):
a['Array'] = b'12345678'

class TestConformantVaryingString(NDRTest):
class theClass(NDRSTRUCT):
structure = (
('Array', NDRConformantVaryingString),
)

def populate(self, a):
a['Array'] = b'12345678'

class TestPointerNULL(NDRTest):
class theClass(NDRSTRUCT):
structure = (
('Array', NDRPOINTERNULL),
)
def populate(self, a):
pass

if __name__ == '__main__':
TestUniFixedArray().run()
#TestUniConformantArray().run()
TestUniVaryingArray().run()
TestUniConformantVaryingArray().run()
TestVaryingString().run()
TestConformantVaryingString().run()
TestPointerNULL().run()
TestStructWithPad().run()
46 changes: 0 additions & 46 deletions impacket/dns.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,49 +570,3 @@ def is_edns0(self):
if response_type == DNSType.OPT:
return True
return False

if __name__ == "__main__":
pkts = [
b"\x6a\x8c\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x03\x77\x77\x77"
b"\x05\x74\x61\x72\x74\x61\x03\x63\x6f\x6d\x00\x00\x01\x00\x01",
b"\x6a\x8c\x81\x80\x00\x01\x00\x02\x00\x02\x00\x00\x03\x77\x77\x77"
b"\x05\x74\x61\x72\x74\x61\x03\x63\x6f\x6d\x00\x00\x01\x00\x01\xc0"
b"\x0c\x00\x05\x00\x01\x00\x00\x07\x08\x00\x02\xc0\x10\xc0\x10\x00"
b"\x01\x00\x01\x00\x00\x07\x08\x00\x04\x45\x59\x1f\xc7\xc0\x10\x00"
b"\x02\x00\x01\x00\x02\xa3\x00\x00\x0f\x03\x6e\x73\x31\x08\x62\x6c"
b"\x75\x65\x68\x6f\x73\x74\xc0\x16\xc0\x10\x00\x02\x00\x01\x00\x02"
b"\xa3\x00\x00\x06\x03\x6e\x73\x32\xc0\x4d",
b"\x82\x75\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x03\x77\x77\x77"
b"\x04\x6a\x68\x6f\x6e\x03\x63\x6f\x6d\x00\x00\x01\x00\x01",
b"\x82\x75\x81\x80\x00\x01\x00\x01\x00\x02\x00\x02\x03\x77\x77\x77"
b"\x04\x6a\x68\x6f\x6e\x03\x63\x6f\x6d\x00\x00\x01\x00\x01\xc0\x0c"
b"\x00\x01\x00\x01\x00\x00\x00\x05\x00\x04\xd1\x3b\xc3\x14\xc0\x10"
b"\x00\x02\x00\x01\x00\x00\x06\xf8\x00\x0f\x03\x6e\x73\x31\x08\x74"
b"\x72\x61\x66\x66\x69\x63\x7a\xc0\x15\xc0\x10\x00\x02\x00\x01\x00"
b"\x00\x06\xf8\x00\x06\x03\x6e\x73\x32\xc0\x3e\xc0\x3a\x00\x01\x00"
b"\x01\x00\x00\x00\x0d\x00\x04\xd1\x3b\xc2\xf6\xc0\x55\x00\x01\x00"
b"\x01\x00\x00\x00\x85\x00\x04\xd1\x3b\xc3\xf6",
b"\xef\x55\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x04\x6d\x61\x69"
b"\x6c\x06\x67\x6f\x6f\x67\x6c\x65\x03\x63\x6f\x6d\x00\x00\x01\x00"
b"\x01",
b"\xef\x55\x81\x80\x00\x01\x00\x04\x00\x04\x00\x04\x04\x6d\x61\x69"
b"\x6c\x06\x67\x6f\x6f\x67\x6c\x65\x03\x63\x6f\x6d\x00\x00\x01\x00"
b"\x01\xc0\x0c\x00\x05\x00\x01\x00\x00\x06\x79\x00\x0f\x0a\x67\x6f"
b"\x6f\x67\x6c\x65\x6d\x61\x69\x6c\x01\x6c\xc0\x11\xc0\x2d\x00\x01"
b"\x00\x01\x00\x00\x00\x77\x00\x04\xd1\x55\xc3\x53\xc0\x2d\x00\x01"
b"\x00\x01\x00\x00\x00\x77\x00\x04\xd1\x55\xc3\x12\xc0\x2d\x00\x01"
b"\x00\x01\x00\x00\x00\x77\x00\x04\xd1\x55\xc3\x13\xc0\x11\x00\x02"
b"\x00\x01\x00\x00\x00\x5d\x00\x06\x03\x6e\x73\x33\xc0\x11\xc0\x11"
b"\x00\x02\x00\x01\x00\x00\x00\x5d\x00\x06\x03\x6e\x73\x34\xc0\x11"
b"\xc0\x11\x00\x02\x00\x01\x00\x00\x00\x5d\x00\x06\x03\x6e\x73\x31"
b"\xc0\x11\xc0\x11\x00\x02\x00\x01\x00\x00\x00\x5d\x00\x06\x03\x6e"
b"\x73\x32\xc0\x11\xc0\x9c\x00\x01\x00\x01\x00\x00\x04\x4e\x00\x04"
b"\xd8\xef\x20\x0a\xc0\xae\x00\x01\x00\x01\x00\x00\x06\x64\x00\x04"
b"\xd8\xef\x22\x0a\xc0\x78\x00\x01\x00\x01\x00\x00\x00\x05\x00\x04"
b"\xd8\xef\x24\x0a\xc0\x8a\x00\x01\x00\x01\x00\x00\x00\x08\x00\x04"
b"\xd8\xef\x26\x0a"
]

for pkt in pkts:
d = DNS(pkt)
print(d)
Loading

0 comments on commit a43e434

Please sign in to comment.