Skip to content

Commit

Permalink
added a few more tests for request module
Browse files Browse the repository at this point in the history
  • Loading branch information
robamu committed Aug 20, 2024
1 parent c3e6f64 commit 7502f05
Showing 1 changed file with 67 additions and 1 deletion.
68 changes: 67 additions & 1 deletion src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,14 +549,66 @@ pub mod alloc_mod {

#[cfg(test)]
mod tests {
use spacepackets::util::UbfU16;
use std::string::String;

use spacepackets::{
cfdp::tlv::{msg_to_user::MsgToUserTlv, ReadableTlv},
util::UbfU16,
};

use super::*;

pub const DEST_ID: UbfU16 = UbfU16::new(5);

#[test]
fn test_put_request_basic() {
let src_file = "/tmp/hello.txt";
let dest_file = "/tmp/hello2.txt";
let put_request = PutRequest::new(
DEST_ID.into(),
Some(src_file),
Some(dest_file),
None,
None,
None,
None,
None,
None,
None,
)
.unwrap();
let identical_request =
PutRequest::new_regular_request(DEST_ID.into(), src_file, dest_file, None, None)
.unwrap();
assert_eq!(put_request, identical_request);
}

#[test]
fn test_put_request_path_checks() {
let mut invalid_path = String::from("/tmp/");
invalid_path += "a".repeat(u8::MAX as usize).as_str();
let dest_file = "/tmp/hello2.txt";
let error =
PutRequest::new_regular_request(DEST_ID.into(), &invalid_path, dest_file, None, None);
assert!(error.is_err());
let error = error.unwrap_err();
assert_eq!(u8::MAX as usize + 5, error.0);
}

#[test]
fn test_put_request_path_checks_dest_file() {
let mut invalid_path = String::from("/tmp/");
invalid_path += "a".repeat(u8::MAX as usize).as_str();
let source_file = "/tmp/hello2.txt";
let error =
PutRequest::new_regular_request(DEST_ID.into(), source_file, &invalid_path, None, None);
assert!(error.is_err());
let error = error.unwrap_err();
assert_eq!(u8::MAX as usize + 5, error.0);
}

#[test]
fn test_put_request_basic_small_ctor() {
let src_file = "/tmp/hello.txt";
let dest_file = "/tmp/hello2.txt";
let put_request =
Expand Down Expand Up @@ -641,4 +693,18 @@ mod tests {
assert_eq!(put_request_cached.static_fields.dest_file_len, 0);
assert_eq!(put_request_cached.opts_len(), 0);
}

#[test]
fn test_messages_to_user_ctor() {
let msg_to_user = MsgToUserTlv::new(&[1, 2, 3]).expect("creating message to user failed");
let put_request = PutRequestOwned::new_msgs_to_user_only(DEST_ID.into(), &[msg_to_user])
.expect("creating msgs to user only put request failed");
let msg_to_user_iter = put_request.msgs_to_user();
assert!(msg_to_user_iter.is_some());
let msg_to_user_iter = msg_to_user_iter.unwrap();
for msg_to_user_tlv in msg_to_user_iter {
assert_eq!(msg_to_user_tlv.value(), msg_to_user.value());
assert_eq!(msg_to_user_tlv.tlv_type().unwrap(), TlvType::MsgToUser);
}
}
}

0 comments on commit 7502f05

Please sign in to comment.