forked from BobBuildTool/bob
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_utils_dirhasher.py
226 lines (184 loc) · 7.15 KB
/
test_utils_dirhasher.py
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# Bob build tool
# Copyright (C) 2016 Jan Klötzke
#
# SPDX-License-Identifier: GPL-3.0-or-later
from tempfile import NamedTemporaryFile, TemporaryDirectory
from unittest import TestCase
from unittest.mock import MagicMock, mock_open, patch
import binascii
import os
import sys
from bob.utils import hashFile, hashDirectory, binStat
class TestHashFile(TestCase):
def testBigFile(self):
with NamedTemporaryFile() as f:
for i in range(1000):
f.write(b'0123456789' * 1024)
f.flush()
hashFile(f.name) == binascii.unhexlify(
"c94d8ee379dcbef70b3da8fb57df8020b76b0c70")
def testMissingFile(self):
"""Missing files should be treated as empty"""
# assertLogs was introduced in python 3.4
if sys.version_info < (3, 4):
assert hashFile("does-not-exist") == binascii.unhexlify(
"da39a3ee5e6b4b0d3255bfef95601890afd80709")
else:
with self.assertLogs(level='WARNING') as cm:
assert hashFile("does-not-exist") == binascii.unhexlify(
"da39a3ee5e6b4b0d3255bfef95601890afd80709")
self.assertEqual(cm.records[0].msg, "Cannot hash file: %s")
class TestHashDir(TestCase):
def setUp(self):
self.umask = os.umask(0o022)
def tearDown(self):
os.umask(self.umask)
def testDirAndFile(self):
"""Test hashing a directory with one file.
The hash sum should stay stable in the long run as this might be used
for binary artifact matching in the future.
"""
with TemporaryDirectory() as tmp:
os.mkdir(os.path.join(tmp, "dir"))
with open(os.path.join(tmp, "dir", "file"), 'wb') as f:
f.write(b'abc')
sum1 = hashDirectory(tmp)
assert len(sum1) == 20
assert sum1 == binascii.unhexlify(
"640f516de78fba0b6d2ddde4451000f142d06b0d")
sum2 = hashDirectory(tmp)
assert sum1 == sum2
def testRenameDirectory(self):
"""Test that renaming directories has an influence on the checksum"""
with TemporaryDirectory() as tmp:
os.mkdir(os.path.join(tmp, "dir"))
with open(os.path.join(tmp, "dir", "file"), 'wb') as f:
f.write(b'abc')
sum1 = hashDirectory(tmp)
os.rename(os.path.join(tmp, "dir"), os.path.join(tmp, "foo"))
sum2 = hashDirectory(tmp)
assert sum1 != sum2
def testRenameFile(self):
"""Test that renaming files has an influence on the checksum"""
with TemporaryDirectory() as tmp:
with open(os.path.join(tmp, "foo"), 'wb') as f:
f.write(b'abc')
sum1 = hashDirectory(tmp)
os.rename(os.path.join(tmp, "foo"), os.path.join(tmp, "bar"))
sum2 = hashDirectory(tmp)
assert sum1 != sum2
def testRewriteFile(self):
"""Changing the file content should change the hash sum"""
with NamedTemporaryFile() as index:
with TemporaryDirectory() as tmp:
with open(os.path.join(tmp, "foo"), 'wb') as f:
f.write(b'abc')
sum1 = hashDirectory(tmp, index.name)
with open(index.name, "rb") as f:
assert f.read(4) == b'BOB1'
with open(os.path.join(tmp, "foo"), 'wb') as f:
f.write(b'qwer')
sum2 = hashDirectory(tmp, index.name)
with open(index.name, "rb") as f:
assert f.read(4) == b'BOB1'
assert sum1 != sum2
def testBigIno(self):
"""Test that index handles big inode numbers as found on Windows"""
s = MagicMock()
s.st_mode=33188
s.st_ino=15345198597064824875
s.st_dev=65027
s.st_nlink=1
s.st_uid=1000
s.st_gid=1000
s.st_size=3
s.st_atime_ns=1452798827
s.st_mtime_ns=1452798827
s.st_ctime_ns=1452798827
mock_lstat = MagicMock()
mock_lstat.return_value = s
with NamedTemporaryFile() as index:
with TemporaryDirectory() as tmp:
with open(os.path.join(tmp, "ghost"), 'wb') as f:
f.write(b'abc')
with patch('os.lstat', mock_lstat):
hashDirectory(tmp, index.name)
with open(index.name, "rb") as f:
assert f.read(4) == b'BOB1'
def testOldFile(self):
"""Test negative time fields for files from the past"""
s = MagicMock()
s.st_mode=33188
s.st_ino=270794
s.st_dev=65026
s.st_nlink=1
s.st_uid=1000
s.st_gid=1000
s.st_size=4
s.st_atime_ns=1601623698
s.st_mtime_ns=-3600
s.st_ctime_ns=1601623698
mock_lstat = MagicMock()
mock_lstat.return_value = s
with NamedTemporaryFile() as index:
with TemporaryDirectory() as tmp:
with open(os.path.join(tmp, "McFly"), 'wb') as f:
pass
with patch('os.lstat', mock_lstat):
h = hashDirectory(tmp, index.name)
with patch('os.stat', mock_lstat):
b = binStat("whatever")
self.assertEqual(h, b'\xdc\xe1\xf4\x02\x01\xc3\xa1\xf7j\xac\xbc\xbf=1ey\x11\x1a\xc8\xda')
self.assertEqual(type(b), bytes)
def testBlockDev(self):
"""Test that index handles block devices"""
s = MagicMock()
s.st_mode=25008
s.st_ino=8325
s.st_dev=6
s.st_nlink=1
s.st_uid=0
s.st_gid=6
s.st_rdev=2048
s.st_size=0
s.st_atime_ns=1453317243
s.st_mtime_ns=1451854748
s.st_ctime_ns=1451854748
mock_lstat = MagicMock()
mock_lstat.return_value = s
entry = MagicMock()
entry.is_dir = MagicMock(return_value=False)
entry.name = b'sda'
entry.stat = mock_lstat
entries = MagicMock()
entries.return_value = [ entry ]
with NamedTemporaryFile() as index:
with patch('os.scandir', entries):
h = hashDirectory("whatever", index.name)
self.assertEqual(h, b'\xe8\x8e\xad\x9bv\xcbt\xc4\xcd\xa7x\xdb\xde\x96\xab@\x18\xb1\xdcX')
def testChrDev(self):
"""Test that index handles character devices"""
s = MagicMock()
s.st_mode=8630
s.st_ino=8325
s.st_dev=6
s.st_nlink=1
s.st_uid=0
s.st_gid=6
s.st_rdev=1280
s.st_size=0
s.st_atime_ns=1453317243
s.st_mtime_ns=1451854748
s.st_ctime_ns=1451854748
mock_lstat = MagicMock()
mock_lstat.return_value = s
entry = MagicMock()
entry.is_dir = MagicMock(return_value=False)
entry.name = b'tty'
entry.stat = mock_lstat
entries = MagicMock()
entries.return_value = [ entry ]
with NamedTemporaryFile() as index:
with patch('os.scandir', entries):
h = hashDirectory("whatever", index.name)
self.assertEqual(h, b"\x9b\x98~\xa5\xd5\xc4\x1e\xe29'\x8d\x1e\xe1\x12\xdd\xf4\xa51\xf5d")