-
Notifications
You must be signed in to change notification settings - Fork 20
/
main.rs
411 lines (383 loc) · 14.8 KB
/
main.rs
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#![feature(box_patterns)]
use std::collections::HashMap;
use std::fmt::Write;
use std::process::Command;
use anyhow::{bail, ensure, Context, Result};
use wasefire_cli_tools::{cmd, fs};
use wasefire_error::Error;
use wasefire_protocol::{Api, Descriptor, Request, Response, DESCRIPTORS, VERSION};
use wasefire_wire::schema::{View, ViewEnum, ViewFields};
use wasefire_wire::{Wire, Yoke};
fn main() -> Result<()> {
let new = Schema::new();
new.write().context("writing bin")?;
new.print().context("printing txt")?;
let old = Schema::old()?;
check(&old.get().result, &new.result).context("checking result")?;
check(&old.get().request, &new.request).context("checking request")?;
check(&old.get().response, &new.response).context("checking response")?;
check_versions(&old.get().versions, &new.versions).context("checking versions")?;
Ok(())
}
fn check(old: &View, new: &View) -> Result<()> {
let old = old.simplify();
let new = new.simplify();
let mut old_map = HashMap::new();
for (_, tag, api) in get_enum(&old).context("in old")? {
ensure!(old_map.insert(tag, api).is_none(), "duplicate tag {tag}");
}
for (_, tag, new_api) in get_enum(&new).context("in new")? {
if let Some(old_api) = old_map.remove(tag) {
let old_api = ViewFields(old_api);
let new_api = ViewFields(new_api);
ensure!(old_api == new_api, "incompatible API for {tag}: {old_api} != {new_api}");
}
}
#[cfg(feature = "host")]
ensure!(old_map.is_empty(), "deleted tag {}", old_map.keys().next().unwrap());
Ok(())
}
fn check_versions(old: &Versions, new: &Versions) -> Result<()> {
let mut old_map = HashMap::new();
for Descriptor { tag, versions } in &old.descriptors {
ensure!(old_map.insert(tag, *versions).is_none(), "duplicate tag {tag}");
}
let mut changed = false;
let old_version = old.version;
let new_version = new.version;
for Descriptor { tag, versions: new } in &new.descriptors {
#[cfg(feature = "device")]
ensure!(new.max.is_none(), "max version is defined for {tag}");
match old_map.remove(&tag) {
Some(old) => {
ensure!(new.min == old.min, "min version changed for {tag}");
match (old.max, new.max) {
(None, Some(new)) if new == old_version => changed = true,
(None, Some(_)) => bail!("max version for {tag} must be {old_version}"),
(old, new) if old == new => (),
_ => bail!("max version changed for {tag}"),
}
}
None => {
changed = true;
ensure!(new.min == new_version, "min version for {tag} must be {new_version}");
ensure!(new.max.is_none(), "max version is defined for {tag}");
}
}
}
changed |= !old_map.is_empty();
#[cfg(feature = "host")]
ensure!(old_map.is_empty(), "deleted tag {}", old_map.keys().next().unwrap());
match (new_version.checked_sub(old_version), changed) {
(Some(0), false) | (Some(1), true) => (),
(Some(0), true) => bail!("version must increase by 1"),
(Some(1), false) => bail!("version must stay the same"),
(None, _) => bail!("version cannot decrease"),
(Some(_), _) => bail!("version can only increase by 1"),
}
Ok(())
}
#[derive(Debug, Wire)]
struct Schema<'a> {
result: View<'a>, // Result<(), Error>
request: View<'a>, // Api<Request>
response: View<'a>, // Api<Response>
versions: Versions,
}
#[derive(Debug, Wire)]
struct Versions {
version: u32,
descriptors: Vec<Descriptor>,
}
impl Schema<'static> {
fn new() -> Self {
Schema {
result: View::new::<Result<(), Error>>(),
request: View::new::<Api<Request>>(),
response: View::new::<Api<Response>>(),
versions: Versions { version: VERSION, descriptors: DESCRIPTORS.to_vec() },
}
}
fn old() -> Result<Yoke<Schema<'static>>> {
let base = Self::base()?;
let mut git = Command::new("git");
git.args(["show", &format!("{base}:./{SIDE}.bin")]);
let data = cmd::output(&mut git)?.stdout.into_boxed_slice();
Ok(wasefire_wire::decode_yoke(data)?)
}
fn base() -> Result<String> {
use std::env::var;
use std::env::VarError::*;
Ok(match var("GITHUB_EVENT_NAME").as_deref() {
Ok("pull_request") => format!("origin/{}", var("GITHUB_BASE_REF")?),
Ok("push" | "schedule") => format!("origin/{}", var("GITHUB_REF_NAME")?),
Ok(x) => bail!("unexpected GITHUB_EVENT_NAME {x:?}"),
Err(NotPresent) => match var("BASE_REF") {
Ok(x) => x,
Err(NotPresent) => "origin/main".to_string(),
Err(NotUnicode(x)) => bail!("invalid BASE_REF {x:?}"),
},
Err(NotUnicode(x)) => bail!("invalid GITHUB_EVENT_NAME {x:?}"),
})
}
fn write(&self) -> Result<()> {
fs::write(format!("{SIDE}.bin"), wasefire_wire::encode(self)?)
}
fn print(&self) -> Result<()> {
let mut output = String::new();
writeln!(&mut output, "result: {}", self.result)?;
writeln!(&mut output, "version: {}", self.versions.version)?;
let request = get_enum(&self.request).context("in request")?.iter();
let mut response = get_enum(&self.response).context("in response")?.iter();
let mut descriptor = self.versions.descriptors.iter();
for request in request {
let name = request.0;
let tag = request.1;
let response = response.next().unwrap();
let descriptor = descriptor.next().unwrap();
assert_eq!(response.0, name);
assert_eq!(response.1, tag);
assert_eq!(descriptor.tag, tag);
let request = ViewFields(&request.2);
let response = ViewFields(&response.2);
writeln!(&mut output, "{descriptor} {name}: {request} -> {response}")?;
}
assert!(response.next().is_none());
assert!(descriptor.next().is_none());
fs::write(format!("{SIDE}.txt"), &output)
}
}
#[cfg(feature = "host")]
const SIDE: &str = "host";
#[cfg(feature = "device")]
const SIDE: &str = "device";
fn get_enum<'a>(x: &'a View<'a>) -> Result<&'a ViewEnum<'a>> {
match x {
View::Enum(xs) => Ok(xs),
x => bail!("expected enum, got {x}"),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn adding_variant() {
#[derive(Wire)]
enum Old {
Foo,
}
#[derive(Wire)]
enum New {
Foo,
Bar,
}
// Adding a variant is always accepted.
assert!(check(&View::new::<Old>(), &View::new::<New>()).is_ok());
}
#[test]
fn removing_variant() {
#[derive(Wire)]
enum Old {
Foo,
Bar,
}
#[derive(Wire)]
enum New {
Foo,
}
// Removing a variant is accepted on the device.
#[cfg(feature = "device")]
assert!(check(&View::new::<Old>(), &View::new::<New>()).is_ok());
// But rejected on the host.
#[cfg(feature = "host")]
assert!(check(&View::new::<Old>(), &View::new::<New>()).is_err());
}
#[test]
fn updating_variant_incompatible() {
#[derive(Wire)]
enum Old {
Foo,
Bar(u8),
}
#[derive(Wire)]
enum New {
Foo,
Bar(i8),
}
// Updating a variant in an incompatible way is always rejected. Which updates are
// considered compatible should always be sound but may be incomplete.
assert!(check(&View::new::<Old>(), &View::new::<New>()).is_err());
}
#[test]
fn updating_variant_compatible() {
#[derive(Wire)]
enum Old {
Foo,
Bar(u8),
}
#[derive(Wire)]
enum New {
Foo,
Bar { bar: u8 },
}
// Updating a variant in a compatible way is always accepted.
assert!(check(&View::new::<Old>(), &View::new::<New>()).is_ok());
}
#[test]
fn renaming_variant() {
#[derive(Wire)]
enum Old {
Foo,
Bar,
}
#[derive(Wire)]
enum New {
Foo,
Baz,
}
// Renaming a variant is always accepted.
assert!(check(&View::new::<Old>(), &View::new::<New>()).is_ok());
}
macro_rules! build_versions {
($ver:literal $($tag:literal [$min:literal - $($max:literal)?])*) => {
Versions {
version: $ver,
descriptors: vec![$(build_versions!([1] $tag [$min - $($max)?])),*],
}
};
([1] $tag:literal [$min:literal - $($max:literal)?]) => {
Descriptor {
tag: $tag,
versions: wasefire_protocol::Versions {
min: $min,
max: build_versions!([2] $($max)?),
},
}
};
([2]) => (None);
([2] $max:literal) => (Some($max));
}
#[track_caller]
fn assert_is_err<T: std::fmt::Debug>(x: Result<T>, m: &'static str) {
let e = x.unwrap_err();
match e.downcast_ref::<String>() {
Some(a) => assert_eq!(a, m),
None => assert_eq!(e.downcast::<&'static str>().unwrap(), m),
}
}
#[test]
fn adding_service() {
let old = build_versions!(41 0 [13 -]);
// Correct usage.
let new = build_versions!(42 0 [13 -] 1 [42 -]);
assert!(check_versions(&old, &new).is_ok());
// The added service is already deprecated.
let new = build_versions!(42 0 [13 -] 1 [42 - 42]);
assert_is_err(check_versions(&old, &new), "max version is defined for 1");
// The added service uses the wrong version.
let new = build_versions!(42 0 [13 -] 1 [41 -]);
assert_is_err(check_versions(&old, &new), "min version for 1 must be 42");
// The version is not bumped.
let new = build_versions!(41 0 [13 -] 1 [41 -]);
assert_is_err(check_versions(&old, &new), "version must increase by 1");
// The version is decreased.
let new = build_versions!(40 0 [13 -] 1 [40 -]);
assert_is_err(check_versions(&old, &new), "version cannot decrease");
// The version is increased too much.
let new = build_versions!(43 0 [13 -] 1 [43 -]);
assert_is_err(check_versions(&old, &new), "version can only increase by 1");
}
#[test]
#[cfg(feature = "device")]
fn removing_service() {
let old = build_versions!(41 0 [13 -]);
// Correct usage.
let new = build_versions!(42);
assert!(check_versions(&old, &new).is_ok());
// The version is not bumped.
let new = build_versions!(41);
assert_is_err(check_versions(&old, &new), "version must increase by 1");
// The version is decreased.
let new = build_versions!(40);
assert_is_err(check_versions(&old, &new), "version cannot decrease");
// The version is increased too much.
let new = build_versions!(43);
assert_is_err(check_versions(&old, &new), "version can only increase by 1");
}
#[test]
#[cfg(feature = "host")]
fn removing_service() {
let old = build_versions!(41 0 [13 -]);
// Correct usage.
let new = build_versions!(42 0 [13 - 41]);
assert!(check_versions(&old, &new).is_ok());
// The removed service uses the wrong version.
let new = build_versions!(42 0 [13 - 42]);
assert_is_err(check_versions(&old, &new), "max version for 0 must be 41");
// The version is not bumped.
let new = build_versions!(41 0 [13 - 41]);
assert_is_err(check_versions(&old, &new), "version must increase by 1");
// The version is decreased.
let new = build_versions!(40 0 [13 - 41]);
assert_is_err(check_versions(&old, &new), "version cannot decrease");
// The version is increased too much.
let new = build_versions!(43 0 [13 - 41]);
assert_is_err(check_versions(&old, &new), "version can only increase by 1");
}
#[test]
#[cfg(feature = "device")]
fn unchanging_service() {
let old = build_versions!(41 0 [13 -]);
// Correct usage.
let new = build_versions!(41 0 [13 -]);
assert!(check_versions(&old, &new).is_ok());
// The service cannot change min version.
let new = build_versions!(41 0 [14 -]);
assert_is_err(check_versions(&old, &new), "min version changed for 0");
// The service cannot have a max version.
let new = build_versions!(41 0 [13 - 27]);
assert_is_err(check_versions(&old, &new), "max version is defined for 0");
// The version cannot decrease.
let new = build_versions!(40 0 [13 -]);
assert_is_err(check_versions(&old, &new), "version cannot decrease");
// The version cannot increase.
let new = build_versions!(42 0 [13 -]);
assert_is_err(check_versions(&old, &new), "version must stay the same");
}
#[test]
#[cfg(feature = "host")]
fn unchanging_service() {
let old = build_versions!(41 0 [13 - 27]);
// Correct usage.
let new = build_versions!(41 0 [13 - 27]);
assert!(check_versions(&old, &new).is_ok());
// The service cannot change min version.
let new = build_versions!(41 0 [14 - 27]);
assert_is_err(check_versions(&old, &new), "min version changed for 0");
// The service cannot change max version.
let new = build_versions!(41 0 [13 - 26]);
assert_is_err(check_versions(&old, &new), "max version changed for 0");
// The service cannot remove max version.
let new = build_versions!(41 0 [13 -]);
assert_is_err(check_versions(&old, &new), "max version changed for 0");
// The version cannot decrease.
let new = build_versions!(40 0 [13 - 27]);
assert_is_err(check_versions(&old, &new), "version cannot decrease");
// The version cannot increase.
let new = build_versions!(42 0 [13 - 27]);
assert_is_err(check_versions(&old, &new), "version must stay the same");
}
}