oxedyne/fe2o3/fe2o3_net/tests/dns.rs
5.3 KiB, 5 runs
created by r1870400018:4003, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | use oxedyne_fe2o3_net::{ |
| 2 | dns::Fqdn, |
| 3 | }; |
| 4 | |
| 5 | use oxedyne_fe2o3_core::{ |
| 6 | prelude::*, |
| 7 | test::test_it, |
| 8 | }; |
| 9 | |
| 10 | |
| 11 | pub fn test_dns(filter: &'static str) -> Outcome<()> { |
| 12 | |
| 13 | res!(test_it(filter, &["Simple", "all", "dns"], || { |
| 14 | |
| 15 | let dn_255 = &("a".repeat(63) + "." + &"b".repeat(63) + "." + &"b".repeat(63) + "." + &"b".repeat(63)); |
| 16 | let dn_256 = &("a".repeat(63) + "." + &"b".repeat(63) + "." + &"b".repeat(63) + "." + &"b".repeat(64)); |
| 17 | let dn_lab_63 = &("a".repeat(63) + ".com"); |
| 18 | |
| 19 | let domains: Vec<(&str, bool)> = vec![ |
| 20 | // Valid domains |
| 21 | ("example.com", true), // Simple valid domain |
| 22 | ("sub.example.com", true), // Valid subdomain |
| 23 | ("sub-domain.example.com", true), // Valid hyphen in label |
| 24 | ("xn--mnchen-3ya.de", true), // Valid IDN (München) |
| 25 | ("a.b.c.d.e.f.g.h.i.j.com", true), // Many subdomains (within 253 char limit) |
| 26 | ("123.example.com", true), // Numbers in subdomain |
| 27 | ("example-123.com", true), // Alphanumeric with hyphen |
| 28 | ("sub.sub.example.co.uk", true), // Multiple levels with country code |
| 29 | ("valid.domain-name.org", true), // Hyphen in second level |
| 30 | ("a-very-long-domain-name-but-still-under-63-chars.com", true), // Long but valid label |
| 31 | ("localhost", true), // Special case - localhost is valid per RFC 6761 |
| 32 | ("localhost.", true), // Ends with dot is ok |
| 33 | ("A.B.C", true), // Upper case |
| 34 | ("a1.b2.c3", true), // Single char with numbers |
| 35 | ("1.2.3", true), // All numeric labels (but not special) |
| 36 | ("x.y.z", true), // Single letter labels |
| 37 | (dn_255, true), // Exactly 255 bytes total |
| 38 | (dn_lab_63, true), // Exactly 63 bytes in label |
| 39 | ("test", true), // RFC 6761 special |
| 40 | ("invalid", true), // RFC 6761 special |
| 41 | ("example", true), // RFC 6761 special |
| 42 | ("example.123", true), // All-numeric TLD, allowed but not used |
| 43 | ("example.c", true), // Single char TLD allowed but not used |
| 44 | // Edge cases for hyphen rules |
| 45 | ("a--b.example.com", true), // Multiple consecutive hyphens in middle |
| 46 | ("123example.com", true), // Numbers at start |
| 47 | ("example123.com", true), // Numbers at end |
| 48 | ("exa123mple.com", true), // Numbers in middle |
| 49 | ("123.456.789.com", true), // All-numeric non-TLD labels |
| 50 | |
| 51 | // Invalid domains |
| 52 | ("", false), // Empty string |
| 53 | (".", false), // Single dot |
| 54 | (".com", false), // Starts with dot |
| 55 | ("com.", false), // Single label with dot |
| 56 | (".", false), // Root only |
| 57 | ("\u{0000}.com", false), // Null character |
| 58 | ("x\u{00A0}y.com", false), // Non-ASCII space |
| 59 | ("example..com", false), // Consecutive dots |
| 60 | ("-example.com", false), // Starts with hyphen |
| 61 | ("example-.com", false), // Ends with hyphen |
| 62 | ("exam ple.com", false), // Contains space |
| 63 | ("exam@ple.com", false), // Contains @ symbol |
| 64 | ("ex*mple.com", false), // Contains asterisk |
| 65 | (".example.com.", false), // Ends with dot |
| 66 | ("really-long-subdomain-that-exceeds-sixty-three-characters-which-is-not-allowed.com", false), // Label > 63 chars |
| 67 | (dn_256, false), // Exactly 256 bytes total |
| 68 | ("exa!mple.com", false), // Contains invalid character |
| 69 | ("ab", false), // Too short overall |
| 70 | // Edge cases for hyphen rules |
| 71 | ("-ab.example.com", false), // Hyphen at start of first label |
| 72 | ("ab-.example.com", false), // Hyphen at end of first label |
| 73 | ("ab.-cd.example.com", false), // Hyphen at start of middle label |
| 74 | ("ab.cd-.example.com", false), // Hyphen at end of middle label |
| 75 | ]; |
| 76 | for (domain, valid) in domains { |
| 77 | match Fqdn::new(domain) { |
| 78 | Ok(_fqdn) => { |
| 79 | if !valid { |
| 80 | return Err(err!( |
| 81 | "Domain name '{}' was incorrectly classed as valid.", domain; |
| 82 | Test, Invalid, Input, String)); |
| 83 | } else { |
| 84 | test!("'{}' was correctly classed as valid.", domain); |
| 85 | } |
| 86 | } |
| 87 | Err(e) => { |
| 88 | if valid { |
| 89 | return Err(err!(e, |
| 90 | "Domain name '{}' was incorrectly classed as invalid.", domain; |
| 91 | Test, Invalid, Input, String)); |
| 92 | } else { |
| 93 | test!("'{}' was correctly classed as invalid.", domain); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | Ok(()) |
| 99 | })); |
| 100 | |
| 101 | Ok(()) |
| 102 | } |