Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 1 | #! /usr/bin/env python3 |
| 2 | # |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 3 | # Copyright (C) 2018-2019 Garmin Ltd. |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 4 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 5 | # SPDX-License-Identifier: GPL-2.0-only |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 6 | # |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 7 | |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 8 | from . import create_server, create_client |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 9 | import hashlib |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 10 | import logging |
| 11 | import multiprocessing |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 12 | import os |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 13 | import sys |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 14 | import tempfile |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 15 | import threading |
| 16 | import unittest |
Andrew Geissler | c3d88e4 | 2020-10-02 09:45:00 -0500 | [diff] [blame] | 17 | import socket |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 18 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame^] | 19 | def _run_server(server, idx): |
| 20 | # logging.basicConfig(level=logging.DEBUG, filename='bbhashserv.log', filemode='w', |
| 21 | # format='%(levelname)s %(filename)s:%(lineno)d %(message)s') |
| 22 | sys.stdout = open('bbhashserv-%d.log' % idx, 'w') |
| 23 | sys.stderr = sys.stdout |
| 24 | server.serve_forever() |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 25 | |
| 26 | class TestHashEquivalenceServer(object): |
| 27 | METHOD = 'TestMethod' |
| 28 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame^] | 29 | server_index = 0 |
| 30 | |
| 31 | def start_server(self, dbpath=None, upstream=None): |
| 32 | self.server_index += 1 |
| 33 | if dbpath is None: |
| 34 | dbpath = os.path.join(self.temp_dir.name, "db%d.sqlite" % self.server_index) |
| 35 | |
| 36 | def cleanup_thread(thread): |
| 37 | thread.terminate() |
| 38 | thread.join() |
| 39 | |
| 40 | server = create_server(self.get_server_addr(self.server_index), dbpath, upstream=upstream) |
| 41 | server.dbpath = dbpath |
| 42 | |
| 43 | server.thread = multiprocessing.Process(target=_run_server, args=(server, self.server_index)) |
| 44 | server.thread.start() |
| 45 | self.addCleanup(cleanup_thread, server.thread) |
| 46 | |
| 47 | def cleanup_client(client): |
| 48 | client.close() |
| 49 | |
| 50 | client = create_client(server.address) |
| 51 | self.addCleanup(cleanup_client, client) |
| 52 | |
| 53 | return (client, server) |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 54 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 55 | def setUp(self): |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 56 | if sys.version_info < (3, 5, 0): |
| 57 | self.skipTest('Python 3.5 or later required') |
| 58 | |
| 59 | self.temp_dir = tempfile.TemporaryDirectory(prefix='bb-hashserv') |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame^] | 60 | self.addCleanup(self.temp_dir.cleanup) |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 61 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame^] | 62 | (self.client, self.server) = self.start_server() |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 63 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame^] | 64 | def assertClientGetHash(self, client, taskhash, unihash): |
| 65 | result = client.get_unihash(self.METHOD, taskhash) |
| 66 | self.assertEqual(result, unihash) |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 67 | |
| 68 | def test_create_hash(self): |
| 69 | # Simple test that hashes can be created |
| 70 | taskhash = '35788efcb8dfb0a02659d81cf2bfd695fb30faf9' |
| 71 | outhash = '2765d4a5884be49b28601445c2760c5f21e7e5c0ee2b7e3fce98fd7e5970796f' |
| 72 | unihash = 'f46d3fbb439bd9b921095da657a4de906510d2cd' |
| 73 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame^] | 74 | self.assertClientGetHash(self.client, taskhash, None) |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 75 | |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 76 | result = self.client.report_unihash(taskhash, self.METHOD, outhash, unihash) |
| 77 | self.assertEqual(result['unihash'], unihash, 'Server returned bad unihash') |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 78 | |
| 79 | def test_create_equivalent(self): |
| 80 | # Tests that a second reported task with the same outhash will be |
| 81 | # assigned the same unihash |
| 82 | taskhash = '53b8dce672cb6d0c73170be43f540460bfc347b4' |
| 83 | outhash = '5a9cb1649625f0bf41fc7791b635cd9c2d7118c7f021ba87dcd03f72b67ce7a8' |
| 84 | unihash = 'f37918cc02eb5a520b1aff86faacbc0a38124646' |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 85 | |
| 86 | result = self.client.report_unihash(taskhash, self.METHOD, outhash, unihash) |
| 87 | self.assertEqual(result['unihash'], unihash, 'Server returned bad unihash') |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 88 | |
| 89 | # Report a different task with the same outhash. The returned unihash |
| 90 | # should match the first task |
| 91 | taskhash2 = '3bf6f1e89d26205aec90da04854fbdbf73afe6b4' |
| 92 | unihash2 = 'af36b199320e611fbb16f1f277d3ee1d619ca58b' |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 93 | result = self.client.report_unihash(taskhash2, self.METHOD, outhash, unihash2) |
| 94 | self.assertEqual(result['unihash'], unihash, 'Server returned bad unihash') |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 95 | |
| 96 | def test_duplicate_taskhash(self): |
| 97 | # Tests that duplicate reports of the same taskhash with different |
| 98 | # outhash & unihash always return the unihash from the first reported |
| 99 | # taskhash |
| 100 | taskhash = '8aa96fcffb5831b3c2c0cb75f0431e3f8b20554a' |
| 101 | outhash = 'afe240a439959ce86f5e322f8c208e1fedefea9e813f2140c81af866cc9edf7e' |
| 102 | unihash = '218e57509998197d570e2c98512d0105985dffc9' |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 103 | self.client.report_unihash(taskhash, self.METHOD, outhash, unihash) |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 104 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame^] | 105 | self.assertClientGetHash(self.client, taskhash, unihash) |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 106 | |
| 107 | outhash2 = '0904a7fe3dc712d9fd8a74a616ddca2a825a8ee97adf0bd3fc86082c7639914d' |
| 108 | unihash2 = 'ae9a7d252735f0dafcdb10e2e02561ca3a47314c' |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 109 | self.client.report_unihash(taskhash, self.METHOD, outhash2, unihash2) |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 110 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame^] | 111 | self.assertClientGetHash(self.client, taskhash, unihash) |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 112 | |
| 113 | outhash3 = '77623a549b5b1a31e3732dfa8fe61d7ce5d44b3370f253c5360e136b852967b4' |
| 114 | unihash3 = '9217a7d6398518e5dc002ed58f2cbbbc78696603' |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 115 | self.client.report_unihash(taskhash, self.METHOD, outhash3, unihash3) |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 116 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame^] | 117 | self.assertClientGetHash(self.client, taskhash, unihash) |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 118 | |
Andrew Geissler | 475cb72 | 2020-07-10 16:00:51 -0500 | [diff] [blame] | 119 | def test_huge_message(self): |
| 120 | # Simple test that hashes can be created |
| 121 | taskhash = 'c665584ee6817aa99edfc77a44dd853828279370' |
| 122 | outhash = '3c979c3db45c569f51ab7626a4651074be3a9d11a84b1db076f5b14f7d39db44' |
| 123 | unihash = '90e9bc1d1f094c51824adca7f8ea79a048d68824' |
| 124 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame^] | 125 | self.assertClientGetHash(self.client, taskhash, None) |
Andrew Geissler | 475cb72 | 2020-07-10 16:00:51 -0500 | [diff] [blame] | 126 | |
| 127 | siginfo = "0" * (self.client.max_chunk * 4) |
| 128 | |
| 129 | result = self.client.report_unihash(taskhash, self.METHOD, outhash, unihash, { |
| 130 | 'outhash_siginfo': siginfo |
| 131 | }) |
| 132 | self.assertEqual(result['unihash'], unihash, 'Server returned bad unihash') |
| 133 | |
| 134 | result = self.client.get_taskhash(self.METHOD, taskhash, True) |
| 135 | self.assertEqual(result['taskhash'], taskhash) |
| 136 | self.assertEqual(result['unihash'], unihash) |
| 137 | self.assertEqual(result['method'], self.METHOD) |
| 138 | self.assertEqual(result['outhash'], outhash) |
| 139 | self.assertEqual(result['outhash_siginfo'], siginfo) |
| 140 | |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 141 | def test_stress(self): |
| 142 | def query_server(failures): |
| 143 | client = Client(self.server.address) |
| 144 | try: |
| 145 | for i in range(1000): |
| 146 | taskhash = hashlib.sha256() |
| 147 | taskhash.update(str(i).encode('utf-8')) |
| 148 | taskhash = taskhash.hexdigest() |
| 149 | result = client.get_unihash(self.METHOD, taskhash) |
| 150 | if result != taskhash: |
| 151 | failures.append("taskhash mismatch: %s != %s" % (result, taskhash)) |
| 152 | finally: |
| 153 | client.close() |
| 154 | |
| 155 | # Report hashes |
| 156 | for i in range(1000): |
| 157 | taskhash = hashlib.sha256() |
| 158 | taskhash.update(str(i).encode('utf-8')) |
| 159 | taskhash = taskhash.hexdigest() |
| 160 | self.client.report_unihash(taskhash, self.METHOD, taskhash, taskhash) |
| 161 | |
| 162 | failures = [] |
| 163 | threads = [threading.Thread(target=query_server, args=(failures,)) for t in range(100)] |
| 164 | |
| 165 | for t in threads: |
| 166 | t.start() |
| 167 | |
| 168 | for t in threads: |
| 169 | t.join() |
| 170 | |
| 171 | self.assertFalse(failures) |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 172 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame^] | 173 | def test_upstream_server(self): |
| 174 | # Tests upstream server support. This is done by creating two servers |
| 175 | # that share a database file. The downstream server has it upstream |
| 176 | # set to the test server, whereas the side server doesn't. This allows |
| 177 | # verification that the hash requests are being proxied to the upstream |
| 178 | # server by verifying that they appear on the downstream client, but not |
| 179 | # the side client. It also verifies that the results are pulled into |
| 180 | # the downstream database by checking that the downstream and side servers |
| 181 | # match after the downstream is done waiting for all backfill tasks |
| 182 | (down_client, down_server) = self.start_server(upstream=self.server.address) |
| 183 | (side_client, side_server) = self.start_server(dbpath=down_server.dbpath) |
| 184 | |
| 185 | def check_hash(taskhash, unihash, old_sidehash): |
| 186 | nonlocal down_client |
| 187 | nonlocal side_client |
| 188 | |
| 189 | # check upstream server |
| 190 | self.assertClientGetHash(self.client, taskhash, unihash) |
| 191 | |
| 192 | # Hash should *not* be present on the side server |
| 193 | self.assertClientGetHash(side_client, taskhash, old_sidehash) |
| 194 | |
| 195 | # Hash should be present on the downstream server, since it |
| 196 | # will defer to the upstream server. This will trigger |
| 197 | # the backfill in the downstream server |
| 198 | self.assertClientGetHash(down_client, taskhash, unihash) |
| 199 | |
| 200 | # After waiting for the downstream client to finish backfilling the |
| 201 | # task from the upstream server, it should appear in the side server |
| 202 | # since the database is populated |
| 203 | down_client.backfill_wait() |
| 204 | self.assertClientGetHash(side_client, taskhash, unihash) |
| 205 | |
| 206 | # Basic report |
| 207 | taskhash = '8aa96fcffb5831b3c2c0cb75f0431e3f8b20554a' |
| 208 | outhash = 'afe240a439959ce86f5e322f8c208e1fedefea9e813f2140c81af866cc9edf7e' |
| 209 | unihash = '218e57509998197d570e2c98512d0105985dffc9' |
| 210 | self.client.report_unihash(taskhash, self.METHOD, outhash, unihash) |
| 211 | |
| 212 | check_hash(taskhash, unihash, None) |
| 213 | |
| 214 | # Duplicated taskhash with multiple output hashes and unihashes. |
| 215 | # All servers should agree with the originally reported hash |
| 216 | outhash2 = '0904a7fe3dc712d9fd8a74a616ddca2a825a8ee97adf0bd3fc86082c7639914d' |
| 217 | unihash2 = 'ae9a7d252735f0dafcdb10e2e02561ca3a47314c' |
| 218 | self.client.report_unihash(taskhash, self.METHOD, outhash2, unihash2) |
| 219 | |
| 220 | check_hash(taskhash, unihash, unihash) |
| 221 | |
| 222 | # Report an equivalent task. The sideload will originally report |
| 223 | # no unihash until backfilled |
| 224 | taskhash3 = "044c2ec8aaf480685a00ff6ff49e6162e6ad34e1" |
| 225 | unihash3 = "def64766090d28f627e816454ed46894bb3aab36" |
| 226 | self.client.report_unihash(taskhash3, self.METHOD, outhash, unihash3) |
| 227 | |
| 228 | check_hash(taskhash3, unihash, None) |
| 229 | |
| 230 | # Test that reporting a unihash in the downstream client isn't |
| 231 | # propagating to the upstream server |
| 232 | taskhash4 = "e3da00593d6a7fb435c7e2114976c59c5fd6d561" |
| 233 | outhash4 = "1cf8713e645f491eb9c959d20b5cae1c47133a292626dda9b10709857cbe688a" |
| 234 | unihash4 = "3b5d3d83f07f259e9086fcb422c855286e18a57d" |
| 235 | down_client.report_unihash(taskhash4, self.METHOD, outhash4, unihash4) |
| 236 | down_client.backfill_wait() |
| 237 | |
| 238 | self.assertClientGetHash(down_client, taskhash4, unihash4) |
| 239 | self.assertClientGetHash(side_client, taskhash4, unihash4) |
| 240 | self.assertClientGetHash(self.client, taskhash4, None) |
| 241 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 242 | |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 243 | class TestHashEquivalenceUnixServer(TestHashEquivalenceServer, unittest.TestCase): |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame^] | 244 | def get_server_addr(self, server_idx): |
| 245 | return "unix://" + os.path.join(self.temp_dir.name, 'sock%d' % server_idx) |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 246 | |
| 247 | |
| 248 | class TestHashEquivalenceTCPServer(TestHashEquivalenceServer, unittest.TestCase): |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame^] | 249 | def get_server_addr(self, server_idx): |
Andrew Geissler | c3d88e4 | 2020-10-02 09:45:00 -0500 | [diff] [blame] | 250 | # Some hosts cause asyncio module to misbehave, when IPv6 is not enabled. |
| 251 | # If IPv6 is enabled, it should be safe to use localhost directly, in general |
| 252 | # case it is more reliable to resolve the IP address explicitly. |
| 253 | return socket.gethostbyname("localhost") + ":0" |
| 254 | |