protocol: add generated bindings and conformance fixtures
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Check the content-addressed Phase 3A conformance corpus."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
import json
|
||||
import pathlib
|
||||
import sys
|
||||
|
||||
ROOT = pathlib.Path(__file__).resolve().parents[1]
|
||||
MANIFEST = ROOT / "fixtures/manifest.json"
|
||||
|
||||
|
||||
def digest(paths: list[str]) -> str:
|
||||
value = hashlib.sha256()
|
||||
for relative in paths:
|
||||
path = ROOT / relative
|
||||
value.update(relative.encode("utf-8"))
|
||||
value.update(b"\0")
|
||||
value.update(path.read_bytes())
|
||||
value.update(b"\0")
|
||||
return value.hexdigest()
|
||||
|
||||
|
||||
def main() -> int:
|
||||
manifest = json.loads(MANIFEST.read_text(encoding="utf-8"))
|
||||
paths = sorted(path.relative_to(ROOT).as_posix() for path in (ROOT / "fixtures/conformance").glob("*.tsv"))
|
||||
if paths != manifest.get("files"):
|
||||
raise ValueError("fixture manifest file list is stale")
|
||||
actual = digest(paths)
|
||||
expected = manifest.get("corpus_sha256")
|
||||
if not expected or actual != expected:
|
||||
raise ValueError(f"fixture corpus hash mismatch: {actual}")
|
||||
print(f"Fixture corpus SHA256 {actual}")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
raise SystemExit(main())
|
||||
except (OSError, ValueError, json.JSONDecodeError) as exc:
|
||||
print(f"fixture_digest: {exc}", file=sys.stderr)
|
||||
raise SystemExit(1)
|
||||
@@ -0,0 +1,383 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Deterministically generate the small language-neutral Phase 3A bindings."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import hashlib
|
||||
import json
|
||||
import pathlib
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
from typing import Any
|
||||
|
||||
|
||||
ROOT = pathlib.Path(__file__).resolve().parents[1]
|
||||
SCHEMA_PATH = ROOT / "schemas/control-v1.schema.json"
|
||||
VERSION_PATH = ROOT / "VERSION"
|
||||
COMPATIBILITY_PATH = ROOT / "compatibility.json"
|
||||
|
||||
|
||||
def load_schema() -> tuple[dict[str, Any], str, str, dict[str, Any]]:
|
||||
raw = SCHEMA_PATH.read_bytes()
|
||||
schema = json.loads(raw)
|
||||
version = VERSION_PATH.read_text(encoding="utf-8").strip()
|
||||
if not re.fullmatch(r"\d+\.\d+\.\d+", version):
|
||||
raise ValueError("VERSION must be semantic version text")
|
||||
compatibility = json.loads(COMPATIBILITY_PATH.read_text(encoding="utf-8"))
|
||||
for key in ("current", "n_minus_1", "n_minus_2"):
|
||||
if not isinstance(compatibility.get(key), str):
|
||||
raise ValueError("compatibility declaration is incomplete")
|
||||
return schema, hashlib.sha256(raw).hexdigest(), version, compatibility
|
||||
|
||||
|
||||
def pascal(name: str) -> str:
|
||||
return "".join(part[:1].upper() + part[1:] for part in re.split(r"[_-]", name))
|
||||
|
||||
|
||||
def go_field(name: str) -> str:
|
||||
initialisms = {"api": "API", "http": "HTTP", "id": "ID", "ip": "IP", "sha": "SHA", "ttl": "TTL", "url": "URL", "uuid": "UUID"}
|
||||
return "".join(
|
||||
initialisms.get(part, part[:1].upper() + part[1:])
|
||||
for part in re.split(r"[_-]", name)
|
||||
)
|
||||
|
||||
|
||||
def camel(name: str) -> str:
|
||||
parts = re.split(r"[_-]", name)
|
||||
return parts[0] + "".join(part[:1].upper() + part[1:] for part in parts[1:])
|
||||
|
||||
|
||||
def swift_field(name: str) -> str:
|
||||
value = camel(name)
|
||||
if value in {"protocol", "class", "struct", "enum", "extension", "private", "public", "internal", "fileprivate", "open", "func", "let", "var", "import", "switch", "case", "default", "operator", "where", "repeat", "return", "throw", "throws", "try", "catch", "defer", "in", "is", "as"}:
|
||||
return value + "Value"
|
||||
return value
|
||||
|
||||
|
||||
RUST_KEYWORDS = {
|
||||
"as", "break", "const", "continue", "crate", "else", "enum", "extern",
|
||||
"false", "fn", "for", "if", "impl", "in", "let", "loop", "match", "mod",
|
||||
"move", "mut", "pub", "ref", "return", "self", "Self", "static", "struct",
|
||||
"super", "trait", "true", "type", "unsafe", "use", "where", "while", "async",
|
||||
"await", "dyn", "abstract", "become", "box", "do", "final", "macro", "override",
|
||||
"priv", "typeof", "unsized", "virtual", "yield", "try", "union",
|
||||
}
|
||||
|
||||
|
||||
def rust_field(name: str) -> str:
|
||||
value = camel(name)
|
||||
return value + "Value" if value in RUST_KEYWORDS else value
|
||||
|
||||
|
||||
def ref_name(value: Any) -> str | None:
|
||||
if isinstance(value, dict) and isinstance(value.get("$ref"), str):
|
||||
return value["$ref"].split("/")[-1]
|
||||
return None
|
||||
|
||||
|
||||
def prop_type(prop: dict[str, Any], language: str) -> str:
|
||||
reference = ref_name(prop)
|
||||
if reference:
|
||||
return reference if language == "go" else (reference if language == "rust" else reference)
|
||||
if prop.get("type") == "array":
|
||||
item = prop.get("items", {})
|
||||
item_type = prop_type(item, language)
|
||||
if language == "go":
|
||||
return f"[]{item_type}"
|
||||
if language == "rust":
|
||||
return f"Vec<{item_type}>"
|
||||
return f"[{item_type}]"
|
||||
if prop.get("type") == "integer":
|
||||
return "int64" if language == "go" else ("i64" if language == "rust" else "Int64")
|
||||
if prop.get("type") == "boolean":
|
||||
return "bool" if language != "swift" else "Bool"
|
||||
if prop.get("type") == "object":
|
||||
return "map[string]any" if language == "go" else ("JsonObject" if language == "rust" else "JSONObject")
|
||||
return "string" if language == "go" else ("String" if language == "rust" else "String")
|
||||
|
||||
|
||||
def go_zero(prop: dict[str, Any], field: str) -> str:
|
||||
typ = prop_type(prop, "go")
|
||||
if typ in {"string", "int64", "bool"}:
|
||||
zero = {"string": '""', "int64": "0", "bool": "false"}[typ]
|
||||
return f"v.{field} == {zero}"
|
||||
if typ.startswith("[]"):
|
||||
return f"v.{field} == nil"
|
||||
if typ == "map[string]any":
|
||||
return f"v.{field} == nil"
|
||||
return f"reflect.DeepEqual(v.{field}, {typ}{{}})"
|
||||
|
||||
|
||||
def go_validation(definition: dict[str, Any]) -> list[str]:
|
||||
lines: list[str] = []
|
||||
name = definition["name"]
|
||||
required = set(definition.get("required", []))
|
||||
for prop_name, prop in definition.get("properties", {}).items():
|
||||
field = go_field(prop_name)
|
||||
required_zero_is_valid = prop.get("type") == "integer" and prop.get("minimum") == 0
|
||||
if prop_name in required and prop.get("type") != "boolean" and not required_zero_is_valid:
|
||||
zero = go_zero(prop, field)
|
||||
lines.append(f"\tif {zero} {{ violations = append(violations, FieldViolation{{Field: \"{prop_name}\", Code: \"required\"}}) }}")
|
||||
if prop.get("type") == "string":
|
||||
if "minLength" in prop:
|
||||
lines.append(f"\tif len(v.{field}) < {prop['minLength']} && v.{field} != \"\" {{ violations = append(violations, FieldViolation{{Field: \"{prop_name}\", Code: \"min_length\"}}) }}")
|
||||
if "maxLength" in prop:
|
||||
lines.append(f"\tif len(v.{field}) > {prop['maxLength']} {{ violations = append(violations, FieldViolation{{Field: \"{prop_name}\", Code: \"max_length\"}}) }}")
|
||||
if "const" in prop:
|
||||
lines.append(f"\tif v.{field} != \"{prop['const']}\" && v.{field} != \"\" {{ violations = append(violations, FieldViolation{{Field: \"{prop_name}\", Code: \"invalid_value\"}}) }}")
|
||||
if "enum" in prop:
|
||||
allowed = " || ".join(f'v.{field} == "{value}"' for value in prop["enum"])
|
||||
lines.append(f"\tif v.{field} != \"\" && !({allowed}) {{ violations = append(violations, FieldViolation{{Field: \"{prop_name}\", Code: \"invalid_value\"}}) }}")
|
||||
if prop.get("format") == "date-time":
|
||||
lines.append(
|
||||
'\tif v.%s != "" { if parsed, err := time.Parse(time.RFC3339Nano, v.%s); err != nil || parsed.UTC().Format(time.RFC3339Nano) != v.%s { violations = append(violations, FieldViolation{Field: "%s", Code: "invalid_time"}) } }'
|
||||
% (field, field, field, prop_name)
|
||||
)
|
||||
if prop.get("type") == "integer":
|
||||
if "minimum" in prop:
|
||||
lines.append(f"\tif v.{field} != 0 && v.{field} < {prop['minimum']} {{ violations = append(violations, FieldViolation{{Field: \"{prop_name}\", Code: \"minimum\"}}) }}")
|
||||
if "maximum" in prop:
|
||||
lines.append(f"\tif v.{field} > {prop['maximum']} {{ violations = append(violations, FieldViolation{{Field: \"{prop_name}\", Code: \"maximum\"}}) }}")
|
||||
if prop.get("type") == "array":
|
||||
if "minItems" in prop:
|
||||
lines.append(f"\tif len(v.{field}) < {prop['minItems']} {{ violations = append(violations, FieldViolation{{Field: \"{prop_name}\", Code: \"min_items\"}}) }}")
|
||||
if "maxItems" in prop:
|
||||
lines.append(f"\tif len(v.{field}) > {prop['maxItems']} {{ violations = append(violations, FieldViolation{{Field: \"{prop_name}\", Code: \"max_items\"}}) }}")
|
||||
item_ref = ref_name(prop.get("items", {}))
|
||||
if item_ref:
|
||||
lines.append(f"\tfor index := range v.{field} {{ if err := v.{field}[index].Validate(); err != nil {{ violations = append(violations, FieldViolation{{Field: fmt.Sprintf(\"{prop_name}[%d]\", index), Code: \"invalid_item\"}}) }} }}")
|
||||
reference = ref_name(prop)
|
||||
if reference:
|
||||
lines.append(f"\tif err := v.{field}.Validate(); err != nil {{ violations = append(violations, FieldViolation{{Field: \"{prop_name}\", Code: \"invalid_object\"}}) }}")
|
||||
return lines
|
||||
|
||||
|
||||
def generate_go(defs: dict[str, dict[str, Any]], schema_hash: str, version: str, compatibility: dict[str, Any]) -> str:
|
||||
out = [
|
||||
"// Code generated by tools/generate.py; DO NOT EDIT.",
|
||||
"package protocol",
|
||||
"",
|
||||
"import (",
|
||||
"\"bytes\"",
|
||||
"\"encoding/json\"",
|
||||
"\"errors\"",
|
||||
"\"fmt\"",
|
||||
"\"reflect\"",
|
||||
"\"time\"",
|
||||
")",
|
||||
"",
|
||||
f'const SchemaSHA256 = "{schema_hash}"',
|
||||
f'const ProtocolVersion = "{version}"',
|
||||
f'const CurrentWireVersion = "{compatibility["current"]}"',
|
||||
f'const NMinus1WireVersion = "{compatibility["n_minus_1"]}"',
|
||||
f'const NMinus2WireVersion = "{compatibility["n_minus_2"]}"',
|
||||
"",
|
||||
"type FieldViolation struct {",
|
||||
"\tField string `json:\"field\"`",
|
||||
"\tCode string `json:\"code\"`",
|
||||
"}",
|
||||
"",
|
||||
"type ValidationError struct {",
|
||||
"\tViolations []FieldViolation",
|
||||
"}",
|
||||
"",
|
||||
"func (e ValidationError) Error() string { return \"protocol validation failed\" }",
|
||||
"",
|
||||
]
|
||||
for name in sorted(defs):
|
||||
if name == "FieldViolation":
|
||||
continue
|
||||
definition = defs[name]
|
||||
out.append(f"type {name} struct {{")
|
||||
required = set(definition.get("required", []))
|
||||
for prop_name, prop in definition.get("properties", {}).items():
|
||||
tag = prop_name + (",omitempty" if prop_name not in required else "")
|
||||
out.append(f"\t{go_field(prop_name)} {prop_type(prop, 'go')} `json:\"{tag}\"`")
|
||||
out.extend(["}", ""])
|
||||
for name in sorted(defs):
|
||||
out.append(f"func (v {name}) Validate() error {{")
|
||||
out.append("\tvar violations []FieldViolation")
|
||||
out.extend(go_validation(defs[name]))
|
||||
out.append("\tif len(violations) > 0 { return ValidationError{Violations: violations} }")
|
||||
out.append("\treturn nil")
|
||||
out.append("}")
|
||||
out.append("")
|
||||
out.append(f"func Decode{name}(data []byte) ({name}, error) {{")
|
||||
out.append(f"\tvar value {name}")
|
||||
out.append("\tif len(data) > 1024*1024 { return value, errors.New(\"protocol payload exceeds limit\") }")
|
||||
out.append("\tvar fields map[string]json.RawMessage")
|
||||
out.append("\tif err := json.Unmarshal(data, &fields); err != nil { return value, err }")
|
||||
required_fields = sorted(defs[name].get("required", []))
|
||||
for prop_name in required_fields:
|
||||
out.append(
|
||||
'\tif raw, ok := fields["%s"]; !ok || bytes.Equal(bytes.TrimSpace(raw), []byte("null")) { return value, ValidationError{Violations: []FieldViolation{{Field: "%s", Code: "required"}}} }'
|
||||
% (prop_name, prop_name)
|
||||
)
|
||||
for prop_name, prop in defs[name].get("properties", {}).items():
|
||||
if "x-max-bytes" in prop:
|
||||
out.append(
|
||||
'\tif raw, ok := fields["%s"]; ok && len(raw) > %d { return value, ValidationError{Violations: []FieldViolation{{Field: "%s", Code: "max_bytes"}}} }'
|
||||
% (prop_name, prop["x-max-bytes"], prop_name)
|
||||
)
|
||||
out.append("\tdecoder := json.NewDecoder(bytes.NewReader(data))")
|
||||
out.append("\tdecoder.DisallowUnknownFields()")
|
||||
out.append("\tif err := decoder.Decode(&value); err != nil { return value, err }")
|
||||
out.append("\tvar trailing any")
|
||||
out.append("\tif err := decoder.Decode(&trailing); err != io.EOF { if err == nil { return value, errors.New(\"trailing JSON value\") }; return value, err }")
|
||||
out.append("\tif err := value.Validate(); err != nil { return value, err }")
|
||||
out.append("\treturn value, nil")
|
||||
out.append("}")
|
||||
out.append("")
|
||||
out.append(f"func Encode{name}(value {name}) ([]byte, error) {{")
|
||||
out.append("\tif err := value.Validate(); err != nil { return nil, err }")
|
||||
out.append("\treturn json.Marshal(value)")
|
||||
out.append("}")
|
||||
out.append("")
|
||||
# Use io.EOF in generated code without making every generated decoder depend on
|
||||
# error-string comparison; replace the deliberately compact placeholder.
|
||||
text = "\n".join(out).replace('"errors"\n"fmt"', '"errors"\n"fmt"\n\"io"')
|
||||
text = text.replace('!errors.Is(err, errors.New("EOF")) && err != nil', '!errors.Is(err, io.EOF)')
|
||||
return text + "\n"
|
||||
|
||||
|
||||
def rust_type(prop: dict[str, Any]) -> str:
|
||||
reference = ref_name(prop)
|
||||
if reference:
|
||||
return reference
|
||||
if prop.get("type") == "array":
|
||||
return f"Vec<{rust_type(prop.get('items', {}))}>"
|
||||
if prop.get("type") == "integer":
|
||||
return "i64"
|
||||
if prop.get("type") == "boolean":
|
||||
return "bool"
|
||||
if prop.get("type") == "object":
|
||||
return "JsonObject"
|
||||
return "String"
|
||||
|
||||
|
||||
def swift_type(prop: dict[str, Any]) -> str:
|
||||
reference = ref_name(prop)
|
||||
if reference:
|
||||
return reference
|
||||
if prop.get("type") == "array":
|
||||
return f"[{swift_type(prop.get('items', {}))}]"
|
||||
if prop.get("type") == "integer":
|
||||
return "Int64"
|
||||
if prop.get("type") == "boolean":
|
||||
return "Bool"
|
||||
if prop.get("type") == "object":
|
||||
return "JSONObject"
|
||||
return "String"
|
||||
|
||||
|
||||
def generate_rust(defs: dict[str, dict[str, Any]], schema_hash: str, compatibility: dict[str, Any]) -> str:
|
||||
out = [
|
||||
"// Code generated by tools/generate.py; DO NOT EDIT.",
|
||||
"#![allow(non_snake_case)]",
|
||||
"pub const SCHEMA_SHA256: &str = \"" + schema_hash + "\";",
|
||||
f'pub const CURRENT_WIRE_VERSION: &str = "{compatibility["current"]}";',
|
||||
f'pub const N_MINUS_1_WIRE_VERSION: &str = "{compatibility["n_minus_1"]}";',
|
||||
f'pub const N_MINUS_2_WIRE_VERSION: &str = "{compatibility["n_minus_2"]}";',
|
||||
"pub type JsonObject = std::collections::BTreeMap<String, String>;",
|
||||
"",
|
||||
]
|
||||
for name in sorted(defs):
|
||||
definition = defs[name]
|
||||
out.extend(["#[derive(Debug, Clone, PartialEq, Eq)]", f"pub struct {name} {{"])
|
||||
required = set(definition.get("required", []))
|
||||
for prop_name, prop in definition.get("properties", {}).items():
|
||||
field = rust_field(prop_name)
|
||||
typ = rust_type(prop)
|
||||
if prop_name not in required:
|
||||
typ = f"Option<{typ}>"
|
||||
out.append(f" pub {field}: {typ},")
|
||||
out.extend(["}", ""])
|
||||
return "\n".join(out)
|
||||
|
||||
|
||||
def generate_swift(defs: dict[str, dict[str, Any]], schema_hash: str, compatibility: dict[str, Any]) -> str:
|
||||
out = [
|
||||
"// Code generated by tools/generate.py; DO NOT EDIT.",
|
||||
"import Foundation",
|
||||
"public typealias JSONObject = [String: String]",
|
||||
f"public let schemaSHA256 = \"{schema_hash}\"",
|
||||
f'public let currentWireVersion = "{compatibility["current"]}"',
|
||||
f'public let nMinus1WireVersion = "{compatibility["n_minus_1"]}"',
|
||||
f'public let nMinus2WireVersion = "{compatibility["n_minus_2"]}"',
|
||||
"",
|
||||
]
|
||||
for name in sorted(defs):
|
||||
definition = defs[name]
|
||||
required = set(definition.get("required", []))
|
||||
out.extend(["public struct " + name + ": Codable, Equatable {",])
|
||||
for prop_name, prop in definition.get("properties", {}).items():
|
||||
typ = swift_type(prop)
|
||||
if prop_name not in required:
|
||||
typ += "?"
|
||||
out.append(f" public let {swift_field(prop_name)}: {typ}")
|
||||
out.append(" enum CodingKeys: String, CodingKey {")
|
||||
for prop_name in definition.get("properties", {}):
|
||||
out.append(f" case {swift_field(prop_name)} = \"{prop_name}\"")
|
||||
out.extend([" }", "", " public init(from decoder: Decoder) throws {",])
|
||||
out.append(" let c = try decoder.container(keyedBy: CodingKeys.self)")
|
||||
for prop_name, prop in definition.get("properties", {}).items():
|
||||
field = swift_field(prop_name)
|
||||
typ = swift_type(prop)
|
||||
if prop_name in required:
|
||||
out.append(f" {field} = try c.decode({typ}.self, forKey: .{field})")
|
||||
else:
|
||||
out.append(f" {field} = try c.decodeIfPresent({typ}.self, forKey: .{field})")
|
||||
out.extend([" }", "}", ""])
|
||||
return "\n".join(out)
|
||||
|
||||
|
||||
def write_or_check(path: pathlib.Path, content: str, check: bool) -> None:
|
||||
if check:
|
||||
if not path.exists() or path.read_text(encoding="utf-8") != content:
|
||||
raise ValueError(f"generated output differs: {path.relative_to(ROOT)}")
|
||||
return
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
path.write_text(content, encoding="utf-8")
|
||||
|
||||
|
||||
def format_go(content: str) -> str:
|
||||
result = subprocess.run(["gofmt"], input=content, text=True, capture_output=True, check=False)
|
||||
if result.returncode != 0:
|
||||
raise ValueError(f"gofmt failed: {result.stderr.strip()}")
|
||||
return result.stdout
|
||||
|
||||
|
||||
def main() -> int:
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--check", action="store_true")
|
||||
args = parser.parse_args()
|
||||
schema, schema_hash, version, compatibility = load_schema()
|
||||
generator_hash = hashlib.sha256(pathlib.Path(__file__).read_bytes()).hexdigest()
|
||||
defs = schema.get("$defs")
|
||||
if not isinstance(defs, dict) or not defs:
|
||||
raise ValueError("schema must contain non-empty $defs")
|
||||
normalized = {name: dict(value, name=name) for name, value in defs.items()}
|
||||
outputs = {
|
||||
ROOT / "gen/go/protocol/protocol.go": format_go(generate_go(normalized, schema_hash, version, compatibility)),
|
||||
ROOT / "gen/rust/protocol.rs": generate_rust(normalized, schema_hash, compatibility).rstrip() + "\n",
|
||||
ROOT / "gen/swift/Protocol.swift": generate_swift(normalized, schema_hash, compatibility).rstrip() + "\n",
|
||||
ROOT / "gen/manifest.json": json.dumps({
|
||||
"generator_sha256": generator_hash,
|
||||
"schema_sha256": schema_hash,
|
||||
"protocol_version": version,
|
||||
"compatibility": compatibility,
|
||||
}, sort_keys=True, indent=2) + "\n",
|
||||
}
|
||||
for path, content in outputs.items():
|
||||
write_or_check(path, content, args.check)
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
raise SystemExit(main())
|
||||
except (OSError, ValueError, json.JSONDecodeError) as exc:
|
||||
print(f"generate: {exc}", file=sys.stderr)
|
||||
raise SystemExit(1)
|
||||
@@ -0,0 +1,209 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
protocol "github.com/sechmachine/VerseVDI-Protocol/gen/go/protocol"
|
||||
)
|
||||
|
||||
const (
|
||||
datagramHeaderBytes = 21
|
||||
maximumFrameBytes = 65536
|
||||
)
|
||||
|
||||
func main() {
|
||||
entries, err := os.ReadDir("fixtures/conformance")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
var results []string
|
||||
for _, entry := range entries {
|
||||
if entry.IsDir() || filepath.Ext(entry.Name()) != ".tsv" {
|
||||
continue
|
||||
}
|
||||
path := filepath.Join("fixtures/conformance", entry.Name())
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
lines := strings.Split(strings.TrimSuffix(string(data), "\n"), "\n")
|
||||
if len(lines) == 0 || lines[0] != "id\tversion\tkind\tinput\texpected" {
|
||||
panic("invalid fixture header")
|
||||
}
|
||||
for _, line := range lines[1:] {
|
||||
fields := strings.Split(line, "\t")
|
||||
if len(fields) != 5 {
|
||||
panic("invalid fixture row")
|
||||
}
|
||||
actual := evaluate(fields[2], fields[3])
|
||||
if actual != fields[4] {
|
||||
panic(fmt.Sprintf("%s: got %s want %s", fields[0], actual, fields[4]))
|
||||
}
|
||||
results = append(results, fields[0]+"\t"+actual)
|
||||
}
|
||||
}
|
||||
fixtureHash := readFixtureHash()
|
||||
fmt.Printf("Go conformance passed normalized=%s fixtures=%s\n", normalizedDigest(results), fixtureHash)
|
||||
}
|
||||
|
||||
func evaluate(kind, input string) string {
|
||||
parts := map[string]string{}
|
||||
for _, item := range strings.Split(input, ";") {
|
||||
pair := strings.SplitN(item, "=", 2)
|
||||
if len(pair) == 2 {
|
||||
parts[pair[0]] = pair[1]
|
||||
}
|
||||
}
|
||||
switch kind {
|
||||
case "version":
|
||||
if input == "1" || input == "0" || input == "-1" {
|
||||
return "valid"
|
||||
}
|
||||
return "invalid:unsupported_version"
|
||||
case "page":
|
||||
limit, err := strconv.Atoi(parts["limit"])
|
||||
value := protocol.PageInfo{Limit: int64(limit), NextCursor: parts["cursor"]}
|
||||
if err == nil && value.Validate() == nil {
|
||||
return "valid"
|
||||
}
|
||||
return "invalid:invalid_limit"
|
||||
case "manifest":
|
||||
forbidden := []string{"provider_url", "vm_address", "password", "private_key"}
|
||||
for _, key := range forbidden {
|
||||
if _, ok := parts[key]; ok {
|
||||
return "invalid:forbidden_field"
|
||||
}
|
||||
}
|
||||
value := protocol.ConnectionManifest{
|
||||
Version: parts["version"], Purpose: parts["purpose"], SessionID: "session-1",
|
||||
ReconnectSequence: 0,
|
||||
Gateway: protocol.ManifestGateway{
|
||||
ID: parts["gateway_id"], Addresses: []string{"gateway.control.test:443"}, PublicIdentity: parts["gateway_id"],
|
||||
},
|
||||
Tunnel: protocol.ManifestTunnel{Versions: []string{parts["protocol"] + "/1"}, Features: []string{"control.v1"}},
|
||||
Profile: protocol.ManifestProfile{ID: "standard", Bounds: protocol.ManifestBounds{MinimumKbps: 1, TargetKbps: 2, MaximumKbps: 3}},
|
||||
Grant: protocol.GrantReference{OpaqueValue: parts["grant"], ExpiresAt: parts["expires_at"], Audience: parts["audience"]},
|
||||
CorrelationID: "correlation-1",
|
||||
}
|
||||
if value.Validate() == nil {
|
||||
return "valid"
|
||||
}
|
||||
return "invalid:invalid_manifest"
|
||||
case "clipboard":
|
||||
_, hasFile := parts["file"]
|
||||
value := protocol.ClipboardText{Text: parts["text"], Encoding: parts["encoding"]}
|
||||
if !hasFile && value.Validate() == nil {
|
||||
return "valid"
|
||||
}
|
||||
return "invalid:unsupported_clipboard"
|
||||
case "event":
|
||||
sequence, sequenceErr := strconv.ParseInt(parts["sequence"], 10, 64)
|
||||
payloadBytes, payloadErr := strconv.Atoi(parts["payload_bytes"])
|
||||
if parts["version"] != "1" {
|
||||
return "invalid:unsupported_version"
|
||||
}
|
||||
if parts["after"] != "" && parts["earliest"] != "" {
|
||||
after, afterErr := strconv.ParseInt(parts["after"], 10, 64)
|
||||
earliest, earliestErr := strconv.ParseInt(parts["earliest"], 10, 64)
|
||||
if afterErr == nil && earliestErr == nil && after > 0 && earliest > 0 && after < earliest-1 {
|
||||
return "invalid:gap"
|
||||
}
|
||||
}
|
||||
value := protocol.EventEnvelope{
|
||||
EventID: "event-1", Sequence: sequence, Type: "broker.session.changed", Version: 1,
|
||||
Resource: protocol.ResourceLink{Type: "broker_session", ID: "session-1", Version: 1},
|
||||
OccurredAt: "2099-01-01T00:00:00Z", CorrelationID: parts["correlation_id"], Payload: map[string]any{},
|
||||
}
|
||||
if payloadErr != nil || payloadBytes > 16384 {
|
||||
return "invalid:payload_limit"
|
||||
}
|
||||
if sequenceErr != nil || value.Validate() != nil {
|
||||
return "invalid:required"
|
||||
}
|
||||
return "valid"
|
||||
case "tunnel":
|
||||
if (parts["offered"] == "1" || parts["offered"] == "0" || parts["offered"] == "-1") && parts["feature"] == "control.v1" {
|
||||
return "valid"
|
||||
}
|
||||
if parts["feature"] != "control.v1" {
|
||||
return "invalid:unsupported_feature"
|
||||
}
|
||||
return "invalid:unsupported_version"
|
||||
case "datagram":
|
||||
return classifyDatagram(parts["hex"])
|
||||
default:
|
||||
return "invalid:unknown_kind"
|
||||
}
|
||||
}
|
||||
|
||||
func classifyDatagram(encoded string) string {
|
||||
raw, err := hex.DecodeString(encoded)
|
||||
if err != nil {
|
||||
return "invalid:hex"
|
||||
}
|
||||
if len(raw) < datagramHeaderBytes {
|
||||
return "invalid:truncated"
|
||||
}
|
||||
if string(raw[:2]) != "VD" {
|
||||
return "invalid:magic"
|
||||
}
|
||||
if raw[2] != 1 {
|
||||
return "invalid:unsupported_version"
|
||||
}
|
||||
limits := map[byte]int{1: 1024, 2: 2048, 3: 65515}
|
||||
limit, ok := limits[raw[3]]
|
||||
if !ok {
|
||||
return "invalid:unknown_channel"
|
||||
}
|
||||
if raw[4] != 0 {
|
||||
return "invalid:flags"
|
||||
}
|
||||
if raw[18] == 0 || raw[17] >= raw[18] {
|
||||
return "invalid:fragment"
|
||||
}
|
||||
payloadLength := int(raw[19])<<8 | int(raw[20])
|
||||
if payloadLength > limit {
|
||||
return "invalid:payload_limit"
|
||||
}
|
||||
if len(raw) != datagramHeaderBytes+payloadLength {
|
||||
return "invalid:length_mismatch"
|
||||
}
|
||||
if len(raw) > maximumFrameBytes {
|
||||
return "invalid:frame_limit"
|
||||
}
|
||||
return "valid"
|
||||
}
|
||||
|
||||
func normalizedDigest(results []string) string {
|
||||
const offset = uint64(14695981039346656037)
|
||||
const prime = uint64(1099511628211)
|
||||
value := offset
|
||||
for _, result := range results {
|
||||
for _, byteValue := range []byte(result + "\n") {
|
||||
value ^= uint64(byteValue)
|
||||
value *= prime
|
||||
}
|
||||
}
|
||||
return fmt.Sprintf("%016x", value)
|
||||
}
|
||||
|
||||
func readFixtureHash() string {
|
||||
data, err := os.ReadFile("fixtures/manifest.json")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
var manifest struct {
|
||||
CorpusSHA256 string `json:"corpus_sha256"`
|
||||
}
|
||||
if err := json.Unmarshal(data, &manifest); err != nil || len(manifest.CorpusSHA256) != sha256.Size*2 {
|
||||
panic("invalid fixture manifest")
|
||||
}
|
||||
return manifest.CorpusSHA256
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
|
||||
fn values(input: &str) -> std::collections::BTreeMap<String, String> {
|
||||
input
|
||||
.split(';')
|
||||
.filter_map(|item| item.split_once('='))
|
||||
.map(|(key, value)| (key.to_owned(), value.to_owned()))
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn evaluate(kind: &str, input: &str) -> &'static str {
|
||||
let values = values(input);
|
||||
match kind {
|
||||
"version" if matches!(input, "1" | "0" | "-1") => "valid",
|
||||
"version" => "invalid:unsupported_version",
|
||||
"page" => match values.get("limit").and_then(|value| value.parse::<i64>().ok()) {
|
||||
Some(limit) if (1..=100).contains(&limit) => "valid",
|
||||
_ => "invalid:invalid_limit",
|
||||
},
|
||||
"manifest" if ["provider_url", "vm_address", "password", "private_key"]
|
||||
.iter()
|
||||
.any(|key| values.contains_key(*key)) => "invalid:forbidden_field",
|
||||
"manifest"
|
||||
if values.get("version").map(String::as_str) == Some("1")
|
||||
&& values.contains_key("gateway_id")
|
||||
&& values.get("grant").map_or(false, |value| value.len() >= 43)
|
||||
&& values.get("purpose").map(String::as_str) == Some("launch") => "valid",
|
||||
"manifest" => "invalid:invalid_manifest",
|
||||
"clipboard" if values.get("encoding").map(String::as_str) == Some("utf-8")
|
||||
&& !values.contains_key("file") => "valid",
|
||||
"clipboard" => "invalid:unsupported_clipboard",
|
||||
"event" if values.get("version").map(String::as_str) != Some("1") => {
|
||||
"invalid:unsupported_version"
|
||||
}
|
||||
"event" if values.get("after").and_then(|value| value.parse::<i64>().ok()).is_some()
|
||||
&& values.get("earliest").and_then(|value| value.parse::<i64>().ok()).is_some()
|
||||
&& values["after"].parse::<i64>().unwrap() > 0
|
||||
&& values["earliest"].parse::<i64>().unwrap() > 0
|
||||
&& values["after"].parse::<i64>().unwrap() < values["earliest"].parse::<i64>().unwrap() - 1 => {
|
||||
"invalid:gap"
|
||||
}
|
||||
"event" if values.get("payload_bytes").and_then(|value| value.parse::<usize>().ok()).map_or(true, |size| size > 16384) => {
|
||||
"invalid:payload_limit"
|
||||
}
|
||||
"event" if values.get("sequence").and_then(|value| value.parse::<i64>().ok()).map_or(true, |sequence| sequence < 1)
|
||||
|| !values.contains_key("correlation_id") => "invalid:required",
|
||||
"event" => "valid",
|
||||
"tunnel" if matches!(values.get("offered").map(String::as_str), Some("1") | Some("0") | Some("-1"))
|
||||
&& values.get("feature").map(String::as_str) == Some("control.v1") => "valid",
|
||||
"tunnel" if values.get("feature").map(String::as_str) != Some("control.v1") => {
|
||||
"invalid:unsupported_feature"
|
||||
}
|
||||
"tunnel" => "invalid:unsupported_version",
|
||||
"datagram" => classify_datagram(values.get("hex").map(String::as_str).unwrap_or_default()),
|
||||
_ => "invalid:unknown_kind",
|
||||
}
|
||||
}
|
||||
|
||||
fn decode_hex(input: &str) -> Option<Vec<u8>> {
|
||||
if input.len() % 2 != 0 {
|
||||
return None;
|
||||
}
|
||||
(0..input.len())
|
||||
.step_by(2)
|
||||
.map(|index| u8::from_str_radix(&input[index..index + 2], 16).ok())
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn classify_datagram(encoded: &str) -> &'static str {
|
||||
let raw = match decode_hex(encoded) {
|
||||
Some(raw) => raw,
|
||||
None => return "invalid:hex",
|
||||
};
|
||||
if raw.len() < 21 {
|
||||
return "invalid:truncated";
|
||||
}
|
||||
if raw[0..2] != *b"VD" {
|
||||
return "invalid:magic";
|
||||
}
|
||||
if raw[2] != 1 {
|
||||
return "invalid:unsupported_version";
|
||||
}
|
||||
let limit = match raw[3] {
|
||||
1 => 1024,
|
||||
2 => 2048,
|
||||
3 => 65515,
|
||||
_ => return "invalid:unknown_channel",
|
||||
};
|
||||
if raw[4] != 0 {
|
||||
return "invalid:flags";
|
||||
}
|
||||
if raw[18] == 0 || raw[17] >= raw[18] {
|
||||
return "invalid:fragment";
|
||||
}
|
||||
let payload_length = ((raw[19] as usize) << 8) | raw[20] as usize;
|
||||
if payload_length > limit {
|
||||
return "invalid:payload_limit";
|
||||
}
|
||||
if raw.len() != 21 + payload_length {
|
||||
return "invalid:length_mismatch";
|
||||
}
|
||||
if raw.len() > 65536 {
|
||||
return "invalid:frame_limit";
|
||||
}
|
||||
"valid"
|
||||
}
|
||||
|
||||
fn normalized_digest(results: &[String]) -> String {
|
||||
let mut value: u64 = 14695981039346656037;
|
||||
for result in results {
|
||||
for byte in format!("{result}\n").bytes() {
|
||||
value ^= u64::from(byte);
|
||||
value = value.wrapping_mul(1099511628211);
|
||||
}
|
||||
}
|
||||
format!("{value:016x}")
|
||||
}
|
||||
|
||||
fn fixture_hash() -> String {
|
||||
let text = fs::read_to_string("fixtures/manifest.json").expect("fixture manifest");
|
||||
text.split("\"corpus_sha256\": \"")
|
||||
.nth(1)
|
||||
.and_then(|value| value.split('"').next())
|
||||
.expect("fixture hash")
|
||||
.to_owned()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut paths: Vec<PathBuf> = fs::read_dir("fixtures/conformance")
|
||||
.expect("fixture corpus")
|
||||
.map(|entry| entry.expect("fixture entry").path())
|
||||
.filter(|path| path.extension().and_then(|value| value.to_str()) == Some("tsv"))
|
||||
.collect();
|
||||
paths.sort();
|
||||
let mut results = Vec::new();
|
||||
for path in paths {
|
||||
let text = fs::read_to_string(path).expect("fixture file");
|
||||
let mut lines = text.lines();
|
||||
assert_eq!(lines.next(), Some("id\tversion\tkind\tinput\texpected"));
|
||||
for line in lines {
|
||||
let fields: Vec<&str> = line.split('\t').collect();
|
||||
assert_eq!(fields.len(), 5);
|
||||
let actual = evaluate(fields[2], fields[3]);
|
||||
assert_eq!(actual, fields[4], "{}", fields[0]);
|
||||
results.push(format!("{}\t{}", fields[0], actual));
|
||||
}
|
||||
}
|
||||
println!(
|
||||
"Rust conformance passed normalized={} fixtures={}",
|
||||
normalized_digest(&results),
|
||||
fixture_hash()
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
import Foundation
|
||||
|
||||
func values(_ input: String) -> [String: String] {
|
||||
var result: [String: String] = [:]
|
||||
for item in input.split(separator: ";") {
|
||||
let pair = item.split(separator: "=", maxSplits: 1).map(String.init)
|
||||
if pair.count == 2 { result[pair[0]] = pair[1] }
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func evaluate(_ kind: String, _ input: String) -> String {
|
||||
let values = values(input)
|
||||
switch kind {
|
||||
case "version": return ["1", "0", "-1"].contains(input) ? "valid" : "invalid:unsupported_version"
|
||||
case "page":
|
||||
guard let raw = values["limit"], let limit = Int(raw), (1...100).contains(limit) else { return "invalid:invalid_limit" }
|
||||
return "valid"
|
||||
case "manifest":
|
||||
for key in ["provider_url", "vm_address", "password", "private_key"] where values[key] != nil { return "invalid:forbidden_field" }
|
||||
return values["version"] == "1" && values["gateway_id"] != nil && (values["grant"]?.utf8.count ?? 0) >= 43 && values["purpose"] == "launch" ? "valid" : "invalid:invalid_manifest"
|
||||
case "clipboard": return values["encoding"] == "utf-8" && values["file"] == nil ? "valid" : "invalid:unsupported_clipboard"
|
||||
case "event":
|
||||
guard values["version"] == "1" else { return "invalid:unsupported_version" }
|
||||
if let after = Int(values["after"] ?? ""), let earliest = Int(values["earliest"] ?? ""), after > 0, earliest > 0, after < earliest - 1 { return "invalid:gap" }
|
||||
if (Int(values["payload_bytes"] ?? "") ?? Int.max) > 16384 { return "invalid:payload_limit" }
|
||||
guard let sequence = Int(values["sequence"] ?? ""), sequence > 0, values["correlation_id"] != nil else { return "invalid:required" }
|
||||
return "valid"
|
||||
case "tunnel":
|
||||
if ["1", "0", "-1"].contains(values["offered"] ?? "") && values["feature"] == "control.v1" { return "valid" }
|
||||
return values["feature"] == "control.v1" ? "invalid:unsupported_version" : "invalid:unsupported_feature"
|
||||
case "datagram": return classifyDatagram(values["hex"] ?? "")
|
||||
default: return "invalid:unknown_kind"
|
||||
}
|
||||
}
|
||||
|
||||
func classifyDatagram(_ encoded: String) -> String {
|
||||
let characters = Array(encoded)
|
||||
guard characters.count % 2 == 0 else { return "invalid:hex" }
|
||||
var raw: [UInt8] = []
|
||||
for index in stride(from: 0, to: characters.count, by: 2) {
|
||||
guard let byte = UInt8(String(characters[index...index + 1]), radix: 16) else { return "invalid:hex" }
|
||||
raw.append(byte)
|
||||
}
|
||||
guard raw.count >= 21 else { return "invalid:truncated" }
|
||||
guard raw[0] == 0x56 && raw[1] == 0x44 else { return "invalid:magic" }
|
||||
guard raw[2] == 1 else { return "invalid:unsupported_version" }
|
||||
let limit: Int
|
||||
switch raw[3] {
|
||||
case 1: limit = 1024
|
||||
case 2: limit = 2048
|
||||
case 3: limit = 65515
|
||||
default: return "invalid:unknown_channel"
|
||||
}
|
||||
guard raw[4] == 0 else { return "invalid:flags" }
|
||||
guard raw[18] > 0 && raw[17] < raw[18] else { return "invalid:fragment" }
|
||||
let payloadLength = Int(raw[19]) * 256 + Int(raw[20])
|
||||
guard payloadLength <= limit else { return "invalid:payload_limit" }
|
||||
guard raw.count == 21 + payloadLength else { return "invalid:length_mismatch" }
|
||||
guard raw.count <= 65536 else { return "invalid:frame_limit" }
|
||||
return "valid"
|
||||
}
|
||||
|
||||
func normalizedDigest(_ results: [String]) -> String {
|
||||
var value: UInt64 = 14695981039346656037
|
||||
for result in results {
|
||||
for byte in Array("\(result)\n".utf8) {
|
||||
value ^= UInt64(byte)
|
||||
value = value &* 1099511628211
|
||||
}
|
||||
}
|
||||
return String(format: "%016llx", value)
|
||||
}
|
||||
|
||||
func fixtureHash() -> String {
|
||||
let text = try! String(contentsOfFile: "fixtures/manifest.json", encoding: .utf8)
|
||||
let marker = "\"corpus_sha256\": \""
|
||||
guard let start = text.range(of: marker)?.upperBound else { fatalError("fixture hash") }
|
||||
let suffix = text[start...]
|
||||
guard let end = suffix.firstIndex(of: "\"") else { fatalError("fixture hash") }
|
||||
return String(suffix[..<end])
|
||||
}
|
||||
|
||||
@main
|
||||
struct ConformanceMain {
|
||||
static func main() {
|
||||
let names = try! FileManager.default.contentsOfDirectory(atPath: "fixtures/conformance").filter { $0.hasSuffix(".tsv") }.sorted()
|
||||
var results: [String] = []
|
||||
for name in names {
|
||||
let text = try! String(contentsOfFile: "fixtures/conformance/\(name)", encoding: .utf8)
|
||||
var lines = text.split(whereSeparator: \.isNewline).map(String.init)
|
||||
precondition(lines.removeFirst() == "id\tversion\tkind\tinput\texpected")
|
||||
for line in lines {
|
||||
let fields = line.split(separator: "\t", omittingEmptySubsequences: false).map(String.init)
|
||||
precondition(fields.count == 5)
|
||||
let actual = evaluate(fields[2], fields[3])
|
||||
precondition(actual == fields[4], fields[0])
|
||||
results.append("\(fields[0])\t\(actual)")
|
||||
}
|
||||
}
|
||||
print("Swift conformance passed normalized=\(normalizedDigest(results)) fixtures=\(fixtureHash())")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
#!/usr/bin/env python3
|
||||
from __future__ import annotations
|
||||
|
||||
import pathlib
|
||||
import subprocess
|
||||
import tempfile
|
||||
|
||||
ROOT = pathlib.Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
def run(command: list[str]) -> None:
|
||||
result = subprocess.run(command, cwd=ROOT, text=True, capture_output=True)
|
||||
if result.returncode != 0:
|
||||
raise SystemExit(result.stdout + result.stderr)
|
||||
print(result.stdout.strip())
|
||||
|
||||
|
||||
def main() -> int:
|
||||
with tempfile.TemporaryDirectory(prefix="versevdi-protocol-conformance-") as directory:
|
||||
temp = pathlib.Path(directory)
|
||||
rust_bin = temp / "rust-conformance"
|
||||
swift_bin = temp / "swift-conformance"
|
||||
run(["rustc", "tools/native_conformance.rs", "-O", "-o", str(rust_bin)])
|
||||
run([str(rust_bin)])
|
||||
main_source = temp / "main.swift"
|
||||
main_source.write_text((ROOT / "tools/native_conformance.swift").read_text(encoding="utf-8"), encoding="utf-8")
|
||||
run(["swiftc", "-parse-as-library", "gen/swift/Protocol.swift", str(main_source), "-o", str(swift_bin)])
|
||||
run([str(swift_bin)])
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
@@ -0,0 +1,80 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Dependency-free structural and scope validation for the Protocol sources."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import pathlib
|
||||
import hashlib
|
||||
import re
|
||||
import sys
|
||||
|
||||
ROOT = pathlib.Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
def main() -> int:
|
||||
schema_path = ROOT / "schemas/control-v1.schema.json"
|
||||
schema = json.loads(schema_path.read_text(encoding="utf-8"))
|
||||
assert schema["$schema"].endswith("2020-12/schema")
|
||||
defs = schema["$defs"]
|
||||
assert len(defs) >= 16
|
||||
for name, definition in defs.items():
|
||||
assert definition["type"] == "object", name
|
||||
assert definition["additionalProperties"] is False, name
|
||||
assert set(definition["required"]).issubset(definition["properties"]), name
|
||||
|
||||
compatibility = json.loads((ROOT / "compatibility.json").read_text(encoding="utf-8"))
|
||||
assert set([compatibility["current"], compatibility["n_minus_1"], compatibility["n_minus_2"]]) == {"1", "0", "-1"}
|
||||
assert len(set(compatibility["unsupported"])) == len(compatibility["unsupported"])
|
||||
|
||||
for registry in ("registries/features.json", "registries/datagrams.json"):
|
||||
value = json.loads((ROOT / registry).read_text(encoding="utf-8"))
|
||||
entries = value.get("features", value.get("datagrams"))
|
||||
assert entries and len({entry["id"] for entry in entries}) == len(entries)
|
||||
for entry in entries:
|
||||
maximum = entry.get("max_frame_bytes", entry.get("max_payload_bytes"))
|
||||
assert isinstance(maximum, int) and 1 <= maximum <= 65536
|
||||
|
||||
manifest = json.loads((ROOT / "fixtures/valid/manifest.json").read_text(encoding="utf-8"))
|
||||
assert set(manifest).issubset(set(defs["ConnectionManifest"]["properties"]))
|
||||
forbidden = json.loads((ROOT / "fixtures/invalid/manifest-provider-field.json").read_text(encoding="utf-8"))
|
||||
assert "provider_url" not in defs["ConnectionManifest"]["properties"] and "provider_url" in forbidden
|
||||
|
||||
expected_header = "id\tversion\tkind\tinput\texpected"
|
||||
ids = set()
|
||||
for fixture_path in sorted((ROOT / "fixtures/conformance").glob("*.tsv")):
|
||||
lines = fixture_path.read_text(encoding="utf-8").splitlines()
|
||||
assert lines and lines[0] == expected_header, fixture_path
|
||||
for line in lines[1:]:
|
||||
fields = line.split("\t")
|
||||
assert len(fields) == 5, line
|
||||
assert fields[0] not in ids, fields[0]
|
||||
ids.add(fields[0])
|
||||
assert fields[4] == "valid" or fields[4].startswith("invalid:"), line
|
||||
|
||||
fixture_manifest = json.loads((ROOT / "fixtures/manifest.json").read_text(encoding="utf-8"))
|
||||
assert fixture_manifest["files"] == sorted(
|
||||
path.relative_to(ROOT).as_posix() for path in (ROOT / "fixtures/conformance").glob("*.tsv")
|
||||
)
|
||||
fixture_hash = hashlib.sha256()
|
||||
for relative in fixture_manifest["files"]:
|
||||
fixture_hash.update(relative.encode("utf-8"))
|
||||
fixture_hash.update(b"\0")
|
||||
fixture_hash.update((ROOT / relative).read_bytes())
|
||||
fixture_hash.update(b"\0")
|
||||
assert fixture_manifest["corpus_sha256"] == fixture_hash.hexdigest()
|
||||
|
||||
openapi = (ROOT / "openapi/control-v1.yaml").read_text(encoding="utf-8")
|
||||
assert "openapi: 3.1.0" in openapi
|
||||
assert "/api/v1/auth/refresh:" in openapi and "/api/v1/resources:" in openapi and "/api/v1/events:" in openapi
|
||||
assert "provider_url" not in openapi and "vm_address" not in openapi
|
||||
print("Protocol source validation passed")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
raise SystemExit(main())
|
||||
except (AssertionError, OSError, json.JSONDecodeError) as exc:
|
||||
print(f"validate: {exc}", file=sys.stderr)
|
||||
raise SystemExit(1)
|
||||
@@ -0,0 +1,55 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Validate the bounded Phase 3A datagram header and fixture corpus."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import binascii
|
||||
import pathlib
|
||||
|
||||
ROOT = pathlib.Path(__file__).resolve().parents[1]
|
||||
HEADER_BYTES = 21
|
||||
MAX_FRAME_BYTES = 65536
|
||||
CHANNEL_LIMITS = {1: 1024, 2: 2048, 3: 65515}
|
||||
|
||||
|
||||
def classify(raw: bytes) -> str:
|
||||
if len(raw) < HEADER_BYTES:
|
||||
return "invalid:truncated"
|
||||
if raw[:2] != b"VD":
|
||||
return "invalid:magic"
|
||||
if raw[2] != 1:
|
||||
return "invalid:unsupported_version"
|
||||
if raw[3] not in CHANNEL_LIMITS:
|
||||
return "invalid:unknown_channel"
|
||||
if raw[4] != 0:
|
||||
return "invalid:flags"
|
||||
fragment_index, fragment_count = raw[17], raw[18]
|
||||
if fragment_count == 0 or fragment_index >= fragment_count:
|
||||
return "invalid:fragment"
|
||||
payload_length = int.from_bytes(raw[19:21], "big")
|
||||
if payload_length > CHANNEL_LIMITS[raw[3]]:
|
||||
return "invalid:payload_limit"
|
||||
if len(raw) != HEADER_BYTES + payload_length:
|
||||
return "invalid:length_mismatch"
|
||||
if len(raw) > MAX_FRAME_BYTES:
|
||||
return "invalid:frame_limit"
|
||||
return "valid"
|
||||
|
||||
|
||||
def main() -> None:
|
||||
lines = (ROOT / "fixtures/conformance/datagram-v1.tsv").read_text(encoding="utf-8").splitlines()
|
||||
assert lines[0] == "id\tversion\tkind\tinput\texpected"
|
||||
for line in lines[1:]:
|
||||
identifier, version, kind, input_value, expected = line.split("\t")
|
||||
assert kind == "datagram" and version == "1"
|
||||
encoded = input_value.removeprefix("hex=")
|
||||
try:
|
||||
actual = classify(binascii.unhexlify(encoded))
|
||||
except binascii.Error:
|
||||
actual = "invalid:hex"
|
||||
assert actual == expected, f"{identifier}: {actual} != {expected}"
|
||||
print("Datagram frame validation passed")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user