1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
#!/usr/bin/env python
import pytest
import ssdeep
class TestFunctionsFail(object):
def test_compare(self):
with pytest.raises(TypeError):
ssdeep.compare(
"3:AXGBicFlgVNhBGcL6wCrFQEv:AXGHsNhxLsr2C",
None
)
with pytest.raises(TypeError):
ssdeep.compare(
None,
"3:AXGBicFlIHBGcL6wCrFQEv:AXGH6xLsr2C"
)
with pytest.raises(ssdeep.InternalError):
ssdeep.compare(
"3:AXGBicFlgVNhBGcL6wCrFQEv:AXGHsNhxLsr2C",
""
)
def test_hash(self):
with pytest.raises(TypeError):
ssdeep.hash(None)
with pytest.raises(TypeError):
ssdeep.hash(1234)
class TestFunctions(object):
def test_compare(self):
res = ssdeep.compare(
"3:AXGBicFlgVNhBGcL6wCrFQEv:AXGHsNhxLsr2C",
"3:AXGBicFlIHBGcL6wCrFQEv:AXGH6xLsr2C"
)
assert res == 22
res = ssdeep.compare(
b"3:AXGBicFlgVNhBGcL6wCrFQEv:AXGHsNhxLsr2C",
b"3:AXGBicFlIHBGcL6wCrFQEv:AXGH6xLsr2C"
)
assert res == 22
def test_hash_1(self):
res = ssdeep.hash("Also called fuzzy hashes, Ctph can match inputs that have homologies.")
assert res == "3:AXGBicFlgVNhBGcL6wCrFQEv:AXGHsNhxLsr2C"
def test_hash_2(self):
res = ssdeep.hash("Also called fuzzy hashes, CTPH can match inputs that have homologies.")
assert res == "3:AXGBicFlIHBGcL6wCrFQEv:AXGH6xLsr2C"
def test_hash_3(self):
res = ssdeep.hash(b"Also called fuzzy hashes, CTPH can match inputs that have homologies.")
assert res == "3:AXGBicFlIHBGcL6wCrFQEv:AXGH6xLsr2C"
def test_hash_from_file(self):
with pytest.raises(IOError):
ssdeep.hash_from_file("tests/files/")
with pytest.raises(IOError):
ssdeep.hash_from_file("tests/files/file-does-not-exist.txt")
res = ssdeep.hash_from_file("tests/files/file.txt")
assert res == "3:AXGBicFlgVNhBGcL6wCrFQE3:AXGHsNhxLsr2s"
class TestHashClass(object):
def test_copy(self):
obj = ssdeep.Hash()
obj.update("Also called fuzzy hashes, ")
new_obj = obj.copy()
assert isinstance(new_obj, ssdeep.Hash)
res = obj.digest()
new_res = new_obj.digest()
assert res == "3:AXGBicFlF:AXGHR"
assert new_res == "3:AXGBicFlF:AXGHR"
# Update only original object
obj.update("Ctph can match inputs that have homologies.")
res = obj.digest()
new_res = new_obj.digest()
assert res == "3:AXGBicFlgVNhBGcL6wCrFQEv:AXGHsNhxLsr2C"
assert new_res == "3:AXGBicFlF:AXGHR"
# Update only new object
new_obj.update("Ctph can match inputs that have homologies.")
res = obj.digest()
new_res = new_obj.digest()
assert res == "3:AXGBicFlgVNhBGcL6wCrFQEv:AXGHsNhxLsr2C"
assert new_res == "3:AXGBicFlgVNhBGcL6wCrFQEv:AXGHsNhxLsr2C"
def test_hashlib(self):
obj = ssdeep.Hash()
assert obj.name == "ssdeep"
obj.update("Ctph can match inputs that have homologies.")
assert obj.block_size == 3
def test_update(self):
obj = ssdeep.Hash()
obj.update("Also called fuzzy hashes, Ctph can match inputs that have homologies.")
res = obj.digest()
assert res == "3:AXGBicFlgVNhBGcL6wCrFQEv:AXGHsNhxLsr2C"
class TestPseudoHashClass(object):
def test_copy(self):
obj = ssdeep.PseudoHash()
obj.update("Also called fuzzy hashes, ")
new_obj = obj.copy()
assert isinstance(new_obj, ssdeep.PseudoHash)
res = obj.digest()
new_res = new_obj.digest()
assert res == "3:AXGBicFlF:AXGHR"
assert new_res == "3:AXGBicFlF:AXGHR"
# Update only original object
obj.update("Ctph can match inputs that have homologies.")
res = obj.digest()
new_res = new_obj.digest()
assert res == "3:AXGBicFlgVNhBGcL6wCrFQEv:AXGHsNhxLsr2C"
assert new_res == "3:AXGBicFlF:AXGHR"
# Update only new object
new_obj.update("Ctph can match inputs that have homologies.")
res = obj.digest()
new_res = new_obj.digest()
assert res == "3:AXGBicFlgVNhBGcL6wCrFQEv:AXGHsNhxLsr2C"
assert new_res == "3:AXGBicFlgVNhBGcL6wCrFQEv:AXGHsNhxLsr2C"
def test_hashlib(self):
obj = ssdeep.PseudoHash()
assert obj.name == "ssdeep"
obj.update("Ctph can match inputs that have homologies.")
assert obj.block_size == 3
def test_update(self):
obj = ssdeep.PseudoHash()
obj.update("Also called fuzzy hashes, ")
obj.update("Ctph can match inputs that have homologies.")
res = obj.digest()
assert res == "3:AXGBicFlgVNhBGcL6wCrFQEv:AXGHsNhxLsr2C"
class TestHashClassFail(object):
def test_update_01(self):
obj = ssdeep.Hash()
with pytest.raises(TypeError):
obj.update(None)
def test_update_02(self):
obj = ssdeep.Hash()
with pytest.raises(TypeError):
obj.update(1234)
class TestPseudoHashClassFail(object):
def test_update_01(self):
obj = ssdeep.PseudoHash()
with pytest.raises(TypeError):
obj.update(None)
def test_update_02(self):
obj = ssdeep.PseudoHash()
with pytest.raises(TypeError):
obj.update(1234)
|