networklocation: add geocoder and JNI Rust tests
- Commit
61399763006c0e3dc090e95d0b3190fe9f9c56ab- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-18 16:13 UTC (17 days ago)
- Parents
dcb8559
Raise network_location_position_estimation_rust line coverage from 67.5% to 84.8%: a synthetic v5 geocoder DB fixture (reverse/forward/prefix search, grid index, malformed-input rejection) plus JNI wrapper tests for estimatePosition and the GeocoderNative fd store. No production logic changed. Verified with cargo test -p network_location_position_estimation_rust (65 passed) and cargo llvm-cov (84.79%).
Diff
networklocation/src/main/rust/Cargo.toml
diff --git a/networklocation/src/main/rust/Cargo.toml b/networklocation/src/main/rust/Cargo.tomlindex 78e3666a02..51ed49fde6 100644--- a/networklocation/src/main/rust/Cargo.toml+++ b/networklocation/src/main/rust/Cargo.toml@@ -23,3 +23,4 @@ libc.workspace = true [dev-dependencies] rand = { workspace = true, features = ["std_rng"] }+jni = { workspace = true, features = ["invocation"] }
networklocation/src/main/rust/src/geocoder_tests.rs
diff --git a/networklocation/src/main/rust/src/geocoder_tests.rs b/networklocation/src/main/rust/src/geocoder_tests.rsindex fe932ebc43..fa4a7a9e62 100644--- a/networklocation/src/main/rust/src/geocoder_tests.rs+++ b/networklocation/src/main/rust/src/geocoder_tests.rs@@ -262,3 +262,134 @@ fn dict_rejects_bad_geometry() { bad[8..12].copy_from_slice(&5u32.to_be_bytes()); assert!(Dict::new(&bad, 0, bad.len()).is_none()); }++// ---------------------------------------------------------------------------+// Whole-database fixture: 3 records in 2 grid cells, in the exact v5 layout+// `Reader::from_map` walks. Rec 0 is an address, rec 1 a POI in the same+// cell, rec 2 a place in a neighbouring cell.+// ---------------------------------------------------------------------------++const REC_COLS: [[i32; 10]; 3] = [+ [3_777_493, -12_241_942, -1, 0, 0, 0, 0, 0, 0, 1],+ [3_777_500, -12_241_900, 0, -1, 0, 0, 0, 0, -1, 3],+ [3_800_000, -12_200_000, 1, -1, 1, 0, 0, 0, 1, 4],+];++fn cell_of(lat_e5: i32, lon_e5: i32) -> i64 {+ ((lat_e5 as i64 - MIN_LAT as i64) / CELL) * COLS + (lon_e5 as i64 - MIN_LON as i64) / CELL+}++fn synthetic_db() -> Vec<u8> {+ let dicts: [&[&str]; DICTS] = [+ &["Alpha Cafe", "Beta Park", "Gamma Plaza"],+ &["10", "20"],+ &["Main St", "Oak Ave"],+ &["Springfield"],+ &["Illinois"],+ &["US"],+ &["00001", "00002"],+ ];+ let mut bodies: Vec<Vec<u8>> = dicts.iter().map(|d| dict_body(d, 2)).collect();+ for c in 0..COLUMNS {+ let vals: Vec<i32> = REC_COLS.iter().map(|r| r[c]).collect();+ bodies.push(column_body(&vals, DELTA[c]));+ }+ let mut grid = Vec::new();+ push_be32(&mut grid, 2);+ grid.extend_from_slice(&cell_of(REC_COLS[0][0], REC_COLS[0][1]).to_be_bytes());+ push_be32(&mut grid, 0);+ grid.extend_from_slice(&cell_of(REC_COLS[2][0], REC_COLS[2][1]).to_be_bytes());+ push_be32(&mut grid, 2);+ bodies.push(grid);+ bodies.push(column_body(&[0, 1, 2], true));+ bodies.push(column_body(&[0, 1], true));+ bodies.push(column_body(&[1, 2], false));+ assert_eq!(bodies.len(), SECTIONS);++ let mut out = Vec::new();+ push_be32(&mut out, MAGIC);+ push_be32(&mut out, VERSION);+ push_be32(&mut out, REC_COLS.len() as u32);+ for b in &bodies {+ push_be32(&mut out, b.len() as u32);+ out.extend_from_slice(b);+ }+ out+}++fn open_synthetic(bytes: &[u8]) -> Reader {+ use std::sync::atomic::{AtomicUsize, Ordering};+ static SEQ: AtomicUsize = AtomicUsize::new(0);+ let path = std::env::temp_dir().join(format!("geocoder-synthetic-{}-{}.db", std::process::id(), SEQ.fetch_add(1, Ordering::Relaxed)));+ std::fs::write(&path, bytes).expect("write synthetic db");+ let r = Reader::open_path(&path);+ let _ = std::fs::remove_file(&path);+ r.expect("synthetic db parses")+}++#[test]+fn synthetic_db_reverse_resolve_and_grid() {+ let mut r = open_synthetic(&synthetic_db());+ assert_eq!(r.n, 3);+ assert_eq!(r.grid_index(cell_of(REC_COLS[0][0], REC_COLS[0][1])), Some(0));+ assert_eq!(r.grid_index(cell_of(REC_COLS[2][0], REC_COLS[2][1])), Some(1));+ assert_eq!(r.grid_index(42), None);++ // Exact record position resolves to that record (distance zero wins).+ assert_eq!(r.reverse(37.77493, -122.41942), Some(0));+ // A hair east is nearer the POI than the address.+ assert_eq!(r.reverse(37.775_00, -122.419_00), Some(1));+ // The far cell's record, and nothing near the Gulf of Guinea.+ assert_eq!(r.reverse(38.0, -122.0), Some(2));+ assert_eq!(r.reverse(0.0, 0.0), None);++ let a = r.resolve(0);+ assert_eq!(a, ["37.77493", "-122.41942", "", "10", "Main St", "Springfield", "Illinois", "US", "00001", "1"]);+ let b = r.resolve(1);+ assert_eq!(b[2], "Alpha Cafe");+ assert_eq!(b[3], "", "missing house id resolves empty");+ assert_eq!(b[8], "", "missing postcode id resolves empty");+ assert_eq!(b[9], "3");+ let c = r.resolve(2);+ assert_eq!(c[2], "Beta Park");+ assert_eq!(c[4], "Oak Ave");+ assert_eq!(c[9], "4");+}++#[test]+fn synthetic_db_forward_and_name_search() {+ let mut r = open_synthetic(&synthetic_db());+ assert_eq!(r.forward("US", "Illinois", "Springfield", "Main St", 10), vec![0, 1]);+ assert_eq!(r.forward("US", "Illinois", "Springfield", "Main St", 1), vec![0]);+ assert_eq!(r.forward("US", "Illinois", "Springfield", "Oak Ave", 10), vec![2]);+ assert!(r.forward("US", "Illinois", "Springfield", "Nowhere Rd", 10).is_empty());+ assert!(r.forward("CA", "Illinois", "Springfield", "Main St", 10).is_empty());++ assert_eq!(r.name_prefix_range("Alpha"), (0, 1));+ assert_eq!(r.name_prefix_range("Beta"), (1, 2));+ assert_eq!(r.search_name("Alpha", 10), vec![1]);+ assert_eq!(r.search_name("Beta", 10), vec![2]);+ assert_eq!(r.search_name("Gamma", 10), Vec::<i32>::new(), "in dict but no record");+ assert!(r.search_name("", 10).is_empty());+ assert!(r.search_name("Zzz", 10).is_empty());+ assert_eq!(r.compare_key(0, [0, 0, 0, 0]), 0);+ assert!(r.compare_key(2, [0, 0, 0, 0]) > 0);+ assert!(r.compare_key(0, [0, 0, 0, 1]) < 0);+}++#[test]+fn synthetic_db_rejects_bad_headers() {+ let good = synthetic_db();+ let mut bad_magic = good.clone();+ bad_magic[0..4].copy_from_slice(&0xDEAD_BEEFu32.to_be_bytes());+ let path = std::env::temp_dir().join(format!("geocoder-bad-{}-{}.db", std::process::id(), std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.as_nanos()).unwrap_or(0)));+ std::fs::write(&path, &bad_magic).expect("write");+ assert!(Reader::open_path(&path).is_none(), "bad magic rejected");+ let mut bad_ver = good.clone();+ bad_ver[4..8].copy_from_slice(&4u32.to_be_bytes());+ std::fs::write(&path, &bad_ver).expect("write");+ assert!(Reader::open_path(&path).is_none(), "old version rejected");+ std::fs::write(&path, &good[..good.len() / 2]).expect("write");+ assert!(Reader::open_path(&path).is_none(), "truncated db rejected");+ let _ = std::fs::remove_file(&path);+}
networklocation/src/main/rust/src/jni_tests.rs
diff --git a/networklocation/src/main/rust/src/jni_tests.rs b/networklocation/src/main/rust/src/jni_tests.rsnew file mode 100644index 0000000000..1a461dc556--- /dev/null+++ b/networklocation/src/main/rust/src/jni_tests.rs@@ -0,0 +1,277 @@+//! Host JVM tests for the JNI wrappers (`NetworkLocationNative`,+//! `GeocoderNative`, `WpsStoreNative`).+//!+//! The wrappers are thin, but they own the handle/null contracts Kotlin+//! relies on. These tests boot one host JVM, open a synthetic geocoder+//! database through a real file descriptor, and drive the exported symbols+//! exactly as the Kotlin loaders do.++use jni::objects::{JClass, JDoubleArray, JObjectArray, JString};+use jni::{Env, EnvUnowned, JavaVM};+use std::io::Cursor;+use std::os::unix::io::AsRawFd;+use std::sync::OnceLock;++static JVM: OnceLock<JavaVM> = OnceLock::new();++fn jvm() -> &'static JavaVM {+ JVM.get_or_init(|| {+ let args = jni::InitArgsBuilder::new().build().expect("JNI init args");+ JavaVM::new(args).expect("host JVM must start for JNI tests")+ })+}++fn with_env<R>(f: impl FnOnce(&mut Env) -> R) -> R {+ let mut out: Option<R> = None;+ jvm()+ .attach_current_thread(|env| {+ out = Some(f(env));+ Ok::<(), jni::errors::Error>(())+ })+ .expect("attach_current_thread");+ out.expect("closure ran")+}++fn un<'a>(env: &Env<'a>) -> EnvUnowned<'a> {+ unsafe { EnvUnowned::from_raw(env.get_raw()) }+}++// ---------------------------------------------------------- synthetic geodb+// Same v5 fixture as `geocoder::geocoder_tests`: 3 records in 2 grid cells.++fn compress(raw: &[u8]) -> Vec<u8> {+ ruzstd::encoding::compress_to_vec(Cursor::new(raw), ruzstd::encoding::CompressionLevel::Uncompressed)+}++fn zigzag(v: i32) -> u32 {+ ((v << 1) ^ (v >> 31)) as u32+}++fn push_varint(mut v: u32, out: &mut Vec<u8>) {+ loop {+ let b = (v & 0x7F) as u8;+ v >>= 7;+ if v == 0 {+ out.push(b);+ return;+ }+ out.push(b | 0x80);+ }+}++fn be32(out: &mut Vec<u8>, v: u32) {+ out.extend_from_slice(&v.to_be_bytes());+}++fn column_body(vals: &[i32], delta: bool) -> Vec<u8> {+ let mut raw = Vec::new();+ let mut prev = 0i32;+ for &v in vals {+ let dv = if delta { v.wrapping_sub(prev) } else { v };+ prev = v;+ push_varint(zigzag(dv), &mut raw);+ }+ let comp = compress(&raw);+ let mut out = Vec::new();+ be32(&mut out, vals.len() as u32);+ be32(&mut out, 1);+ be32(&mut out, raw.len() as u32);+ be32(&mut out, comp.len() as u32);+ out.extend_from_slice(&comp);+ out+}++fn dict_body(strings: &[&str], per: usize) -> Vec<u8> {+ let blocks: Vec<&[&str]> = strings.chunks(per).collect();+ let firsts: Vec<u8> = blocks.iter().flat_map(|b| b[0].bytes()).collect();+ let mut out = Vec::new();+ be32(&mut out, strings.len() as u32);+ be32(&mut out, per as u32);+ be32(&mut out, blocks.len() as u32);+ let mut end = 0u32;+ for b in &blocks {+ end += b[0].len() as u32;+ be32(&mut out, end);+ }+ out.extend_from_slice(&firsts);+ let mut end = 0u32;+ let mut comps = Vec::new();+ for b in &blocks {+ let mut raw = Vec::new();+ for s in *b {+ be32(&mut raw, s.len() as u32);+ raw.extend_from_slice(s.as_bytes());+ }+ let c = compress(&raw);+ end += c.len() as u32;+ be32(&mut out, end);+ comps.push(c);+ }+ for c in comps {+ out.extend_from_slice(&c);+ }+ out+}++fn synthetic_db() -> Vec<u8> {+ const CELL: i64 = 5_000;+ const COLS: i64 = 7_200;+ const MIN_LAT: i32 = -9_000_000;+ const MIN_LON: i32 = -18_000_000;+ const REC: [[i32; 10]; 3] = [+ [3_777_493, -12_241_942, -1, 0, 0, 0, 0, 0, 0, 1],+ [3_777_500, -12_241_900, 0, -1, 0, 0, 0, 0, -1, 3],+ [3_800_000, -12_200_000, 1, -1, 1, 0, 0, 0, 1, 4],+ ];+ const DELTA: [bool; 10] = [true, true, false, false, false, false, false, false, false, false];+ let cell = |r: &[i32; 10]| ((r[0] as i64 - MIN_LAT as i64) / CELL) * COLS + (r[1] as i64 - MIN_LON as i64) / CELL;+ let dicts: [&[&str]; 7] = [+ &["Alpha Cafe", "Beta Park", "Gamma Plaza"],+ &["10", "20"],+ &["Main St", "Oak Ave"],+ &["Springfield"],+ &["Illinois"],+ &["US"],+ &["00001", "00002"],+ ];+ let mut bodies: Vec<Vec<u8>> = dicts.iter().map(|d| dict_body(d, 2)).collect();+ for c in 0..10 {+ bodies.push(column_body(&REC.iter().map(|r| r[c]).collect::<Vec<_>>(), DELTA[c]));+ }+ let mut grid = Vec::new();+ be32(&mut grid, 2);+ grid.extend_from_slice(&cell(&REC[0]).to_be_bytes());+ be32(&mut grid, 0);+ grid.extend_from_slice(&cell(&REC[2]).to_be_bytes());+ be32(&mut grid, 2);+ bodies.push(grid);+ bodies.push(column_body(&[0, 1, 2], true));+ bodies.push(column_body(&[0, 1], true));+ bodies.push(column_body(&[1, 2], false));++ let mut out = Vec::new();+ be32(&mut out, 0x4D41_4745);+ be32(&mut out, 5);+ be32(&mut out, 3);+ for b in &bodies {+ be32(&mut out, b.len() as u32);+ out.extend_from_slice(b);+ }+ out+}++fn read_strings(env: &mut Env, raw: jni::sys::jobjectArray) -> Vec<String> {+ assert!(!raw.is_null(), "expected non-null string array");+ let arr = unsafe { JObjectArray::<JString>::from_raw(env, raw) };+ let n = arr.len(env).expect("len");+ (0..n)+ .map(|i| {+ let s = arr.get_element(env, i).expect("element");+ s.try_to_string(env).expect("string")+ })+ .collect()+}++#[test]+fn estimate_position_via_jni() {+ use crate::jni::Java_com_vayunmathur_networklocation_NetworkLocationNative_estimatePosition as estimate;+ with_env(|env| {+ fn mk<'a>(env: &mut Env<'a>, vals: &[f64]) -> JDoubleArray<'a> {+ let a = env.new_double_array(vals.len()).expect("array");+ a.set_region(env, 0, vals).expect("set");+ a+ }+ // Three consistent beacons around (37.0, -122.0).+ let beacons = mk(&mut *env, &[37.0, -122.0, 50.0, 37.0001, -122.0, 50.0, 37.0, -121.9999, 50.0]);+ let raw = estimate(un(&*env), JClass::null(), beacons);+ assert!(!raw.is_null());+ let arr = unsafe { JDoubleArray::from_raw(&*env, raw) };+ let mut buf = [0f64; 3];+ #[allow(deprecated)]+ env.get_double_array_region(&arr, 0, &mut buf).expect("region");+ assert!((buf[0] - 37.0).abs() < 0.01, "lat {}", buf[0]);+ assert!((buf[1] + 122.0).abs() < 0.01, "lon {}", buf[1]);+ assert!(buf[2] > 0.0, "accuracy {}", buf[2]);++ // No usable input yields null, never a bogus fix.+ let empty = mk(&mut *env, &[]);+ assert!(estimate(un(&*env), JClass::null(), empty).is_null());+ let ragged = mk(&mut *env, &[37.0, -122.0]);+ assert!(estimate(un(&*env), JClass::null(), ragged).is_null());+ env.exception_clear();+ });+}++#[test]+fn geocoder_via_jni() {+ use crate::geocoder::{+ Java_com_vayunmathur_networklocation_GeocoderNative_close as geo_close,+ Java_com_vayunmathur_networklocation_GeocoderNative_forward as geo_forward,+ Java_com_vayunmathur_networklocation_GeocoderNative_open as geo_open,+ Java_com_vayunmathur_networklocation_GeocoderNative_reverse as geo_reverse,+ Java_com_vayunmathur_networklocation_GeocoderNative_searchName as geo_search,+ };+ let path = std::env::temp_dir().join(format!("geocoder-jni-{}.db", std::process::id()));+ std::fs::write(&path, synthetic_db()).expect("write db");+ let file = std::fs::File::open(&path).expect("open db");+ let fd = file.as_raw_fd();++ with_env(|env| {+ let handle = geo_open(un(&*env), JClass::null(), fd, 0, 0);+ assert_ne!(handle, 0, "synthetic db opens through the JNI entry");++ let raw = geo_reverse(un(&*env), JClass::null(), handle, 37.77493, -122.41942);+ let fields = read_strings(&mut *env, raw);+ assert_eq!(fields.len(), 10);+ assert_eq!(fields[4], "Main St");+ assert_eq!(fields[5], "Springfield");+ assert_eq!(fields[7], "US");++ let (c, s, ci, st) = (+ env.new_string("US").expect("s"),+ env.new_string("Illinois").expect("s"),+ env.new_string("Springfield").expect("s"),+ env.new_string("Main St").expect("s"),+ );+ let raw = geo_forward(un(&*env), JClass::null(), handle, c, s, ci, st, 10);+ let hits = read_strings(&mut *env, raw);+ assert_eq!(hits.len(), 20, "two Main St records x 10 fields");++ let p = env.new_string("Alpha").expect("s");+ let raw = geo_search(un(&*env), JClass::null(), handle, p, 10);+ let hits = read_strings(&mut *env, raw);+ assert_eq!(hits.len(), 10);+ assert_eq!(hits[2], "Alpha Cafe");++ // Null-handle contract: every query returns null.+ let raw = geo_reverse(un(&*env), JClass::null(), 0, 37.0, -122.0);+ assert!(raw.is_null());+ geo_close(un(&*env), JClass::null(), handle);+ geo_close(un(&*env), JClass::null(), 0);+ env.exception_clear();+ });+ drop(file);+ let _ = std::fs::remove_file(&path);++ // A bad descriptor fails open cleanly (handle 0), for both stores.+ with_env(|env| {+ assert_eq!(geo_open(un(&*env), JClass::null(), -1, 0, 0), 0);+ use crate::wpsdb::Java_com_vayunmathur_networklocation_WpsStoreNative_open as wps_open;+ assert_eq!(wps_open(un(&*env), JClass::null(), -1, 0, 0), 0);+ env.exception_clear();+ });+}++#[test]+fn wps_store_null_handle_contract() {+ use crate::wpsdb::{+ Java_com_vayunmathur_networklocation_WpsStoreNative_close as wps_close,+ Java_com_vayunmathur_networklocation_WpsStoreNative_lookup as wps_lookup,+ };+ with_env(|env| {+ let raw = wps_lookup(un(&*env), JClass::null(), 0, 0, 0);+ assert!(raw.is_null(), "handle 0 looks up nothing");+ wps_close(un(&*env), JClass::null(), 0);+ env.exception_clear();+ });+}
networklocation/src/main/rust/src/lib.rs
diff --git a/networklocation/src/main/rust/src/lib.rs b/networklocation/src/main/rust/src/lib.rsindex 4ca44bfdbb..8e1573328e 100644--- a/networklocation/src/main/rust/src/lib.rs+++ b/networklocation/src/main/rust/src/lib.rs@@ -24,6 +24,9 @@ pub mod multilateration; pub mod position; pub mod wpsdb; +#[cfg(test)]+mod jni_tests;+ /// Estimated position. pub struct EstimatedPosition { /// estimated position