// This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at https://mozilla.org/MPL/2.0/. //! Preview host names (T4, written before the code): `--.`. //! Branch and project names come from a git host and are hostile: every one must map to a //! safe DNS label, two different branches must never share a host, and the preview table of //! a host file must agree with its host name. #![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)] use std::collections::BTreeMap; use ianitor_core::preview::{self, PreviewError}; use ianitor_core::{SourceFile, load}; /// A label a public CA and every resolver accept: LDH, 1–63, no leading or trailing dash, and /// no `--` in positions 3 and 4 (reserved for IDNA A-labels, refused by Let's Encrypt). fn assert_safe_label(l: &str) { assert!(!l.is_empty() && l.len() <= 63, "length of {l:?}"); assert!( l.bytes() .all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == b'-'), "{l:?} is not lowercase LDH" ); assert!( !l.starts_with('-') && !l.ends_with('-'), "{l:?} has an edge dash" ); assert!( l.get(2..4) != Some("--"), "{l:?} has -- in positions 3 and 4" ); } #[test] fn plain_names_stay_readable() { assert_eq!( preview::label("mnovak/Arca", "main").unwrap(), "main--arca-4a93cc" ); assert_eq!(preview::label("shop", "fix-42").unwrap(), "fix-42--shop"); assert_eq!( preview::hostname("shop", "fix-42", "preview.example.org").unwrap(), "fix-42--shop.preview.example.org" ); } #[test] fn hostile_branch_names_map_to_safe_labels() { let long = "a".repeat(250); let dots = "x.".repeat(100); let hostile: &[&str] = &[ "main", "Main", "feature/login", "feature//login", "feature.login", "..", "...hidden", "v1.2.3", "ab", "xn", "a", "-", "--", "_", "fix/ünïcødé", "日本語のブランチ", "emoji-🦊-branch", "Ⅻ-roman", "fi-ligature", "\u{200b}zero-width", "right-to-left-\u{202e}gnp.exe", "has space and ~^:?*[", "semi;colon&