]> www.average.org Git - loctrkd.git/blob - test/test_storage.py
Update changelog for 2.00 release
[loctrkd.git] / test / test_storage.py
1 """ Send junk to the collector """
2
3 from random import Random
4 from socket import getaddrinfo, socket, AF_INET, SOCK_STREAM
5 from sqlite3 import connect
6 from time import sleep
7 from typing import Any
8 import unittest
9 from .common import send_and_drain, TestWithServers
10 from loctrkd.zx303proto import *
11 from loctrkd.zx303proto import (
12     STATUS,
13     WIFI_POSITIONING,
14     WIFI_OFFLINE_POSITIONING,
15     WIFI_POSITIONING,
16     LOGIN,
17     HIBERNATION,
18     SETUP,
19 )
20 from loctrkd.ocid_dload import SCHEMA
21
22
23 class Storage(TestWithServers):
24     def setUp(self, *args: str, **kwargs: Any) -> None:
25         super().setUp(
26             "collector", "storage", "rectifier", "termconfig", verbose=True
27         )
28         with connect(self.conf.get("opencellid", "dbfn")) as ldb:
29             ldb.execute(SCHEMA)
30             ldb.executemany(
31                 """insert into cells
32                     (radio, mcc, net, area, cell, unit, lon, lat, range,
33                      samples, changeable, created, updated, averageSignal)
34                     values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""",
35                 (
36                     (
37                         "GSM",
38                         262,
39                         3,
40                         24420,
41                         16594,
42                         -1,
43                         12.681939,
44                         53.52603,
45                         22733,
46                         1999,
47                         1,
48                         1556575612,
49                         1653387028,
50                         0,
51                     ),
52                     (
53                         "GSM",
54                         262,
55                         3,
56                         24420,
57                         36243,
58                         -1,
59                         12.66442,
60                         53.527534,
61                         21679,
62                         1980,
63                         1,
64                         1540870608,
65                         1653387028,
66                         0,
67                     ),
68                     (
69                         "GSM",
70                         262,
71                         3,
72                         24420,
73                         17012,
74                         -1,
75                         12.741093,
76                         53.529854,
77                         23463,
78                         874,
79                         1,
80                         1563404603,
81                         1653268184,
82                         0,
83                     ),
84                     (
85                         "GSM",
86                         262,
87                         3,
88                         24420,
89                         26741,
90                         -1,
91                         12.658822,
92                         53.530832,
93                         18809,
94                         1687,
95                         1,
96                         1539939964,
97                         1653265176,
98                         0,
99                     ),
100                     (
101                         "GSM",
102                         262,
103                         2,
104                         24420,
105                         36243,
106                         -1,
107                         12.61111,
108                         53.536626,
109                         1000,
110                         4,
111                         1,
112                         1623218739,
113                         1652696033,
114                         0,
115                     ),
116                     (
117                         "GSM",
118                         262,
119                         1,
120                         24420,
121                         36243,
122                         -1,
123                         12.611135,
124                         53.536636,
125                         1000,
126                         3,
127                         1,
128                         1568587946,
129                         1628827437,
130                         0,
131                     ),
132                     (
133                         "GSM",
134                         262,
135                         2,
136                         24420,
137                         17012,
138                         -1,
139                         12.829655,
140                         53.536654,
141                         1000,
142                         2,
143                         1,
144                         1609913384,
145                         1612934718,
146                         0,
147                     ),
148                     (
149                         "GSM",
150                         262,
151                         3,
152                         24000,
153                         35471,
154                         -1,
155                         11.505135,
156                         53.554216,
157                         11174,
158                         829,
159                         1,
160                         1544494558,
161                         1651063300,
162                         0,
163                     ),
164                     (
165                         "GSM",
166                         262,
167                         3,
168                         24420,
169                         37156,
170                         -1,
171                         11.918188,
172                         53.870522,
173                         1000,
174                         1,
175                         1,
176                         1550199983,
177                         1550199983,
178                         0,
179                     ),
180                 ),
181             )
182             ldb.commit()
183         for fam, typ, pro, cnm, skadr in getaddrinfo(
184             "127.0.0.1",
185             self.conf.getint("collector", "port"),
186             family=AF_INET,
187             type=SOCK_STREAM,
188         ):
189             break  # Just take the first element
190         self.sock = socket(AF_INET, SOCK_STREAM)
191         self.sock.connect(skadr)
192
193     def tearDown(self) -> None:
194         sleep(1)  # give collector some time
195         super().tearDown()
196
197     def test_storage(self) -> None:
198         for msg in (
199             LOGIN.In(imei="9999123456780000", ver=9),
200             WIFI_POSITIONING.In(
201                 mnc=3,
202                 mcc=262,
203                 wifi_aps=[
204                     ("02:03:04:05:06:07", -89),
205                     ("92:93:94:95:96:97", -70),
206                 ],
207                 gsm_cells=[
208                     (24420, 27178, -90),
209                     (24420, 36243, -78),
210                     (24420, 17012, -44),
211                 ],
212             ),
213             SETUP.In(),
214             STATUS.In(signal=87),
215             HIBERNATION.In(),
216         ):
217             print("Send:", msg)
218             send_and_drain(self.sock, b"xx" + msg.packed + b"\r\n")
219         sleep(1)
220         self.sock.close()
221         got = set()
222         with connect(self.conf.get("storage", "dbfn")) as db:
223             for is_incoming, packet in db.execute(
224                 "select is_incoming, packet from events"
225             ):
226                 msg = parse_message(packet, is_incoming=is_incoming)
227                 print("Stored:", msg)
228                 got.add(type(msg))
229         self.assertEqual(
230             got,
231             {
232                 LOGIN.Out,
233                 HIBERNATION.In,
234                 LOGIN.In,
235                 SETUP.In,
236                 SETUP.Out,
237                 STATUS.Out,
238                 STATUS.In,
239                 WIFI_POSITIONING.In,
240                 WIFI_POSITIONING.Out,
241             },
242         )
243
244
245 if __name__ == "__main__":
246     unittest.main()