blob: f343c586b5d4d987c777ea0bcf3e686b1b94dca2 [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):
Andrew Geisslereff27472021-10-29 15:35:00 -050022 logging.basicConfig(level=logging.DEBUG, filename='bbhashserv-%d.log' % idx, filemode='w',
Patrick Williams213cb262021-08-07 19:21:33 -050023 format='%(levelname)s %(filename)s:%(lineno)d %(message)s')
24 server.logger.debug("Running server %d" % idx)
Andrew Geisslereff27472021-10-29 15:35:00 -050025 sys.stdout = open('bbhashserv-stdout-%d.log' % idx, 'w')
Andrew Geissler6ce62a22020-11-30 19:58:47 -060026 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')
Andrew Geissler20137392023-10-12 04:59:14 -060087 return taskhash, outhash, unihash
Brad Bishop19323692019-04-05 15:28:33 -040088
89 def test_create_equivalent(self):
90 # Tests that a second reported task with the same outhash will be
91 # assigned the same unihash
92 taskhash = '53b8dce672cb6d0c73170be43f540460bfc347b4'
93 outhash = '5a9cb1649625f0bf41fc7791b635cd9c2d7118c7f021ba87dcd03f72b67ce7a8'
94 unihash = 'f37918cc02eb5a520b1aff86faacbc0a38124646'
Brad Bishopa34c0302019-09-23 22:34:48 -040095
96 result = self.client.report_unihash(taskhash, self.METHOD, outhash, unihash)
97 self.assertEqual(result['unihash'], unihash, 'Server returned bad unihash')
Brad Bishop19323692019-04-05 15:28:33 -040098
99 # Report a different task with the same outhash. The returned unihash
100 # should match the first task
101 taskhash2 = '3bf6f1e89d26205aec90da04854fbdbf73afe6b4'
102 unihash2 = 'af36b199320e611fbb16f1f277d3ee1d619ca58b'
Brad Bishopa34c0302019-09-23 22:34:48 -0400103 result = self.client.report_unihash(taskhash2, self.METHOD, outhash, unihash2)
104 self.assertEqual(result['unihash'], unihash, 'Server returned bad unihash')
Brad Bishop19323692019-04-05 15:28:33 -0400105
106 def test_duplicate_taskhash(self):
107 # Tests that duplicate reports of the same taskhash with different
108 # outhash & unihash always return the unihash from the first reported
109 # taskhash
110 taskhash = '8aa96fcffb5831b3c2c0cb75f0431e3f8b20554a'
111 outhash = 'afe240a439959ce86f5e322f8c208e1fedefea9e813f2140c81af866cc9edf7e'
112 unihash = '218e57509998197d570e2c98512d0105985dffc9'
Brad Bishopa34c0302019-09-23 22:34:48 -0400113 self.client.report_unihash(taskhash, self.METHOD, outhash, unihash)
Brad Bishop19323692019-04-05 15:28:33 -0400114
Andrew Geissler6ce62a22020-11-30 19:58:47 -0600115 self.assertClientGetHash(self.client, taskhash, unihash)
Brad Bishop19323692019-04-05 15:28:33 -0400116
117 outhash2 = '0904a7fe3dc712d9fd8a74a616ddca2a825a8ee97adf0bd3fc86082c7639914d'
118 unihash2 = 'ae9a7d252735f0dafcdb10e2e02561ca3a47314c'
Brad Bishopa34c0302019-09-23 22:34:48 -0400119 self.client.report_unihash(taskhash, self.METHOD, outhash2, unihash2)
Brad Bishop19323692019-04-05 15:28:33 -0400120
Andrew Geissler6ce62a22020-11-30 19:58:47 -0600121 self.assertClientGetHash(self.client, taskhash, unihash)
Brad Bishop19323692019-04-05 15:28:33 -0400122
123 outhash3 = '77623a549b5b1a31e3732dfa8fe61d7ce5d44b3370f253c5360e136b852967b4'
124 unihash3 = '9217a7d6398518e5dc002ed58f2cbbbc78696603'
Brad Bishopa34c0302019-09-23 22:34:48 -0400125 self.client.report_unihash(taskhash, self.METHOD, outhash3, unihash3)
Brad Bishop19323692019-04-05 15:28:33 -0400126
Andrew Geissler6ce62a22020-11-30 19:58:47 -0600127 self.assertClientGetHash(self.client, taskhash, unihash)
Brad Bishopa34c0302019-09-23 22:34:48 -0400128
Andrew Geissler20137392023-10-12 04:59:14 -0600129 def test_remove_taskhash(self):
130 taskhash, outhash, unihash = self.test_create_hash()
131 result = self.client.remove({"taskhash": taskhash})
132 self.assertGreater(result["count"], 0)
133 self.assertClientGetHash(self.client, taskhash, None)
134
135 result_outhash = self.client.get_outhash(self.METHOD, outhash, taskhash)
136 self.assertIsNone(result_outhash)
137
138 def test_remove_unihash(self):
139 taskhash, outhash, unihash = self.test_create_hash()
140 result = self.client.remove({"unihash": unihash})
141 self.assertGreater(result["count"], 0)
142 self.assertClientGetHash(self.client, taskhash, None)
143
144 def test_remove_outhash(self):
145 taskhash, outhash, unihash = self.test_create_hash()
146 result = self.client.remove({"outhash": outhash})
147 self.assertGreater(result["count"], 0)
148
149 result_outhash = self.client.get_outhash(self.METHOD, outhash, taskhash)
150 self.assertIsNone(result_outhash)
151
152 def test_remove_method(self):
153 taskhash, outhash, unihash = self.test_create_hash()
154 result = self.client.remove({"method": self.METHOD})
155 self.assertGreater(result["count"], 0)
156 self.assertClientGetHash(self.client, taskhash, None)
157
158 result_outhash = self.client.get_outhash(self.METHOD, outhash, taskhash)
159 self.assertIsNone(result_outhash)
160
161 def test_clean_unused(self):
162 taskhash, outhash, unihash = self.test_create_hash()
163
164 # Clean the database, which should not remove anything because all hashes an in-use
165 result = self.client.clean_unused(0)
166 self.assertEqual(result["count"], 0)
167 self.assertClientGetHash(self.client, taskhash, unihash)
168
169 # Remove the unihash. The row in the outhash table should still be present
170 self.client.remove({"unihash": unihash})
171 result_outhash = self.client.get_outhash(self.METHOD, outhash, taskhash, False)
172 self.assertIsNotNone(result_outhash)
173
174 # Now clean with no minimum age which will remove the outhash
175 result = self.client.clean_unused(0)
176 self.assertEqual(result["count"], 1)
177 result_outhash = self.client.get_outhash(self.METHOD, outhash, taskhash, False)
178 self.assertIsNone(result_outhash)
179
Andrew Geissler475cb722020-07-10 16:00:51 -0500180 def test_huge_message(self):
181 # Simple test that hashes can be created
182 taskhash = 'c665584ee6817aa99edfc77a44dd853828279370'
183 outhash = '3c979c3db45c569f51ab7626a4651074be3a9d11a84b1db076f5b14f7d39db44'
184 unihash = '90e9bc1d1f094c51824adca7f8ea79a048d68824'
185
Andrew Geissler6ce62a22020-11-30 19:58:47 -0600186 self.assertClientGetHash(self.client, taskhash, None)
Andrew Geissler475cb722020-07-10 16:00:51 -0500187
188 siginfo = "0" * (self.client.max_chunk * 4)
189
190 result = self.client.report_unihash(taskhash, self.METHOD, outhash, unihash, {
191 'outhash_siginfo': siginfo
192 })
193 self.assertEqual(result['unihash'], unihash, 'Server returned bad unihash')
194
Andrew Geisslereff27472021-10-29 15:35:00 -0500195 result_unihash = self.client.get_taskhash(self.METHOD, taskhash, True)
196 self.assertEqual(result_unihash['taskhash'], taskhash)
197 self.assertEqual(result_unihash['unihash'], unihash)
198 self.assertEqual(result_unihash['method'], self.METHOD)
199
200 result_outhash = self.client.get_outhash(self.METHOD, outhash, taskhash)
201 self.assertEqual(result_outhash['taskhash'], taskhash)
202 self.assertEqual(result_outhash['method'], self.METHOD)
203 self.assertEqual(result_outhash['unihash'], unihash)
204 self.assertEqual(result_outhash['outhash'], outhash)
205 self.assertEqual(result_outhash['outhash_siginfo'], siginfo)
Andrew Geissler475cb722020-07-10 16:00:51 -0500206
Brad Bishopa34c0302019-09-23 22:34:48 -0400207 def test_stress(self):
208 def query_server(failures):
209 client = Client(self.server.address)
210 try:
211 for i in range(1000):
212 taskhash = hashlib.sha256()
213 taskhash.update(str(i).encode('utf-8'))
214 taskhash = taskhash.hexdigest()
215 result = client.get_unihash(self.METHOD, taskhash)
216 if result != taskhash:
217 failures.append("taskhash mismatch: %s != %s" % (result, taskhash))
218 finally:
219 client.close()
220
221 # Report hashes
222 for i in range(1000):
223 taskhash = hashlib.sha256()
224 taskhash.update(str(i).encode('utf-8'))
225 taskhash = taskhash.hexdigest()
226 self.client.report_unihash(taskhash, self.METHOD, taskhash, taskhash)
227
228 failures = []
229 threads = [threading.Thread(target=query_server, args=(failures,)) for t in range(100)]
230
231 for t in threads:
232 t.start()
233
234 for t in threads:
235 t.join()
236
237 self.assertFalse(failures)
Brad Bishop19323692019-04-05 15:28:33 -0400238
Andrew Geissler6ce62a22020-11-30 19:58:47 -0600239 def test_upstream_server(self):
240 # Tests upstream server support. This is done by creating two servers
241 # that share a database file. The downstream server has it upstream
242 # set to the test server, whereas the side server doesn't. This allows
243 # verification that the hash requests are being proxied to the upstream
244 # server by verifying that they appear on the downstream client, but not
245 # the side client. It also verifies that the results are pulled into
246 # the downstream database by checking that the downstream and side servers
247 # match after the downstream is done waiting for all backfill tasks
248 (down_client, down_server) = self.start_server(upstream=self.server.address)
249 (side_client, side_server) = self.start_server(dbpath=down_server.dbpath)
250
251 def check_hash(taskhash, unihash, old_sidehash):
252 nonlocal down_client
253 nonlocal side_client
254
255 # check upstream server
256 self.assertClientGetHash(self.client, taskhash, unihash)
257
258 # Hash should *not* be present on the side server
259 self.assertClientGetHash(side_client, taskhash, old_sidehash)
260
261 # Hash should be present on the downstream server, since it
262 # will defer to the upstream server. This will trigger
263 # the backfill in the downstream server
264 self.assertClientGetHash(down_client, taskhash, unihash)
265
266 # After waiting for the downstream client to finish backfilling the
267 # task from the upstream server, it should appear in the side server
268 # since the database is populated
269 down_client.backfill_wait()
270 self.assertClientGetHash(side_client, taskhash, unihash)
271
272 # Basic report
273 taskhash = '8aa96fcffb5831b3c2c0cb75f0431e3f8b20554a'
274 outhash = 'afe240a439959ce86f5e322f8c208e1fedefea9e813f2140c81af866cc9edf7e'
275 unihash = '218e57509998197d570e2c98512d0105985dffc9'
276 self.client.report_unihash(taskhash, self.METHOD, outhash, unihash)
277
278 check_hash(taskhash, unihash, None)
279
280 # Duplicated taskhash with multiple output hashes and unihashes.
281 # All servers should agree with the originally reported hash
282 outhash2 = '0904a7fe3dc712d9fd8a74a616ddca2a825a8ee97adf0bd3fc86082c7639914d'
283 unihash2 = 'ae9a7d252735f0dafcdb10e2e02561ca3a47314c'
284 self.client.report_unihash(taskhash, self.METHOD, outhash2, unihash2)
285
286 check_hash(taskhash, unihash, unihash)
287
288 # Report an equivalent task. The sideload will originally report
289 # no unihash until backfilled
290 taskhash3 = "044c2ec8aaf480685a00ff6ff49e6162e6ad34e1"
291 unihash3 = "def64766090d28f627e816454ed46894bb3aab36"
292 self.client.report_unihash(taskhash3, self.METHOD, outhash, unihash3)
293
294 check_hash(taskhash3, unihash, None)
295
296 # Test that reporting a unihash in the downstream client isn't
297 # propagating to the upstream server
298 taskhash4 = "e3da00593d6a7fb435c7e2114976c59c5fd6d561"
299 outhash4 = "1cf8713e645f491eb9c959d20b5cae1c47133a292626dda9b10709857cbe688a"
300 unihash4 = "3b5d3d83f07f259e9086fcb422c855286e18a57d"
301 down_client.report_unihash(taskhash4, self.METHOD, outhash4, unihash4)
302 down_client.backfill_wait()
303
304 self.assertClientGetHash(down_client, taskhash4, unihash4)
305 self.assertClientGetHash(side_client, taskhash4, unihash4)
306 self.assertClientGetHash(self.client, taskhash4, None)
307
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600308 # Test that reporting a unihash in the downstream is able to find a
309 # match which was previously reported to the upstream server
310 taskhash5 = '35788efcb8dfb0a02659d81cf2bfd695fb30faf9'
311 outhash5 = '2765d4a5884be49b28601445c2760c5f21e7e5c0ee2b7e3fce98fd7e5970796f'
312 unihash5 = 'f46d3fbb439bd9b921095da657a4de906510d2cd'
313 result = self.client.report_unihash(taskhash5, self.METHOD, outhash5, unihash5)
314
315 taskhash6 = '35788efcb8dfb0a02659d81cf2bfd695fb30fafa'
316 unihash6 = 'f46d3fbb439bd9b921095da657a4de906510d2ce'
317 result = down_client.report_unihash(taskhash6, self.METHOD, outhash5, unihash6)
318 self.assertEqual(result['unihash'], unihash5, 'Server failed to copy unihash from upstream')
319
Andrew Geisslereff27472021-10-29 15:35:00 -0500320 # Tests read through from server with
321 taskhash7 = '9d81d76242cc7cfaf7bf74b94b9cd2e29324ed74'
322 outhash7 = '8470d56547eea6236d7c81a644ce74670ca0bbda998e13c629ef6bb3f0d60b69'
323 unihash7 = '05d2a63c81e32f0a36542ca677e8ad852365c538'
324 self.client.report_unihash(taskhash7, self.METHOD, outhash7, unihash7)
325
326 result = down_client.get_taskhash(self.METHOD, taskhash7, True)
327 self.assertEqual(result['unihash'], unihash7, 'Server failed to copy unihash from upstream')
328 self.assertEqual(result['outhash'], outhash7, 'Server failed to copy unihash from upstream')
329 self.assertEqual(result['taskhash'], taskhash7, 'Server failed to copy unihash from upstream')
330 self.assertEqual(result['method'], self.METHOD)
331
332 taskhash8 = '86978a4c8c71b9b487330b0152aade10c1ee58aa'
333 outhash8 = 'ca8c128e9d9e4a28ef24d0508aa20b5cf880604eacd8f65c0e366f7e0cc5fbcf'
334 unihash8 = 'd8bcf25369d40590ad7d08c84d538982f2023e01'
335 self.client.report_unihash(taskhash8, self.METHOD, outhash8, unihash8)
336
337 result = down_client.get_outhash(self.METHOD, outhash8, taskhash8)
338 self.assertEqual(result['unihash'], unihash8, 'Server failed to copy unihash from upstream')
339 self.assertEqual(result['outhash'], outhash8, 'Server failed to copy unihash from upstream')
340 self.assertEqual(result['taskhash'], taskhash8, 'Server failed to copy unihash from upstream')
341 self.assertEqual(result['method'], self.METHOD)
342
343 taskhash9 = 'ae6339531895ddf5b67e663e6a374ad8ec71d81c'
344 outhash9 = 'afc78172c81880ae10a1fec994b5b4ee33d196a001a1b66212a15ebe573e00b5'
345 unihash9 = '6662e699d6e3d894b24408ff9a4031ef9b038ee8'
346 self.client.report_unihash(taskhash9, self.METHOD, outhash9, unihash9)
347
348 result = down_client.get_taskhash(self.METHOD, taskhash9, False)
349 self.assertEqual(result['unihash'], unihash9, 'Server failed to copy unihash from upstream')
350 self.assertEqual(result['taskhash'], taskhash9, 'Server failed to copy unihash from upstream')
351 self.assertEqual(result['method'], self.METHOD)
352
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600353 def test_ro_server(self):
354 (ro_client, ro_server) = self.start_server(dbpath=self.server.dbpath, read_only=True)
355
356 # Report a hash via the read-write server
357 taskhash = '35788efcb8dfb0a02659d81cf2bfd695fb30faf9'
358 outhash = '2765d4a5884be49b28601445c2760c5f21e7e5c0ee2b7e3fce98fd7e5970796f'
359 unihash = 'f46d3fbb439bd9b921095da657a4de906510d2cd'
360
361 result = self.client.report_unihash(taskhash, self.METHOD, outhash, unihash)
362 self.assertEqual(result['unihash'], unihash, 'Server returned bad unihash')
363
364 # Check the hash via the read-only server
365 self.assertClientGetHash(ro_client, taskhash, unihash)
366
367 # Ensure that reporting via the read-only server fails
368 taskhash2 = 'c665584ee6817aa99edfc77a44dd853828279370'
369 outhash2 = '3c979c3db45c569f51ab7626a4651074be3a9d11a84b1db076f5b14f7d39db44'
370 unihash2 = '90e9bc1d1f094c51824adca7f8ea79a048d68824'
371
Andrew Geisslerc926e172021-05-07 16:11:35 -0500372 with self.assertRaises(ConnectionError):
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600373 ro_client.report_unihash(taskhash2, self.METHOD, outhash2, unihash2)
374
375 # Ensure that the database was not modified
376 self.assertClientGetHash(self.client, taskhash2, None)
377
Brad Bishop19323692019-04-05 15:28:33 -0400378
Patrick Williams213cb262021-08-07 19:21:33 -0500379 def test_slow_server_start(self):
Andrew Geisslereff27472021-10-29 15:35:00 -0500380 # Ensures that the server will exit correctly even if it gets a SIGTERM
381 # before entering the main loop
Patrick Williams213cb262021-08-07 19:21:33 -0500382
383 event = multiprocessing.Event()
384
385 def prefunc(server, idx):
386 nonlocal event
387 server_prefunc(server, idx)
388 event.wait()
389
390 def do_nothing(signum, frame):
391 pass
392
393 old_signal = signal.signal(signal.SIGTERM, do_nothing)
394 self.addCleanup(signal.signal, signal.SIGTERM, old_signal)
395
396 _, server = self.start_server(prefunc=prefunc)
397 server.process.terminate()
398 time.sleep(30)
399 event.set()
400 server.process.join(300)
401 self.assertIsNotNone(server.process.exitcode, "Server did not exit in a timely manner!")
402
Andrew Geisslereff27472021-10-29 15:35:00 -0500403 def test_diverging_report_race(self):
404 # Tests that a reported task will correctly pick up an updated unihash
405
406 # This is a baseline report added to the database to ensure that there
407 # is something to match against as equivalent
408 outhash1 = 'afd11c366050bcd75ad763e898e4430e2a60659b26f83fbb22201a60672019fa'
409 taskhash1 = '3bde230c743fc45ab61a065d7a1815fbfa01c4740e4c895af2eb8dc0f684a4ab'
410 unihash1 = '3bde230c743fc45ab61a065d7a1815fbfa01c4740e4c895af2eb8dc0f684a4ab'
411 result = self.client.report_unihash(taskhash1, self.METHOD, outhash1, unihash1)
412
413 # Add a report that is equivalent to Task 1. It should ignore the
414 # provided unihash and report the unihash from task 1
415 taskhash2 = '6259ae8263bd94d454c086f501c37e64c4e83cae806902ca95b4ab513546b273'
416 unihash2 = taskhash2
417 result = self.client.report_unihash(taskhash2, self.METHOD, outhash1, unihash2)
418 self.assertEqual(result['unihash'], unihash1)
419
420 # Add another report for Task 2, but with a different outhash (e.g. the
421 # task is non-deterministic). It should still be marked with the Task 1
422 # unihash because it has the Task 2 taskhash, which is equivalent to
423 # Task 1
424 outhash3 = 'd2187ee3a8966db10b34fe0e863482288d9a6185cb8ef58a6c1c6ace87a2f24c'
425 result = self.client.report_unihash(taskhash2, self.METHOD, outhash3, unihash2)
426 self.assertEqual(result['unihash'], unihash1)
427
428
429 def test_diverging_report_reverse_race(self):
430 # Same idea as the previous test, but Tasks 2 and 3 are reported in
431 # reverse order the opposite order
432
433 outhash1 = 'afd11c366050bcd75ad763e898e4430e2a60659b26f83fbb22201a60672019fa'
434 taskhash1 = '3bde230c743fc45ab61a065d7a1815fbfa01c4740e4c895af2eb8dc0f684a4ab'
435 unihash1 = '3bde230c743fc45ab61a065d7a1815fbfa01c4740e4c895af2eb8dc0f684a4ab'
436 result = self.client.report_unihash(taskhash1, self.METHOD, outhash1, unihash1)
437
438 taskhash2 = '6259ae8263bd94d454c086f501c37e64c4e83cae806902ca95b4ab513546b273'
439 unihash2 = taskhash2
440
441 # Report Task 3 first. Since there is nothing else in the database it
442 # will use the client provided unihash
443 outhash3 = 'd2187ee3a8966db10b34fe0e863482288d9a6185cb8ef58a6c1c6ace87a2f24c'
444 result = self.client.report_unihash(taskhash2, self.METHOD, outhash3, unihash2)
445 self.assertEqual(result['unihash'], unihash2)
446
447 # Report Task 2. This is equivalent to Task 1 but there is already a mapping for
448 # taskhash2 so it will report unihash2
449 result = self.client.report_unihash(taskhash2, self.METHOD, outhash1, unihash2)
450 self.assertEqual(result['unihash'], unihash2)
451
452 # The originally reported unihash for Task 3 should be unchanged even if it
453 # shares a taskhash with Task 2
454 self.assertClientGetHash(self.client, taskhash2, unihash2)
Patrick Williams213cb262021-08-07 19:21:33 -0500455
Andrew Geissler09209ee2020-12-13 08:44:15 -0600456class TestHashEquivalenceUnixServer(HashEquivalenceTestSetup, HashEquivalenceCommonTests, unittest.TestCase):
Andrew Geissler6ce62a22020-11-30 19:58:47 -0600457 def get_server_addr(self, server_idx):
458 return "unix://" + os.path.join(self.temp_dir.name, 'sock%d' % server_idx)
Brad Bishopa34c0302019-09-23 22:34:48 -0400459
460
Andrew Geissler09209ee2020-12-13 08:44:15 -0600461class TestHashEquivalenceUnixServerLongPath(HashEquivalenceTestSetup, unittest.TestCase):
462 DEEP_DIRECTORY = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb/ccccccccccccccccccccccccccccccccccccccccccc"
463 def get_server_addr(self, server_idx):
464 os.makedirs(os.path.join(self.temp_dir.name, self.DEEP_DIRECTORY), exist_ok=True)
465 return "unix://" + os.path.join(self.temp_dir.name, self.DEEP_DIRECTORY, 'sock%d' % server_idx)
466
467
468 def test_long_sock_path(self):
469 # Simple test that hashes can be created
470 taskhash = '35788efcb8dfb0a02659d81cf2bfd695fb30faf9'
471 outhash = '2765d4a5884be49b28601445c2760c5f21e7e5c0ee2b7e3fce98fd7e5970796f'
472 unihash = 'f46d3fbb439bd9b921095da657a4de906510d2cd'
473
474 self.assertClientGetHash(self.client, taskhash, None)
475
476 result = self.client.report_unihash(taskhash, self.METHOD, outhash, unihash)
477 self.assertEqual(result['unihash'], unihash, 'Server returned bad unihash')
478
479
480class TestHashEquivalenceTCPServer(HashEquivalenceTestSetup, HashEquivalenceCommonTests, unittest.TestCase):
Andrew Geissler6ce62a22020-11-30 19:58:47 -0600481 def get_server_addr(self, server_idx):
Andrew Geisslerc3d88e42020-10-02 09:45:00 -0500482 # Some hosts cause asyncio module to misbehave, when IPv6 is not enabled.
483 # If IPv6 is enabled, it should be safe to use localhost directly, in general
484 # case it is more reliable to resolve the IP address explicitly.
485 return socket.gethostbyname("localhost") + ":0"