use std::borrow::Cow;
use std::collections::HashSet;
use serde_json::Value;
use crate::error::{Error, ErrorCode};
use crate::generated::types::Person;
const SGID_PURPOSE_ATTACHABLE: &str = "attachable";
const MAX_SGID_PAYLOAD_BYTES: usize = 4096;
const MAX_SGID_ENCODED_BYTES: usize = MAX_SGID_PAYLOAD_BYTES / 3 * 4 + 4;
pub fn mentioned_person_ids(rich_text: &str) -> Vec<i64> {
let mut ids = Vec::new();
let mut seen = HashSet::new();
for sgid in bc_attachment_sgids(rich_text) {
let Some(id) = person_id_from_sgid(&sgid) else {
continue;
};
if seen.insert(id) {
ids.push(id);
}
}
ids
}
pub fn person_id_from_sgid(sgid: &str) -> Option<i64> {
let gid = global_id_from_sgid(sgid)?;
let (model, raw_id) = parse_global_id(&gid)?;
if model != "Person" {
return None;
}
let id: i64 = raw_id.parse().ok()?;
(id > 0).then_some(id)
}
pub fn mention_markup(person: &Person) -> Result<String, Error> {
let sgid = person.attachable_sgid.as_deref().unwrap_or_default();
if sgid.is_empty() {
return Err(Error::new(
ErrorCode::Usage,
format!("person {} has no attachable_sgid to mention", person.id),
)
.with_hint("read the person through people().get to obtain one"));
}
if sgid.contains(['"', '\'', '<', '>', '&']) {
return Err(Error::new(
ErrorCode::Usage,
format!("person {} has a malformed attachable_sgid", person.id),
));
}
if person_id_from_sgid(sgid) != Some(person.id) {
return Err(Error::new(
ErrorCode::Usage,
format!(
"person {}'s attachable_sgid does not name that person",
person.id
),
)
.with_hint("read the person through people().get to obtain their own"));
}
Ok(format!(r#"<bc-attachment sgid="{sgid}"></bc-attachment>"#))
}
pub fn with_mentions(content: &str, people: &[Person]) -> Result<String, Error> {
let mut present: HashSet<String> = bc_attachment_sgids(content).into_iter().collect();
let mut tags = Vec::new();
for person in people {
let tag = mention_markup(person)?;
let sgid = person.attachable_sgid.clone().unwrap_or_default();
if present.insert(sgid) {
tags.push(tag);
}
}
if tags.is_empty() {
return Ok(content.to_string());
}
let prefix = format!("{} ", tags.join(" "));
match leading_block_end(content) {
Some(end) => Ok(format!("{}{prefix}{}", &content[..end], &content[end..])),
None => Ok(format!("{prefix}{content}")),
}
}
fn bc_attachment_sgids(text: &str) -> Vec<String> {
let bytes = text.as_bytes();
let mut sgids = Vec::new();
let mut pos = 0usize;
while pos < bytes.len() {
let Some(offset) = bytes[pos..].iter().position(|byte| *byte == b'<') else {
break;
};
pos += offset + 1;
let rest = &bytes[pos..];
if rest.starts_with(b"!--") {
let Some(stop) = find(rest, b"-->") else {
return sgids; };
pos += stop + 3;
continue;
}
if matches!(rest.first(), Some(b'!' | b'?' | b'/')) {
let Some(stop) = rest.iter().position(|byte| *byte == b'>') else {
return sgids;
};
pos += stop + 1;
continue;
}
let name_end = rest
.iter()
.position(|byte| !is_tag_name_char(*byte))
.unwrap_or(rest.len());
if name_end == 0 {
continue; }
let Some((attributes, end)) = parse_attributes(bytes, pos + name_end) else {
return sgids; };
if equal_fold(&rest[..name_end], b"bc-attachment")
&& let Some(sgid) = attributes
&& !sgid.is_empty()
{
sgids.push(sgid);
}
pos = end;
}
sgids
}
fn is_tag_name_char(byte: u8) -> bool {
!is_space(byte) && !matches!(byte, b'/' | b'>' | b'<' | b'=' | b'"' | b'\'')
}
fn is_tag_name_end(byte: u8) -> bool {
is_space(byte) || matches!(byte, b'/' | b'>')
}
fn is_space(byte: u8) -> bool {
matches!(byte, b' ' | b'\t' | b'\n' | b'\r' | 0x0c)
}
fn find(haystack: &[u8], needle: &[u8]) -> Option<usize> {
haystack
.windows(needle.len())
.position(|window| window == needle)
}
fn parse_attributes(text: &[u8], mut pos: usize) -> Option<(Option<String>, usize)> {
let mut sgid: Option<String> = None;
while pos < text.len() {
while pos < text.len() && (is_space(text[pos]) || text[pos] == b'/') {
pos += 1;
}
if pos >= text.len() {
return None;
}
if text[pos] == b'>' {
return Some((sgid, pos + 1));
}
let name_start = pos;
while pos < text.len() && !is_space(text[pos]) && !matches!(text[pos], b'=' | b'>' | b'/') {
pos += 1;
}
let name = &text[name_start..pos];
while pos < text.len() && is_space(text[pos]) {
pos += 1;
}
let mut value: &[u8] = b"";
if pos < text.len() && text[pos] == b'=' {
pos += 1;
while pos < text.len() && is_space(text[pos]) {
pos += 1;
}
if pos < text.len() && matches!(text[pos], b'"' | b'\'') {
let quote = text[pos];
pos += 1;
let closing = text[pos..].iter().position(|byte| *byte == quote)?;
value = &text[pos..pos + closing];
pos += closing + 1;
} else {
let value_start = pos;
while pos < text.len() && !is_space(text[pos]) && text[pos] != b'>' {
pos += 1;
}
value = &text[value_start..pos];
}
}
if name.is_empty() {
pos += 1;
continue;
}
if sgid.is_none() && equal_fold(name, b"sgid") {
sgid = Some(unescape(&String::from_utf8_lossy(value)));
}
}
None
}
fn equal_fold(text: &[u8], ascii_needle: &[u8]) -> bool {
const LONG_S: &[u8] = &[0xc5, 0xbf]; const KELVIN: &[u8] = &[0xe2, 0x84, 0xaa]; debug_assert!(
ascii_needle.is_ascii(),
"equal_fold's needle must be ASCII; folding is only modelled from that side"
);
let mut at = 0usize;
for &wanted in ascii_needle {
let rest = &text[at..];
let Some(&byte) = rest.first() else {
return false;
};
if byte < 0x80 {
if !byte.eq_ignore_ascii_case(&wanted) {
return false;
}
at += 1;
} else if wanted.eq_ignore_ascii_case(&b's') && rest.starts_with(LONG_S) {
at += LONG_S.len();
} else if wanted.eq_ignore_ascii_case(&b'k') && rest.starts_with(KELVIN) {
at += KELVIN.len();
} else {
return false;
}
}
at == text.len()
}
fn leading_block_end(content: &str) -> Option<usize> {
let bytes = content.as_bytes();
let mut start = 0usize;
while start < bytes.len() && is_space(bytes[start]) {
start += 1;
}
for name in [b"<p".as_slice(), b"<div".as_slice()] {
let after = start + name.len();
if bytes.len() < after || !equal_fold(&bytes[start..after], name) {
continue;
}
if after < bytes.len() && !is_tag_name_end(bytes[after]) {
continue;
}
return parse_attributes(bytes, after).map(|(_, end)| end);
}
None
}
const MAX_ENTITY_NAME: usize = 17;
const VERDICT_RELEVANT_ENTITIES: &[(&str, &str)] = &[
("MediumSpace;", "\u{205f}"),
("NewLine;", "\u{a}"),
("NonBreakingSpace;", "\u{a0}"),
("Tab;", "\u{9}"),
("ThinSpace;", "\u{2009}"),
("UnderBar;", "\u{5f}"),
("ThickSpace;", "\u{205f}\u{200a}"),
("VeryThinSpace;", "\u{200a}"),
("amp", "\u{26}"),
("amp;", "\u{26}"),
("apos;", "\u{27}"),
("emsp;", "\u{2003}"),
("emsp13;", "\u{2004}"),
("emsp14;", "\u{2005}"),
("ensp;", "\u{2002}"),
("equals;", "\u{3d}"),
("fjlig;", "\u{66}\u{6a}"),
("gt", "\u{3e}"),
("gt;", "\u{3e}"),
("hairsp;", "\u{200a}"),
("lowbar;", "\u{5f}"),
("lt", "\u{3c}"),
("lt;", "\u{3c}"),
("nbsp", "\u{a0}"),
("nbsp;", "\u{a0}"),
("numsp;", "\u{2007}"),
("plus;", "\u{2b}"),
("puncsp;", "\u{2008}"),
("quot", "\u{22}"),
("quot;", "\u{22}"),
("sol;", "\u{2f}"),
("thinsp;", "\u{2009}"),
];
fn unescape(value: &str) -> String {
if !value.contains('&') {
return value.to_string();
}
let mut out = String::with_capacity(value.len());
let mut rest = value;
while let Some(start) = rest.find('&') {
out.push_str(&rest[..start]);
rest = &rest[start..];
if let Some((decoded, length)) = entity_at(rest) {
out.push_str(&decoded);
rest = &rest[length..];
} else {
out.push('&');
rest = &rest[1..];
}
}
out.push_str(rest);
out
}
fn entity_at(rest: &str) -> Option<(Cow<'static, str>, usize)> {
if let Some(body) = rest.strip_prefix("&#") {
return numeric_entity(body);
}
let body = rest.get(1..)?;
let run = body
.bytes()
.take_while(u8::is_ascii_alphanumeric)
.count()
.min(MAX_ENTITY_NAME);
let semicolon = usize::from(body.as_bytes().get(run) == Some(&b';'));
let limit = (run + semicolon).min(MAX_ENTITY_NAME);
for length in (1..=limit).rev() {
let Some(name) = body.get(..length) else {
continue;
};
if let Some((_, decoded)) = VERDICT_RELEVANT_ENTITIES
.iter()
.find(|(candidate, _)| *candidate == name)
{
return Some((Cow::Borrowed(*decoded), 1 + length));
}
}
None
}
const C1_REPLACEMENTS: [char; 32] = [
'\u{20ac}', '\u{81}', '\u{201a}', '\u{192}', '\u{201e}', '\u{2026}', '\u{2020}', '\u{2021}',
'\u{2c6}', '\u{2030}', '\u{160}', '\u{2039}', '\u{152}', '\u{8d}', '\u{17d}', '\u{8f}',
'\u{90}', '\u{2018}', '\u{2019}', '\u{201c}', '\u{201d}', '\u{2022}', '\u{2013}', '\u{2014}',
'\u{2dc}', '\u{2122}', '\u{161}', '\u{203a}', '\u{153}', '\u{9d}', '\u{17e}', '\u{178}',
];
fn numeric_scalar(code: u32) -> char {
const REPLACEMENT: char = '\u{fffd}';
match code {
0x80..=0x9f => C1_REPLACEMENTS[(code - 0x80) as usize],
0 => REPLACEMENT,
code => char::from_u32(code).unwrap_or(REPLACEMENT),
}
}
fn numeric_entity(body: &str) -> Option<(Cow<'static, str>, usize)> {
let (radix, digits, prefix) = match body.strip_prefix(['x', 'X']) {
Some(hex) => (16, hex, 3usize),
None => (10, body, 2usize),
};
let taken = digits
.find(|character: char| !character.is_digit(radix))
.unwrap_or(digits.len());
let semicolon = usize::from(digits[taken..].starts_with(';'));
let consumed = prefix + taken + semicolon;
if consumed <= 3 || (taken == 0 && (radix == 10 || semicolon == 0)) {
return None;
}
let mut accumulator: i32 = 0;
let radix = i32::try_from(radix).ok()?;
for digit in digits[..taken].chars() {
let value = i32::try_from(digit.to_digit(36)?).ok()?;
accumulator = accumulator.wrapping_mul(radix).wrapping_add(value);
}
#[allow(clippy::cast_sign_loss)] let code = accumulator as u32;
Some((Cow::Owned(numeric_scalar(code).to_string()), consumed))
}
fn global_id_from_sgid(sgid: &str) -> Option<String> {
let value = sgid.trim();
if let Some(index) = value.rfind("--")
&& index > 0
&& let Some(gid) = envelope_gid(&value[..index])
{
return Some(gid);
}
envelope_gid(value)
}
fn envelope_gid(payload: &str) -> Option<String> {
if payload.is_empty() || payload.len() > MAX_SGID_ENCODED_BYTES {
return None;
}
let normalized: String = payload
.trim_end_matches('=')
.chars()
.map(|character| match character {
'-' => '+',
'_' => '/',
other => other,
})
.collect();
let raw = base64_decode(&normalized)?;
if raw.is_empty() || raw.len() > MAX_SGID_PAYLOAD_BYTES {
return None;
}
let envelope = if raw.starts_with(&[0x04, 0x08]) {
unmarshal_ruby(&raw[2..]).ok()?
} else if raw[0] == b'{' {
serde_json::from_slice::<Value>(&raw).ok()?
} else {
return None;
};
let top = envelope.as_object()?;
if let Some(rails) = top.get("_rails").and_then(Value::as_object) {
if rails.get("pur").and_then(Value::as_str) != Some(SGID_PURPOSE_ATTACHABLE) {
return None;
}
return rails
.get("data")
.and_then(Value::as_str)
.filter(|gid| !gid.is_empty())
.map(str::to_string);
}
if top.get("purpose").and_then(Value::as_str) != Some(SGID_PURPOSE_ATTACHABLE) {
return None;
}
top.get("gid")
.and_then(Value::as_str)
.filter(|gid| !gid.is_empty())
.map(str::to_string)
}
fn parse_global_id(gid: &str) -> Option<(String, String)> {
let (before_fragment, fragment) = match gid.split_once('#') {
Some((before, fragment)) => (before, Some(fragment)),
None => (gid, None),
};
if before_fragment
.bytes()
.any(|byte| byte < b' ' || byte == 0x7f)
{
return None;
}
if let Some(fragment) = fragment
&& !valid_escapes(fragment)
{
return None;
}
let after_scheme = strip_scheme(gid)?;
let authority_and_path = after_scheme.split(['?', '#']).next().unwrap_or_default();
let (authority, path) = authority_and_path.split_once('/')?;
let host = parse_authority(authority)?;
if host.is_empty() {
return None;
}
let path = percent_decode(path)?;
let (model, raw_id) = path.split_once('/')?;
if model.is_empty()
|| raw_id.is_empty()
|| raw_id.contains('/')
|| !raw_id.bytes().all(|byte| byte.is_ascii_digit())
{
return None;
}
Some((model.to_string(), raw_id.to_string()))
}
fn valid_escapes(text: &str) -> bool {
let bytes = text.as_bytes();
let mut index = 0;
while index < bytes.len() {
if bytes[index] == b'%' {
if index + 2 >= bytes.len()
|| !bytes[index + 1].is_ascii_hexdigit()
|| !bytes[index + 2].is_ascii_hexdigit()
{
return false;
}
index += 3;
} else {
index += 1;
}
}
true
}
fn strip_scheme(gid: &str) -> Option<&str> {
let (scheme, rest) = gid.split_once("://")?;
scheme.eq_ignore_ascii_case("gid").then_some(rest)
}
fn parse_authority(authority: &str) -> Option<String> {
match authority.rfind('@') {
Some(at) => {
if !valid_userinfo(&authority[..at]) {
return None;
}
parse_host(&authority[at + 1..])
}
None => parse_host(authority),
}
}
fn valid_userinfo(userinfo: &str) -> bool {
let bytes = userinfo.as_bytes();
for (index, byte) in bytes.iter().enumerate() {
if *byte == b'%'
&& !userinfo
.get(index + 1..index + 3)
.is_some_and(|hex| hex.bytes().all(|byte| byte.is_ascii_hexdigit()))
{
return false;
}
}
userinfo.bytes().all(|byte| {
byte.is_ascii_alphanumeric()
|| matches!(
byte,
b'-' | b'.'
| b'_'
| b':'
| b'~'
| b'!'
| b'$'
| b'&'
| b'\''
| b'('
| b')'
| b'*'
| b'+'
| b','
| b';'
| b'='
| b'%'
| b'@'
)
})
}
fn parse_host(host: &str) -> Option<String> {
let Some(open) = host.rfind('[') else {
if let Some(colon) = host.rfind(':')
&& !valid_optional_port(&host[colon..])
{
return None;
}
return unescape_in(host, HostPart::Host);
};
if open > 0 {
return None; }
let close = host.rfind(']')?;
let colon_port = &host[close + 1..];
if !valid_optional_port(colon_port) {
return None;
}
let unescaped_port = unescape_in(colon_port, HostPart::Host)?;
let hostname = &host[1..close];
let unescaped = match hostname.find("%25") {
Some(zone) => {
let head = unescape_in(&hostname[..zone], HostPart::Host)?;
let tail = unescape_in(&hostname[zone..], HostPart::Zone)?;
format!("{head}{tail}")
}
None => unescape_in(hostname, HostPart::Host)?,
};
let (address, zone) = match unescaped.split_once('%') {
Some((address, zone)) => (address, Some(zone)),
None => (unescaped.as_str(), None),
};
if zone == Some("") {
return None;
}
let address: std::net::IpAddr = address.parse().ok()?;
if address.is_ipv4() {
return None; }
Some(format!("[{unescaped}]{unescaped_port}"))
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum HostPart {
Host,
Zone,
}
fn valid_optional_port(port: &str) -> bool {
match port.strip_prefix(':') {
None => port.is_empty(),
Some(digits) => digits.bytes().all(|byte| byte.is_ascii_digit()),
}
}
fn unescape_in(host: &str, part: HostPart) -> Option<String> {
let bytes = host.as_bytes();
let mut out = Vec::with_capacity(bytes.len());
let mut pos = 0usize;
while pos < bytes.len() {
match bytes[pos] {
b'%' => {
let triple = host.get(pos..pos + 3)?;
let hex = &triple[1..];
let value = u8::from_str_radix(hex, 16).ok()?;
if !hex.bytes().all(|byte| byte.is_ascii_hexdigit()) {
return None;
}
let refused = match part {
HostPart::Host => unhex(bytes[pos + 1]) < 8 && triple != "%25",
HostPart::Zone => triple != "%25" && value != b' ' && !allowed_in_host(value),
};
if refused {
return None;
}
out.push(value);
pos += 3;
}
byte if byte < 0x80 && !allowed_in_host(byte) => return None,
byte => {
out.push(byte);
pos += 1;
}
}
}
Some(String::from_utf8_lossy(&out).into_owned())
}
fn unhex(byte: u8) -> u8 {
match byte {
b'0'..=b'9' => byte - b'0',
b'a'..=b'f' => byte - b'a' + 10,
b'A'..=b'F' => byte - b'A' + 10,
_ => 255,
}
}
fn allowed_in_host(byte: u8) -> bool {
byte.is_ascii_alphanumeric()
|| matches!(
byte,
b'-' | b'_'
| b'.'
| b'~'
| b'!'
| b'$'
| b'&'
| b'\''
| b'('
| b')'
| b'*'
| b'+'
| b','
| b';'
| b'='
| b':'
| b'['
| b']'
| b'<'
| b'>'
| b'"'
)
}
fn percent_decode(path: &str) -> Option<String> {
if !path.contains('%') {
return Some(path.to_string());
}
let bytes = path.as_bytes();
let mut out = Vec::with_capacity(bytes.len());
let mut pos = 0usize;
while pos < bytes.len() {
if bytes[pos] == b'%' {
let hex = path.get(pos + 1..pos + 3)?;
if !hex.bytes().all(|byte| byte.is_ascii_hexdigit()) {
return None;
}
out.push(u8::from_str_radix(hex, 16).ok()?);
pos += 3;
} else {
out.push(bytes[pos]);
pos += 1;
}
}
String::from_utf8(out).ok()
}
fn base64_decode(input: &str) -> Option<Vec<u8>> {
let mut out = Vec::with_capacity(input.len() / 4 * 3);
let mut accumulator: u32 = 0;
let mut bits = 0u32;
for byte in input.bytes() {
let value = match byte {
b'A'..=b'Z' => byte - b'A',
b'a'..=b'z' => byte - b'a' + 26,
b'0'..=b'9' => byte - b'0' + 52,
b'+' => 62,
b'/' => 63,
b'\n' | b'\r' => continue,
_ => return None,
};
accumulator = ((accumulator << 6) | u32::from(value)) & 0x3fff;
bits += 6;
if bits >= 8 {
bits -= 8;
out.push(u8::try_from((accumulator >> bits) & 0xff).ok()?);
}
}
(bits < 6).then_some(out)
}
const RUBY_MARSHAL_MAX_DEPTH: u32 = 32;
fn unmarshal_ruby(data: &[u8]) -> Result<Value, &'static str> {
let mut reader = MarshalReader {
data,
pos: 0,
symbols: Vec::new(),
};
let value = reader.value(0)?;
if reader.pos != data.len() {
return Err("marshal: trailing bytes");
}
Ok(value)
}
struct MarshalReader<'a> {
data: &'a [u8],
pos: usize,
symbols: Vec<String>,
}
impl MarshalReader<'_> {
fn byte(&mut self) -> Result<u8, &'static str> {
let byte = *self
.data
.get(self.pos)
.ok_or("marshal: unexpected end of data")?;
self.pos += 1;
Ok(byte)
}
fn bytes(&mut self, n: usize) -> Result<&[u8], &'static str> {
if n > self.data.len() - self.pos {
return Err("marshal: unexpected end of data");
}
let taken = &self.data[self.pos..self.pos + n];
self.pos += n;
Ok(taken)
}
fn int(&mut self) -> Result<i64, &'static str> {
let lead = i64::from(i8::from_ne_bytes([self.byte()?]));
match lead {
0 => Ok(0),
c if c > 4 => Ok(c - 5),
c if c < -4 => Ok(c + 5),
c if c > 0 => {
let raw = self.bytes(usize::try_from(c).unwrap_or_default())?;
let mut value: i64 = 0;
for (index, byte) in raw.iter().enumerate() {
value |= i64::from(*byte) << (8 * index);
}
Ok(value)
}
c => {
let raw = self.bytes(usize::try_from(-c).unwrap_or_default())?;
let mut value: i64 = -1;
for (index, byte) in raw.iter().enumerate() {
value &= !(0xff_i64 << (8 * index));
value |= i64::from(*byte) << (8 * index);
}
Ok(value)
}
}
}
fn count(&mut self) -> Result<usize, &'static str> {
let n = self.int()?;
let remaining = i64::try_from(self.data.len() - self.pos).unwrap_or(i64::MAX);
if n < 0 || n > remaining {
return Err("marshal: bad count");
}
usize::try_from(n).map_err(|_| "marshal: bad count")
}
fn value(&mut self, depth: u32) -> Result<Value, &'static str> {
if depth > RUBY_MARSHAL_MAX_DEPTH {
return Err("marshal: nesting too deep");
}
match self.byte()? {
b'0' => Ok(Value::Null),
b'T' => Ok(Value::Bool(true)),
b'F' => Ok(Value::Bool(false)),
b'i' => Ok(Value::from(self.int()?)),
b'"' => {
let n = self.count()?;
let raw = self.bytes(n)?;
Ok(Value::String(String::from_utf8_lossy(raw).into_owned()))
}
b':' => {
let n = self.count()?;
let symbol = String::from_utf8_lossy(self.bytes(n)?).into_owned();
self.symbols.push(symbol.clone());
Ok(Value::String(symbol))
}
b';' => {
let index = self.int()?;
let symbol = usize::try_from(index)
.ok()
.and_then(|index| self.symbols.get(index))
.ok_or("marshal: bad symbol link")?;
Ok(Value::String(symbol.clone()))
}
b'I' => {
let inner = self.value(depth + 1)?;
let n = self.count()?;
for _ in 0..n {
self.value(depth + 1)?; self.value(depth + 1)?; }
Ok(inner)
}
b'[' => {
let n = self.count()?;
let mut items = Vec::new();
for _ in 0..n {
items.push(self.value(depth + 1)?);
}
Ok(Value::Array(items))
}
b'{' => {
let n = self.count()?;
let mut map = serde_json::Map::new();
for _ in 0..n {
let key = self.value(depth + 1)?;
let value = self.value(depth + 1)?;
let Value::String(key) = key else {
return Err("marshal: non-string hash key");
};
map.insert(key, value);
}
Ok(Value::Object(map))
}
_ => Err("marshal: unsupported type"),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
const DECODE_DIGEST: u64 = 419_185_206_463_269_138;
const ANNIE_SGID: &str = "BAh7CEkiCGdpZAY6BkVUSSIrZ2lkOi8vYmMzL1BlcnNvbi8xMDQ5NzE1OTE1P2V4cGlyZXNfaW4GOwBUSSIMcHVycG9zZQY7AFRJIg9hdHRhY2hhYmxlBjsAVEkiD2V4cGlyZXNfYXQGOwBUMA==--aeb392ebf54ffd820e45f27add22bae3a8c7da56";
fn person(id: i64, sgid: Option<&str>) -> Person {
Person {
id,
attachable_sgid: sgid.map(str::to_string),
..Default::default()
}
}
#[test]
fn a_marshal_envelope_names_its_person() {
assert_eq!(person_id_from_sgid(ANNIE_SGID), Some(1_049_715_915));
}
#[test]
fn a_json_envelope_names_its_person_in_both_layouts() {
let current =
json_sgid(r#"{"_rails":{"data":"gid://bc3/Person/77","pur":"attachable","exp":null}}"#);
let older = json_sgid(r#"{"gid":"gid://bc3/Person/77","purpose":"attachable"}"#);
assert_eq!(person_id_from_sgid(¤t), Some(77));
assert_eq!(person_id_from_sgid(&older), Some(77));
}
#[test]
fn a_purpose_other_than_attachable_is_not_a_mention() {
let readable = json_sgid(r#"{"gid":"gid://bc3/Person/77","purpose":"readable"}"#);
let rails = json_sgid(r#"{"_rails":{"data":"gid://bc3/Person/77","pur":"readable"}}"#);
let none = json_sgid(r#"{"gid":"gid://bc3/Person/77"}"#);
assert_eq!(person_id_from_sgid(&readable), None);
assert_eq!(person_id_from_sgid(&rails), None);
assert_eq!(person_id_from_sgid(&none), None);
}
const NET_URL_ANSWERS: &[(&str, Option<i64>)] = &[
("gid://user@bc3/Person/77", Some(77)),
("gid://user:pw@bc3/Person/77", Some(77)),
("gid://a@b@bc3/Person/77", Some(77)),
("gid://@bc3/Person/77", Some(77)),
("gid://bc3:8080/Person/77", Some(77)),
("gid://bc3:/Person/77", Some(77)),
("gid://bc3:xx/Person/77", None),
("gid://[::1]/Person/77", Some(77)),
("gid://b%63%33/Person/77", None),
("gid://%20/Person/77", None),
(r"gid://bc3/Person/77?\u0001", None),
(r"gid://bc3/Person/77#\u0001", Some(77)),
("gid://user:pw@bc3/Person/77", Some(77)),
("gid://%40@bc3/Person/77", Some(77)),
("gid://u s@bc3/Person/77", None),
("gid://u%s@bc3/Person/77", None),
("gid://[::1]/Person/77", Some(77)),
("gid://[::1]:80/Person/77", Some(77)),
("gid://[::1%25eth0]/Person/77", Some(77)),
("gid://[bad]/Person/77", None),
("gid://[192.0.2.1]/Person/77", None),
("gid://[::1/Person/77", None),
("gid://bc3[/Person/77", None),
("gid://a:1]/Person/77", None),
("gid://[not-an-ip]/Person/1", None),
("gid://[abc]/Person/1", None),
("gid://[1]/Person/1", None),
("gid://[::1::2]/Person/1", None),
("gid://[]/Person/1", None),
("gid://[::ffff:192.0.2.1]/Person/1", Some(1)),
("gid://[fe80::1%25eth0]:8080/Person/1", Some(1)),
("gid://[::1]:/Person/1", Some(1)),
("gid://[not-an-address%25zone]/Person/77", None),
("gid://[abc%25z]/Person/77", None),
("gid://[1%25z]/Person/77", None),
("gid://[bad%25x]/Person/77", None),
("gid://[%25]/Person/77", None),
("gid://[::1%25]/Person/77", None),
("gid://[192.0.2.1%25eth0]/Person/77", None),
("gid://[::ffff:192.0.2.1%25eth0]/Person/77", Some(77)),
("gid://[::1%25%41]/Person/77", Some(77)),
("gid://[::1%25a%20b]/Person/77", Some(77)),
("gid://[::1%25a%2fb]/Person/77", None),
("gid://[::1%25%01]/Person/77", None),
("gid://[v7.x]/Person/77", None),
("gid://[V7.x]/Person/77", None),
("gid://[vz.x]/Person/77", None),
("gid://[v1.fe80::a+en1]/Person/77", None),
("gid://@/Person/1", None),
("gid://:@/Person/1", None),
("gid://%20@/Person/1", None),
("gid:///Person/1", None),
("gid://@:/Person/1", Some(1)),
("gid://@:8080/Person/1", Some(1)),
("gid://:8080/Person/77", Some(77)),
("gid://:/Person/77", Some(77)),
("gid://%25/Person/77", Some(77)),
("gid://bc%80/Person/77", Some(77)),
("gid://%C3%A9/Person/77", Some(77)),
("gid://%bc3/Person/77", Some(77)),
("gid://b%63%33/Person/77", None),
("gid://bc3%/Person/77", None),
("gid://bc3/Person/+77", None),
("gid://bc3/Person/-77", None),
("gid://bc3/Person/++77", None),
("gid://bc3/Person/+0", None),
("gid://bc3/Person/ 77", None),
("gid://bc3/Person/77 ", None),
("gid://bc3/Person/7_7", None),
("gid://bc3/Person/0x4d", None),
("gid://bc3/Person/77#%zz", None),
("gid://bc3/Person/77#%", None),
("gid://bc3/Person/77#%2", None),
("gid://bc3/Person/77#x%0gy", None),
("gid://bc3/Person/77?ok#%zz", None),
("gid://bc3/Person/77#a#%zz", None),
("gid://bc3/Person/77#%41", Some(77)),
("gid://bc3/Person/77#%ff", Some(77)),
("gid://bc3/Person/77#ok", Some(77)),
("gid://bc3/Person/77#", Some(77)),
("gid://bc3/Person/77?%zz", Some(77)),
("gid://bc3/Person/77?x=%zz", Some(77)),
("gid://bc3/Person/77?%", Some(77)),
("gid://b<3/Person/77", Some(77)),
("gid://b;3/Person/77", Some(77)),
("gid://b^3/Person/77", None),
("gid://b|3/Person/77", None),
("gid://bc3/Person/77", Some(77)),
("gid://bc3/Person/%37%37", Some(77)),
("gid://bc3/Pe%72son/77", Some(77)),
("GID://bc3/Person/77", Some(77)),
("Gid://bc3/Person/77", Some(77)),
("gid:// /Person/77", None),
("gid://b c3/Person/77", None),
("gid:///Person/77", None),
("gid://bc3/Person/%3", None),
("gid://bc3/Person/%zz", None),
("gid://bc3/Person/77/extra", None),
("gid://bc3/Person/12x", None),
];
#[test]
fn the_gid_parse_answers_what_net_url_answers() {
for (gid, expected) in NET_URL_ANSWERS {
let sgid = json_sgid(&format!(r#"{{"gid":"{gid}","purpose":"attachable"}}"#));
assert_eq!(person_id_from_sgid(&sgid), *expected, "{gid}");
}
}
#[test]
fn entities_decode_where_go_decodes_them() {
let go = [
("=", "="),
("/", "/"),
("+", "+"),
("_", "_"),
("_", "_"),
("&", "&"),
(""", "\""),
("B", "B"),
("B", "B"),
("B", "B"),
("B", "B"),
("	", "\t"),
("	", "	"),
("	B", "	B"),
("", "\u{1}"),
("", "\u{c}"),
("	", "\t"),
("", "\u{b}"),
("…", "\u{2026}"),
("…", "\u{2026}"),
("€", "\u{20ac}"),
("Ÿ", "\u{178}"),
("", "\u{81}"),
("", "\u{8d}"),
("", "\u{9d}"),
(" ", "\u{a0}"),
("�", "\u{fffd}"),
("�", "�"),
("�", "\u{fffd}"),
("�", "\u{fffd}"),
("&#x;", "\u{fffd}"),
("&#;", "&#;"),
("", "\u{10ffff}"),
("�", "A"),
("�", "A"),
("�", " "),
("�", "A"),
("&unknownthing;", "&unknownthing;"),
("&", "&"),
("&;", "&;"),
("a&b", "a&b"),
];
for (input, expected) in go {
assert_eq!(unescape(input), expected, "{input:?}");
}
}
#[test]
fn an_sgid_whose_base64_characters_are_escaped_still_names_its_person() {
let escaped = ANNIE_SGID.replace('=', "=");
let markup = format!(r#"<div><bc-attachment sgid="{escaped}"></bc-attachment></div>"#);
assert_eq!(mentioned_person_ids(&markup), vec![1_049_715_915_i64]);
let annie = person(1_049_715_915, Some(ANNIE_SGID));
let expanded = with_mentions(&markup, std::slice::from_ref(&annie)).unwrap();
assert_eq!(expanded.matches("bc-attachment sgid=").count(), 1);
}
#[test]
fn a_hostile_run_of_ampersands_stays_linear() {
let elapsed = |n: usize| {
let hostile = format!("<p sgid=\"{}\">x</p>", "&".repeat(n) + ";");
let started = std::time::Instant::now();
assert!(mentioned_person_ids(&hostile).is_empty());
started.elapsed()
};
let small = elapsed(20_000);
let large = elapsed(80_000);
let budget = small.max(std::time::Duration::from_micros(200)) * 10;
assert!(
large < budget,
"entity scanning is not linear: {small:?} for 20k, {large:?} for 80k"
);
}
#[test]
fn a_query_or_fragment_never_smuggles_a_path_past_the_authority() {
for gid in [
"gid://bc3?x/Person/77",
"gid://bc3#x/Person/77",
"gid://?/Person/77",
"gid://bc3?/Person/77",
] {
let sgid = json_sgid(&format!(r#"{{"gid":"{gid}","purpose":"attachable"}}"#));
assert_eq!(person_id_from_sgid(&sgid), None, "{gid}");
}
let real = json_sgid(r#"{"gid":"gid://bc3/Person/77?expires_in","purpose":"attachable"}"#);
assert_eq!(person_id_from_sgid(&real), Some(77));
}
#[test]
fn a_gid_that_is_not_a_person_is_not_a_mention() {
let blob =
json_sgid(r#"{"gid":"gid://bc3/ActiveStorage::Blob/12","purpose":"attachable"}"#);
let nested =
json_sgid(r#"{"gid":"gid://bc3/Document/gid://bc3/Person/77","purpose":"attachable"}"#);
let not_numeric = json_sgid(r#"{"gid":"gid://bc3/Person/12x","purpose":"attachable"}"#);
let extra_segment =
json_sgid(r#"{"gid":"gid://bc3/Person/77/extra","purpose":"attachable"}"#);
assert_eq!(person_id_from_sgid(&blob), None);
assert_eq!(person_id_from_sgid(&nested), None);
assert_eq!(person_id_from_sgid(¬_numeric), None);
assert_eq!(person_id_from_sgid(&extra_segment), None);
}
#[test]
fn junk_and_oversized_sgids_decode_to_nothing() {
assert_eq!(person_id_from_sgid(""), None);
assert_eq!(person_id_from_sgid("--"), None);
assert_eq!(person_id_from_sgid("not base64 at all!"), None);
assert_eq!(
person_id_from_sgid(&"A".repeat(MAX_SGID_ENCODED_BYTES + 1)),
None
);
}
#[test]
fn mentions_are_read_in_document_order_without_repeats() {
let other = json_sgid(r#"{"gid":"gid://bc3/Person/88","purpose":"attachable"}"#);
let blob = json_sgid(r#"{"gid":"gid://bc3/ActiveStorage::Blob/9","purpose":"attachable"}"#);
let content = format!(
r#"<div><bc-attachment sgid="{ANNIE_SGID}"></bc-attachment> hi <bc-attachment sgid="{other}"></bc-attachment><bc-attachment sgid="{ANNIE_SGID}"></bc-attachment><bc-attachment sgid="{blob}"></bc-attachment></div>"#
);
assert_eq!(mentioned_person_ids(&content), vec![1_049_715_915_i64, 88]);
}
#[test]
fn a_quoted_mention_counts_and_a_commented_one_does_not() {
let quoted = format!(
r#"<blockquote><bc-attachment sgid="{ANNIE_SGID}"></bc-attachment></blockquote>"#
);
let commented =
format!(r#"<div><!-- <bc-attachment sgid="{ANNIE_SGID}"></bc-attachment> --></div>"#);
let in_an_attribute =
format!(r#"<div title="<bc-attachment sgid="{ANNIE_SGID}">">x</div>"#);
assert_eq!(mentioned_person_ids("ed), vec![1_049_715_915_i64]);
assert!(mentioned_person_ids(&commented).is_empty());
assert!(mentioned_person_ids(&in_an_attribute).is_empty());
}
#[test]
fn attribute_tokenizing_survives_quotes_case_and_order() {
let cases = [
format!("<BC-ATTACHMENT SGID='{ANNIE_SGID}'></bc-attachment>"),
format!(r#"<bc-attachment caption="a > b" sgid="{ANNIE_SGID}"></bc-attachment>"#),
format!(r#"<bc-attachment data-x="sgid=nope" sgid={ANNIE_SGID}></bc-attachment>"#),
format!(r#"<bc-attachment sgid="{ANNIE_SGID}" sgid="later"></bc-attachment>"#),
];
for case in &cases {
assert_eq!(
mentioned_person_ids(case),
vec![1_049_715_915_i64],
"{case}"
);
}
}
#[test]
fn a_tag_name_that_merely_starts_with_the_name_is_not_one() {
let near = format!(r#"<bc-attachment_x sgid="{ANNIE_SGID}"></bc-attachment_x>"#);
assert!(mentioned_person_ids(&near).is_empty());
}
#[test]
fn the_fold_admits_exactly_the_two_runes_that_fold_onto_ascii() {
assert!(equal_fold("\u{17f}gid".as_bytes(), b"sgid"));
assert!(equal_fold("sgi\u{17f}".as_bytes(), b"sgis"));
assert!(equal_fold("\u{212a}".as_bytes(), b"k"));
assert!(equal_fold("\u{212a}".as_bytes(), b"K"));
assert!(equal_fold("mil\u{212a}".as_bytes(), b"MILK"));
assert!(equal_fold("\u{17f}\u{212a}".as_bytes(), b"sk"));
assert!(!equal_fold("\u{212a}gid".as_bytes(), b"sgid"));
assert!(!equal_fold("\u{17f}".as_bytes(), b"k"));
assert!(equal_fold(b"BC-ATTACHMENT", b"bc-attachment"));
assert!(equal_fold(b"<DIV", b"<div"));
assert!(!equal_fold(b"<div", b"<p"));
assert!(!equal_fold("\u{17f}gid".as_bytes(), b"sgi"));
assert!(!equal_fold(b"sgid", b"sgi"));
assert!(!equal_fold(b"sgi", b"sgid"));
assert!(equal_fold(b"", b""));
assert!(!equal_fold(b"x", b""));
assert!(!equal_fold(b"", b"x"));
assert!(!equal_fold(&[b'<', 0xc5, b'p'], b"<sp"));
assert!(!equal_fold(&[0xff], b"s"));
assert!(!equal_fold(&[0xc5], b"s"));
assert!(!equal_fold(&[0xc5, 0xbf, 0xbf], b"s"));
assert!(!equal_fold(&[0xe0, 0x85, 0xbf], b"s"));
assert!(!equal_fold(&[0xe2, 0x84], b"k"));
}
#[test]
fn a_name_folds_the_way_the_reference_folds_it_and_the_scheme_does_not() {
let long_s = format!("<bc-attachment \u{17f}gid=\"{ANNIE_SGID}\"></bc-attachment>");
assert_eq!(mentioned_person_ids(&long_s), vec![1_049_715_915_i64]);
let upper = format!("<BC-ATTACHMENT SGID=\"{ANNIE_SGID}\"></BC-ATTACHMENT>");
assert_eq!(mentioned_person_ids(&upper), vec![1_049_715_915_i64]);
for miss in ["\u{131}gid", "\u{130}gid", "égid", "zgid"] {
let markup = format!("<bc-attachment {miss}=\"{ANNIE_SGID}\"></bc-attachment>");
assert!(
mentioned_person_ids(&markup).is_empty(),
"{miss} is not the sgid attribute"
);
}
for miss in [
"sgi\u{17f}",
"\u{17f}gi\u{17f}",
"\u{212a}gid",
"sgi\u{212a}",
"\u{17f}\u{17f}gid",
] {
let markup = format!("<bc-attachment {miss}=\"{ANNIE_SGID}\"></bc-attachment>");
assert!(
mentioned_person_ids(&markup).is_empty(),
"{miss} is not the sgid attribute"
);
}
for miss in ["sgi", "sgidd"] {
let markup = format!("<bc-attachment {miss}=\"{ANNIE_SGID}\"></bc-attachment>");
assert!(
mentioned_person_ids(&markup).is_empty(),
"{miss} is the wrong length"
);
}
for scheme in ["g\u{131}d", "G\u{130}D", "\u{131}d"] {
let payload = json_sgid(&format!(
r#"{{"gid":"{scheme}://bc3/Person/77","purpose":"attachable"}}"#
));
assert_eq!(person_id_from_sgid(&payload), None, "{scheme} is not gid");
}
for scheme in ["gid", "GID", "Gid"] {
let payload = json_sgid(&format!(
r#"{{"gid":"{scheme}://bc3/Person/77","purpose":"attachable"}}"#
));
assert_eq!(person_id_from_sgid(&payload), Some(77), "{scheme} is gid");
}
}
#[test]
fn an_unterminated_tag_ends_the_walk() {
let unterminated = format!(
r#"<bc-attachment sgid="{ANNIE_SGID}"></bc-attachment><bc-attachment sgid="oops"#
);
assert_eq!(mentioned_person_ids(&unterminated), vec![1_049_715_915_i64]);
}
#[test]
fn markup_needs_an_sgid_that_names_the_person() {
assert!(mention_markup(&person(1, None)).is_err());
assert!(mention_markup(&person(1, Some(""))).is_err());
assert!(mention_markup(&person(1, Some("a\"b"))).is_err());
assert!(mention_markup(&person(1, Some(ANNIE_SGID))).is_err());
assert_eq!(
mention_markup(&person(1_049_715_915, Some(ANNIE_SGID))).unwrap(),
format!(r#"<bc-attachment sgid="{ANNIE_SGID}"></bc-attachment>"#)
);
}
#[test]
fn mentions_go_inside_the_first_block_or_in_front_of_bare_text() {
let annie = person(1_049_715_915, Some(ANNIE_SGID));
let tag = mention_markup(&annie).unwrap();
assert_eq!(
with_mentions("<div>On it.</div>", std::slice::from_ref(&annie)).unwrap(),
format!("<div>{tag} On it.</div>")
);
assert_eq!(
with_mentions(r#"<p class="x">On it.</p>"#, std::slice::from_ref(&annie)).unwrap(),
format!(r#"<p class="x">{tag} On it.</p>"#)
);
assert_eq!(
with_mentions("On it.", std::slice::from_ref(&annie)).unwrap(),
format!("{tag} On it.")
);
assert_eq!(
with_mentions("<span>On it.</span>", std::slice::from_ref(&annie)).unwrap(),
format!("{tag} <span>On it.</span>")
);
}
#[test]
fn the_same_sgid_is_never_written_twice() {
let annie = person(1_049_715_915, Some(ANNIE_SGID));
let content = format!(r#"<div><bc-attachment sgid="{ANNIE_SGID}"></bc-attachment></div>"#);
assert_eq!(
with_mentions(&content, std::slice::from_ref(&annie)).unwrap(),
content
);
let twice = with_mentions("Hi", &[annie.clone(), annie]).unwrap();
assert_eq!(twice.matches("bc-attachment sgid=").count(), 1);
}
#[test]
fn an_overflowing_numeric_reference_still_completes_the_sgid_it_sits_in() {
let head = &ANNIE_SGID[..1];
let tail = &ANNIE_SGID[2..];
for reference in [
"�",
"�",
"�",
"�",
] {
let markup =
format!(r#"<bc-attachment sgid="{head}{reference}{tail}"></bc-attachment>"#);
assert_eq!(
mentioned_person_ids(&markup),
vec![1_049_715_915_i64],
"{reference} must decode to A and complete the sgid"
);
}
}
#[test]
fn a_digit_is_an_ascii_digit_everywhere_a_digit_is_read() {
for digits in [
"\u{ff10}\u{ff11}\u{ff12}\u{ff13}\u{ff14}\u{ff15}\u{ff16}\u{ff17}\u{ff18}\u{ff19}",
"\u{660}\u{661}\u{662}\u{663}\u{664}\u{665}\u{666}\u{667}\u{668}\u{669}",
"\u{966}\u{967}\u{968}\u{969}\u{96a}\u{96b}\u{96c}\u{96d}\u{96e}\u{96f}",
"\u{1d7ce}\u{1d7cf}\u{1d7d0}\u{1d7d1}\u{1d7d2}\u{1d7d3}\u{1d7d4}\u{1d7d5}\u{1d7d6}\u{1d7d7}",
] {
let digit: Vec<char> = digits.chars().collect();
let seven = digit[7];
let payload = json_sgid(&format!(
r#"{{"gid":"gid://bc3/Person/{seven}{seven}","purpose":"attachable"}}"#
));
assert_eq!(
person_id_from_sgid(&payload),
None,
"{seven} is not a digit"
);
let mixed = json_sgid(&format!(
r#"{{"gid":"gid://bc3/Person/7{seven}","purpose":"attachable"}}"#
));
assert_eq!(person_id_from_sgid(&mixed), None, "7{seven} is not 7");
let head = &ANNIE_SGID[..1];
let tail = &ANNIE_SGID[2..];
for reference in [
format!("&#{}{};", digit[6], digit[5]),
format!("&#x{}{};", digit[4], digit[1]),
] {
let markup =
format!(r#"<bc-attachment sgid="{head}{reference}{tail}"></bc-attachment>"#);
assert!(
mentioned_person_ids(&markup).is_empty(),
"{reference} is not a numeric reference"
);
}
let ported = json_sgid(&format!(
r#"{{"gid":"gid://bc3:{seven}0/Person/77","purpose":"attachable"}}"#
));
assert_eq!(person_id_from_sgid(&ported), None, "{seven}0 is not a port");
}
assert_eq!(
person_id_from_sgid(&json_sgid(
r#"{"gid":"gid://bc3:80/Person/77","purpose":"attachable"}"#
)),
Some(77)
);
let ascii = format!(
r#"<bc-attachment sgid="{}A{}"></bc-attachment>"#,
&ANNIE_SGID[..1],
&ANNIE_SGID[2..]
);
assert_eq!(mentioned_person_ids(&ascii), vec![1_049_715_915_i64]);
}
#[test]
fn a_crafted_suffix_cannot_suppress_a_real_mention() {
let annie = person(1_049_715_915, Some(ANNIE_SGID));
let content_with = |suffix: &str| {
format!(r#"<div><bc-attachment sgid="{ANNIE_SGID}{suffix}"></bc-attachment>hi</div>"#)
};
let tags = |suffix: &str| {
with_mentions(&content_with(suffix), std::slice::from_ref(&annie))
.unwrap()
.matches("bc-attachment sgid=")
.count()
};
assert_eq!(tags(""), 1, "the dedupe fires when it should");
for suffix in [
"",
"",
"",
"",
"",
"�",
"",
"�",
"",
"​",
"�",
"&#;",
"&#",
"&#x",
"&",
"&&",
"&;",
"
",
"	",
" ",
" ",
"  ",
"fj",
"&",
"=",
"&unknownthing;",
] {
assert_eq!(
tags(suffix),
2,
"a real mention was suppressed by {suffix:?}"
);
}
}
#[test]
fn dedupe_is_on_the_sgid_string_not_on_the_person_id_it_decodes_to() {
let annie = person(1_049_715_915, Some(ANNIE_SGID));
let stale = json_sgid(r#"{"gid":"gid://bc3/Person/1049715915","purpose":"attachable"}"#);
let content = format!(r#"<div><bc-attachment sgid="{stale}"></bc-attachment></div>"#);
let expanded = with_mentions(&content, std::slice::from_ref(&annie)).unwrap();
assert!(expanded.contains(ANNIE_SGID));
assert_eq!(expanded.matches("bc-attachment sgid=").count(), 2);
}
#[test]
fn one_person_without_an_sgid_fails_the_whole_expansion() {
let annie = person(1_049_715_915, Some(ANNIE_SGID));
assert!(with_mentions("Hi", &[annie, person(2, None)]).is_err());
}
#[test]
fn a_marshal_payload_that_is_not_one_value_is_refused() {
assert!(unmarshal_ruby(b"0T").is_err());
assert!(unmarshal_ruby(b"c").is_err()); assert!(unmarshal_ruby(b"\"\x7f").is_err()); }
#[test]
fn base64_matches_go_raw_std_encoding_in_both_directions() {
let go = [
("QQ", Some(vec![b'A'])),
("QR", Some(vec![b'A'])),
("QUJD", Some(vec![b'A', b'B', b'C'])),
("QUJ", Some(vec![b'A', b'B'])),
("QUK", Some(vec![b'A', b'B'])),
("QU\nJD", Some(vec![b'A', b'B', b'C'])),
("QU\r\nJD", Some(vec![b'A', b'B', b'C'])),
("QUJDR", None),
("A", None),
("QU JD", None),
("~~~~", None),
];
for (input, expected) in go {
assert_eq!(base64_decode(input), expected, "{input:?}");
}
}
#[test]
fn a_long_payload_decodes_byte_for_byte_past_the_accumulator_width() {
let payload = ANNIE_SGID.split("--").next().unwrap();
let decoded = base64_decode(
&payload
.trim_end_matches('=')
.replace('-', "+")
.replace('_', "/"),
)
.expect("the payload decodes");
assert_eq!(decoded.len(), 109, "as Go's RawStdEncoding reports");
assert_eq!(&decoded[..2], &[0x04, 0x08], "a Marshal 4.8 header");
assert!(
String::from_utf8_lossy(&decoded).contains("gid://bc3/Person/1049715915"),
"the gid survives the whole decode"
);
let oversized = "A".repeat(MAX_SGID_ENCODED_BYTES + 1);
assert_eq!(person_id_from_sgid(&oversized), None);
}
#[test]
fn a_line_wrapped_sgid_decodes_exactly_where_go_decodes_it() {
let split = format!("{}\n{}", &ANNIE_SGID[..20], &ANNIE_SGID[20..]);
assert_eq!(person_id_from_sgid(&split), Some(1_049_715_915));
let before_digest = ANNIE_SGID.replacen("--", "\n--", 1);
assert_eq!(person_id_from_sgid(&before_digest), None);
}
#[test]
fn the_sgid_read_agrees_with_the_go_function_row_for_row() {
const ANNIE: i64 = 1_049_715_915;
let cases: &[(&str, Option<i64>)] = &[
(
"BAh7CEkiCGdpZAY6BkVUSSIrZ2lkOi8vYmMzL1BlcnNvbi8xMDQ5NzE1OTE1P2V4cGlyZXNfaW4GOwBUSSIMcHVycG9zZQY7AFRJIg9hdHRhY2hhYmxlBjsAVEkiD2V4cGlyZXNfYXQGOwBUMA==--919d2c8b11ff403eefcab9db42dd26846d0c3102",
Some(ANNIE),
),
(
"BAh7CEkiCGdpZAY6BkVUSSIrZ2lkOi8vYmMzL1BlcnNvbi8xMDQ5NzE1OTE1P2V4cGlyZXNfaW4GOwBUSSIMcHVycG9zZQY7AFRJIg9hdHRhY2hhYmxlBjsAVEkiD2V4cGlyZXNfYXQGOwBUMA==\\n--919d2c8b11ff403eefcab9db42dd26846d0c3102",
None,
),
(
"BAh7CEkiCGdpZAY6BkVUSSIrZ2lkOi8vYmMzL1BlcnNvbi8xMDQ5NzE1OTE1P2V4cGlyZXNfaW4GOwBUSSIMcHVycG9zZQY7AFRJIg9hdHRhY2hhYmxlBjsAVEkiD2V4cGlyZXNfYXQGOwBUMA==\\r--919d2c8b11ff403eefcab9db42dd26846d0c3102",
None,
),
(
"BAh7CEkiCGdpZAY6BkVUSSIrZ2lkOi8vYmMzL1BlcnNvbi8xMDQ5NzE1OTE1P2V4cGlyZXNfaW4GOwBUSSIMcHVycG9zZQY7AFRJIg9hdHRhY2hhYmxlBjsAVEkiD2V4cGlyZXNfYXQGOwBUMA=\\n=--919d2c8b11ff403eefcab9db42dd26846d0c3102",
None,
),
(
"BAh7CEkiCGdpZAY6BkVUSSIrZ2lkOi8vYmMzL1BlcnNvbi8xMDQ5NzE1OTE1P2V4cGlyZXNfaW4GOwBUSSIMcHVycG9zZQY7AFRJIg9hdHRhY2hhYmxlBjsAVEkiD2V4cGlyZXNfYXQGOwBUMA\\n==--919d2c8b11ff403eefcab9db42dd26846d0c3102",
Some(ANNIE),
),
(
"BAh7CEkiCGdpZAY6BkVU\\nSSIrZ2lkOi8vYmMzL1BlcnNvbi8xMDQ5NzE1OTE1P2V4cGlyZXNfaW4GOwBUSSIMcHVycG9zZQY7AFRJIg9hdHRhY2hhYmxlBjsAVEkiD2V4cGlyZXNfYXQGOwBUMA==--919d2c8b11ff403eefcab9db42dd26846d0c3102",
Some(ANNIE),
),
(
"BAh7CEkiCGdpZAY6BkVUSSIrZ2lkOi8vYmMzL1BlcnNvbi8xMDQ5NzE1OTE1P2V4cGlyZXNfaW4GOwBUSSIMcHVycG9zZQY7AFRJIg9hdHRhY2hhYmxlBjsAVEkiD2V4cGlyZXNfYXQGOwBUMA==--\\n919d2c8b11ff403eefcab9db42dd26846d0c3102",
Some(ANNIE),
),
(
"BAh7CEkiCGdpZAY6BkVUSSIrZ2lkOi8vYmMzL1BlcnNvbi8xMDQ5NzE1OTE1P2V4cGlyZXNfaW4GOwBUSSIMcHVycG9zZQY7AFRJIg9hdHRhY2hhYmxlBjsAVEkiD2V4cGlyZXNfYXQGOwBUMA==--919d2c8b11ff403eefcab9db42dd26846d0c3102\\n",
Some(ANNIE),
),
(
"\\nBAh7CEkiCGdpZAY6BkVUSSIrZ2lkOi8vYmMzL1BlcnNvbi8xMDQ5NzE1OTE1P2V4cGlyZXNfaW4GOwBUSSIMcHVycG9zZQY7AFRJIg9hdHRhY2hhYmxlBjsAVEkiD2V4cGlyZXNfYXQGOwBUMA==--919d2c8b11ff403eefcab9db42dd26846d0c3102",
Some(ANNIE),
),
(
"\\s\\sBAh7CEkiCGdpZAY6BkVUSSIrZ2lkOi8vYmMzL1BlcnNvbi8xMDQ5NzE1OTE1P2V4cGlyZXNfaW4GOwBUSSIMcHVycG9zZQY7AFRJIg9hdHRhY2hhYmxlBjsAVEkiD2V4cGlyZXNfYXQGOwBUMA==--919d2c8b11ff403eefcab9db42dd26846d0c3102\\s\\s",
Some(ANNIE),
),
(
"BAh7CEkiCGdpZAY6BkVUSSIrZ2lkOi8vYmMzL1BlcnNvbi8xMDQ5NzE1OTE1P2V4cGlyZXNfaW4GOwBUSSIMcHVycG9zZQY7AFRJIg9hdHRhY2hhYmxlBjsAVEkiD2V4cGlyZXNfYXQGOwBUMA==--919d2c8b11ff403eefcab9db42dd26846d0c3102\\t",
Some(ANNIE),
),
(
"BAh7CEkiCGdpZAY6BkVUSSIrZ2lkOi8vYmMzL1BlcnNvbi8xMDQ5NzE1OTE1P2V4cGlyZXNfaW4GOwBUSSIMcHVycG9zZQY7AFRJIg9hdHRhY2hhYmxlBjsAVEkiD2V4cGlyZXNfYXQGOwBUMA==",
Some(ANNIE),
),
(
"BAh7CEkiCGdpZAY6BkVUSSIrZ2lkOi8vYmMzL1BlcnNvbi8xMDQ5NzE1OTE1P2V4cGlyZXNfaW4GOwBUSSIMcHVycG9zZQY7AFRJIg9hdHRhY2hhYmxlBjsAVEkiD2V4cGlyZXNfYXQGOwBUMA==\\n",
Some(ANNIE),
),
(
"\\nBAh7CEkiCGdpZAY6BkVUSSIrZ2lkOi8vYmMzL1BlcnNvbi8xMDQ5NzE1OTE1P2V4cGlyZXNfaW4GOwBUSSIMcHVycG9zZQY7AFRJIg9hdHRhY2hhYmxlBjsAVEkiD2V4cGlyZXNfYXQGOwBUMA==",
Some(ANNIE),
),
(
"BAh7CEkiCGdpZAY6BkVUSSIrZ2lkOi8vYmMzL1BlcnNvbi8xMDQ5NzE1OTE1P2V4cGlyZXNfaW4GOwBUSSIMcHVycG9zZQY7AFRJIg9hdHRhY2hhYmxlBjsAVEkiD2V4cGlyZXNfYXQGOwBUMA=\\n=",
None,
),
(
"BAh7CEkiCGdpZAY6BkVUSSIrZ2lkOi8vYmMzL1BlcnNvbi8xMDQ5NzE1OTE1P2V4cGlyZXNfaW4GOwBUSSIMcHVycG9zZQY7AFRJIg9hdHRhY2hhYmxlBjsAVEkiD2V4cGlyZXNfYXQGOwBUMA\\n==",
Some(ANNIE),
),
(
"BAh7CEkiCGdpZAY6BkVU\\nSSIrZ2lkOi8vYmMzL1BlcnNvbi8xMDQ5NzE1OTE1P2V4cGlyZXNfaW4GOwBUSSIMcHVycG9zZQY7AFRJIg9hdHRhY2hhYmxlBjsAVEkiD2V4cGlyZXNfYXQGOwBUMA==",
Some(ANNIE),
),
(
"BAh7CEkiCGdpZAY6BkVUSSIrZ2lkOi8vYmMzL1BlcnNvbi8xMDQ5NzE1OTE1P2V4cGlyZXNfaW4GOwBUSSIMcHVycG9zZQY7AFRJIg9hdHRhY2hhYmxlBjsAVEkiD2V4cGlyZXNfYXQGOwBUMA--919d2c8b11ff403eefcab9db42dd26846d0c3102",
Some(ANNIE),
),
(
"BAh7CEkiCGdpZAY6BkVUSSIrZ2lkOi8vYmMzL1BlcnNvbi8xMDQ5NzE1OTE1P2V4cGlyZXNfaW4GOwBUSSIMcHVycG9zZQY7AFRJIg9hdHRhY2hhYmxlBjsAVEkiD2V4cGlyZXNfYXQGOwBUMA==--919d2c8b11ff403eefcab9db42dd26846d0c3102--extra",
None,
),
(
"BAh7CEkiCGdpZAY6BkVUSSIrZ2lkOi8vYmMzL1BlcnNvbi8xMDQ5NzE1OTE1P2V4cGlyZXNfaW4GOwBUSSIMcHVycG9zZQY7AFRJIg9hdHRhY2hhYmxlBjsAVEkiD2V4cGlyZXNfYXQGOwBUMA==--",
Some(ANNIE),
),
];
for (escaped, expected) in cases {
let sgid = expand(escaped);
assert_eq!(person_id_from_sgid(&sgid), *expected, "{escaped}");
}
}
fn expand(value: &str) -> String {
let bytes = value.as_bytes();
let mut out = String::new();
let mut index = 0;
while index < bytes.len() {
if bytes[index] == b'\\' && index + 1 < bytes.len() {
let replacement = match bytes[index + 1] {
b'n' => Some('\n'),
b'r' => Some('\r'),
b's' => Some(' '),
b't' => Some('\t'),
_ => None,
};
if let Some(character) = replacement {
out.push(character);
index += 2;
continue;
}
}
out.push(char::from(bytes[index]));
index += 1;
}
out
}
#[test]
fn the_decode_is_identical_on_every_toolchain_that_builds_this() {
let mut digest: u64 = 0xcbf2_9ce4_8422_2325;
let mut feed = |text: &str| {
for byte in text.as_bytes() {
digest ^= u64::from(*byte);
digest = digest.wrapping_mul(0x0000_0100_0000_01b3);
}
digest ^= 0xff;
digest = digest.wrapping_mul(0x0000_0100_0000_01b3);
};
for code in (0u32..0x0300).chain([0xd7ff, 0xd800, 0xdfff, 0xe000, 0x0010_ffff, 0x0011_0000])
{
for spelling in [
format!("&#{code};"),
format!("&#{code}"),
format!("&#x{code:x};"),
format!("&#x{code:x}"),
format!("�{code};"),
format!("&#X{code:X};"),
] {
feed(&unescape(&spelling));
}
}
for (name, _) in VERDICT_RELEVANT_ENTITIES {
feed(&unescape(&format!("&{name}")));
feed(&unescape(&format!("&{}", name.trim_end_matches(';'))));
feed(&unescape(&format!("&{name}tail")));
}
for code in (0u32..0x3001).step_by(7) {
let Some(character) = char::from_u32(code) else {
continue;
};
let sgid = format!("{character}{ANNIE_SGID}{character}");
feed(match person_id_from_sgid(&sgid) {
Some(_) => "mention",
None => "none",
});
}
assert_eq!(
digest, DECODE_DIGEST,
"the decode differs on this toolchain; the pinned tables above say which rows are \
right, this only says something moved"
);
}
#[test]
fn a_space_at_one_end_and_a_stray_in_the_digest_agree_with_go() {
const ANNIE: i64 = 1_049_715_915;
let payload = ANNIE_SGID.split("--").next().unwrap().to_string();
let digest = ANNIE_SGID.split("--").nth(1).unwrap().to_string();
let signed = |lead: &str, stray: &str| {
format!("{lead}{payload}--{}{stray}{}", &digest[..10], &digest[10..])
};
for lead in ["\u{20}", "\u{a0}", "\u{2009}", "\u{3000}", "\u{85}"] {
for stray in ["\u{fffd}", "\u{200b}", "\u{feff}", "\u{b7}"] {
assert_eq!(
person_id_from_sgid(&signed(lead, stray)),
Some(ANNIE),
"lead {lead:?} stray {stray:?}"
);
}
}
for space in [
"\u{20}", "\u{9}", "\u{a}", "\u{d}", "\u{b}", "\u{c}", "\u{85}", "\u{a0}", "\u{1680}",
"\u{2000}", "\u{2009}", "\u{200a}", "\u{202f}", "\u{205f}", "\u{3000}", "\u{2028}",
"\u{2029}",
] {
assert_eq!(
person_id_from_sgid(&format!("{space}{ANNIE_SGID}")),
Some(ANNIE),
"leading {space:?}"
);
assert_eq!(
person_id_from_sgid(&format!("{space}{ANNIE_SGID}{space}")),
Some(ANNIE),
"both ends {space:?}"
);
}
for stray in ["\u{fffd}", "\u{200b}", "\u{2060}", "\u{feff}", "\u{b7}"] {
assert_eq!(
person_id_from_sgid(&format!("{stray}{ANNIE_SGID}")),
None,
"leading {stray:?}"
);
}
assert_eq!(person_id_from_sgid(&signed("", "")), Some(ANNIE));
assert_eq!(
person_id_from_sgid(&format!("{payload}\u{a0}--{digest}")),
None
);
}
#[test]
fn whitespace_around_the_whole_sgid_is_trimmed_before_anything_reads_it() {
let unsigned = ANNIE_SGID.split("--").next().unwrap().to_string();
for (name, sgid) in [
("trailing LF", format!("{ANNIE_SGID}\n")),
("trailing CRLF", format!("{ANNIE_SGID}\r\n")),
("trailing spaces", format!("{ANNIE_SGID} ")),
("leading LF", format!("\n{ANNIE_SGID}")),
("surrounded", format!(" {ANNIE_SGID}\n")),
("unsigned with a trailing LF", format!("{unsigned}\n")),
("unsigned bare", unsigned.clone()),
] {
assert_eq!(person_id_from_sgid(&sgid), Some(1_049_715_915), "{name}");
}
}
#[test]
fn a_malformed_byte_in_a_json_envelope_declines_where_go_substitutes() {
let gid = br#""gid":"gid://bc3/Person/77","purpose":"attachable""#;
for (name, damaged) in [
("a raw continuation byte", &b"\x80"[..]),
("a raw 0xff, which is in no UTF-8 sequence", &b"\xff"[..]),
("an overlong NUL", &b"\xc0\x80"[..]),
("a surrogate encoded as UTF-8", &b"\xed\xa0\x80"[..]),
("a truncated three-byte sequence", &b"\xe2\x82"[..]),
("a code point above the maximum", &b"\xf4\x90\x80\x80"[..]),
("an unpaired high surrogate escape", &br"\ud800"[..]),
("an unpaired low surrogate escape", &br"\udfff"[..]),
] {
for (position, envelope) in [
(
"an ignored value",
[b"{".as_slice(), gid, br#","x":""#, damaged, br#""}"#].concat(),
),
(
"an ignored key",
[b"{".as_slice(), gid, b",\"", damaged, br#"":"y"}"#].concat(),
),
] {
assert_eq!(
person_id_from_sgid(&json_sgid_bytes(&envelope)),
None,
"{name} in {position}: Go answers 77 here; this port declines, and the \
divergence is deliberate"
);
}
}
assert_eq!(
person_id_from_sgid(&json_sgid_bytes(
&[b"{".as_slice(), gid, br#","x":"ok"}"#].concat()
)),
Some(77)
);
for valid in [&b"\xf0\x9f\x98\x80"[..], &br"\ud83d\ude00"[..]] {
assert_eq!(
person_id_from_sgid(&json_sgid_bytes(
&[b"{".as_slice(), gid, br#","x":""#, valid, br#""}"#].concat()
)),
Some(77)
);
}
for (position, damaged_gid) in [
("the query", &br"gid://bc3/Person/77?x=\ud800"[..]),
("the query, raw", &b"gid://bc3/Person/77?x=\xff"[..]),
("the fragment", &br"gid://bc3/Person/77#\ud800"[..]),
("the fragment, raw", &b"gid://bc3/Person/77#\xff"[..]),
("the host", &b"gid://bc\xff3/Person/77"[..]),
] {
let envelope = [
br#"{"gid":""#.as_slice(),
damaged_gid,
br#"","purpose":"attachable"}"#,
]
.concat();
assert_eq!(
person_id_from_sgid(&json_sgid_bytes(&envelope)),
None,
"damage in {position}: Go answers 77, this declines"
);
}
for marshal in [
"BAh7CEkiCGdpZAY6BkVUSSIYZ2lkOi8vYmMzL1BlcnNvbi83NwY6BkVUSSIMcHVycG9zZQY6BkVUSSIPYXR0YWNoYWJsZQY6BkVUSSIGeAY6BkVUSSIG_wY6BkVU",
"BAh7B0kiCGdpZAY6BkVUSSIcZ2lkOi8vYmMzL1BlcnNvbi83Nz94Pf8GOgZFVEkiDHB1cnBvc2UGOgZFVEkiD2F0dGFjaGFibGUGOgZFVA",
] {
assert_eq!(person_id_from_sgid(marshal), Some(77));
}
for (position, envelope) in [
(
"the id",
r#"{"gid":"gid://bc3/Person/77\ufffd","purpose":"attachable"}"#,
),
(
"the model name",
r#"{"gid":"gid://bc3/Pers\ufffdon/77","purpose":"attachable"}"#,
),
(
"the purpose",
r#"{"gid":"gid://bc3/Person/77","purpose":"attach\ufffdable"}"#,
),
] {
assert_eq!(
person_id_from_sgid(&json_sgid(envelope)),
None,
"U+FFFD in {position}"
);
}
assert_eq!(
person_id_from_sgid(&json_sgid(
r#"{"gid":"gid://bc\ufffd3/Person/77","purpose":"attachable"}"#
)),
Some(77)
);
}
fn json_sgid(json: &str) -> String {
json_sgid_bytes(json.as_bytes())
}
fn json_sgid_bytes(bytes: &[u8]) -> String {
const ALPHABET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
let mut out = String::new();
for chunk in bytes.chunks(3) {
let mut buffer = [0u8; 3];
buffer[..chunk.len()].copy_from_slice(chunk);
let packed =
(u32::from(buffer[0]) << 16) | (u32::from(buffer[1]) << 8) | u32::from(buffer[2]);
let encoded = 4 - (3 - chunk.len());
for index in 0..encoded {
let slot = usize::try_from((packed >> (18 - 6 * index)) & 0x3f).unwrap_or_default();
out.push(char::from(ALPHABET[slot]));
}
}
out
}
}