|
14 | 14 | // limitations under the License. |
15 | 15 | //===----------------------------------------------------------------------===// |
16 | 16 |
|
| 17 | +import Foundation |
17 | 18 | import Testing |
18 | 19 |
|
19 | 20 | @testable import ContainerizationExtras |
@@ -308,4 +309,66 @@ struct TestCIDR { |
308 | 309 | let cidr = try CIDR("192.168.1.100/24") |
309 | 310 | #expect(cidr.description == "192.168.1.100/24") |
310 | 311 | } |
| 312 | + |
| 313 | + @Test( |
| 314 | + "CIDRv4 Codable encodes to string representation", |
| 315 | + arguments: [ |
| 316 | + "192.168.1.0/24", |
| 317 | + "10.0.0.0/8", |
| 318 | + "172.16.0.0/12", |
| 319 | + ] |
| 320 | + ) |
| 321 | + func testCIDRv4CodableEncode(cidr: String) throws { |
| 322 | + let original = try CIDRv4(cidr) |
| 323 | + let encoded = try JSONEncoder().encode(original) |
| 324 | + let jsonString = String(data: encoded, encoding: .utf8)! |
| 325 | + #expect(jsonString.contains(original.address.description)) |
| 326 | + #expect(jsonString.contains("\(original.prefix.length)")) |
| 327 | + } |
| 328 | + |
| 329 | + @Test( |
| 330 | + "CIDRv4 Codable decodes from string representation", |
| 331 | + arguments: [ |
| 332 | + "192.168.1.0/24", |
| 333 | + "10.0.0.0/8", |
| 334 | + "172.16.0.0/12", |
| 335 | + ] |
| 336 | + ) |
| 337 | + func testCIDRv4CodableDecode(cidr: String) throws { |
| 338 | + let json = Data("\"\(cidr)\"".utf8) |
| 339 | + let decoded = try JSONDecoder().decode(CIDRv4.self, from: json) |
| 340 | + let expected = try CIDRv4(cidr) |
| 341 | + #expect(decoded == expected) |
| 342 | + } |
| 343 | + |
| 344 | + @Test( |
| 345 | + "CIDRv6 Codable encodes to string representation", |
| 346 | + arguments: [ |
| 347 | + ("2001:db8::/32", "2001:db8::", 32), |
| 348 | + ("fe80::/10", "fe80::", 10), |
| 349 | + ("::1/128", "::1", 128), |
| 350 | + ] |
| 351 | + ) |
| 352 | + func testCIDRv6CodableEncode(cidr: String, expectedAddr: String, expectedPrefix: UInt8) throws { |
| 353 | + let original = try CIDRv6(cidr) |
| 354 | + let encoded = try JSONEncoder().encode(original) |
| 355 | + let jsonString = String(data: encoded, encoding: .utf8)! |
| 356 | + #expect(jsonString.contains(expectedAddr)) |
| 357 | + #expect(jsonString.contains("\(expectedPrefix)")) |
| 358 | + } |
| 359 | + |
| 360 | + @Test( |
| 361 | + "CIDRv6 Codable decodes from string representation", |
| 362 | + arguments: [ |
| 363 | + "2001:db8::/32", |
| 364 | + "fe80::/10", |
| 365 | + "::1/128", |
| 366 | + ] |
| 367 | + ) |
| 368 | + func testCIDRv6CodableDecode(cidr: String) throws { |
| 369 | + let json = Data("\"\(cidr)\"".utf8) |
| 370 | + let decoded = try JSONDecoder().decode(CIDRv6.self, from: json) |
| 371 | + let expected = try CIDRv6(cidr) |
| 372 | + #expect(decoded == expected) |
| 373 | + } |
311 | 374 | } |
0 commit comments