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 |
| 12 | import sys |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 13 | import tempfile |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 14 | import threading |
| 15 | import unittest |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 16 | |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 17 | |
| 18 | class TestHashEquivalenceServer(object): |
| 19 | METHOD = 'TestMethod' |
| 20 | |
| 21 | def _run_server(self): |
| 22 | # logging.basicConfig(level=logging.DEBUG, filename='bbhashserv.log', filemode='w', |
| 23 | # format='%(levelname)s %(filename)s:%(lineno)d %(message)s') |
| 24 | self.server.serve_forever() |
| 25 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 26 | def setUp(self): |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 27 | if sys.version_info < (3, 5, 0): |
| 28 | self.skipTest('Python 3.5 or later required') |
| 29 | |
| 30 | self.temp_dir = tempfile.TemporaryDirectory(prefix='bb-hashserv') |
| 31 | self.dbfile = os.path.join(self.temp_dir.name, 'db.sqlite') |
| 32 | |
| 33 | self.server = create_server(self.get_server_addr(), self.dbfile) |
| 34 | self.server_thread = multiprocessing.Process(target=self._run_server) |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 35 | self.server_thread.start() |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 36 | self.client = create_client(self.server.address) |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 37 | |
| 38 | def tearDown(self): |
| 39 | # Shutdown server |
| 40 | s = getattr(self, 'server', None) |
| 41 | if s is not None: |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 42 | self.server_thread.terminate() |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 43 | self.server_thread.join() |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 44 | self.client.close() |
| 45 | self.temp_dir.cleanup() |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 46 | |
| 47 | def test_create_hash(self): |
| 48 | # Simple test that hashes can be created |
| 49 | taskhash = '35788efcb8dfb0a02659d81cf2bfd695fb30faf9' |
| 50 | outhash = '2765d4a5884be49b28601445c2760c5f21e7e5c0ee2b7e3fce98fd7e5970796f' |
| 51 | unihash = 'f46d3fbb439bd9b921095da657a4de906510d2cd' |
| 52 | |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 53 | result = self.client.get_unihash(self.METHOD, taskhash) |
| 54 | self.assertIsNone(result, msg='Found unexpected task, %r' % result) |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 55 | |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 56 | result = self.client.report_unihash(taskhash, self.METHOD, outhash, unihash) |
| 57 | self.assertEqual(result['unihash'], unihash, 'Server returned bad unihash') |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 58 | |
| 59 | def test_create_equivalent(self): |
| 60 | # Tests that a second reported task with the same outhash will be |
| 61 | # assigned the same unihash |
| 62 | taskhash = '53b8dce672cb6d0c73170be43f540460bfc347b4' |
| 63 | outhash = '5a9cb1649625f0bf41fc7791b635cd9c2d7118c7f021ba87dcd03f72b67ce7a8' |
| 64 | unihash = 'f37918cc02eb5a520b1aff86faacbc0a38124646' |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 65 | |
| 66 | result = self.client.report_unihash(taskhash, self.METHOD, outhash, unihash) |
| 67 | self.assertEqual(result['unihash'], unihash, 'Server returned bad unihash') |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 68 | |
| 69 | # Report a different task with the same outhash. The returned unihash |
| 70 | # should match the first task |
| 71 | taskhash2 = '3bf6f1e89d26205aec90da04854fbdbf73afe6b4' |
| 72 | unihash2 = 'af36b199320e611fbb16f1f277d3ee1d619ca58b' |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 73 | result = self.client.report_unihash(taskhash2, self.METHOD, outhash, unihash2) |
| 74 | self.assertEqual(result['unihash'], unihash, 'Server returned bad unihash') |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 75 | |
| 76 | def test_duplicate_taskhash(self): |
| 77 | # Tests that duplicate reports of the same taskhash with different |
| 78 | # outhash & unihash always return the unihash from the first reported |
| 79 | # taskhash |
| 80 | taskhash = '8aa96fcffb5831b3c2c0cb75f0431e3f8b20554a' |
| 81 | outhash = 'afe240a439959ce86f5e322f8c208e1fedefea9e813f2140c81af866cc9edf7e' |
| 82 | unihash = '218e57509998197d570e2c98512d0105985dffc9' |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 83 | self.client.report_unihash(taskhash, self.METHOD, outhash, unihash) |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 84 | |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 85 | result = self.client.get_unihash(self.METHOD, taskhash) |
| 86 | self.assertEqual(result, unihash) |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 87 | |
| 88 | outhash2 = '0904a7fe3dc712d9fd8a74a616ddca2a825a8ee97adf0bd3fc86082c7639914d' |
| 89 | unihash2 = 'ae9a7d252735f0dafcdb10e2e02561ca3a47314c' |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 90 | self.client.report_unihash(taskhash, self.METHOD, outhash2, unihash2) |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 91 | |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 92 | result = self.client.get_unihash(self.METHOD, taskhash) |
| 93 | self.assertEqual(result, unihash) |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 94 | |
| 95 | outhash3 = '77623a549b5b1a31e3732dfa8fe61d7ce5d44b3370f253c5360e136b852967b4' |
| 96 | unihash3 = '9217a7d6398518e5dc002ed58f2cbbbc78696603' |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 97 | self.client.report_unihash(taskhash, self.METHOD, outhash3, unihash3) |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 98 | |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 99 | result = self.client.get_unihash(self.METHOD, taskhash) |
| 100 | self.assertEqual(result, unihash) |
| 101 | |
Andrew Geissler | 475cb72 | 2020-07-10 16:00:51 -0500 | [diff] [blame^] | 102 | def test_huge_message(self): |
| 103 | # Simple test that hashes can be created |
| 104 | taskhash = 'c665584ee6817aa99edfc77a44dd853828279370' |
| 105 | outhash = '3c979c3db45c569f51ab7626a4651074be3a9d11a84b1db076f5b14f7d39db44' |
| 106 | unihash = '90e9bc1d1f094c51824adca7f8ea79a048d68824' |
| 107 | |
| 108 | result = self.client.get_unihash(self.METHOD, taskhash) |
| 109 | self.assertIsNone(result, msg='Found unexpected task, %r' % result) |
| 110 | |
| 111 | siginfo = "0" * (self.client.max_chunk * 4) |
| 112 | |
| 113 | result = self.client.report_unihash(taskhash, self.METHOD, outhash, unihash, { |
| 114 | 'outhash_siginfo': siginfo |
| 115 | }) |
| 116 | self.assertEqual(result['unihash'], unihash, 'Server returned bad unihash') |
| 117 | |
| 118 | result = self.client.get_taskhash(self.METHOD, taskhash, True) |
| 119 | self.assertEqual(result['taskhash'], taskhash) |
| 120 | self.assertEqual(result['unihash'], unihash) |
| 121 | self.assertEqual(result['method'], self.METHOD) |
| 122 | self.assertEqual(result['outhash'], outhash) |
| 123 | self.assertEqual(result['outhash_siginfo'], siginfo) |
| 124 | |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 125 | def test_stress(self): |
| 126 | def query_server(failures): |
| 127 | client = Client(self.server.address) |
| 128 | try: |
| 129 | for i in range(1000): |
| 130 | taskhash = hashlib.sha256() |
| 131 | taskhash.update(str(i).encode('utf-8')) |
| 132 | taskhash = taskhash.hexdigest() |
| 133 | result = client.get_unihash(self.METHOD, taskhash) |
| 134 | if result != taskhash: |
| 135 | failures.append("taskhash mismatch: %s != %s" % (result, taskhash)) |
| 136 | finally: |
| 137 | client.close() |
| 138 | |
| 139 | # Report hashes |
| 140 | for i in range(1000): |
| 141 | taskhash = hashlib.sha256() |
| 142 | taskhash.update(str(i).encode('utf-8')) |
| 143 | taskhash = taskhash.hexdigest() |
| 144 | self.client.report_unihash(taskhash, self.METHOD, taskhash, taskhash) |
| 145 | |
| 146 | failures = [] |
| 147 | threads = [threading.Thread(target=query_server, args=(failures,)) for t in range(100)] |
| 148 | |
| 149 | for t in threads: |
| 150 | t.start() |
| 151 | |
| 152 | for t in threads: |
| 153 | t.join() |
| 154 | |
| 155 | self.assertFalse(failures) |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 156 | |
| 157 | |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 158 | class TestHashEquivalenceUnixServer(TestHashEquivalenceServer, unittest.TestCase): |
| 159 | def get_server_addr(self): |
| 160 | return "unix://" + os.path.join(self.temp_dir.name, 'sock') |
| 161 | |
| 162 | |
| 163 | class TestHashEquivalenceTCPServer(TestHashEquivalenceServer, unittest.TestCase): |
| 164 | def get_server_addr(self): |
| 165 | return "localhost:0" |