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