Browse Source

More tests

Johann Schmitz 1 year ago
parent
commit
336afef13c
1 changed files with 46 additions and 1 deletions
  1. 46
    1
      tests/test_format.py

+ 46
- 1
tests/test_format.py View File

@@ -5,6 +5,8 @@ from unittest import mock
5 5
 
6 6
 import binascii
7 7
 
8
+import pytest
9
+
8 10
 from peix.format import EixFileFormat
9 11
 
10 12
 
@@ -68,11 +70,23 @@ class TestFormat(object):
68 70
             assert eff.read_vector(eff.read_string) == []
69 71
         
70 72
         # vector with one element (3 bytes string)
73
+        b = b'\x01\x01\x01'
74
+        with mock.patch('peix.format.os', new=BytesMock(b + junk)):
75
+            eff = EixFileFormat()
76
+            assert eff.read_vector(eff.read_number) == [1]
77
+
78
+    def test_read_hash(self):
79
+        # empty vector
80
+        with mock.patch('peix.format.os', new=BytesMock(b'\x00' + junk)):
81
+            eff = EixFileFormat()
82
+            assert eff.read_vector(eff.read_string) == []
83
+
84
+        # vector with one element (3 bytes string)
71 85
         b = b'\x01\x03abc'
72 86
         with mock.patch('peix.format.os', new=BytesMock(b + junk)):
73 87
             eff = EixFileFormat()
74 88
             assert eff.read_vector(eff.read_string) == ['abc']
75
-    
89
+
76 90
     def test_read_hashed_string(self):
77 91
         l = ['foo', 'bar', 'baz']
78 92
         
@@ -80,3 +94,34 @@ class TestFormat(object):
80 94
         with mock.patch('peix.format.os', new=BytesMock(b + junk)):
81 95
             eff = EixFileFormat()
82 96
             assert eff.read_hashed_string(l) == 'bar'
97
+
98
+    def test_read_hashed_word(self):
99
+
100
+        # empty vector
101
+        b = b'\x00'
102
+        with mock.patch('peix.format.os', new=BytesMock(b + junk)):
103
+            eff = EixFileFormat()
104
+            assert eff.read_hashed_words(['foo', 'bar', 'baz']) == ''
105
+
106
+        # single item
107
+        b = b'\x01\x00'
108
+        with mock.patch('peix.format.os', new=BytesMock(b + junk)):
109
+            eff = EixFileFormat()
110
+            assert eff.read_hashed_words(['foo', 'bar', 'baz']) == 'foo'
111
+
112
+        # multiple items - should be concatenated with a whitespace
113
+        b = b'\x02\x00\x01'
114
+        with mock.patch('peix.format.os', new=BytesMock(b + junk)):
115
+            eff = EixFileFormat()
116
+            assert eff.read_hashed_words(['foo', 'bar', 'baz']) == 'foo bar'
117
+
118
+    def test_read_magic(self):
119
+
120
+        b = b'eix\n'
121
+        with mock.patch('peix.format.os', new=BytesMock(b + junk)):
122
+            EixFileFormat().read_magic()
123
+
124
+        b = b'foobar'
125
+        with mock.patch('peix.format.os', new=BytesMock(b + junk)):
126
+            with pytest.raises(AssertionError):
127
+                EixFileFormat().read_magic()