blob: 4566f2473824d8465509390073e6676facd4ba60 [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
Brad Bishop19323692019-04-05 15:28:33 -040018
Brad Bishopa34c0302019-09-23 22:34:48 -040019
20class TestHashEquivalenceServer(object):
21 METHOD = 'TestMethod'
22
23 def _run_server(self):
24 # logging.basicConfig(level=logging.DEBUG, filename='bbhashserv.log', filemode='w',
25 # format='%(levelname)s %(filename)s:%(lineno)d %(message)s')
26 self.server.serve_forever()
27
Brad Bishop19323692019-04-05 15:28:33 -040028 def setUp(self):
Brad Bishopa34c0302019-09-23 22:34:48 -040029 if sys.version_info < (3, 5, 0):
30 self.skipTest('Python 3.5 or later required')
31
32 self.temp_dir = tempfile.TemporaryDirectory(prefix='bb-hashserv')
33 self.dbfile = os.path.join(self.temp_dir.name, 'db.sqlite')
34
35 self.server = create_server(self.get_server_addr(), self.dbfile)
36 self.server_thread = multiprocessing.Process(target=self._run_server)
Brad Bishop19323692019-04-05 15:28:33 -040037 self.server_thread.start()
Brad Bishopa34c0302019-09-23 22:34:48 -040038 self.client = create_client(self.server.address)
Brad Bishop19323692019-04-05 15:28:33 -040039
40 def tearDown(self):
41 # Shutdown server
42 s = getattr(self, 'server', None)
43 if s is not None:
Brad Bishop08902b02019-08-20 09:16:51 -040044 self.server_thread.terminate()
Brad Bishop19323692019-04-05 15:28:33 -040045 self.server_thread.join()
Brad Bishopa34c0302019-09-23 22:34:48 -040046 self.client.close()
47 self.temp_dir.cleanup()
Brad Bishop19323692019-04-05 15:28:33 -040048
49 def test_create_hash(self):
50 # Simple test that hashes can be created
51 taskhash = '35788efcb8dfb0a02659d81cf2bfd695fb30faf9'
52 outhash = '2765d4a5884be49b28601445c2760c5f21e7e5c0ee2b7e3fce98fd7e5970796f'
53 unihash = 'f46d3fbb439bd9b921095da657a4de906510d2cd'
54
Brad Bishopa34c0302019-09-23 22:34:48 -040055 result = self.client.get_unihash(self.METHOD, taskhash)
56 self.assertIsNone(result, msg='Found unexpected task, %r' % result)
Brad Bishop19323692019-04-05 15:28:33 -040057
Brad Bishopa34c0302019-09-23 22:34:48 -040058 result = self.client.report_unihash(taskhash, self.METHOD, outhash, unihash)
59 self.assertEqual(result['unihash'], unihash, 'Server returned bad unihash')
Brad Bishop19323692019-04-05 15:28:33 -040060
61 def test_create_equivalent(self):
62 # Tests that a second reported task with the same outhash will be
63 # assigned the same unihash
64 taskhash = '53b8dce672cb6d0c73170be43f540460bfc347b4'
65 outhash = '5a9cb1649625f0bf41fc7791b635cd9c2d7118c7f021ba87dcd03f72b67ce7a8'
66 unihash = 'f37918cc02eb5a520b1aff86faacbc0a38124646'
Brad Bishopa34c0302019-09-23 22:34:48 -040067
68 result = self.client.report_unihash(taskhash, self.METHOD, outhash, unihash)
69 self.assertEqual(result['unihash'], unihash, 'Server returned bad unihash')
Brad Bishop19323692019-04-05 15:28:33 -040070
71 # Report a different task with the same outhash. The returned unihash
72 # should match the first task
73 taskhash2 = '3bf6f1e89d26205aec90da04854fbdbf73afe6b4'
74 unihash2 = 'af36b199320e611fbb16f1f277d3ee1d619ca58b'
Brad Bishopa34c0302019-09-23 22:34:48 -040075 result = self.client.report_unihash(taskhash2, self.METHOD, outhash, unihash2)
76 self.assertEqual(result['unihash'], unihash, 'Server returned bad unihash')
Brad Bishop19323692019-04-05 15:28:33 -040077
78 def test_duplicate_taskhash(self):
79 # Tests that duplicate reports of the same taskhash with different
80 # outhash & unihash always return the unihash from the first reported
81 # taskhash
82 taskhash = '8aa96fcffb5831b3c2c0cb75f0431e3f8b20554a'
83 outhash = 'afe240a439959ce86f5e322f8c208e1fedefea9e813f2140c81af866cc9edf7e'
84 unihash = '218e57509998197d570e2c98512d0105985dffc9'
Brad Bishopa34c0302019-09-23 22:34:48 -040085 self.client.report_unihash(taskhash, self.METHOD, outhash, unihash)
Brad Bishop19323692019-04-05 15:28:33 -040086
Brad Bishopa34c0302019-09-23 22:34:48 -040087 result = self.client.get_unihash(self.METHOD, taskhash)
88 self.assertEqual(result, unihash)
Brad Bishop19323692019-04-05 15:28:33 -040089
90 outhash2 = '0904a7fe3dc712d9fd8a74a616ddca2a825a8ee97adf0bd3fc86082c7639914d'
91 unihash2 = 'ae9a7d252735f0dafcdb10e2e02561ca3a47314c'
Brad Bishopa34c0302019-09-23 22:34:48 -040092 self.client.report_unihash(taskhash, self.METHOD, outhash2, unihash2)
Brad Bishop19323692019-04-05 15:28:33 -040093
Brad Bishopa34c0302019-09-23 22:34:48 -040094 result = self.client.get_unihash(self.METHOD, taskhash)
95 self.assertEqual(result, unihash)
Brad Bishop19323692019-04-05 15:28:33 -040096
97 outhash3 = '77623a549b5b1a31e3732dfa8fe61d7ce5d44b3370f253c5360e136b852967b4'
98 unihash3 = '9217a7d6398518e5dc002ed58f2cbbbc78696603'
Brad Bishopa34c0302019-09-23 22:34:48 -040099 self.client.report_unihash(taskhash, self.METHOD, outhash3, unihash3)
Brad Bishop19323692019-04-05 15:28:33 -0400100
Brad Bishopa34c0302019-09-23 22:34:48 -0400101 result = self.client.get_unihash(self.METHOD, taskhash)
102 self.assertEqual(result, unihash)
103
Andrew Geissler475cb722020-07-10 16:00:51 -0500104 def test_huge_message(self):
105 # Simple test that hashes can be created
106 taskhash = 'c665584ee6817aa99edfc77a44dd853828279370'
107 outhash = '3c979c3db45c569f51ab7626a4651074be3a9d11a84b1db076f5b14f7d39db44'
108 unihash = '90e9bc1d1f094c51824adca7f8ea79a048d68824'
109
110 result = self.client.get_unihash(self.METHOD, taskhash)
111 self.assertIsNone(result, msg='Found unexpected task, %r' % result)
112
113 siginfo = "0" * (self.client.max_chunk * 4)
114
115 result = self.client.report_unihash(taskhash, self.METHOD, outhash, unihash, {
116 'outhash_siginfo': siginfo
117 })
118 self.assertEqual(result['unihash'], unihash, 'Server returned bad unihash')
119
120 result = self.client.get_taskhash(self.METHOD, taskhash, True)
121 self.assertEqual(result['taskhash'], taskhash)
122 self.assertEqual(result['unihash'], unihash)
123 self.assertEqual(result['method'], self.METHOD)
124 self.assertEqual(result['outhash'], outhash)
125 self.assertEqual(result['outhash_siginfo'], siginfo)
126
Brad Bishopa34c0302019-09-23 22:34:48 -0400127 def test_stress(self):
128 def query_server(failures):
129 client = Client(self.server.address)
130 try:
131 for i in range(1000):
132 taskhash = hashlib.sha256()
133 taskhash.update(str(i).encode('utf-8'))
134 taskhash = taskhash.hexdigest()
135 result = client.get_unihash(self.METHOD, taskhash)
136 if result != taskhash:
137 failures.append("taskhash mismatch: %s != %s" % (result, taskhash))
138 finally:
139 client.close()
140
141 # Report hashes
142 for i in range(1000):
143 taskhash = hashlib.sha256()
144 taskhash.update(str(i).encode('utf-8'))
145 taskhash = taskhash.hexdigest()
146 self.client.report_unihash(taskhash, self.METHOD, taskhash, taskhash)
147
148 failures = []
149 threads = [threading.Thread(target=query_server, args=(failures,)) for t in range(100)]
150
151 for t in threads:
152 t.start()
153
154 for t in threads:
155 t.join()
156
157 self.assertFalse(failures)
Brad Bishop19323692019-04-05 15:28:33 -0400158
159
Brad Bishopa34c0302019-09-23 22:34:48 -0400160class TestHashEquivalenceUnixServer(TestHashEquivalenceServer, unittest.TestCase):
161 def get_server_addr(self):
162 return "unix://" + os.path.join(self.temp_dir.name, 'sock')
163
164
165class TestHashEquivalenceTCPServer(TestHashEquivalenceServer, unittest.TestCase):
166 def get_server_addr(self):
Andrew Geisslerc3d88e42020-10-02 09:45:00 -0500167 # Some hosts cause asyncio module to misbehave, when IPv6 is not enabled.
168 # If IPv6 is enabled, it should be safe to use localhost directly, in general
169 # case it is more reliable to resolve the IP address explicitly.
170 return socket.gethostbyname("localhost") + ":0"
171