blob: e851535c59cf03fad2100fa296a03df7d769fb6e [file] [log] [blame]
Brad Bishop19323692019-04-05 15:28:33 -04001#! /usr/bin/env python3
2#
Brad Bishopa34c0302019-09-23 22:34:48 -04003# Copyright (C) 2018-2019 Garmin Ltd.
Brad Bishop19323692019-04-05 15:28:33 -04004#
Brad Bishopc342db32019-05-15 21:57:59 -04005# SPDX-License-Identifier: GPL-2.0-only
Brad Bishop19323692019-04-05 15:28:33 -04006#
Brad Bishop19323692019-04-05 15:28:33 -04007
Brad Bishopa34c0302019-09-23 22:34:48 -04008from . import create_server, create_client
Brad Bishop19323692019-04-05 15:28:33 -04009import hashlib
Brad Bishopa34c0302019-09-23 22:34:48 -040010import logging
11import multiprocessing
Andrew Geisslerc9f78652020-09-18 14:11:35 -050012import os
Brad Bishopa34c0302019-09-23 22:34:48 -040013import sys
Brad Bishop08902b02019-08-20 09:16:51 -040014import tempfile
Brad Bishopa34c0302019-09-23 22:34:48 -040015import threading
16import unittest
Andrew Geisslerc3d88e42020-10-02 09:45:00 -050017import socket
Patrick Williams213cb262021-08-07 19:21:33 -050018import time
19import signal
Brad Bishop19323692019-04-05 15:28:33 -040020
Patrick Williams213cb262021-08-07 19:21:33 -050021def server_prefunc(server, idx):
22 logging.basicConfig(level=logging.DEBUG, filename='bbhashserv.log', filemode='w',
23 format='%(levelname)s %(filename)s:%(lineno)d %(message)s')
24 server.logger.debug("Running server %d" % idx)
Andrew Geissler6ce62a22020-11-30 19:58:47 -060025 sys.stdout = open('bbhashserv-%d.log' % idx, 'w')
26 sys.stderr = sys.stdout
Andrew Geissler09209ee2020-12-13 08:44:15 -060027
28class HashEquivalenceTestSetup(object):
Brad Bishopa34c0302019-09-23 22:34:48 -040029 METHOD = 'TestMethod'
30
Andrew Geissler6ce62a22020-11-30 19:58:47 -060031 server_index = 0
32
Patrick Williams213cb262021-08-07 19:21:33 -050033 def start_server(self, dbpath=None, upstream=None, read_only=False, prefunc=server_prefunc):
Andrew Geissler6ce62a22020-11-30 19:58:47 -060034 self.server_index += 1
35 if dbpath is None:
36 dbpath = os.path.join(self.temp_dir.name, "db%d.sqlite" % self.server_index)
37
Patrick Williams213cb262021-08-07 19:21:33 -050038 def cleanup_server(server):
39 if server.process.exitcode is not None:
40 return
41
42 server.process.terminate()
43 server.process.join()
Andrew Geissler6ce62a22020-11-30 19:58:47 -060044
Andrew Geisslerd1e89492021-02-12 15:35:20 -060045 server = create_server(self.get_server_addr(self.server_index),
46 dbpath,
47 upstream=upstream,
48 read_only=read_only)
Andrew Geissler6ce62a22020-11-30 19:58:47 -060049 server.dbpath = dbpath
50
Patrick Williams213cb262021-08-07 19:21:33 -050051 server.serve_as_process(prefunc=prefunc, args=(self.server_index,))
52 self.addCleanup(cleanup_server, server)
Andrew Geissler6ce62a22020-11-30 19:58:47 -060053
54 def cleanup_client(client):
55 client.close()
56
57 client = create_client(server.address)
58 self.addCleanup(cleanup_client, client)
59
60 return (client, server)
Brad Bishopa34c0302019-09-23 22:34:48 -040061
Brad Bishop19323692019-04-05 15:28:33 -040062 def setUp(self):
Brad Bishopa34c0302019-09-23 22:34:48 -040063 if sys.version_info < (3, 5, 0):
64 self.skipTest('Python 3.5 or later required')
65
66 self.temp_dir = tempfile.TemporaryDirectory(prefix='bb-hashserv')
Andrew Geissler6ce62a22020-11-30 19:58:47 -060067 self.addCleanup(self.temp_dir.cleanup)
Brad Bishopa34c0302019-09-23 22:34:48 -040068
Andrew Geissler6ce62a22020-11-30 19:58:47 -060069 (self.client, self.server) = self.start_server()
Brad Bishop19323692019-04-05 15:28:33 -040070
Andrew Geissler6ce62a22020-11-30 19:58:47 -060071 def assertClientGetHash(self, client, taskhash, unihash):
72 result = client.get_unihash(self.METHOD, taskhash)
73 self.assertEqual(result, unihash)
Brad Bishop19323692019-04-05 15:28:33 -040074
Andrew Geissler09209ee2020-12-13 08:44:15 -060075
76class HashEquivalenceCommonTests(object):
Brad Bishop19323692019-04-05 15:28:33 -040077 def test_create_hash(self):
78 # Simple test that hashes can be created
79 taskhash = '35788efcb8dfb0a02659d81cf2bfd695fb30faf9'
80 outhash = '2765d4a5884be49b28601445c2760c5f21e7e5c0ee2b7e3fce98fd7e5970796f'
81 unihash = 'f46d3fbb439bd9b921095da657a4de906510d2cd'
82
Andrew Geissler6ce62a22020-11-30 19:58:47 -060083 self.assertClientGetHash(self.client, taskhash, None)
Brad Bishop19323692019-04-05 15:28:33 -040084
Brad Bishopa34c0302019-09-23 22:34:48 -040085 result = self.client.report_unihash(taskhash, self.METHOD, outhash, unihash)
86 self.assertEqual(result['unihash'], unihash, 'Server returned bad unihash')
Brad Bishop19323692019-04-05 15:28:33 -040087
88 def test_create_equivalent(self):
89 # Tests that a second reported task with the same outhash will be
90 # assigned the same unihash
91 taskhash = '53b8dce672cb6d0c73170be43f540460bfc347b4'
92 outhash = '5a9cb1649625f0bf41fc7791b635cd9c2d7118c7f021ba87dcd03f72b67ce7a8'
93 unihash = 'f37918cc02eb5a520b1aff86faacbc0a38124646'
Brad Bishopa34c0302019-09-23 22:34:48 -040094
95 result = self.client.report_unihash(taskhash, self.METHOD, outhash, unihash)
96 self.assertEqual(result['unihash'], unihash, 'Server returned bad unihash')
Brad Bishop19323692019-04-05 15:28:33 -040097
98 # Report a different task with the same outhash. The returned unihash
99 # should match the first task
100 taskhash2 = '3bf6f1e89d26205aec90da04854fbdbf73afe6b4'
101 unihash2 = 'af36b199320e611fbb16f1f277d3ee1d619ca58b'
Brad Bishopa34c0302019-09-23 22:34:48 -0400102 result = self.client.report_unihash(taskhash2, self.METHOD, outhash, unihash2)
103 self.assertEqual(result['unihash'], unihash, 'Server returned bad unihash')
Brad Bishop19323692019-04-05 15:28:33 -0400104
105 def test_duplicate_taskhash(self):
106 # Tests that duplicate reports of the same taskhash with different
107 # outhash & unihash always return the unihash from the first reported
108 # taskhash
109 taskhash = '8aa96fcffb5831b3c2c0cb75f0431e3f8b20554a'
110 outhash = 'afe240a439959ce86f5e322f8c208e1fedefea9e813f2140c81af866cc9edf7e'
111 unihash = '218e57509998197d570e2c98512d0105985dffc9'
Brad Bishopa34c0302019-09-23 22:34:48 -0400112 self.client.report_unihash(taskhash, self.METHOD, outhash, unihash)
Brad Bishop19323692019-04-05 15:28:33 -0400113
Andrew Geissler6ce62a22020-11-30 19:58:47 -0600114 self.assertClientGetHash(self.client, taskhash, unihash)
Brad Bishop19323692019-04-05 15:28:33 -0400115
116 outhash2 = '0904a7fe3dc712d9fd8a74a616ddca2a825a8ee97adf0bd3fc86082c7639914d'
117 unihash2 = 'ae9a7d252735f0dafcdb10e2e02561ca3a47314c'
Brad Bishopa34c0302019-09-23 22:34:48 -0400118 self.client.report_unihash(taskhash, self.METHOD, outhash2, unihash2)
Brad Bishop19323692019-04-05 15:28:33 -0400119
Andrew Geissler6ce62a22020-11-30 19:58:47 -0600120 self.assertClientGetHash(self.client, taskhash, unihash)
Brad Bishop19323692019-04-05 15:28:33 -0400121
122 outhash3 = '77623a549b5b1a31e3732dfa8fe61d7ce5d44b3370f253c5360e136b852967b4'
123 unihash3 = '9217a7d6398518e5dc002ed58f2cbbbc78696603'
Brad Bishopa34c0302019-09-23 22:34:48 -0400124 self.client.report_unihash(taskhash, self.METHOD, outhash3, unihash3)
Brad Bishop19323692019-04-05 15:28:33 -0400125
Andrew Geissler6ce62a22020-11-30 19:58:47 -0600126 self.assertClientGetHash(self.client, taskhash, unihash)
Brad Bishopa34c0302019-09-23 22:34:48 -0400127
Andrew Geissler475cb722020-07-10 16:00:51 -0500128 def test_huge_message(self):
129 # Simple test that hashes can be created
130 taskhash = 'c665584ee6817aa99edfc77a44dd853828279370'
131 outhash = '3c979c3db45c569f51ab7626a4651074be3a9d11a84b1db076f5b14f7d39db44'
132 unihash = '90e9bc1d1f094c51824adca7f8ea79a048d68824'
133
Andrew Geissler6ce62a22020-11-30 19:58:47 -0600134 self.assertClientGetHash(self.client, taskhash, None)
Andrew Geissler475cb722020-07-10 16:00:51 -0500135
136 siginfo = "0" * (self.client.max_chunk * 4)
137
138 result = self.client.report_unihash(taskhash, self.METHOD, outhash, unihash, {
139 'outhash_siginfo': siginfo
140 })
141 self.assertEqual(result['unihash'], unihash, 'Server returned bad unihash')
142
143 result = self.client.get_taskhash(self.METHOD, taskhash, True)
144 self.assertEqual(result['taskhash'], taskhash)
145 self.assertEqual(result['unihash'], unihash)
146 self.assertEqual(result['method'], self.METHOD)
147 self.assertEqual(result['outhash'], outhash)
148 self.assertEqual(result['outhash_siginfo'], siginfo)
149
Brad Bishopa34c0302019-09-23 22:34:48 -0400150 def test_stress(self):
151 def query_server(failures):
152 client = Client(self.server.address)
153 try:
154 for i in range(1000):
155 taskhash = hashlib.sha256()
156 taskhash.update(str(i).encode('utf-8'))
157 taskhash = taskhash.hexdigest()
158 result = client.get_unihash(self.METHOD, taskhash)
159 if result != taskhash:
160 failures.append("taskhash mismatch: %s != %s" % (result, taskhash))
161 finally:
162 client.close()
163
164 # Report hashes
165 for i in range(1000):
166 taskhash = hashlib.sha256()
167 taskhash.update(str(i).encode('utf-8'))
168 taskhash = taskhash.hexdigest()
169 self.client.report_unihash(taskhash, self.METHOD, taskhash, taskhash)
170
171 failures = []
172 threads = [threading.Thread(target=query_server, args=(failures,)) for t in range(100)]
173
174 for t in threads:
175 t.start()
176
177 for t in threads:
178 t.join()
179
180 self.assertFalse(failures)
Brad Bishop19323692019-04-05 15:28:33 -0400181
Andrew Geissler6ce62a22020-11-30 19:58:47 -0600182 def test_upstream_server(self):
183 # Tests upstream server support. This is done by creating two servers
184 # that share a database file. The downstream server has it upstream
185 # set to the test server, whereas the side server doesn't. This allows
186 # verification that the hash requests are being proxied to the upstream
187 # server by verifying that they appear on the downstream client, but not
188 # the side client. It also verifies that the results are pulled into
189 # the downstream database by checking that the downstream and side servers
190 # match after the downstream is done waiting for all backfill tasks
191 (down_client, down_server) = self.start_server(upstream=self.server.address)
192 (side_client, side_server) = self.start_server(dbpath=down_server.dbpath)
193
194 def check_hash(taskhash, unihash, old_sidehash):
195 nonlocal down_client
196 nonlocal side_client
197
198 # check upstream server
199 self.assertClientGetHash(self.client, taskhash, unihash)
200
201 # Hash should *not* be present on the side server
202 self.assertClientGetHash(side_client, taskhash, old_sidehash)
203
204 # Hash should be present on the downstream server, since it
205 # will defer to the upstream server. This will trigger
206 # the backfill in the downstream server
207 self.assertClientGetHash(down_client, taskhash, unihash)
208
209 # After waiting for the downstream client to finish backfilling the
210 # task from the upstream server, it should appear in the side server
211 # since the database is populated
212 down_client.backfill_wait()
213 self.assertClientGetHash(side_client, taskhash, unihash)
214
215 # Basic report
216 taskhash = '8aa96fcffb5831b3c2c0cb75f0431e3f8b20554a'
217 outhash = 'afe240a439959ce86f5e322f8c208e1fedefea9e813f2140c81af866cc9edf7e'
218 unihash = '218e57509998197d570e2c98512d0105985dffc9'
219 self.client.report_unihash(taskhash, self.METHOD, outhash, unihash)
220
221 check_hash(taskhash, unihash, None)
222
223 # Duplicated taskhash with multiple output hashes and unihashes.
224 # All servers should agree with the originally reported hash
225 outhash2 = '0904a7fe3dc712d9fd8a74a616ddca2a825a8ee97adf0bd3fc86082c7639914d'
226 unihash2 = 'ae9a7d252735f0dafcdb10e2e02561ca3a47314c'
227 self.client.report_unihash(taskhash, self.METHOD, outhash2, unihash2)
228
229 check_hash(taskhash, unihash, unihash)
230
231 # Report an equivalent task. The sideload will originally report
232 # no unihash until backfilled
233 taskhash3 = "044c2ec8aaf480685a00ff6ff49e6162e6ad34e1"
234 unihash3 = "def64766090d28f627e816454ed46894bb3aab36"
235 self.client.report_unihash(taskhash3, self.METHOD, outhash, unihash3)
236
237 check_hash(taskhash3, unihash, None)
238
239 # Test that reporting a unihash in the downstream client isn't
240 # propagating to the upstream server
241 taskhash4 = "e3da00593d6a7fb435c7e2114976c59c5fd6d561"
242 outhash4 = "1cf8713e645f491eb9c959d20b5cae1c47133a292626dda9b10709857cbe688a"
243 unihash4 = "3b5d3d83f07f259e9086fcb422c855286e18a57d"
244 down_client.report_unihash(taskhash4, self.METHOD, outhash4, unihash4)
245 down_client.backfill_wait()
246
247 self.assertClientGetHash(down_client, taskhash4, unihash4)
248 self.assertClientGetHash(side_client, taskhash4, unihash4)
249 self.assertClientGetHash(self.client, taskhash4, None)
250
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600251 # Test that reporting a unihash in the downstream is able to find a
252 # match which was previously reported to the upstream server
253 taskhash5 = '35788efcb8dfb0a02659d81cf2bfd695fb30faf9'
254 outhash5 = '2765d4a5884be49b28601445c2760c5f21e7e5c0ee2b7e3fce98fd7e5970796f'
255 unihash5 = 'f46d3fbb439bd9b921095da657a4de906510d2cd'
256 result = self.client.report_unihash(taskhash5, self.METHOD, outhash5, unihash5)
257
258 taskhash6 = '35788efcb8dfb0a02659d81cf2bfd695fb30fafa'
259 unihash6 = 'f46d3fbb439bd9b921095da657a4de906510d2ce'
260 result = down_client.report_unihash(taskhash6, self.METHOD, outhash5, unihash6)
261 self.assertEqual(result['unihash'], unihash5, 'Server failed to copy unihash from upstream')
262
263 def test_ro_server(self):
264 (ro_client, ro_server) = self.start_server(dbpath=self.server.dbpath, read_only=True)
265
266 # Report a hash via the read-write server
267 taskhash = '35788efcb8dfb0a02659d81cf2bfd695fb30faf9'
268 outhash = '2765d4a5884be49b28601445c2760c5f21e7e5c0ee2b7e3fce98fd7e5970796f'
269 unihash = 'f46d3fbb439bd9b921095da657a4de906510d2cd'
270
271 result = self.client.report_unihash(taskhash, self.METHOD, outhash, unihash)
272 self.assertEqual(result['unihash'], unihash, 'Server returned bad unihash')
273
274 # Check the hash via the read-only server
275 self.assertClientGetHash(ro_client, taskhash, unihash)
276
277 # Ensure that reporting via the read-only server fails
278 taskhash2 = 'c665584ee6817aa99edfc77a44dd853828279370'
279 outhash2 = '3c979c3db45c569f51ab7626a4651074be3a9d11a84b1db076f5b14f7d39db44'
280 unihash2 = '90e9bc1d1f094c51824adca7f8ea79a048d68824'
281
Andrew Geisslerc926e172021-05-07 16:11:35 -0500282 with self.assertRaises(ConnectionError):
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600283 ro_client.report_unihash(taskhash2, self.METHOD, outhash2, unihash2)
284
285 # Ensure that the database was not modified
286 self.assertClientGetHash(self.client, taskhash2, None)
287
Brad Bishop19323692019-04-05 15:28:33 -0400288
Patrick Williams213cb262021-08-07 19:21:33 -0500289 def test_slow_server_start(self):
290 """
291 Ensures that the server will exit correctly even if it gets a SIGTERM
292 before entering the main loop
293 """
294
295 event = multiprocessing.Event()
296
297 def prefunc(server, idx):
298 nonlocal event
299 server_prefunc(server, idx)
300 event.wait()
301
302 def do_nothing(signum, frame):
303 pass
304
305 old_signal = signal.signal(signal.SIGTERM, do_nothing)
306 self.addCleanup(signal.signal, signal.SIGTERM, old_signal)
307
308 _, server = self.start_server(prefunc=prefunc)
309 server.process.terminate()
310 time.sleep(30)
311 event.set()
312 server.process.join(300)
313 self.assertIsNotNone(server.process.exitcode, "Server did not exit in a timely manner!")
314
315
Andrew Geissler09209ee2020-12-13 08:44:15 -0600316class TestHashEquivalenceUnixServer(HashEquivalenceTestSetup, HashEquivalenceCommonTests, unittest.TestCase):
Andrew Geissler6ce62a22020-11-30 19:58:47 -0600317 def get_server_addr(self, server_idx):
318 return "unix://" + os.path.join(self.temp_dir.name, 'sock%d' % server_idx)
Brad Bishopa34c0302019-09-23 22:34:48 -0400319
320
Andrew Geissler09209ee2020-12-13 08:44:15 -0600321class TestHashEquivalenceUnixServerLongPath(HashEquivalenceTestSetup, unittest.TestCase):
322 DEEP_DIRECTORY = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb/ccccccccccccccccccccccccccccccccccccccccccc"
323 def get_server_addr(self, server_idx):
324 os.makedirs(os.path.join(self.temp_dir.name, self.DEEP_DIRECTORY), exist_ok=True)
325 return "unix://" + os.path.join(self.temp_dir.name, self.DEEP_DIRECTORY, 'sock%d' % server_idx)
326
327
328 def test_long_sock_path(self):
329 # Simple test that hashes can be created
330 taskhash = '35788efcb8dfb0a02659d81cf2bfd695fb30faf9'
331 outhash = '2765d4a5884be49b28601445c2760c5f21e7e5c0ee2b7e3fce98fd7e5970796f'
332 unihash = 'f46d3fbb439bd9b921095da657a4de906510d2cd'
333
334 self.assertClientGetHash(self.client, taskhash, None)
335
336 result = self.client.report_unihash(taskhash, self.METHOD, outhash, unihash)
337 self.assertEqual(result['unihash'], unihash, 'Server returned bad unihash')
338
339
340class TestHashEquivalenceTCPServer(HashEquivalenceTestSetup, HashEquivalenceCommonTests, unittest.TestCase):
Andrew Geissler6ce62a22020-11-30 19:58:47 -0600341 def get_server_addr(self, server_idx):
Andrew Geisslerc3d88e42020-10-02 09:45:00 -0500342 # Some hosts cause asyncio module to misbehave, when IPv6 is not enabled.
343 # If IPv6 is enabled, it should be safe to use localhost directly, in general
344 # case it is more reliable to resolve the IP address explicitly.
345 return socket.gethostbyname("localhost") + ":0"