use rand::prelude::*; use rand_chacha::ChaCha8Rng; use crate::*; use core::sync::atomic::{AtomicU64, Ordering}; const UPPER_BOUND: usize = 9000; const LOWER_BOUND: usize = 12; // needs to be big enough to compute murmur3 hash + hold checksum static RNG_LOCAL_STATE: AtomicU64 = AtomicU64::new(xous::TESTING_RNG_SEED); fn gen_key(dictname: &str, keynum: usize, lower_size_bound: usize, upper_size_bound: usize) -> (String, Vec::) { let mut rng = ChaCha8Rng::seed_from_u64(RNG_LOCAL_STATE.load(Ordering::SeqCst)); // we want roughly half our keys to be in the small bin, and half in the large bin let keylen = if rng.gen_bool(0.5) { rng.gen_range(lower_size_bound..VPAGE_SIZE) } else { rng.gen_range(VPAGE_SIZE..upper_size_bound) }; // record the owning dictionary name & length with the key. This isn't mandatory for a key name, // but it helps the test checking program check things. let keyname = format!("sanitycheck|{}|key{}|len{}", dictname, keynum, keylen); let mut keydata = Vec::::new(); for i in 0..keylen-4 { // the data starts with a number equal to the key number, and increments from there keydata.push((keynum + i) as u8); } // a checksum is appended to each run of data // copy the stored data, and pad it out to a multiple of word lengths with 0's so we can compute the checksum let mut checkdata = Vec::::new(); for &b in &keydata { checkdata.push(b); } while checkdata.len() % 4 != 0 { checkdata.push(0); } let checksum = murmur3_32(&checkdata, 0); keydata.append(&mut checksum.to_le_bytes().to_vec()); RNG_LOCAL_STATE.store(rng.next_u64(), Ordering::SeqCst); (keyname, keydata) } pub(crate) fn create_basis_testcase(hw: &mut PddbOs, basis_cache: &mut BasisCache, maybe_num_dicts: Option, maybe_num_keys: Option, maybe_key_sizes: Option<(usize, usize)>, maybe_extra_reserved: Option, ) { hw.pddb_format(false).unwrap(); let sys_basis = hw.pddb_mount().expect("couldn't mount system basis"); basis_cache.basis_add(sys_basis); let num_dicts = maybe_num_dicts.unwrap_or(4); let num_keys = maybe_num_keys.unwrap_or(34); let (key_lower_bound, key_upper_bound) = maybe_key_sizes.unwrap_or((LOWER_BOUND, UPPER_BOUND - 4)); // 4 bytes for the CI checksum let extra_reserved = maybe_extra_reserved.unwrap_or(0); // make all the directories first for dictnum in 1..=num_dicts { let dictname = format!("dict{}", dictnum); basis_cache.dict_add(hw, &dictname, None).unwrap(); } // now add keys of various sizes, striping through each dictionary. Adding the keys cross-dictionary // exercises the large pool allocator, as it is shared across multiple dictionaries. for keynum in 1..=num_keys { for dictnum in 1..=num_dicts { let dictname = format!("dict{}", dictnum); let (keyname, keydata) = gen_key(&dictname, keynum, key_lower_bound, key_upper_bound); // now we're ready to write it out if maybe_extra_reserved.is_none() { // we do this slightly funky way instead of just passing Some(0) because we want to test the "None" path explicitly basis_cache.key_update(hw, &dictname, &keyname, &keydata, None, None, None, false).unwrap(); } else { basis_cache.key_update(hw, &dictname, &keyname, &keydata, None, Some(keydata.len() + extra_reserved), None, false).unwrap(); } } } } /// Delete & add dictionary consistency check pub(crate) fn delete_add_dict_consistency(hw: &mut PddbOs, basis_cache: &mut BasisCache, maybe_num_dicts: Option, maybe_num_keys: Option, maybe_key_sizes: Option<(usize, usize)>, maybe_extra_reserved: Option, basis_name: Option<&str> ) { let evict_count = maybe_num_dicts.unwrap_or(1); let num_keys = maybe_num_keys.unwrap_or(36); let (key_lower_bound, key_upper_bound) = maybe_key_sizes.unwrap_or((LOWER_BOUND, UPPER_BOUND - 4)); let extra_reserved = maybe_extra_reserved.unwrap_or(0); let dict_list = basis_cache.dict_list(hw, None); let dict_start_index = dict_list.len(); for (evicted, evict_dict) in dict_list.iter().enumerate() { if evicted < evict_count { match basis_cache.dict_remove(hw, evict_dict, basis_name, false) { Ok(_) => {}, Err(e) => log::error!("Error evicting dictionary {}: {:?}", evict_dict, e), } } else { break; } } for keynum in 1..=num_keys { for dictnum in 1..=evict_count { let dictname = format!("dict{}", dictnum + dict_start_index); let (keyname, keydata) = gen_key(&dictname, keynum, key_lower_bound, key_upper_bound); // now we're ready to write it out if maybe_extra_reserved.is_none() { // we do this slightly funky way instead of just passing Some(0) because we want to test the "None" path explicitly basis_cache.key_update(hw, &dictname, &keyname, &keydata, None, None, basis_name, false).unwrap(); } else { basis_cache.key_update(hw, &dictname, &keyname, &keydata, None, Some(keydata.len() + extra_reserved), basis_name, false).unwrap(); } } } } /// patch data check pub(crate) fn patch_test(hw: &mut PddbOs, basis_cache: &mut BasisCache, maybe_patch_offset: Option, maybe_patch_data: Option, extend: bool) { let patch_offset = maybe_patch_offset.unwrap_or(5); let patch_data = maybe_patch_data.unwrap_or("patched!".to_string()); let dict_list = basis_cache.dict_list(hw, None); for dict in dict_list.iter() { if let Ok(key_list) = basis_cache.key_list(hw, dict, None) { for key in key_list.iter() { // this actually does something a bit more complicated on a multi-basis system than you'd think: // it will get the union of all key names, and then patch the *latest open basis* only with new data. // note: if the key didn't already exist in the latest open basis, it's added, with just that patch data in it. // to override this behavior, you'd want to specify a _specific_ basis, but for testing purposes, we only have one // so we're doing this way that would lead to surprising results if it were copied as template code elsewhere. match basis_cache.key_update(hw, dict, key, patch_data.as_bytes(), Some(patch_offset), None, None, false) { Ok(_) => (), Err(e) => { log::error!("couldn't update key {}:{} with {} bytes data offset {}: {:?}", dict, key, patch_data.as_bytes().len(), patch_offset, e); panic!("error updating patch key"); } } // now fix the CI checksum. structured as two separate patches. not because it's efficient, // but because it exercises the code harder. let mut patchbuf = [0u8; UPPER_BOUND]; let readlen = basis_cache.key_read(hw, dict, key, &mut patchbuf, Some(0), None).unwrap(); if !extend { // now re-compute the checksum let mut checkdata = Vec::::new(); for &b in &patchbuf[..readlen-4] { checkdata.push(b); } log::trace!("checkdata len: {}", checkdata.len()); while checkdata.len() % 4 != 0 { checkdata.push(0); } let checksum = murmur3_32(&checkdata, 0); basis_cache.key_update(hw, dict, key, &checksum.to_le_bytes(), Some(readlen-4), None, None, false).unwrap(); } else { // this gloms a new checksum onto the existing record, adding 4 new bytes. let mut checkdata = Vec::::new(); for &b in &patchbuf[..readlen] { checkdata.push(b); } log::trace!("checkdata len: {}", checkdata.len()); while checkdata.len() % 4 != 0 { checkdata.push(0); } let checksum = murmur3_32(&checkdata, 0); basis_cache.key_update(hw, dict, key, &checksum.to_le_bytes(), Some(readlen), None, None, false).unwrap(); } } } } } pub(crate) fn delete_pattern(hw: &mut PddbOs, basis_cache: &mut BasisCache, maybe_num_dicts: Option, maybe_num_keys: Option, maybe_key_sizes: Option<(usize, usize)>, maybe_extra_reserved: Option, ) { let evict_count = maybe_num_dicts.unwrap_or(2); let num_keys = maybe_num_keys.unwrap_or(36); let (key_lower_bound, key_upper_bound) = maybe_key_sizes.unwrap_or((LOWER_BOUND, UPPER_BOUND - 4)); let extra_reserved = maybe_extra_reserved.unwrap_or(0); let mut evicted_dicts = Vec::::new(); let dict_list = basis_cache.dict_list(hw, None); let mut evict_iter = 0; for (evicted, evict_dict) in dict_list.iter().enumerate() { if evicted < evict_count { for (index, key) in basis_cache.key_list(hw, evict_dict, None).unwrap().iter().enumerate() { if index % 2 == (evict_iter % 2) { // exercise odd/even patterns log::info!("deleting {}:{}", evict_dict, key); basis_cache.key_remove(hw, evict_dict, key,None, false).unwrap(); let da = basis_cache.dict_attributes(hw, evict_dict, None).unwrap(); log::info!("{:?}", da); } } } else { break; } evicted_dicts.push(evict_dict.to_string()); evict_iter += 1; } for dict in basis_cache.dict_list(hw, None).iter() { let da = basis_cache.dict_attributes(hw, dict, None).unwrap(); log::debug!("{:?}", da); } for keynum in 1..=num_keys { for dictname in evicted_dicts.iter() { let (keyname, keydata) = gen_key(dictname, keynum, key_lower_bound, key_upper_bound); // now we're ready to write it out if maybe_extra_reserved.is_none() { // we do this slightly funky way instead of just passing Some(0) because we want to test the "None" path explicitly basis_cache.key_update(hw, dictname, &keyname, &keydata, None, None, None, false).unwrap(); } else { basis_cache.key_update(hw, dictname, &keyname, &keydata, None, Some(keydata.len() + extra_reserved), None, false).unwrap(); } } } for dict in basis_cache.dict_list(hw, None).iter() { let da = basis_cache.dict_attributes(hw, dict, None).unwrap(); log::debug!("{:?}", da); } log::info!("doing all-basis sync"); basis_cache.sync(hw, None).unwrap(); log::info!("all-basis sync done"); list_all(hw, basis_cache); for dict in basis_cache.dict_list(hw, None).iter() { let da = basis_cache.dict_attributes(hw, dict, None).unwrap(); log::info!("{:?}", da); } } pub(crate) fn list_all(hw: &mut PddbOs, basis_cache: &mut BasisCache) { // now list all the attributes of the basis for dict in basis_cache.dict_list(hw, None).iter() { let da = match basis_cache.dict_attributes(hw, &dict, None) { Ok(da) => da, Err(e) => { log::warn!("dictionary {} not in sub-basis (this is normal for multi-basis systems, {:?}", dict, e); continue; } }; log::debug!("{:?}", da); let mut sanity_count = 0; for key in basis_cache.key_list(hw, dict, None).unwrap().iter() { let attrs = match basis_cache.key_attributes(hw, dict, key, None) { Ok(a) => a, Err(e) => {log::debug!("key not in basis, searching another basis, {:?}", e); continue}, }; log::debug!("{}:{}=>{:?}", dict, key, attrs); sanity_count += 1; } log::info!("sanity check count: {}", sanity_count); } }