1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
//! Lexicon document validation. Reads the checked-in lexicons only; no network.
use std::fs;
use std::path::{Path, PathBuf};
use serde_json::{Value, json};
const RELEASE_LEXICONS: [&str; 5] = [
"software.release.project",
"software.release.channel",
"software.release.version",
"software.release.manifest",
"software.release.status",
];
/// Metadata lexicons live under `lexicons/com/nahdig/`.
const METADATA_LEXICONS: [&str; 2] = [
"com.nahdig.release.projectMeta",
"com.nahdig.release.gitMeta",
];
fn lexicons_dir() -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR")).join("lexicons")
}
fn collect_json(dir: &Path, out: &mut Vec<PathBuf>) {
for entry in fs::read_dir(dir).expect("read lexicons directory") {
let path = entry.expect("read lexicons entry").path();
if path.is_dir() {
collect_json(&path, out);
} else if path.extension().is_some_and(|ext| ext == "json") {
out.push(path);
}
}
}
fn load(path: &Path) -> Value {
let text = fs::read_to_string(path).unwrap_or_else(|e| panic!("read {}: {e}", path.display()));
serde_json::from_str(&text).unwrap_or_else(|e| panic!("parse {}: {e}", path.display()))
}
fn lexicon(id: &str) -> Value {
let mut files = Vec::new();
collect_json(&lexicons_dir(), &mut files);
for path in files {
let doc = load(&path);
if doc["id"] == json!(id) {
return doc;
}
}
panic!("lexicon {id} not found");
}
fn description(value: &Value) -> &str {
value["description"].as_str().expect("description string")
}
#[test]
fn every_lexicon_parses_with_lexicon_one_and_matching_id() {
let mut files = Vec::new();
collect_json(&lexicons_dir(), &mut files);
assert!(!files.is_empty(), "no lexicons found");
for path in &files {
let doc = load(path);
assert_eq!(
doc["lexicon"],
json!(1),
"{} must be lexicon 1",
path.display()
);
let id = doc["id"]
.as_str()
.unwrap_or_else(|| panic!("{} has no string .id", path.display()));
assert!(!id.is_empty(), "{} has an empty .id", path.display());
let stem = path
.file_stem()
.and_then(|s| s.to_str())
.unwrap_or_else(|| panic!("{} has a non-UTF-8 stem", path.display()));
assert!(
stem == id || id.ends_with(&format!(".{stem}")),
"{} id {id} does not match file stem {stem}",
path.display()
);
assert!(
doc["defs"].is_object(),
"{} has no defs object",
path.display()
);
}
}
#[test]
fn release_lexicons_are_vendored() {
for id in RELEASE_LEXICONS {
let path = lexicons_dir().join(format!("{id}.json"));
assert!(path.is_file(), "missing release lexicon {}", path.display());
let doc = lexicon(id);
assert_eq!(doc["id"], json!(id));
assert_eq!(doc["defs"]["main"]["type"], json!("record"));
}
}
#[test]
fn release_defs_lexicon_is_vendored() {
let doc = lexicon("software.release.defs");
for name in [
"target",
"blobArtifact",
"gitRepository",
"gitRevision",
"gitCommit",
] {
assert!(
doc["defs"][name].is_object(),
"software.release.defs missing {name}"
);
}
}
#[test]
fn atproto_v1_software_ref_has_optional_release_refs() {
let doc = lexicon("com.nahdig.cve.atprotoV1");
let software_ref = &doc["defs"]["softwareRef"];
assert_eq!(
software_ref["required"],
json!(["project", "part", "vendor", "product"])
);
let properties = &software_ref["properties"];
let strong_ref = json!({
"type": "ref",
"ref": "com.atproto.repo.strongRef#main"
});
for name in ["versions", "channels", "manifests"] {
let property = &properties[name];
assert_eq!(property["type"], json!("array"), "{name} must be an array");
assert_eq!(property["items"], strong_ref, "{name} must hold strongRefs");
}
for name in ["versionLabels", "affectedRanges"] {
let property = &properties[name];
assert_eq!(property["type"], json!("array"), "{name} must be an array");
assert_eq!(
property["items"]["type"],
json!("string"),
"{name} must hold strings"
);
}
assert_eq!(
doc["defs"]["main"]["record"]["properties"]["software"]["items"],
json!({ "type": "ref", "ref": "#softwareRef" })
);
}
#[test]
fn release_rkey_and_same_did_constraints_are_encoded() {
let project = lexicon("software.release.project");
let project_main = &project["defs"]["main"];
assert_eq!(project_main["key"], json!("any"));
let project_name = &project_main["record"]["properties"]["name"];
assert_eq!(project_name["minLength"], json!(1));
assert_eq!(project_name["maxLength"], json!(64));
assert!(description(project_name).contains("MUST equal the record key"));
assert!(description(project_main).contains("no dots or colons"));
let channel = lexicon("software.release.channel");
let channel_main = &channel["defs"]["main"];
assert_eq!(channel_main["key"], json!("any"));
let channel_record = &channel_main["record"];
let channel_name = &channel_record["properties"]["name"];
assert_eq!(channel_name["minLength"], json!(1));
assert_eq!(channel_name["maxLength"], json!(64));
assert!(description(channel_name).contains("MUST match the record rkey after the colon"));
assert!(description(channel_main).contains("{projectName}:{channelName}"));
assert!(description(channel_main).contains("exactly one colon"));
assert!(description(channel_main).contains("no dots"));
assert!(description(&channel_record["properties"]["project"]).contains("same-DID"));
assert!(description(&channel_record["properties"]["version"]).contains("same project"));
assert!(
channel_record["required"]
.as_array()
.expect("channel required array")
.iter()
.any(|v| v == "name")
);
let version = lexicon("software.release.version");
let version_main = &version["defs"]["main"];
assert_eq!(version_main["key"], json!("any"));
let version_record = &version_main["record"];
let version_field = &version_record["properties"]["version"];
assert_eq!(version_field["maxLength"], json!(64));
assert!(description(version_field).contains("MUST match the record rkey after the colon"));
assert!(description(version_main).contains("{projectName}:{version}"));
assert!(description(version_main).contains("exactly one colon"));
assert!(description(version_main).contains("allow dots"));
assert!(description(&version_record["properties"]["project"]).contains("same-DID"));
assert!(description(&version_record["properties"]["manifest"]).contains("same project"));
let manifest = lexicon("software.release.manifest");
let manifest_main = &manifest["defs"]["main"];
assert_eq!(manifest_main["key"], json!("tid"));
assert!(description(&manifest_main["record"]["properties"]["project"]).contains("same-DID"));
assert!(description(manifest_main).contains("matches the referenced project's DID"));
let status = lexicon("software.release.status");
let status_main = &status["defs"]["main"];
assert_eq!(status_main["key"], json!("tid"));
assert_eq!(
status_main["record"]["required"],
json!(["subject", "status"])
);
assert!(description(&status_main["record"]["properties"]["subject"]).contains("same-DID"));
let known = status_main["record"]["properties"]["status"]["knownValues"]
.as_array()
.expect("status knownValues array");
for value in ["yanked", "active", "deprecated", "archived", "retracted"] {
assert!(known.iter().any(|v| v == value), "missing status {value}");
}
assert!(description(status_main).contains("append-only"));
assert!(description(status_main).contains("same-DID"));
}
#[test]
fn metadata_lexicons_are_vendored() {
for id in METADATA_LEXICONS {
let doc = lexicon(id);
assert_eq!(doc["id"], json!(id));
assert_eq!(doc["lexicon"], json!(1));
}
let project_meta = lexicon("com.nahdig.release.projectMeta");
let main = &project_meta["defs"]["main"];
assert_eq!(main["type"], json!("record"));
assert_eq!(main["key"], json!("any"));
let record = &main["record"];
assert_eq!(record["required"], json!(["project", "name"]));
assert_eq!(record["properties"]["project"]["format"], json!("at-uri"));
assert!(description(&record["properties"]["project"]).contains("same-DID"));
assert_eq!(record["properties"]["name"]["maxLength"], json!(64));
assert!(description(&record["properties"]["name"]).contains("MUST equal the record key"));
for name in [
"description",
"homepage",
"license",
"repository",
"versioningScheme",
] {
assert!(
record["properties"][name].is_object(),
"projectMeta missing {name}"
);
}
let git_meta = lexicon("com.nahdig.release.gitMeta");
let main = &git_meta["defs"]["main"];
assert_eq!(main["type"], json!("object"));
assert_eq!(main["required"], json!(["repository"]));
for name in ["repository", "revision", "commit", "tag"] {
assert!(
main["properties"][name].is_object(),
"gitMeta missing {name}"
);
}
assert_eq!(main["properties"]["repository"]["format"], json!("uri"));
}