Skip to content

Commit

Permalink
[rust] Fix bug if file exists but less than 1024 bytes long
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristopherRabotin committed Dec 29, 2023
1 parent dbd8c5e commit 5a96b87
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 34 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,9 @@ jobs:

- name: CLI SPK
run: |
cargo run --bin anise-cli --workspace --exclude anise-py -- inspect data/gmat-hermite.bsp
cargo run --bin anise-cli --workspace --exclude anise-py -- inspect data/de440.bsp
cargo build --bin anise-cli --workspace --exclude anise-py
./target/debug/anise-cli inspect data/gmat-hermite.bsp
./target/debug/anise-cli --workspace --exclude anise-py -- inspect data/de440.bsp
- name: Rust-SPICE JPL DE validation
run: RUST_BACKTRACE=1 cargo test validate_jplde --features spkezr_validation --release --workspace --exclude anise-gui --exclude anise-py -- --nocapture --include-ignored --test-threads 1
Expand Down
1 change: 1 addition & 0 deletions anise-py/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ venv/
*.egg-info/
.installed.cfg
*.egg
.ruff_cache

# Installer logs
pip-log.txt
Expand Down
16 changes: 14 additions & 2 deletions anise/src/naif/daf/daf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,20 @@ impl<R: NAIFSummaryRecord> DAF<R> {
}

pub fn file_record(&self) -> Result<FileRecord, DAFError> {
let file_record = FileRecord::read_from(&self.bytes[..FileRecord::SIZE]).unwrap();
let file_record = FileRecord::read_from(
self.bytes
.get(..FileRecord::SIZE)
.ok_or_else(|| DecodingError::InaccessibleBytes {
start: 0,
end: FileRecord::SIZE,
size: self.bytes.len(),
})
.with_context(|_| DecodingDataSnafu {
idx: 0_usize,
kind: R::NAME,
})?,
)
.unwrap();
// Check that the endian-ness is compatible with this platform.
file_record
.endianness()
Expand Down Expand Up @@ -355,7 +368,6 @@ impl<R: NAIFSummaryRecord> DAF<R> {
}

/// Writes the contents of this DAF file to a new location.
pub fn persist<P: AsRef<Path>>(&self, path: P) -> IoResult<()> {
let mut fs = File::create(path)?;

Expand Down
4 changes: 1 addition & 3 deletions anise/tests/ephemerides/parent_translation_verif.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ fn invalid_load_from_static() {

#[test]
fn de438s_parent_translation_verif() {
if pretty_env_logger::try_init().is_err() {
println!("could not init env_logger");
}
let _ = pretty_env_logger::try_init();

let bytes = file2heap!("../data/de440s.bsp").unwrap();
let de438s = SPK::parse(bytes).unwrap();
Expand Down
4 changes: 1 addition & 3 deletions anise/tests/ephemerides/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ use anise::prelude::*;
/// Tests that direct path computations match what SPICE returned to within good precision.
#[test]
fn common_root_verif() {
if pretty_env_logger::try_init().is_err() {
println!("could not init env_logger");
}
let _ = pretty_env_logger::try_init();

// SLS Launch epoch!!! IT'S LIIIIVEE!!
let epoch = Epoch::from_str("2022-11-15T23:47:36+06:00").unwrap();
Expand Down
4 changes: 1 addition & 3 deletions anise/tests/ephemerides/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ const VELOCITY_EPSILON_KM_S: f64 = 5e-10;
#[ignore = "Requires Rust SPICE -- must be executed serially"]
#[test]
fn de440s_transform_verif_venus2emb() {
if pretty_env_logger::try_init().is_err() {
println!("could not init env_logger");
}
let _ = pretty_env_logger::try_init();

let spk_path = "../data/de440s.bsp";
let bpc_path = "../data/earth_latest_high_prec.bpc";
Expand Down
12 changes: 3 additions & 9 deletions anise/tests/ephemerides/translation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ const VELOCITY_EPSILON_KM_S: f64 = 5e-9;

#[test]
fn de440s_translation_verif_venus2emb() {
if pretty_env_logger::try_init().is_err() {
println!("could not init env_logger");
}
let _ = pretty_env_logger::try_init();

// "Load" the file via a memory map (avoids allocations)
let path = "../data/de440s.bsp";
Expand Down Expand Up @@ -107,9 +105,7 @@ fn de440s_translation_verif_venus2emb() {

#[test]
fn de438s_translation_verif_venus2luna() {
if pretty_env_logger::try_init().is_err() {
println!("could not init env_logger");
}
let _ = pretty_env_logger::try_init();

// "Load" the file via a memory map (avoids allocations)
let path = "../data/de440s.bsp";
Expand Down Expand Up @@ -199,9 +195,7 @@ fn de438s_translation_verif_venus2luna() {

#[test]
fn de438s_translation_verif_emb2luna() {
if pretty_env_logger::try_init().is_err() {
println!("could not init env_logger");
}
let _ = pretty_env_logger::try_init();

// "Load" the file via a memory map (avoids allocations)
let path = "../data/de440s.bsp";
Expand Down
4 changes: 1 addition & 3 deletions anise/tests/ephemerides/validation/compare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,7 @@ impl CompareEphem {
output_file_name: String,
num_queries_per_pair: usize,
) -> Self {
if pretty_env_logger::try_init().is_err() {
println!("could not init env_logger");
}
let _ = pretty_env_logger::try_init();

// Build the schema
let schema = Schema::new(vec![
Expand Down
22 changes: 13 additions & 9 deletions anise/tests/naif.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ use anise::{

#[test]
fn test_binary_pck_load() {
if pretty_env_logger::try_init().is_err() {
println!("could not init env_logger");
}
let _ = pretty_env_logger::try_init();

// Using the DE421 as demo because the correct data is in the DAF documentation
let filename = "../data/earth_latest_high_prec.bpc";
Expand All @@ -50,9 +48,7 @@ fn test_binary_pck_load() {

#[test]
fn test_spk_load_bytes() {
if pretty_env_logger::try_init().is_err() {
println!("could not init env_logger");
}
let _ = pretty_env_logger::try_init();

// Using the DE421 as demo because the correct data is in the DAF documentation
let bytes = file2heap!("../data/de421.bsp").unwrap();
Expand Down Expand Up @@ -167,9 +163,7 @@ fn test_spk_load_bytes() {

#[test]
fn test_spk_rename_summary() {
if pretty_env_logger::try_init().is_err() {
println!("could not init env_logger");
}
let _ = pretty_env_logger::try_init();

let path = "../data/variable-seg-size-hermite.bsp";

Expand All @@ -188,3 +182,13 @@ fn test_spk_rename_summary() {

example_data.persist("../target/rename-test.bsp").unwrap();
}

#[test]
fn test_invalid_load() {
let _ = pretty_env_logger::try_init();

// Check that it doesn't fail if the file does not exist
assert!(BPC::load("i_dont_exist.bpc").is_err());
// Check that a file that's too small does not panic
assert!(BPC::load("../.gitattributes").is_err());
}

0 comments on commit 5a96b87

Please sign in to comment.