Skip to content

Commit

Permalink
added error handling and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joalopez1206 committed Dec 29, 2024
1 parent dfa7bc9 commit 7274028
Showing 1 changed file with 129 additions and 1 deletion.
130 changes: 129 additions & 1 deletion src/message/rdata/opt_rdata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ impl FromBytes<Result<Self, &'static str>> for OptRdata {

let option_data = OptionData::from_with_opt_type(option_data, option_code);

if let Err(_) = option_data {
return Err("Format Error");
}

let option_data = option_data.unwrap();

let option = OptOption::new(option_code, option_length, option_data);

opt_rdata.option.push(option);
Expand Down Expand Up @@ -116,7 +122,7 @@ impl fmt::Display for OptRdata {
for option in &self.option {
let option_code = option.get_option_code();
let option_length = option.get_option_len();
let option_data = option.get_opt_data().to_bytes();
let option_data = option.get_opt_data();
result.push_str(&format!("OPTION-CODE: {}\n", option_code));
result.push_str(&format!("OPTION-LENGTH: {}\n", option_length));
result.push_str(&format!("OPTION-DATA: {:?} \n", option_data));
Expand All @@ -132,6 +138,10 @@ impl fmt::Display for OptRdata {
#[cfg(test)]
mod opt_rdata_test{
use crate::message::rdata::opt_rdata::option_data::OptionData;
use crate::message::rdata::opt_rdata::option_code::OptionCode;
use crate::message::rdata::opt_rdata::ede_optdata::EdeOptData;
use crate::message::rdata::opt_rdata::ede_code::EdeCode;

use super::*;

#[test]
Expand Down Expand Up @@ -184,4 +194,122 @@ mod opt_rdata_test{

assert_eq!(opt_rdata.get_option(), option);
}

#[test]
fn test_opt_rdata_ede_stale_answer() {
let mut opt_rdata = OptRdata::new();

// Create EDE data
let code = EdeCode::StaleAns;
let msg = "Stale Answer".to_string();
let ede_data = EdeOptData::new(code, msg.clone());

// Wrap it in OptionData::EDE
let option_data = OptionData::EDE(ede_data);
// Determine the length for the OPT option
let option_data_bytes = option_data.to_bytes();
let option_len = option_data_bytes.len() as u16;

// Build an OptOption with OptionCode::EDE
let option = OptOption::new(OptionCode::EDE, option_len, option_data);
opt_rdata.option.push(option);

// Round-trip: to_bytes -> from_bytes
let serialized = opt_rdata.to_bytes();
let deserialized = OptRdata::from_bytes(&serialized, &serialized).unwrap();

// Check we got one option
let retrieved_options = deserialized.get_option();
assert_eq!(retrieved_options.len(), 1);

let retrieved_option = &retrieved_options[0];
assert_eq!(retrieved_option.get_option_code(), OptionCode::EDE);
assert_eq!(retrieved_option.get_option_len(), option_len);

// Now confirm the EDE contents
match retrieved_option.get_opt_data() {
OptionData::EDE(ede) => {
assert_eq!(ede.get_err_code(), EdeCode::StaleAns);
assert_eq!(ede.get_err_message(), msg);
}
_ => panic!("Expected OptionData::EDE, got something else!"),
}
}

/// 2) Test an EDE "DNSSEC Bogus" code
#[test]
fn test_opt_rdata_ede_dnssec_bogus() {
let mut opt_rdata = OptRdata::new();

let code = EdeCode::DnssecBogus;
let msg = "DNSSEC Bogus".to_string();
let ede_data = EdeOptData::new(code, msg.clone());

let option_data = OptionData::EDE(ede_data);
let option_data_bytes = option_data.to_bytes();
let option_len = option_data_bytes.len() as u16;

let option = OptOption::new(OptionCode::EDE, option_len, option_data);
opt_rdata.option.push(option);

let serialized = opt_rdata.to_bytes();
let deserialized = OptRdata::from_bytes(&serialized, &serialized).unwrap();

let retrieved_options = deserialized.get_option();
assert_eq!(retrieved_options.len(), 1);

let retrieved_option = &retrieved_options[0];
assert_eq!(retrieved_option.get_option_code(), OptionCode::EDE);
assert_eq!(retrieved_option.get_option_len(), option_len);

match retrieved_option.get_opt_data() {
OptionData::EDE(ede) => {
assert_eq!(ede.get_err_code(), EdeCode::DnssecBogus);
assert_eq!(ede.get_err_message(), msg);
}
_ => panic!("Expected OptionData::EDE, got something else!"),
}
}

/// 3) Test an EDE with an Unknown code, plus demonstrate using setters
#[test]
fn test_opt_rdata_ede_unknown() {
let mut opt_rdata = OptRdata::new();

// Start with an unknown code
let code = EdeCode::Unknown(999);
let msg = "Some unknown EDE error".to_string();
let mut ede_data = EdeOptData::new(code, msg);

// Use your EdeOptData's setters (if you have them)
ede_data.set_err_code(EdeCode::Unknown(1000));
ede_data.set_err_message("Modified unknown EDE".to_string());

let option_data = OptionData::EDE(ede_data);
let option_data_bytes = option_data.to_bytes();
let option_len = option_data_bytes.len() as u16;

let option = OptOption::new(OptionCode::EDE, option_len, option_data);
opt_rdata.option.push(option);

// Serialize
let serialized = opt_rdata.to_bytes();
// Deserialize
let deserialized = OptRdata::from_bytes(&serialized, &serialized).unwrap();

let retrieved_options = deserialized.get_option();
assert_eq!(retrieved_options.len(), 1);

let retrieved_option = &retrieved_options[0];
assert_eq!(retrieved_option.get_option_code(), OptionCode::EDE);
assert_eq!(retrieved_option.get_option_len(), option_len);

match retrieved_option.get_opt_data() {
OptionData::EDE(ede) => {
assert_eq!(ede.get_err_code(), EdeCode::Unknown(1000));
assert_eq!(ede.get_err_message(), "Modified unknown EDE");
}
_ => panic!("Expected OptionData::EDE, got something else!"),
}
}
}

0 comments on commit 7274028

Please sign in to comment.