Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 1 | #! /usr/bin/env python3 |
| 2 | # |
| 3 | # Copyright (C) 2018 Garmin Ltd. |
| 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 | |
| 8 | import unittest |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 9 | import multiprocessing |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 10 | import sqlite3 |
| 11 | import hashlib |
| 12 | import urllib.request |
| 13 | import json |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 14 | import tempfile |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 15 | from . import create_server |
| 16 | |
| 17 | class TestHashEquivalenceServer(unittest.TestCase): |
| 18 | def setUp(self): |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 19 | # Start a hash equivalence server in the background bound to |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 20 | # an ephemeral port |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 21 | self.dbfile = tempfile.NamedTemporaryFile(prefix="bb-hashserv-db-") |
| 22 | self.server = create_server(('localhost', 0), self.dbfile.name) |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 23 | self.server_addr = 'http://localhost:%d' % self.server.socket.getsockname()[1] |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 24 | self.server_thread = multiprocessing.Process(target=self.server.serve_forever) |
| 25 | self.server_thread.daemon = True |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 26 | self.server_thread.start() |
| 27 | |
| 28 | def tearDown(self): |
| 29 | # Shutdown server |
| 30 | s = getattr(self, 'server', None) |
| 31 | if s is not None: |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 32 | self.server_thread.terminate() |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 33 | self.server_thread.join() |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 34 | |
| 35 | def send_get(self, path): |
| 36 | url = '%s/%s' % (self.server_addr, path) |
| 37 | request = urllib.request.Request(url) |
| 38 | response = urllib.request.urlopen(request) |
| 39 | return json.loads(response.read().decode('utf-8')) |
| 40 | |
| 41 | def send_post(self, path, data): |
| 42 | headers = {'content-type': 'application/json'} |
| 43 | url = '%s/%s' % (self.server_addr, path) |
| 44 | request = urllib.request.Request(url, json.dumps(data).encode('utf-8'), headers) |
| 45 | response = urllib.request.urlopen(request) |
| 46 | return json.loads(response.read().decode('utf-8')) |
| 47 | |
| 48 | def test_create_hash(self): |
| 49 | # Simple test that hashes can be created |
| 50 | taskhash = '35788efcb8dfb0a02659d81cf2bfd695fb30faf9' |
| 51 | outhash = '2765d4a5884be49b28601445c2760c5f21e7e5c0ee2b7e3fce98fd7e5970796f' |
| 52 | unihash = 'f46d3fbb439bd9b921095da657a4de906510d2cd' |
| 53 | |
| 54 | d = self.send_get('v1/equivalent?method=TestMethod&taskhash=%s' % taskhash) |
| 55 | self.assertIsNone(d, msg='Found unexpected task, %r' % d) |
| 56 | |
| 57 | d = self.send_post('v1/equivalent', { |
| 58 | 'taskhash': taskhash, |
| 59 | 'method': 'TestMethod', |
| 60 | 'outhash': outhash, |
| 61 | 'unihash': unihash, |
| 62 | }) |
| 63 | self.assertEqual(d['unihash'], unihash, 'Server returned bad unihash') |
| 64 | |
| 65 | def test_create_equivalent(self): |
| 66 | # Tests that a second reported task with the same outhash will be |
| 67 | # assigned the same unihash |
| 68 | taskhash = '53b8dce672cb6d0c73170be43f540460bfc347b4' |
| 69 | outhash = '5a9cb1649625f0bf41fc7791b635cd9c2d7118c7f021ba87dcd03f72b67ce7a8' |
| 70 | unihash = 'f37918cc02eb5a520b1aff86faacbc0a38124646' |
| 71 | d = self.send_post('v1/equivalent', { |
| 72 | 'taskhash': taskhash, |
| 73 | 'method': 'TestMethod', |
| 74 | 'outhash': outhash, |
| 75 | 'unihash': unihash, |
| 76 | }) |
| 77 | self.assertEqual(d['unihash'], unihash, 'Server returned bad unihash') |
| 78 | |
| 79 | # Report a different task with the same outhash. The returned unihash |
| 80 | # should match the first task |
| 81 | taskhash2 = '3bf6f1e89d26205aec90da04854fbdbf73afe6b4' |
| 82 | unihash2 = 'af36b199320e611fbb16f1f277d3ee1d619ca58b' |
| 83 | d = self.send_post('v1/equivalent', { |
| 84 | 'taskhash': taskhash2, |
| 85 | 'method': 'TestMethod', |
| 86 | 'outhash': outhash, |
| 87 | 'unihash': unihash2, |
| 88 | }) |
| 89 | self.assertEqual(d['unihash'], unihash, 'Server returned bad unihash') |
| 90 | |
| 91 | def test_duplicate_taskhash(self): |
| 92 | # Tests that duplicate reports of the same taskhash with different |
| 93 | # outhash & unihash always return the unihash from the first reported |
| 94 | # taskhash |
| 95 | taskhash = '8aa96fcffb5831b3c2c0cb75f0431e3f8b20554a' |
| 96 | outhash = 'afe240a439959ce86f5e322f8c208e1fedefea9e813f2140c81af866cc9edf7e' |
| 97 | unihash = '218e57509998197d570e2c98512d0105985dffc9' |
| 98 | d = self.send_post('v1/equivalent', { |
| 99 | 'taskhash': taskhash, |
| 100 | 'method': 'TestMethod', |
| 101 | 'outhash': outhash, |
| 102 | 'unihash': unihash, |
| 103 | }) |
| 104 | |
| 105 | d = self.send_get('v1/equivalent?method=TestMethod&taskhash=%s' % taskhash) |
| 106 | self.assertEqual(d['unihash'], unihash) |
| 107 | |
| 108 | outhash2 = '0904a7fe3dc712d9fd8a74a616ddca2a825a8ee97adf0bd3fc86082c7639914d' |
| 109 | unihash2 = 'ae9a7d252735f0dafcdb10e2e02561ca3a47314c' |
| 110 | d = self.send_post('v1/equivalent', { |
| 111 | 'taskhash': taskhash, |
| 112 | 'method': 'TestMethod', |
| 113 | 'outhash': outhash2, |
| 114 | 'unihash': unihash2 |
| 115 | }) |
| 116 | |
| 117 | d = self.send_get('v1/equivalent?method=TestMethod&taskhash=%s' % taskhash) |
| 118 | self.assertEqual(d['unihash'], unihash) |
| 119 | |
| 120 | outhash3 = '77623a549b5b1a31e3732dfa8fe61d7ce5d44b3370f253c5360e136b852967b4' |
| 121 | unihash3 = '9217a7d6398518e5dc002ed58f2cbbbc78696603' |
| 122 | d = self.send_post('v1/equivalent', { |
| 123 | 'taskhash': taskhash, |
| 124 | 'method': 'TestMethod', |
| 125 | 'outhash': outhash3, |
| 126 | 'unihash': unihash3 |
| 127 | }) |
| 128 | |
| 129 | d = self.send_get('v1/equivalent?method=TestMethod&taskhash=%s' % taskhash) |
| 130 | self.assertEqual(d['unihash'], unihash) |
| 131 | |
| 132 | |