Skip to content

Commit 01216b2

Browse files
committed
refactor: split tlv.rs into several files
1 parent 7167656 commit 01216b2

20 files changed

Lines changed: 5139 additions & 5198 deletions

src/receiver/mod.rs

Lines changed: 107 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2055,7 +2055,9 @@ mod tests {
20552055

20562056
#[test]
20572057
fn test_assemble_unauth_with_cos_tlv_updates_dscp_ecn() {
2058-
use crate::tlv::{ClassOfServiceTlv, TlvType, COS_TLV_VALUE_SIZE, TLV_HEADER_SIZE};
2058+
use crate::tlv::{
2059+
ClassOfServiceTlv, TlvType, TypedTlv, COS_TLV_VALUE_SIZE, TLV_HEADER_SIZE,
2060+
};
20592061

20602062
let sender_packet = PacketUnauthenticated {
20612063
sequence_number: 1,
@@ -2120,7 +2122,9 @@ mod tests {
21202122

21212123
#[test]
21222124
fn test_assemble_auth_with_cos_tlv_updates_dscp_ecn() {
2123-
use crate::tlv::{ClassOfServiceTlv, TlvType, COS_TLV_VALUE_SIZE, TLV_HEADER_SIZE};
2125+
use crate::tlv::{
2126+
ClassOfServiceTlv, TlvType, TypedTlv, COS_TLV_VALUE_SIZE, TLV_HEADER_SIZE,
2127+
};
21242128

21252129
let sender_packet = PacketAuthenticated {
21262130
sequence_number: 1,
@@ -2185,7 +2189,7 @@ mod tests {
21852189

21862190
#[test]
21872191
fn test_set_cos_policy_rejected_unauth() {
2188-
use crate::tlv::ClassOfServiceTlv;
2192+
use crate::tlv::{ClassOfServiceTlv, TypedTlv};
21892193

21902194
// Build an unauthenticated response with a CoS TLV
21912195
let sender_packet = PacketUnauthenticated {
@@ -2228,7 +2232,7 @@ mod tests {
22282232

22292233
#[test]
22302234
fn test_set_cos_policy_rejected_auth() {
2231-
use crate::tlv::ClassOfServiceTlv;
2235+
use crate::tlv::{ClassOfServiceTlv, TypedTlv};
22322236

22332237
// Build an authenticated response with a CoS TLV
22342238
let sender_packet = PacketAuthenticated {
@@ -2292,7 +2296,7 @@ mod tests {
22922296

22932297
#[test]
22942298
fn test_set_cos_policy_rejected_reserved_tlv_before_cos() {
2295-
use crate::tlv::ClassOfServiceTlv;
2299+
use crate::tlv::{ClassOfServiceTlv, TypedTlv};
22962300

22972301
// Build a response with a zero-length Reserved TLV (header 00 00 00 00)
22982302
// followed by a CoS TLV. The Reserved TLV must not be mistaken for padding.
@@ -2328,7 +2332,7 @@ mod tests {
23282332

23292333
#[test]
23302334
fn test_recompute_hmac_after_rp_mutation() {
2331-
use crate::tlv::{ClassOfServiceTlv, TlvList, TLV_HEADER_SIZE};
2335+
use crate::tlv::{ClassOfServiceTlv, TlvList, TypedTlv, TLV_HEADER_SIZE};
23322336

23332337
let key = HmacKey::new(vec![0xAB; 32]).unwrap();
23342338

@@ -2443,7 +2447,7 @@ mod tests {
24432447

24442448
#[test]
24452449
fn test_unauth_dest_node_addr_match() {
2446-
use crate::tlv::DestinationNodeAddressTlv;
2450+
use crate::tlv::{DestinationNodeAddressTlv, TypedTlv};
24472451

24482452
let sender_packet = PacketUnauthenticated {
24492453
sequence_number: 1,
@@ -2483,7 +2487,7 @@ mod tests {
24832487

24842488
#[test]
24852489
fn test_unauth_dest_node_addr_mismatch() {
2486-
use crate::tlv::DestinationNodeAddressTlv;
2490+
use crate::tlv::{DestinationNodeAddressTlv, TypedTlv};
24872491

24882492
let sender_packet = PacketUnauthenticated {
24892493
sequence_number: 1,
@@ -2675,7 +2679,7 @@ mod tests {
26752679

26762680
#[test]
26772681
fn test_unauth_with_micro_session_id_fills_reflector_id() {
2678-
use crate::tlv::MicroSessionIdTlv;
2682+
use crate::tlv::{MicroSessionIdTlv, TypedTlv};
26792683

26802684
let sender_packet = PacketUnauthenticated {
26812685
sequence_number: 1,
@@ -2723,7 +2727,7 @@ mod tests {
27232727

27242728
#[test]
27252729
fn test_unauth_with_micro_session_id_mismatch_discards() {
2726-
use crate::tlv::MicroSessionIdTlv;
2730+
use crate::tlv::{MicroSessionIdTlv, TypedTlv};
27272731

27282732
let sender_packet = PacketUnauthenticated {
27292733
sequence_number: 1,
@@ -2760,4 +2764,97 @@ mod tests {
27602764
ReturnPathAction::SuppressReply
27612765
));
27622766
}
2767+
2768+
#[test]
2769+
fn test_auth_with_micro_session_id_fills_reflector_id() {
2770+
use crate::tlv::{MicroSessionIdTlv, TypedTlv};
2771+
2772+
let sender_packet = PacketAuthenticated {
2773+
sequence_number: 1,
2774+
mbz0: [0; 12],
2775+
timestamp: 100,
2776+
error_estimate: 10,
2777+
mbz1a: [0; 32],
2778+
mbz1b: [0; 32],
2779+
mbz1c: [0; 6],
2780+
hmac: [0; 16],
2781+
};
2782+
2783+
let msid_raw = MicroSessionIdTlv::new(42, 0).to_raw();
2784+
let mut data = sender_packet.to_bytes().to_vec();
2785+
data.extend_from_slice(&msid_raw.to_bytes());
2786+
2787+
let mut ctx = test_ctx(0, 0);
2788+
ctx.reflector_member_link_id = Some(99);
2789+
2790+
let response = assemble_auth_answer_with_tlvs(
2791+
&sender_packet,
2792+
&data,
2793+
ClockFormat::NTP,
2794+
500,
2795+
64,
2796+
0,
2797+
None,
2798+
None,
2799+
TlvHandlingMode::Echo,
2800+
None,
2801+
false,
2802+
&ctx,
2803+
);
2804+
2805+
assert!(!matches!(
2806+
response.return_path_action,
2807+
ReturnPathAction::SuppressReply
2808+
));
2809+
2810+
let tlv_data = &response.data[AUTH_BASE_SIZE..];
2811+
let tlvs = TlvList::parse(tlv_data).unwrap();
2812+
let msid_tlv = &tlvs.non_hmac_tlvs()[0];
2813+
let parsed = MicroSessionIdTlv::from_raw(msid_tlv).unwrap();
2814+
assert_eq!(parsed.sender_micro_session_id, 42);
2815+
assert_eq!(parsed.reflector_micro_session_id, 99);
2816+
}
2817+
2818+
#[test]
2819+
fn test_auth_with_micro_session_id_mismatch_discards() {
2820+
use crate::tlv::{MicroSessionIdTlv, TypedTlv};
2821+
2822+
let sender_packet = PacketAuthenticated {
2823+
sequence_number: 1,
2824+
mbz0: [0; 12],
2825+
timestamp: 100,
2826+
error_estimate: 10,
2827+
mbz1a: [0; 32],
2828+
mbz1b: [0; 32],
2829+
mbz1c: [0; 6],
2830+
hmac: [0; 16],
2831+
};
2832+
2833+
let msid_raw = MicroSessionIdTlv::new(42, 50).to_raw();
2834+
let mut data = sender_packet.to_bytes().to_vec();
2835+
data.extend_from_slice(&msid_raw.to_bytes());
2836+
2837+
let mut ctx = test_ctx(0, 0);
2838+
ctx.reflector_member_link_id = Some(99);
2839+
2840+
let response = assemble_auth_answer_with_tlvs(
2841+
&sender_packet,
2842+
&data,
2843+
ClockFormat::NTP,
2844+
500,
2845+
64,
2846+
0,
2847+
None,
2848+
None,
2849+
TlvHandlingMode::Echo,
2850+
None,
2851+
false,
2852+
&ctx,
2853+
);
2854+
2855+
assert!(matches!(
2856+
response.return_path_action,
2857+
ReturnPathAction::SuppressReply
2858+
));
2859+
}
27632860
}

src/sender.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use crate::{
2626
tlv::{
2727
AccessReportTlv, ClassOfServiceTlv, DestinationNodeAddressTlv, DirectMeasurementTlv,
2828
FollowUpTelemetryTlv, LocationTlv, MicroSessionIdTlv, RawTlv, ReturnPathTlv,
29-
SessionSenderId, SyncSource, TimestampInfoTlv, TimestampMethod, TlvList,
29+
SessionSenderId, SyncSource, TimestampInfoTlv, TimestampMethod, TlvList, TypedTlv,
3030
},
3131
};
3232

0 commit comments

Comments
 (0)