-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbinding_address.rs
More file actions
143 lines (120 loc) · 4.21 KB
/
Copy pathbinding_address.rs
File metadata and controls
143 lines (120 loc) · 4.21 KB
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
//! Binding address value object for tracker services
//!
//! This module provides a type-safe representation of socket addresses
//! with protocol information, used for validating tracker configurations.
use std::fmt;
use std::net::SocketAddr;
use super::Protocol;
/// A binding address combining socket address and protocol
///
/// Represents a complete socket binding specification including both the
/// network address (IP + port) and the protocol (UDP or TCP). This ensures
/// that socket address uniqueness validation accounts for protocol differences.
///
/// # Examples
///
/// ```rust
/// use torrust_tracker_deployer_lib::domain::tracker::{BindingAddress, Protocol};
///
/// let udp_addr = BindingAddress::new(
/// "0.0.0.0:6969".parse().unwrap(),
/// Protocol::Udp
/// );
///
/// let tcp_addr = BindingAddress::new(
/// "0.0.0.0:7070".parse().unwrap(),
/// Protocol::Tcp
/// );
///
/// assert_eq!(udp_addr.socket().port(), 6969);
/// assert_eq!(tcp_addr.protocol(), Protocol::Tcp);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct BindingAddress {
socket: SocketAddr,
protocol: Protocol,
}
impl BindingAddress {
/// Creates a new binding address
///
/// # Examples
///
/// ```rust
/// use torrust_tracker_deployer_lib::domain::tracker::{BindingAddress, Protocol};
///
/// let addr = BindingAddress::new(
/// "0.0.0.0:6969".parse().unwrap(),
/// Protocol::Udp
/// );
/// ```
#[must_use]
pub fn new(socket: SocketAddr, protocol: Protocol) -> Self {
Self { socket, protocol }
}
/// Returns the socket address
#[must_use]
pub fn socket(&self) -> &SocketAddr {
&self.socket
}
/// Returns the protocol
#[must_use]
pub fn protocol(&self) -> Protocol {
self.protocol
}
}
impl fmt::Display for BindingAddress {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} ({})", self.socket, self.protocol)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_should_create_binding_address_with_udp_protocol() {
let socket: SocketAddr = "0.0.0.0:6969".parse().unwrap();
let addr = BindingAddress::new(socket, Protocol::Udp);
assert_eq!(addr.socket(), &socket);
assert_eq!(addr.protocol(), Protocol::Udp);
}
#[test]
fn it_should_create_binding_address_with_tcp_protocol() {
let socket: SocketAddr = "127.0.0.1:7070".parse().unwrap();
let addr = BindingAddress::new(socket, Protocol::Tcp);
assert_eq!(addr.socket(), &socket);
assert_eq!(addr.protocol(), Protocol::Tcp);
}
#[test]
fn it_should_display_binding_address_with_protocol() {
let addr = BindingAddress::new("0.0.0.0:6969".parse().unwrap(), Protocol::Udp);
assert_eq!(addr.to_string(), "0.0.0.0:6969 (UDP)");
let addr = BindingAddress::new("127.0.0.1:7070".parse().unwrap(), Protocol::Tcp);
assert_eq!(addr.to_string(), "127.0.0.1:7070 (TCP)");
}
#[test]
fn it_should_consider_same_socket_different_protocol_as_different() {
let udp_addr = BindingAddress::new("0.0.0.0:7070".parse().unwrap(), Protocol::Udp);
let tcp_addr = BindingAddress::new("0.0.0.0:7070".parse().unwrap(), Protocol::Tcp);
assert_ne!(udp_addr, tcp_addr);
}
#[test]
fn it_should_consider_same_socket_same_protocol_as_equal() {
let addr1 = BindingAddress::new("0.0.0.0:7070".parse().unwrap(), Protocol::Tcp);
let addr2 = BindingAddress::new("0.0.0.0:7070".parse().unwrap(), Protocol::Tcp);
assert_eq!(addr1, addr2);
}
#[test]
fn it_should_consider_different_ips_same_port_as_different() {
let addr1 = BindingAddress::new("192.168.1.10:7070".parse().unwrap(), Protocol::Tcp);
let addr2 = BindingAddress::new("192.168.1.20:7070".parse().unwrap(), Protocol::Tcp);
assert_ne!(addr1, addr2);
}
#[test]
fn it_should_be_usable_as_hash_map_key() {
use std::collections::HashMap;
let mut map = HashMap::new();
let addr = BindingAddress::new("0.0.0.0:6969".parse().unwrap(), Protocol::Udp);
map.insert(addr, "UDP Tracker");
assert_eq!(map.get(&addr), Some(&"UDP Tracker"));
}
}