1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//! Property-based testing for Four-Word Networking
//!
//! This module contains property-based tests that verify mathematical invariants
//! and properties of the encoding system using proptest.
use crate::FourWordAdaptiveEncoder;
use proptest::prelude::*;
use std::net::{Ipv4Addr, Ipv6Addr};
#[cfg(test)]
mod tests {
use super::*;
// Property: IPv4 addresses should always encode to exactly 4 space-separated words
proptest! {
#[test]
fn prop_ipv4_always_4_words(
ip in any::<Ipv4Addr>(),
port in any::<u16>()
) {
let encoder = FourWordAdaptiveEncoder::new().unwrap();
let addr_str = format!("{ip}:{port}");
if let Ok(words) = encoder.encode(&addr_str) {
// Should have exactly 4 words separated by spaces
let word_count = words.split(' ').count();
prop_assert_eq!(word_count, 4);
// Should be lowercase
prop_assert_eq!(words.clone(), words.to_lowercase());
}
}
}
// Property: IPv6 addresses should always encode to 6, 9, or 12 space-separated words
proptest! {
#[test]
fn prop_ipv6_format_consistency(
ip in any::<Ipv6Addr>(),
port in any::<u16>()
) {
let encoder = FourWordAdaptiveEncoder::new().unwrap();
let addr_str = format!("[{ip}]:{port}");
if let Ok(words) = encoder.encode(&addr_str) {
// Should have 6, 9, or 12 words separated by spaces
let word_count = words.split(' ').count();
prop_assert!(word_count == 6 || word_count == 9 || word_count == 12);
// Should be lowercase
prop_assert_eq!(words.clone(), words.to_lowercase());
}
}
}
// Property: Round-trip encoding should preserve the original address
proptest! {
#[test]
fn prop_ipv4_round_trip(
ip in any::<Ipv4Addr>(),
port in any::<u16>()
) {
let encoder = FourWordAdaptiveEncoder::new().unwrap();
let original = format!("{ip}:{port}");
if let Ok(words) = encoder.encode(&original)
&& let Ok(decoded) = encoder.decode(&words) {
prop_assert_eq!(original, decoded);
}
}
}
// Property: Same input should always produce same output (deterministic)
proptest! {
#[test]
fn prop_deterministic_encoding(
ip in any::<Ipv4Addr>(),
port in any::<u16>()
) {
let encoder = FourWordAdaptiveEncoder::new().unwrap();
let addr_str = format!("{ip}:{port}");
if let Ok(words1) = encoder.encode(&addr_str)
&& let Ok(words2) = encoder.encode(&addr_str) {
prop_assert_eq!(words1, words2);
}
}
}
// Property: Different inputs should produce different outputs (no collisions)
proptest! {
#[test]
fn prop_no_collisions_different_ports(
ip in any::<Ipv4Addr>(),
port1 in any::<u16>(),
port2 in any::<u16>()
) {
prop_assume!(port1 != port2);
let encoder = FourWordAdaptiveEncoder::new().unwrap();
let addr1_str = format!("{ip}:{port1}");
let addr2_str = format!("{ip}:{port2}");
if let (Ok(words1), Ok(words2)) = (encoder.encode(&addr1_str), encoder.encode(&addr2_str)) {
prop_assert_ne!(words1, words2);
}
}
}
// Property: Word validation should be consistent
proptest! {
#[test]
fn prop_word_validation_consistency(
ip in any::<Ipv4Addr>(),
port in any::<u16>()
) {
let encoder = FourWordAdaptiveEncoder::new().unwrap();
let addr_str = format!("{ip}:{port}");
if let Ok(words) = encoder.encode(&addr_str) {
// Each word should be from the dictionary
for word in words.split(' ') {
prop_assert!(!word.is_empty()); // Dictionary includes single-character words
// No maximum length restriction - frequency-based words can be longer
prop_assert!(word.chars().all(|c| c.is_ascii_lowercase()));
}
}
}
}
// Property: Encoding should handle edge cases gracefully
proptest! {
#[test]
fn prop_handle_edge_cases(
ip_bytes in prop::array::uniform4(any::<u8>()),
port in any::<u16>()
) {
let encoder = FourWordAdaptiveEncoder::new().unwrap();
let ip = Ipv4Addr::from(ip_bytes);
let addr_str = format!("{ip}:{port}");
// Encoding should either succeed or fail gracefully
match encoder.encode(&addr_str) {
Ok(words) => {
// If encoding succeeds, it should produce valid format
prop_assert_eq!(words.split(' ').count(), 4);
prop_assert!(!words.is_empty());
},
Err(_) => {
// If encoding fails, that's acceptable for edge cases
// but the error should be handled gracefully
}
}
}
}
// Property: String formatting should be consistent
proptest! {
#[test]
fn prop_string_formatting_consistency(
ip in any::<Ipv4Addr>(),
port in any::<u16>()
) {
let encoder = FourWordAdaptiveEncoder::new().unwrap();
let addr_str1 = format!("{ip}:{port}");
let addr_str2 = format!("{ip}:{port}");
let result1 = encoder.encode(&addr_str1);
let result2 = encoder.encode(&addr_str2);
match (result1, result2) {
(Ok(words1), Ok(words2)) => {
prop_assert_eq!(words1, words2);
},
(Err(_), Err(_)) => {
// Both failing is acceptable
},
_ => {
// One succeeding and one failing is not acceptable
prop_assert!(false, "Identical string inputs should have same result");
}
}
}
}
}