def test_cell_agrees_with_lac(self): # This test checks that when a cell is at a lat/lon that # is inside its enclosing LAC, we accept it and tighten # our accuracy accordingly. session = self.db_slave_session key = dict(mcc=BRAZIL_MCC, mnc=VIVO_MNC, lac=12345) data = [ Cell(lat=SAO_PAULO_LAT + 0.002, lon=SAO_PAULO_LON + 0.002, range=1000, radio=RADIO_TYPE['gsm'], cid=6789, **key), CellArea(lat=SAO_PAULO_LAT, lon=SAO_PAULO_LON, range=10000, radio=RADIO_TYPE['gsm'], **key), ] session.add_all(data) session.flush() result = self._make_query(data={ "cell": [dict(radio="gsm", cid=6789, **key)]}) self.assertEqual(result, {'lat': SAO_PAULO_LAT + 0.002, 'lon': SAO_PAULO_LON + 0.002, 'accuracy': CELL_MIN_ACCURACY}) self.check_stats( counter=[ ('m.cell_lac_hit', 0), ('m.cell_hit', 1), ] )
def test_cell_disagrees_with_lac(self): # This test checks that when a cell is at a lat/lon that # is not in the LAC associated with it, we drop back # to the LAC. This likely represents some kind of internal # database consistency error, but it might also just be a # new cell that hasn't been integrated yet or something. session = self.db_slave_session key = dict(mcc=BRAZIL_MCC, mnc=VIVO_MNC, lac=12345) data = [ Cell(lat=PORTO_ALEGRE_LAT, lon=PORTO_ALEGRE_LON, range=1000, radio=RADIO_TYPE['gsm'], cid=6789, **key), CellArea(lat=SAO_PAULO_LAT, lon=SAO_PAULO_LON, range=10000, radio=RADIO_TYPE['gsm'], **key), ] session.add_all(data) session.flush() result = self._make_query( data={"cell": [dict(radio="gsm", cid=6789, **key)]}) self.assertEqual(result, {'lat': SAO_PAULO_LAT, 'lon': SAO_PAULO_LON, 'accuracy': LAC_MIN_ACCURACY}) self.check_stats( counter=[ ('m.cell_lac_hit', 1), ] )
def test_cell_hit_ignores_lac(self): session = self.db_slave_session lat = PARIS_LAT lon = PARIS_LON key = dict(mcc=FRANCE_MCC, mnc=2, lac=3) data = [ Cell(lat=lat, lon=lon, range=1000, radio=2, cid=4, **key), Cell(lat=lat + 0.002, lon=lon + 0.004, range=1000, radio=2, cid=5, **key), Cell(lat=lat + 0.006, lon=lon + 0.006, range=1000, radio=2, cid=6, **key), CellArea(lat=lat + 0.0026666, lon=lon + 0.0033333, radio=2, range=50000, **key), ] session.add_all(data) session.flush() result = self._make_query( data={"cell": [dict(radio="umts", cid=5, **key)]}) self.assertEqual(result, {'lat': PARIS_LAT + 0.002, 'lon': PARIS_LON + 0.004, 'accuracy': CELL_MIN_ACCURACY})
def test_cell_multiple_country_codes_from_mcc(self): session = self.db_slave_session cell_key = { 'radio': RADIO_TYPE['gsm'], 'mcc': GB_MCC, 'mnc': 1, 'lac': 1, } session.add(Cell( lat=GB_LAT, lon=GB_LON, range=6000, cid=1, **cell_key)) session.add(CellArea( lat=GB_LAT, lon=GB_LON, range=9000, **cell_key)) session.flush() # Without a GeoIP, the mcc results in 4 different equally common # mcc values, GB not being the first one. We need to make sure # that we accept any of the country codes as a possible match # and don't discard otherwise good cell data based on this. result = self._make_query(data={'cell': [dict(cid=1, **cell_key)]}) self.assertEqual(result, {'lat': GB_LAT, 'lon': GB_LON, 'accuracy': 6000}) self.check_stats( counter=[ 'm.cell_hit', ], )
def test_cell_miss_lac_hit(self): session = self.db_slave_session lat = PARIS_LAT lon = PARIS_LON key = dict(mcc=FRANCE_MCC, mnc=2, lac=3) umts = RADIO_TYPE['umts'] data = [ Cell(lat=lat, lon=lon, radio=umts, cid=4, **key), Cell(lat=lat + 0.002, lon=lon + 0.004, radio=umts, cid=5, **key), Cell(lat=lat + 0.006, lon=lon + 0.006, radio=umts, cid=6, **key), CellArea(lat=lat + 0.0026666, lon=lon + 0.0033333, radio=umts, range=500000, **key), ] session.add_all(data) session.flush() result = self._make_query( data={"cell": [dict(radio="umts", cid=7, **key)]}, api_key_log=True) self.assertEqual(result, {'lat': PARIS_LAT + 0.0026666, 'lon': PARIS_LON + 0.0033333, 'accuracy': 500000}) self.check_stats( counter=[ 'm.cell_lac_hit', 'm.api_log.test.cell_lac_hit', ('m.api_log.test.cell_hit', 0), ('m.api_log.test.cell_miss', 0), ], )
def test_cell(self): session = self.db_slave_session london = self.geoip_data['London'] cell_key = { 'radio': RADIO_TYPE['gsm'], 'mcc': GB_MCC, 'mnc': 1, 'lac': 1, } session.add(Cell( lat=GB_LAT, lon=GB_LON, range=6000, cid=1, **cell_key)) session.add(CellArea( lat=GB_LAT, lon=GB_LON, range=9000, **cell_key)) session.flush() result = self._make_query(data={'cell': [dict(cid=1, **cell_key)]}, client_addr=london['ip'], api_key_log=True) self.assertEqual(result, {'lat': GB_LAT, 'lon': GB_LON, 'accuracy': 6000}) self.check_stats( counter=[ 'm.cell_hit', ('m.geoip_hit', 0), 'm.api_log.test.cell_hit', ('m.api_log.test.geoip_hit', 0), ], )
def test_cell_multiple_lac_hit(self): session = self.db_slave_session lat = PARIS_LAT lon = PARIS_LON gsm = RADIO_TYPE['gsm'] key = dict(mcc=FRANCE_MCC, mnc=2, lac=3) key2 = dict(mcc=FRANCE_MCC, mnc=2, lac=4) expected_lac = CellArea( lat=lat + 0.2, lon=lon + 0.2, radio=gsm, range=20000, **key) data = [ Cell(lat=lat + 0.02, lon=lon + 0.02, radio=gsm, cid=4, range=2000, **key2), Cell(lat=lat + 0.04, lon=lon + 0.04, radio=gsm, cid=5, range=3000, **key2), Cell(lat=lat + 0.2, lon=lon + 0.4, radio=gsm, cid=5, range=1000, **key), CellArea(lat=lat, lon=lon, radio=gsm, range=30000, **key2), expected_lac, ] session.add_all(data) session.flush() # We have two lacs, both with two cells, but only know about # one cell in one of them and two in the other. # The lac with two known cells wins and we use both their # positions to calculate the final result. result = self._make_query(data={ "cell": [ dict(radio="gsm", cid=4, **key), dict(radio="gsm", cid=9, **key), dict(radio="gsm", cid=4, **key2), dict(radio="gsm", cid=5, **key2), ] }) self.assertEqual(result, {'lat': expected_lac.lat, 'lon': expected_lac.lon, 'accuracy': expected_lac.range})
def test_cell_multiple_lac_lower_range_wins(self): session = self.db_slave_session lat = PARIS_LAT lon = PARIS_LON gsm = RADIO_TYPE['gsm'] key = dict(mcc=FRANCE_MCC, mnc=2, lac=3) key2 = dict(mcc=FRANCE_MCC, mnc=2, lac=4) expected_lac = CellArea( lat=lat + 0.2, lon=lon + 0.2, radio=gsm, range=10000, **key) data = [ Cell(lat=lat + 0.02, lon=lon + 0.02, radio=gsm, cid=4, range=2000, **key2), Cell(lat=lat + 0.2, lon=lon + 0.4, radio=gsm, cid=4, range=4000, **key), CellArea(lat=lat, lon=lon, radio=gsm, range=20000, **key2), expected_lac, ] session.add_all(data) session.flush() # We have two lacs with each one known cell. # The lac with the smallest cell wins. result = self._make_query(data={ "cell": [ dict(radio="gsm", cid=4, **key), dict(radio="gsm", cid=4, **key2), ] }) self.assertEqual(result, {'lat': expected_lac.lat, 'lon': expected_lac.lon, 'accuracy': LAC_MIN_ACCURACY})
def test_cell_multiple_radio_lac_hit_with_min_lac_accuracy(self): session = self.db_slave_session lat = PARIS_LAT lon = PARIS_LON gsm = RADIO_TYPE['gsm'] lte = RADIO_TYPE['lte'] key = dict(mcc=FRANCE_MCC, mnc=3, lac=4) key2 = dict(mcc=FRANCE_MCC, mnc=2, lac=3) expected_lac = CellArea( lat=lat + 0.2, lon=lon + 0.2, radio=gsm, range=3000, **key) data = [ Cell(lat=lat + 0.01, lon=lon + 0.02, radio=lte, cid=4, range=2000, **key2), Cell(lat=lat + 0.2, lon=lon + 0.4, radio=gsm, cid=5, range=500, **key), CellArea(lat=lat, lon=lon, radio=lte, range=10000, **key2), expected_lac, ] session.add_all(data) session.flush() # GSM lac-only hit (cid 9 instead of 5) and a LTE cell hit result = self._make_query(data={ "cell": [ dict(radio="gsm", cid=9, **key), dict(radio="lte", cid=4, **key2), ] }) self.assertEqual(result, {'lat': expected_lac.lat, 'lon': expected_lac.lon, 'accuracy': LAC_MIN_ACCURACY})
def test_scan_lacs_remove(self): session = self.db_master_session redis_client = self.redis_client # create an orphaned lac entry key = dict(radio=1, mcc=1, mnc=1, lac=1) session.add(CellArea(**key)) session.flush() enqueue_lacs(session, redis_client, [CellArea.to_hashkey(key)], UPDATE_KEY['cell_lac']) # after scanning the orphaned record gets removed self.assertEqual(scan_lacs.delay().get(), 1) lacs = session.query(CellArea).all() self.assertEqual(lacs, [])
def test_geoip_mcc_mismatch(self): session = self.db_slave_session gsm = RADIO_TYPE['gsm'] bhutan = self.geoip_data['Bhutan'] key = {'mcc': USA_MCC, 'mnc': 1, 'lac': 1, 'cid': 1} key2 = {'mcc': USA_MCC, 'mnc': 1, 'lac': 1, } session.add(Cell(radio=gsm, lat=FREMONT_LAT, lon=FREMONT_LON, range=1000, **key)) session.add(CellArea(radio=gsm, lat=FREMONT_LAT, lon=FREMONT_LON, range=10000, **key2)) session.flush() result = self._make_query(data={'cell': [dict(radio='gsm', **key)]}, client_addr=bhutan['ip']) self.assertEqual(result, {'lat': FREMONT_LAT, 'lon': FREMONT_LON, 'accuracy': CELL_MIN_ACCURACY})
def test_lac_miss(self): session = self.db_slave_session key = dict(mcc=FRANCE_MCC, mnc=2, lac=3) lat = PARIS_LAT lon = PARIS_LON gsm = RADIO_TYPE['gsm'] data = [ Cell(lat=lat, lon=lon, radio=gsm, cid=4, **key), Cell(lat=lat + 0.002, lon=lon + 0.004, radio=gsm, cid=5, **key), Cell(lat=1.006, lon=1.006, radio=gsm, cid=6, **key), CellArea(lat=1.0026666, lon=1.0033333, radio=gsm, range=50000, **key), ] session.add_all(data) session.flush() result = self._make_query( data={"cell": [dict(radio="gsm", mcc=FRANCE_MCC, mnc=2, lac=4, cid=5)]}) self.assertTrue(result is None)
def test_wifi_agrees_with_cell_and_lac(self): # This test checks that when a wifi is at a lat/lon that # is inside its enclosing LAC and cell, we accept it and # tighten our accuracy accordingly. session = self.db_slave_session key = dict(mcc=BRAZIL_MCC, mnc=VIVO_MNC, lac=12345) wifi1 = dict(key="1234567890ab") wifi2 = dict(key="1234890ab567") wifi3 = dict(key="4321890ab567") lat = SAO_PAULO_LAT + 0.002 lon = SAO_PAULO_LON + 0.002 data = [ Wifi(lat=lat, lon=lon, **wifi1), Wifi(lat=lat, lon=lon, **wifi2), Wifi(lat=lat, lon=lon, **wifi3), Cell(lat=SAO_PAULO_LAT, lon=SAO_PAULO_LON, range=1000, radio=RADIO_TYPE['gsm'], cid=6789, **key), CellArea(lat=SAO_PAULO_LAT, lon=SAO_PAULO_LON, range=10000, radio=RADIO_TYPE['gsm'], **key), ] session.add_all(data) session.flush() result = self._make_query(data={ "cell": [dict(radio="gsm", cid=6789, **key)], "wifi": [wifi1, wifi2, wifi3]}) self.assertEqual(result, {'lat': SAO_PAULO_LAT + 0.002, 'lon': SAO_PAULO_LON + 0.002, 'accuracy': WIFI_MIN_ACCURACY}) self.check_stats( counter=[ ('m.wifi_hit', 1), ] )
def test_wifi_disagrees_with_lac(self): # This test checks that when a wifi is at a lat/lon that # is not in the LAC associated with our query, we drop back # to the LAC. session = self.db_slave_session key = dict(mcc=BRAZIL_MCC, mnc=VIVO_MNC, lac=12345) wifi1 = dict(key="1234567890ab") wifi2 = dict(key="1234890ab567") wifi3 = dict(key="4321890ab567") lat = PORTO_ALEGRE_LAT lon = PORTO_ALEGRE_LON data = [ Wifi(lat=lat, lon=lon, **wifi1), Wifi(lat=lat, lon=lon, **wifi2), Wifi(lat=lat, lon=lon, **wifi3), CellArea(lat=SAO_PAULO_LAT, lon=SAO_PAULO_LON, range=10000, radio=RADIO_TYPE['gsm'], **key), ] session.add_all(data) session.flush() result = self._make_query(data={ "cell": [dict(radio="gsm", cid=6789, **key)], "wifi": [wifi1, wifi2, wifi3], }) self.assertEqual(result, {'lat': SAO_PAULO_LAT, 'lon': SAO_PAULO_LON, 'accuracy': LAC_MIN_ACCURACY}) self.check_stats( counter=[ ('m.wifi_hit', 0), ('m.cell_lac_hit', 1), ] )