@@ -108,6 +108,68 @@ fn test_from_str_socket_addr() {
108
108
assert_eq ! ( None , none) ;
109
109
}
110
110
111
+ #[ test]
112
+ fn test_from_str_ipv4_prefix ( ) {
113
+ assert_eq ! (
114
+ Ok ( Ipv4AddrPrefix :: new_unchecked( Ipv4Addr :: new( 127 , 0 , 0 , 1 ) , 16 ) ) ,
115
+ "127.0.0.1/16" . parse( )
116
+ ) ;
117
+ assert_eq ! (
118
+ Ok ( Ipv4AddrPrefix :: new_unchecked( Ipv4Addr :: new( 255 , 255 , 255 , 255 ) , 32 ) ) ,
119
+ "255.255.255.255/32" . parse( )
120
+ ) ;
121
+ assert_eq ! (
122
+ Ok ( Ipv4AddrPrefix :: new_unchecked( Ipv4Addr :: new( 0 , 0 , 0 , 0 ) , 0 ) ) ,
123
+ "0.0.0.0/0" . parse( )
124
+ ) ;
125
+
126
+ // no prefix
127
+ let none: Option < Ipv4AddrPrefix > = "255.0.0.1" . parse ( ) . ok ( ) ;
128
+ assert_eq ! ( None , none) ;
129
+ // wrong prefix separator
130
+ let none: Option < Ipv4AddrPrefix > = "255.0.0.1:16" . parse ( ) . ok ( ) ;
131
+ assert_eq ! ( None , none) ;
132
+ // prefix can not be longer than 32 bits
133
+ let none: Option < Ipv4AddrPrefix > = "255.0.0.1/35" . parse ( ) . ok ( ) ;
134
+ assert_eq ! ( None , none) ;
135
+ }
136
+
137
+ #[ test]
138
+ fn test_from_str_ipv6_prefix ( ) {
139
+ assert_eq ! (
140
+ Ok ( Ipv6AddrPrefix :: new_unchecked( Ipv6Addr :: new( 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ) , 0 ) ) ,
141
+ "0:0:0:0:0:0:0:0/0" . parse( )
142
+ ) ;
143
+ assert_eq ! (
144
+ Ok ( Ipv6AddrPrefix :: new_unchecked( Ipv6Addr :: new( 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 ) , 128 ) ) ,
145
+ "0:0:0:0:0:0:0:1/128" . parse( )
146
+ ) ;
147
+
148
+ assert_eq ! (
149
+ Ok ( Ipv6AddrPrefix :: new_unchecked( Ipv6Addr :: new( 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 ) , 128 ) ) ,
150
+ "::1/128" . parse( )
151
+ ) ;
152
+ assert_eq ! (
153
+ Ok ( Ipv6AddrPrefix :: new_unchecked( Ipv6Addr :: new( 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ) , 0 ) ) ,
154
+ "::/0" . parse( )
155
+ ) ;
156
+
157
+ assert_eq ! (
158
+ Ok ( Ipv6AddrPrefix :: new_unchecked( Ipv6Addr :: new( 0x2a02 , 0x6b8 , 0 , 0 , 0 , 0 , 0x11 , 0x11 ) , 32 ) ) ,
159
+ "2a02:6b8::11:11/32" . parse( )
160
+ ) ;
161
+
162
+ // no prefix
163
+ let none: Option < Ipv6AddrPrefix > = "1:2:3:4::5:6:7:8" . parse ( ) . ok ( ) ;
164
+ assert_eq ! ( None , none) ;
165
+ // wrong prefix separator
166
+ let none: Option < Ipv6AddrPrefix > = "1:2:3:4::5:6:7:8:16" . parse ( ) . ok ( ) ;
167
+ assert_eq ! ( None , none) ;
168
+ // prefix can not be longer than 128 bits
169
+ let none: Option < Ipv6AddrPrefix > = "1:2:3:4::5:6:7:8/130" . parse ( ) . ok ( ) ;
170
+ assert_eq ! ( None , none) ;
171
+ }
172
+
111
173
#[ test]
112
174
fn ipv4_addr_to_string ( ) {
113
175
assert_eq ! ( Ipv4Addr :: new( 127 , 0 , 0 , 1 ) . to_string( ) , "127.0.0.1" ) ;
@@ -171,6 +233,19 @@ fn ipv6_addr_to_string() {
171
233
assert_eq ! ( "1::4:5:0:0:8" , & format!( "{:#?}" , Ipv6Addr :: new( 1 , 0 , 0 , 4 , 5 , 0 , 0 , 8 ) ) ) ;
172
234
}
173
235
236
+ #[ test]
237
+ fn ip_prefix_to_string ( ) {
238
+ assert_eq ! (
239
+ Ipv4AddrPrefix :: new_unchecked( Ipv4Addr :: new( 127 , 0 , 0 , 1 ) , 24 ) . to_string( ) ,
240
+ "127.0.0.0/24"
241
+ ) ;
242
+ assert_eq ! (
243
+ Ipv6AddrPrefix :: new_unchecked( Ipv6Addr :: new( 0 , 0 , 0 , 0 , 0 , 0xffff , 0x7F00 , 1 ) , 96 )
244
+ . to_string( ) ,
245
+ "::ffff:0.0.0.0/96"
246
+ ) ;
247
+ }
248
+
174
249
#[ test]
175
250
fn ipv4_to_ipv6 ( ) {
176
251
assert_eq ! (
0 commit comments