Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
translate ▾

Modern-Apps.git / networklocation / src / test / java / com / vayunmathur / networklocation / BeaconKeysTest.kt

BeaconKeysTest.kt141 lines · 5.4 KiBRaw
1package com.vayunmathur.networklocation
2
3import kotlin.test.Test
4import kotlin.test.assertEquals
5import kotlin.test.assertFalse
6import kotlin.test.assertNotEquals
7import kotlin.test.assertNull
8import kotlin.test.assertTrue
9import com.vayunmathur.networklocation.domain.BeaconKeys
10import com.vayunmathur.networklocation.domain.BeaconId
11import com.vayunmathur.networklocation.domain.RadioType
12
13/**
14 * Pins the key packing against the store builder.
15 *
16 * These vectors are transcribed into `keys.rs` in `scripts/networklocation/wps_harvest` and
17 * asserted there too. Both sides must agree exactly: a packing mismatch does not throw or log,
18 * it just makes every offline lookup miss, which is indistinguishable from "the database does
19 * not have that beacon".
20 */
21class BeaconKeysTest {
22
23 @Test
24 fun `mac parses big-endian with the oui in the high bits`() {
25 assertEquals(0x001122334455L, BeaconKeys.parseMac("00:11:22:33:44:55"))
26 assertEquals(0xAABBCCDDEEFFL, BeaconKeys.parseMac("aa:bb:cc:dd:ee:ff"))
27 assertEquals(0xAABBCCDDEEFFL, BeaconKeys.parseMac("AA:BB:CC:DD:EE:FF"))
28 assertEquals(0L, BeaconKeys.parseMac("00:00:00:00:00:00"))
29 assertEquals(0xFFFFFFFFFFFFL, BeaconKeys.parseMac("ff:ff:ff:ff:ff:ff"))
30 }
31
32 @Test
33 fun `malformed macs are rejected rather than silently truncated`() {
34 assertNull(BeaconKeys.parseMac(""))
35 assertNull(BeaconKeys.parseMac("00:11:22:33:44"))
36 assertNull(BeaconKeys.parseMac("00:11:22:33:44:55:66"))
37 assertNull(BeaconKeys.parseMac("00-11-22-33-44-55"))
38 assertNull(BeaconKeys.parseMac("zz:11:22:33:44:55"))
39 // A one-digit group would otherwise parse and shift the whole address.
40 assertNull(BeaconKeys.parseMac("0:11:22:33:44:55"))
41 // toIntOrNull(16) accepts a leading sign, which sign-extends into every higher octet.
42 assertNull(BeaconKeys.parseMac("-1:11:22:33:44:55"))
43 }
44
45 @Test
46 fun `locally administered and multicast macs are excluded`() {
47 // Bit 0x02 of the first octet: randomized or virtual, not a fixed AP.
48 assertTrue(BeaconKeys.isRandomizedMac(0xAABBCCDDEEFFL))
49 assertTrue(BeaconKeys.isRandomizedMac(0x020000000000L))
50 // Bit 0x01: multicast, never a real BSSID.
51 assertTrue(BeaconKeys.isRandomizedMac(0x010000000000L))
52 // A real, globally administered OUI.
53 assertFalse(BeaconKeys.isRandomizedMac(0x001122334455L))
54 assertFalse(BeaconKeys.isRandomizedMac(0xFCFBFB000000L))
55 // Only the first octet decides it.
56 assertFalse(BeaconKeys.isRandomizedMac(0x00FFFFFFFFFFL))
57 }
58
59 @Test
60 fun `cell key layout is mcc10 mnc10 radio4 area24 cid36`() {
61 val cell = BeaconId.Cell(
62 mcc = 310,
63 mnc = 260,
64 radio = RadioType.LTE,
65 cellId = 0xABCDEF123L,
66 tacOrLac = 12345,
67 )
68 assertEquals(0x4D904L, BeaconKeys.cellKeyHi(cell))
69 assertEquals(0x3003039ABCDEF123L, BeaconKeys.cellKeyLo(cell))
70 }
71
72 @Test
73 fun `radio type discriminates otherwise identical cells`() {
74 fun cell(radio: RadioType) =
75 BeaconId.Cell(mcc = 234, mnc = 15, radio = radio, cellId = 4242, tacOrLac = 999)
76
77 val keys = RadioType.entries.map { BeaconKeys.cellKeyLo(cell(it)) }
78 assertEquals(keys.size, keys.distinct().size, "each radio type must produce its own key")
79
80 // The specific pairing that used to collide: an LTE ECI and a GSM CID are the same
81 // number in the same PLMN and area code.
82 assertNotEquals(
83 BeaconKeys.cellKeyLo(cell(RadioType.LTE)),
84 BeaconKeys.cellKeyLo(cell(RadioType.GSM)),
85 )
86 }
87
88 @Test
89 fun `full 36-bit 5G nci survives packing`() {
90 val nci = 0xFFFFFFFFFL // 36 bits, all set
91 val cell = BeaconId.Cell(
92 mcc = 1,
93 mnc = 1,
94 radio = RadioType.NR,
95 cellId = nci,
96 tacOrLac = 0,
97 )
98 val lo = BeaconKeys.cellKeyLo(cell)
99 assertEquals(nci, lo and 0xFFFFFFFFFL, "the NCI must round-trip, not truncate to 28 bits")
100
101 // The old 28-bit field aliased these two onto the same key.
102 val truncated = cell.copy(cellId = nci and 0x0FFFFFFFL)
103 assertNotEquals(lo, BeaconKeys.cellKeyLo(truncated))
104 }
105
106 @Test
107 fun `24-bit 5G tac survives packing`() {
108 val tac = 0xFFFFFF // 24 bits, wider than the 16-bit LTE TAC
109 val cell = BeaconId.Cell(
110 mcc = 505,
111 mnc = 1,
112 radio = RadioType.NR,
113 cellId = 1,
114 tacOrLac = tac,
115 )
116 val lo = BeaconKeys.cellKeyLo(cell)
117 assertEquals(tac.toLong(), (lo ushr 36) and 0xFFFFFFL)
118 }
119
120 @Test
121 fun `cell keys stay inside the declared 84-bit universe`() {
122 val widest = BeaconId.Cell(
123 mcc = 999,
124 mnc = 999,
125 radio = RadioType.NR,
126 cellId = 0xFFFFFFFFFL,
127 tacOrLac = 0xFFFFFF,
128 )
129 val hi = BeaconKeys.cellKeyHi(widest)
130 val lo = BeaconKeys.cellKeyLo(widest)
131 assertTrue(hi >= 0 && hi < (1L shl (BeaconKeys.CELL_UNIVERSE_BITS - 64)))
132 // Bit 63 must stay clear so the (hi, lo) pair is unambiguous as two signed longs.
133 assertTrue(lo >= 0)
134 }
135
136 @Test
137 fun `wifi keys stay inside the declared 48-bit universe`() {
138 val mac = BeaconKeys.parseMac("ff:ff:ff:ff:ff:ff")!!
139 assertTrue(mac < (1L shl BeaconKeys.WIFI_UNIVERSE_BITS))
140 }
141}