blob: 806b54c5ebdcdff085d7bd148737777e716fe802 [file] [log] [blame]
Brad Bishop19323692019-04-05 15:28:33 -04001#! /usr/bin/env python3
2#
3# Copyright (C) 2018 Garmin Ltd.
4#
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License version 2 as
7# published by the Free Software Foundation.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License along
15# with this program; if not, write to the Free Software Foundation, Inc.,
16# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18import unittest
19import threading
20import sqlite3
21import hashlib
22import urllib.request
23import json
24from . import create_server
25
26class TestHashEquivalenceServer(unittest.TestCase):
27 def setUp(self):
28 # Start an in memory hash equivalence server in the background bound to
29 # an ephemeral port
30 db = sqlite3.connect(':memory:', check_same_thread=False)
31 self.server = create_server(('localhost', 0), db)
32 self.server_addr = 'http://localhost:%d' % self.server.socket.getsockname()[1]
33 self.server_thread = threading.Thread(target=self.server.serve_forever)
34 self.server_thread.start()
35
36 def tearDown(self):
37 # Shutdown server
38 s = getattr(self, 'server', None)
39 if s is not None:
40 self.server.shutdown()
41 self.server_thread.join()
42 self.server.server_close()
43
44 def send_get(self, path):
45 url = '%s/%s' % (self.server_addr, path)
46 request = urllib.request.Request(url)
47 response = urllib.request.urlopen(request)
48 return json.loads(response.read().decode('utf-8'))
49
50 def send_post(self, path, data):
51 headers = {'content-type': 'application/json'}
52 url = '%s/%s' % (self.server_addr, path)
53 request = urllib.request.Request(url, json.dumps(data).encode('utf-8'), headers)
54 response = urllib.request.urlopen(request)
55 return json.loads(response.read().decode('utf-8'))
56
57 def test_create_hash(self):
58 # Simple test that hashes can be created
59 taskhash = '35788efcb8dfb0a02659d81cf2bfd695fb30faf9'
60 outhash = '2765d4a5884be49b28601445c2760c5f21e7e5c0ee2b7e3fce98fd7e5970796f'
61 unihash = 'f46d3fbb439bd9b921095da657a4de906510d2cd'
62
63 d = self.send_get('v1/equivalent?method=TestMethod&taskhash=%s' % taskhash)
64 self.assertIsNone(d, msg='Found unexpected task, %r' % d)
65
66 d = self.send_post('v1/equivalent', {
67 'taskhash': taskhash,
68 'method': 'TestMethod',
69 'outhash': outhash,
70 'unihash': unihash,
71 })
72 self.assertEqual(d['unihash'], unihash, 'Server returned bad unihash')
73
74 def test_create_equivalent(self):
75 # Tests that a second reported task with the same outhash will be
76 # assigned the same unihash
77 taskhash = '53b8dce672cb6d0c73170be43f540460bfc347b4'
78 outhash = '5a9cb1649625f0bf41fc7791b635cd9c2d7118c7f021ba87dcd03f72b67ce7a8'
79 unihash = 'f37918cc02eb5a520b1aff86faacbc0a38124646'
80 d = self.send_post('v1/equivalent', {
81 'taskhash': taskhash,
82 'method': 'TestMethod',
83 'outhash': outhash,
84 'unihash': unihash,
85 })
86 self.assertEqual(d['unihash'], unihash, 'Server returned bad unihash')
87
88 # Report a different task with the same outhash. The returned unihash
89 # should match the first task
90 taskhash2 = '3bf6f1e89d26205aec90da04854fbdbf73afe6b4'
91 unihash2 = 'af36b199320e611fbb16f1f277d3ee1d619ca58b'
92 d = self.send_post('v1/equivalent', {
93 'taskhash': taskhash2,
94 'method': 'TestMethod',
95 'outhash': outhash,
96 'unihash': unihash2,
97 })
98 self.assertEqual(d['unihash'], unihash, 'Server returned bad unihash')
99
100 def test_duplicate_taskhash(self):
101 # Tests that duplicate reports of the same taskhash with different
102 # outhash & unihash always return the unihash from the first reported
103 # taskhash
104 taskhash = '8aa96fcffb5831b3c2c0cb75f0431e3f8b20554a'
105 outhash = 'afe240a439959ce86f5e322f8c208e1fedefea9e813f2140c81af866cc9edf7e'
106 unihash = '218e57509998197d570e2c98512d0105985dffc9'
107 d = self.send_post('v1/equivalent', {
108 'taskhash': taskhash,
109 'method': 'TestMethod',
110 'outhash': outhash,
111 'unihash': unihash,
112 })
113
114 d = self.send_get('v1/equivalent?method=TestMethod&taskhash=%s' % taskhash)
115 self.assertEqual(d['unihash'], unihash)
116
117 outhash2 = '0904a7fe3dc712d9fd8a74a616ddca2a825a8ee97adf0bd3fc86082c7639914d'
118 unihash2 = 'ae9a7d252735f0dafcdb10e2e02561ca3a47314c'
119 d = self.send_post('v1/equivalent', {
120 'taskhash': taskhash,
121 'method': 'TestMethod',
122 'outhash': outhash2,
123 'unihash': unihash2
124 })
125
126 d = self.send_get('v1/equivalent?method=TestMethod&taskhash=%s' % taskhash)
127 self.assertEqual(d['unihash'], unihash)
128
129 outhash3 = '77623a549b5b1a31e3732dfa8fe61d7ce5d44b3370f253c5360e136b852967b4'
130 unihash3 = '9217a7d6398518e5dc002ed58f2cbbbc78696603'
131 d = self.send_post('v1/equivalent', {
132 'taskhash': taskhash,
133 'method': 'TestMethod',
134 'outhash': outhash3,
135 'unihash': unihash3
136 })
137
138 d = self.send_get('v1/equivalent?method=TestMethod&taskhash=%s' % taskhash)
139 self.assertEqual(d['unihash'], unihash)
140
141