OXYGENHUB
# 33472ccc08cc oxygen / crates / oxy-core / src / protocol.rs
43.1 KiB · Rust · last changed Remote bookmarks work like local ones; a newer format names the oxy that cannot read it (closes #48, closes #49) 1 month ago source blame history raw
1
//! Transport-agnostic protocol (spec §16): what a client asks of a remote and
2
//! what a server does with a push. `LocalRemote` serves another repository
3
//! directory in-process, `HttpRemote` speaks to an `oxy-server` over HTTP(S),
4
//! and the server side reuses `Repo::receive_push` and friends unchanged.
5
//!
6
//! Wire messages are deterministic CBOR (`wire` module); over HTTP each is a
7
//! request or response body with content type `application/oxy-cbor`.
8
9
use crate::attest::Attest;
10
use crate::cbor::{self, Value};
11
use crate::hash::{ChangeHash, ObjectRef, ObjectType};
12
use crate::identity::Signature;
13
use crate::objects::{self as o, Change, ObjectError};
14
use crate::policy::Policy;
15
use crate::repo::{Repo, RepoError, Result};
16
use std::path::{Path, PathBuf};
17
use std::sync::Arc;
18
19
pub const CONTENT_TYPE: &str = "application/oxy-cbor";
20
pub const PROTOCOL_VERSION: u64 = 1;
21
22
/// An attestation together with the signatures over its ObjectRef.
23
#[derive(Clone, Debug, PartialEq, Eq)]
24
pub struct SignedAttest {
25
    pub attest: Attest,
26
    pub sigs: Vec<Signature>,
27
}
28
29
/// A policy together with the root signatures over its ObjectRef.
30
#[derive(Clone, Debug, PartialEq, Eq)]
31
pub struct SignedPolicy {
32
    pub policy: Policy,
33
    pub sigs: Vec<Signature>,
34
}
35
36
impl SignedPolicy {
37
    /// The proposal-file encoding (`wire::policy_value`).
38
    pub fn encode(&self) -> Vec<u8> {
39
        crate::cbor::encode(&wire::policy_value(self))
40
    }
41
    pub fn decode(b: &[u8]) -> Result<SignedPolicy> {
42
        wire::policy_from(
43
            &crate::cbor::decode(b).map_err(|e| RepoError::Object(ObjectError::from(e)))?,
44
        )
45
    }
46
}
47
48
/// What `refs` returns: every bookmark, every tag, and the current policy.
49
#[derive(Clone, Debug, Default)]
50
pub struct RefsResponse {
51
    pub refs: Vec<RemoteRef>,
52
    pub policy: Option<SignedPolicy>,
53
    pub tags: Vec<TagRef>,
54
    /// Earlier policies, ascending by serial, so a client behind by more
55
    /// than one update can verify each step (spec §14.5).
56
    pub history: Vec<SignedPolicy>,
57
    /// The bookmark a clone checks out (spec §16.1), when the remote names one.
58
    pub default_bookmark: Option<String>,
59
}
60
61
/// A tag as a remote reports it (spec §10.1).
62
#[derive(Clone, Debug, PartialEq, Eq)]
63
pub struct TagRef {
64
    pub name: String,
65
    pub state: ObjectRef,
66
    pub heads: Vec<ChangeHash>,
67
    pub attestation: Option<SignedAttest>,
68
}
69
70
/// One reference as a remote reports it.
71
#[derive(Clone, Debug)]
72
pub struct RemoteRef {
73
    pub name: String,
74
    pub state: ObjectRef,
75
    pub heads: Vec<ChangeHash>,
76
    pub attestation: Option<SignedAttest>,
77
}
78
79
/// Everything needed to add a set of changes to a repository.
80
#[derive(Clone, Debug, Default)]
81
pub struct Bundle {
82
    /// In dependency order, each with its individual signatures.
83
    pub changes: Vec<(Change, Vec<Signature>)>,
84
    /// Seals covering the changes.
85
    pub attests: Vec<SignedAttest>,
86
    /// Chunks, blobs, identities, and state objects referenced by the changes.
87
    pub objects: Vec<(ObjectType, Vec<u8>)>,
88
    /// Policies between the receiver's and the one sent with the push,
89
    /// ascending by serial (spec §14.5).
90
    pub policies: Vec<SignedPolicy>,
91
    /// Each change's hash, in the order of `changes`, when whoever made
92
    /// the bundle knows it: the store it came from, or the bytes it was
93
    /// decoded from. Never read from the wire. Empty when unknown.
94
    pub hashes: Vec<ChangeHash>,
95
    /// Changes as stored, with their signatures, in dependency order, for a
96
    /// fetch between repositories on one machine: their text blobs and
97
    /// chunks are among `objects`. Never on the wire.
98
    pub raw_changes: Vec<(Vec<u8>, Vec<Signature>)>,
99
}
100
101
impl Bundle {
102
    pub fn is_empty(&self) -> bool {
103
        self.changes.is_empty() && self.attests.is_empty() && self.objects.is_empty()
104
    }
105
}
106
107
/// A requested bookmark move (spec §10.2, §16.3).
108
#[derive(Clone, Debug)]
109
pub struct RefUpdate {
110
    pub name: String,
111
    pub old_state: ObjectRef,
112
    pub new_state: ObjectRef,
113
    pub force: bool,
114
    pub attestation: Option<SignedAttest>,
115
}
116
117
#[derive(Clone, Debug, PartialEq, Eq)]
118
pub struct RefResult {
119
    pub name: String,
120
    pub ok: bool,
121
    pub reason: String,
122
}
123
124
pub trait Remote {
125
    fn repo_id(&self) -> Result<[u8; 16]>;
126
    fn refs(&self) -> Result<RefsResponse>;
127
    /// Changes in the closure of `want` but not of `have`, in dependency order.
128
    fn negotiate(&self, have: &[ChangeHash], want: &[ChangeHash]) -> Result<Vec<ChangeHash>>;
129
    /// The changes with their signatures, seals, identities, and blobs; blobs
130
    /// larger than `max_blob` bytes are left out for lazy fetching (spec §13.5).
131
    fn fetch(&self, changes: &[ChangeHash], max_blob: Option<u64>) -> Result<Bundle>;
132
    /// Individual objects by reference (spec §16.1 `objects`); a blob comes
133
    /// with its chunks.
134
    fn objects(&self, refs: &[ObjectRef]) -> Result<Vec<(ObjectType, Vec<u8>)>>;
135
    /// A checkpoint of exactly `heads` (spec §16.1 `checkpoint`): the
136
    /// checkpoint object, its payload blob, and the blob's chunks.
137
    fn checkpoint(&self, heads: &[ChangeHash]) -> Result<Bundle>;
138
    /// The changes within `depth` dependency steps of `heads` (spec §17).
139
    fn shallow(&self, heads: &[ChangeHash], depth: u32) -> Result<Vec<ChangeHash>>;
140
    /// Push changes, bookmark updates, new tags, and optionally a newer
141
    /// policy. The policy is applied first; a result named `policy` reports
142
    /// it, and each tag reports as `tags/<name>`.
143
    fn push(
144
        &self,
145
        bundle: Bundle,
146
        updates: Vec<RefUpdate>,
147
        policy: Option<SignedPolicy>,
148
        tags: Vec<TagRef>,
149
    ) -> Result<Vec<RefResult>>;
150
}
151
152
// ---------------------------------------------------------------- local
153
154
/// Another repository on this machine, served in-process.
155
pub struct LocalRemote {
156
    pub repo: Repo,
157
    /// The pushing repository's id, which binds an unbound target on the
158
    /// first push, as `Oxy-Repo-Id` does over HTTP.
159
    pub claimed_id: Option<[u8; 16]>,
160
}
161
162
impl LocalRemote {
163
    pub fn open(path: &Path) -> Result<LocalRemote> {
164
        Ok(LocalRemote {
165
            repo: Repo::open(path)?,
166
            claimed_id: None,
167
        })
168
    }
169
}
170
171
impl Remote for LocalRemote {
172
    fn repo_id(&self) -> Result<[u8; 16]> {
173
        self.repo.id()
174
    }
175
    fn refs(&self) -> Result<RefsResponse> {
176
        self.repo.remote_refs()
177
    }
178
    fn negotiate(&self, have: &[ChangeHash], want: &[ChangeHash]) -> Result<Vec<ChangeHash>> {
179
        self.repo.negotiate(have, want)
180
    }
181
    fn fetch(&self, changes: &[ChangeHash], max_blob: Option<u64>) -> Result<Bundle> {
182
        self.repo.bundle_raw_for(changes, max_blob)
183
    }
184
    fn objects(&self, refs: &[ObjectRef]) -> Result<Vec<(ObjectType, Vec<u8>)>> {
185
        self.repo.objects_for(refs)
186
    }
187
    fn checkpoint(&self, heads: &[ChangeHash]) -> Result<Bundle> {
188
        self.repo.checkpoint_bundle_for(heads)
189
    }
190
    fn shallow(&self, heads: &[ChangeHash], depth: u32) -> Result<Vec<ChangeHash>> {
191
        self.repo.shallow_changes(heads, depth)
192
    }
193
    fn push(
194
        &self,
195
        bundle: Bundle,
196
        updates: Vec<RefUpdate>,
197
        policy: Option<SignedPolicy>,
198
        tags: Vec<TagRef>,
199
    ) -> Result<Vec<RefResult>> {
200
        if self.repo.is_unbound() {
201
            match self.claimed_id {
202
                Some(id) => self.repo.bind_id(id)?,
203
                None => {
204
                    return Err(RepoError::Msg(
205
                        "the remote is unbound and this client did not say which repository it is"
206
                            .into(),
207
                    ))
208
                }
209
            }
210
        }
211
        self.repo.receive_push(bundle, updates, policy, tags)
212
    }
213
}
214
215
// ---------------------------------------------------------------- http
216
217
/// How to reach a remote: credentials and trust.
218
#[derive(Clone, Debug, Default)]
219
pub struct RemoteOptions {
220
    /// Bearer token for pushes.
221
    pub token: Option<String>,
222
    /// PEM file with extra root certificates to trust for `https://` remotes
223
    /// (self-hosted servers); the system web roots are always trusted.
224
    pub ca: Option<PathBuf>,
225
    /// The local repository's id, sent as `Oxy-Repo-Id` so an unbound
226
    /// hosted repository can adopt it on the first push (spec §16.1).
227
    pub repo_id: Option<[u8; 16]>,
228
}
229
230
/// The request header carrying the client's repository id.
231
pub const REPO_ID_HEADER: &str = "Oxy-Repo-Id";
232
233
/// The id of a hosted repository that has not adopted one yet.
234
pub const UNBOUND_ID: [u8; 16] = [0; 16];
235
236
/// A remote served by `oxy-server` at `<base>/oxy/v1/...`.
237
pub struct HttpRemote {
238
    base: String,
239
    token: Option<String>,
240
    repo_id: Option<[u8; 16]>,
241
    agent: ureq::Agent,
242
}
243
244
/// A TLS client configuration trusting the web roots plus `ca`, if given.
245
fn tls_client_config(ca: Option<&Path>) -> Result<Arc<rustls::ClientConfig>> {
246
    let mut roots = rustls::RootCertStore::empty();
247
    roots.extend(webpki_roots::TLS_SERVER_ROOTS.iter().cloned());
248
    if let Some(path) = ca {
249
        let pem = std::fs::read(path)
250
            .map_err(|e| RepoError::Msg(format!("reading CA file {}: {e}", path.display())))?;
251
        let certs = rustls_pemfile::certs(&mut pem.as_slice())
252
            .collect::<std::result::Result<Vec<_>, _>>()
253
            .map_err(|e| RepoError::Msg(format!("parsing CA file {}: {e}", path.display())))?;
254
        if certs.is_empty() {
255
            return Err(RepoError::Msg(format!(
256
                "CA file {} holds no certificates",
257
                path.display()
258
            )));
259
        }
260
        for c in certs {
261
            roots
262
                .add(c)
263
                .map_err(|e| RepoError::Msg(format!("CA file {}: {e}", path.display())))?;
264
        }
265
    }
266
    let cfg = rustls::ClientConfig::builder_with_provider(Arc::new(
267
        rustls::crypto::ring::default_provider(),
268
    ))
269
    .with_safe_default_protocol_versions()
270
    .map_err(|e| RepoError::Msg(e.to_string()))?
271
    .with_root_certificates(roots)
272
    .with_no_client_auth();
273
    Ok(Arc::new(cfg))
274
}
275
276
impl HttpRemote {
277
    pub fn new(base: &str, options: &RemoteOptions) -> Result<HttpRemote> {
278
        refuse_plain_token(base, options)?;
279
        let agent = ureq::AgentBuilder::new()
280
            .tls_config(tls_client_config(options.ca.as_deref())?)
281
            .build();
282
        Ok(HttpRemote {
283
            base: base.trim_end_matches('/').to_string(),
284
            token: options.token.clone(),
285
            repo_id: options.repo_id,
286
            agent,
287
        })
288
    }
289
290
    fn call(&self, post: bool, endpoint: &str, body: Option<&[u8]>) -> Result<Vec<u8>> {
291
        let url = format!("{}/oxy/v1/{endpoint}", self.base);
292
        let mut req = if post {
293
            self.agent.post(&url)
294
        } else {
295
            self.agent.get(&url)
296
        };
297
        req = req.set("Accept", CONTENT_TYPE);
298
        if let Some(t) = &self.token {
299
            req = req.set("Authorization", &format!("Bearer {t}"));
300
        }
301
        if let Some(id) = &self.repo_id {
302
            req = req.set(REPO_ID_HEADER, &hex::encode(id));
303
        }
304
        let resp = match body {
305
            Some(b) => req.set("Content-Type", CONTENT_TYPE).send_bytes(b),
306
            None => req.call(),
307
        };
308
        match resp {
309
            Ok(r) => {
310
                let mut out = Vec::new();
311
                r.into_reader()
312
                    .read_to_end(&mut out)
313
                    .map_err(|e| RepoError::Msg(format!("{url}: {e}")))?;
314
                Ok(out)
315
            }
316
            Err(ureq::Error::Status(code, r)) => {
317
                let text = r.into_string().unwrap_or_default();
318
                let msg = format!("{url}: HTTP {code}: {}", text.trim());
319
                Err(RepoError::Msg(with_hint(msg)))
320
            }
321
            // The transport error already names the URL.
322
            Err(e) => Err(RepoError::Msg(with_hint(e.to_string()))),
323
        }
324
    }
325
}
326
327
/// Refuse to send a token where anyone on the way could read it: plain
328
/// `http` to another machine (see [`crate::credentials::token_allowed`]).
329
fn refuse_plain_token(url: &str, options: &RemoteOptions) -> Result<()> {
330
    if options.token.is_some() && !crate::credentials::token_allowed(url) {
331
        return Err(RepoError::Msg(format!(
332
            "{url}: refusing to send a token over plain http; use https"
333
        )));
334
    }
335
    Ok(())
336
}
337
338
/// What to do about a failed request, recognised from its message.
339
pub fn hint_for(message: &str) -> Option<&'static str> {
340
    let m = message.to_ascii_lowercase();
341
    let hint = if m.contains("http 401") {
342
        "the hub needs you signed in, or did not accept the token: run `oxy auth login HUB` (pushing needs repo:write)"
343
    } else if m.contains("http 403") {
344
        "the token or account lacks permission: pushing needs a token with repo:write and write access to the repository"
345
    } else if m.contains("http 404") {
346
        "nothing at this URL that you can see: check the owner and repository name; a private repository needs you signed in (`oxy auth login HUB`)"
347
    } else if m.contains("unknownissuer")
348
        || m.contains("unknown issuer")
349
        || m.contains("certificate")
350
        || m.contains("invalidcertificate")
351
    {
352
        "the server's certificate is not trusted: for a hub with a self-signed certificate, download it from /.well-known/oxyhub-ca.pem on the hub and run `oxy config remote.<remote>.ca FILE`"
353
    } else if m.contains("connection refused") {
354
        "nothing is listening there: check the URL and port, and that the server is running"
355
    } else if m.contains("dns")
356
        || m.contains("failed to lookup")
357
        || m.contains("name or service not known")
358
    {
359
        "the host name does not resolve: check the URL"
360
    } else if m.contains("timed out") || m.contains("timeout") {
361
        "the server did not answer in time: check the network, or try again"
362
    } else {
363
        return None;
364
    };
365
    Some(hint)
366
}
367
368
/// `message`, followed by what to do about it when that is known.
369
pub fn with_hint(message: String) -> String {
370
    match hint_for(&message) {
371
        Some(h) => format!("{message}\n  hint: {h}"),
372
        None => message,
373
    }
374
}
375
376
/// A JSON request to a hub API (`GET`, `POST` or `DELETE`), with the same
377
/// TLS trust and token rules as remotes. Returns the status and body, an
378
/// error status included; only transport failures are errors.
379
pub fn http_json(
380
    method: &str,
381
    url: &str,
382
    body: Option<&str>,
383
    options: &RemoteOptions,
384
) -> Result<(u16, String)> {
385
    refuse_plain_token(url, options)?;
386
    let agent = ureq::AgentBuilder::new()
387
        .tls_config(tls_client_config(options.ca.as_deref())?)
388
        .timeout(std::time::Duration::from_secs(60))
389
        .build();
390
    let mut req = agent.request(method, url).set("Accept", "application/json");
391
    if let Some(t) = &options.token {
392
        req = req.set("Authorization", &format!("Bearer {t}"));
393
    }
394
    let resp = match body {
395
        Some(b) => req.set("Content-Type", "application/json").send_string(b),
396
        None => req.call(),
397
    };
398
    match resp {
399
        Ok(r) => {
400
            let code = r.status();
401
            Ok((code, r.into_string().unwrap_or_default()))
402
        }
403
        Err(ureq::Error::Status(code, r)) => Ok((code, r.into_string().unwrap_or_default())),
404
        Err(e) => Err(RepoError::Msg(with_hint(e.to_string()))),
405
    }
406
}
407
408
/// An authenticated GET of any URL on a host, with the same TLS trust and
409
/// token as remotes use (for hub APIs: proposals). Returns the body.
410
pub fn http_get(url: &str, options: &RemoteOptions) -> Result<Vec<u8>> {
411
    refuse_plain_token(url, options)?;
412
    let agent = ureq::AgentBuilder::new()
413
        .tls_config(tls_client_config(options.ca.as_deref())?)
414
        .build();
415
    let mut req = agent.get(url).set("Accept", "application/json");
416
    if let Some(t) = &options.token {
417
        req = req.set("Authorization", &format!("Bearer {t}"));
418
    }
419
    match req.call() {
420
        Ok(r) => {
421
            let mut out = Vec::new();
422
            r.into_reader()
423
                .read_to_end(&mut out)
424
                .map_err(|e| RepoError::Msg(format!("{url}: {e}")))?;
425
            Ok(out)
426
        }
427
        Err(ureq::Error::Status(code, r)) => {
428
            let text = r.into_string().unwrap_or_default();
429
            Err(RepoError::Msg(with_hint(format!(
430
                "{url}: HTTP {code}: {}",
431
                text.trim()
432
            ))))
433
        }
434
        // The transport error already names the URL.
435
        Err(e) => Err(RepoError::Msg(with_hint(e.to_string()))),
436
    }
437
}
438
439
impl Remote for HttpRemote {
440
    fn repo_id(&self) -> Result<[u8; 16]> {
441
        wire::decode_capabilities(&self.call(false, "capabilities", None)?)
442
    }
443
    fn refs(&self) -> Result<RefsResponse> {
444
        Ok(wire::decode_refs(&self.call(false, "refs", None)?)?.1)
445
    }
446
    fn negotiate(&self, have: &[ChangeHash], want: &[ChangeHash]) -> Result<Vec<ChangeHash>> {
447
        wire::decode_hashes(&self.call(
448
            true,
449
            "negotiate",
450
            Some(&wire::encode_negotiate(have, want)),
451
        )?)
452
    }
453
    fn fetch(&self, changes: &[ChangeHash], max_blob: Option<u64>) -> Result<Bundle> {
454
        wire::decode_bundle(&self.call(
455
            true,
456
            "fetch",
457
            Some(&wire::encode_fetch(changes, max_blob)),
458
        )?)
459
    }
460
    fn objects(&self, refs: &[ObjectRef]) -> Result<Vec<(ObjectType, Vec<u8>)>> {
461
        Ok(
462
            wire::decode_bundle(&self.call(true, "objects", Some(&wire::encode_hashes(refs)))?)?
463
                .objects,
464
        )
465
    }
466
    fn checkpoint(&self, heads: &[ChangeHash]) -> Result<Bundle> {
467
        wire::decode_bundle(&self.call(true, "checkpoint", Some(&wire::encode_hashes(heads)))?)
468
    }
469
    fn shallow(&self, heads: &[ChangeHash], depth: u32) -> Result<Vec<ChangeHash>> {
470
        wire::decode_hashes(&self.call(
471
            true,
472
            "shallow",
473
            Some(&wire::encode_shallow(heads, depth)),
474
        )?)
475
    }
476
    fn push(
477
        &self,
478
        bundle: Bundle,
479
        updates: Vec<RefUpdate>,
480
        policy: Option<SignedPolicy>,
481
        tags: Vec<TagRef>,
482
    ) -> Result<Vec<RefResult>> {
483
        wire::decode_push_response(&self.call(
484
            true,
485
            "push",
486
            Some(&wire::encode_push(
487
                &bundle,
488
                &updates,
489
                policy.as_ref(),
490
                &tags,
491
            )),
492
        )?)
493
    }
494
}
495
496
/// Open a remote by URL: a local path (optionally `file://`), or `http(s)://`.
497
pub fn open_remote(url: &str, options: &RemoteOptions) -> Result<Box<dyn Remote>> {
498
    if url.starts_with("http://") || url.starts_with("https://") {
499
        return Ok(Box::new(HttpRemote::new(url, options)?));
500
    }
501
    let path = url.strip_prefix("file://").unwrap_or(url);
502
    if path.contains("://") {
503
        return Err(RepoError::Msg(format!("unsupported remote URL {url:?}")));
504
    }
505
    let mut local = LocalRemote::open(Path::new(path))?;
506
    local.claimed_id = options.repo_id;
507
    Ok(Box::new(local))
508
}
509
510
// ---------------------------------------------------------------- wire
511
512
/// CBOR encodings of the protocol messages (spec §16.1).
513
pub mod wire {
514
    use super::*;
515
516
    fn schema(msg: &str) -> RepoError {
517
        RepoError::Object(ObjectError::Schema(msg.into()))
518
    }
519
    fn sigs_value(sigs: &[Signature]) -> Value {
520
        Value::Array(sigs.iter().map(|s| s.to_value()).collect())
521
    }
522
    fn sigs_from(v: &Value) -> Result<Vec<Signature>> {
523
        Ok(o::array(v, "signatures")?
524
            .iter()
525
            .map(Signature::from_value)
526
            .collect::<std::result::Result<Vec<_>, _>>()?)
527
    }
528
    fn hashes_value(hs: &[ObjectRef]) -> Value {
529
        Value::Array(hs.iter().map(|h| Value::Bytes(h.0.to_vec())).collect())
530
    }
531
    fn hashes_from(v: &Value) -> Result<Vec<ObjectRef>> {
532
        Ok(o::array(v, "hashes")?
533
            .iter()
534
            .map(|h| o::oref(h, "hash"))
535
            .collect::<std::result::Result<Vec<_>, _>>()?)
536
    }
537
    fn attest_value(sa: &SignedAttest) -> Value {
538
        Value::Array(vec![Value::Bytes(sa.attest.encode()), sigs_value(&sa.sigs)])
539
    }
540
    fn attest_from(v: &Value) -> Result<SignedAttest> {
541
        let a = o::array(v, "signed attest")?;
542
        if a.len() != 2 {
543
            return Err(schema("signed attest: expected [attest, sigs]"));
544
        }
545
        Ok(SignedAttest {
546
            attest: Attest::decode(o::bytes(&a[0], "attest")?)?,
547
            sigs: sigs_from(&a[1])?,
548
        })
549
    }
550
    fn opt_attest_value(sa: &Option<SignedAttest>) -> Value {
551
        sa.as_ref().map(attest_value).unwrap_or(Value::Null)
552
    }
553
    fn opt_attest_from(v: &Value) -> Result<Option<SignedAttest>> {
554
        if v.is_null() {
555
            Ok(None)
556
        } else {
557
            attest_from(v).map(Some)
558
        }
559
    }
560
    /// `[policy bytes, [signature]]`: also the format of a policy proposal
561
    /// file exchanged between root holders (spec §14.5).
562
    pub fn policy_value(sp: &SignedPolicy) -> Value {
563
        Value::Array(vec![Value::Bytes(sp.policy.encode()), sigs_value(&sp.sigs)])
564
    }
565
    pub fn policy_from(v: &Value) -> Result<SignedPolicy> {
566
        let a = o::array(v, "signed policy")?;
567
        if a.len() != 2 {
568
            return Err(schema("signed policy: expected [policy, sigs]"));
569
        }
570
        Ok(SignedPolicy {
571
            policy: Policy::decode(o::bytes(&a[0], "policy")?)?,
572
            sigs: sigs_from(&a[1])?,
573
        })
574
    }
575
    fn opt_policy_value(sp: Option<&SignedPolicy>) -> Value {
576
        sp.map(policy_value).unwrap_or(Value::Null)
577
    }
578
    fn opt_policy_from(v: &Value) -> Result<Option<SignedPolicy>> {
579
        if v.is_null() {
580
            Ok(None)
581
        } else {
582
            policy_from(v).map(Some)
583
        }
584
    }
585
    fn map<'a>(v: &'a Value, what: &str) -> Result<&'a o::Map> {
586
        Ok(o::map(v, what)?)
587
    }
588
    fn field<'a>(m: &'a o::Map, k: u64, what: &str) -> Result<&'a Value> {
589
        Ok(o::field(m, k, what)?)
590
    }
591
592
    /// A bundle's value. Each change's text blobs and their chunks (spec
593
    /// §6.1) are added to its objects when they are not already there, so a
594
    /// bundle can always be read on its own.
595
    pub fn bundle_value(b: &Bundle) -> Value {
596
        let mut changes = Vec::with_capacity(b.raw_changes.len() + b.changes.len());
597
        // Changes as stored are already their encoding, their texts among
598
        // the objects.
599
        for (bytes, s) in &b.raw_changes {
600
            changes.push(Value::Array(vec![
601
                Value::Bytes(bytes.clone()),
602
                sigs_value(s),
603
            ]));
604
        }
605
        let mut texts: Vec<(ObjectType, Vec<u8>)> = Vec::new();
606
        let mut listed: Option<std::collections::HashSet<ObjectRef>> = None;
607
        for (c, s) in &b.changes {
608
            let (bytes, stored) = c.encode_parts();
609
            for t in stored {
610
                let listed = listed.get_or_insert_with(|| {
611
                    b.objects
612
                        .iter()
613
                        .map(|(ty, body)| crate::hash::hash_object(*ty, body))
614
                        .collect()
615
                });
616
                if !listed.insert(t.blob.hash()) {
617
                    continue;
618
                }
619
                for chunk in crate::chunk::chunks(&t.content) {
620
                    if listed.insert(crate::hash::hash_object(ObjectType::Chunk, chunk)) {
621
                        texts.push((ObjectType::Chunk, chunk.to_vec()));
622
                    }
623
                }
624
                texts.push((ObjectType::Blob, t.blob.encode()));
625
            }
626
            changes.push(Value::Array(vec![Value::Bytes(bytes), sigs_value(s)]));
627
        }
628
        Value::map([
629
            (0, Value::Array(changes)),
630
            (
631
                1,
632
                Value::Array(b.attests.iter().map(attest_value).collect()),
633
            ),
634
            (
635
                2,
636
                Value::Array(
637
                    b.objects
638
                        .iter()
639
                        .chain(&texts)
640
                        .map(|(t, body)| {
641
                            Value::Array(vec![
642
                                Value::Text(t.name().into()),
643
                                Value::Bytes(body.clone()),
644
                            ])
645
                        })
646
                        .collect(),
647
                ),
648
            ),
649
            (
650
                3,
651
                Value::Array(b.policies.iter().map(policy_value).collect()),
652
            ),
653
        ])
654
    }
655
656
    pub fn bundle_from_value(v: &Value) -> Result<Bundle> {
657
        let m = map(v, "bundle")?;
658
        let mut b = Bundle::default();
659
        for ob in o::array(field(m, 2, "objects")?, "objects")? {
660
            let pair = o::array(ob, "object")?;
661
            if pair.len() != 2 {
662
                return Err(schema("object: expected [type, body]"));
663
            }
664
            let ty = ObjectType::from_name(o::text(&pair[0], "object type")?)
665
                .ok_or_else(|| schema("unknown object type"))?;
666
            b.objects
667
                .push((ty, o::bytes(&pair[1], "object body")?.to_vec()));
668
        }
669
        // Stored inserts are read from the bundle's own objects.
670
        let mut by_ref: Option<std::collections::HashMap<ObjectRef, usize>> = None;
671
        for c in o::array(field(m, 0, "changes")?, "changes")? {
672
            let pair = o::array(c, "change entry")?;
673
            if pair.len() != 2 {
674
                return Err(schema("change entry: expected [change, sigs]"));
675
            }
676
            let encoded = o::bytes(&pair[0], "change")?;
677
            b.hashes
678
                .push(crate::hash::hash_object(ObjectType::Change, encoded));
679
            let change = Change::decode_with(encoded, &mut |text, count, form| {
680
                let by_ref = by_ref.get_or_insert_with(|| {
681
                    b.objects
682
                        .iter()
683
                        .enumerate()
684
                        .map(|(i, (ty, body))| (crate::hash::hash_object(*ty, body), i))
685
                        .collect()
686
                });
687
                let get =
688
                    |r: &ObjectRef, want: ObjectType| match by_ref.get(r).map(|i| &b.objects[*i]) {
689
                        Some((ty, body)) if *ty == want => Ok(body),
690
                        _ => Err(ObjectError::Schema(format!(
691
                            "the bundle lacks {} {}",
692
                            want.name(),
693
                            r.short(12)
694
                        ))),
695
                    };
696
                let blob = crate::objects::Blob::decode(get(text, ObjectType::Blob)?)?;
697
                let mut content = Vec::with_capacity(blob.length as usize);
698
                for chunk in &blob.chunks {
699
                    content.extend_from_slice(get(chunk, ObjectType::Chunk)?);
700
                }
701
                crate::objects::text_lines(&content, count, form)
702
            })?;
703
            b.changes.push((change, sigs_from(&pair[1])?));
704
        }
705
        for a in o::array(field(m, 1, "attests")?, "attests")? {
706
            b.attests.push(attest_from(a)?);
707
        }
708
        if let Some(ps) = m.get(&3) {
709
            for p in o::array(ps, "policies")? {
710
                b.policies.push(policy_from(p)?);
711
            }
712
        }
713
        Ok(b)
714
    }
715
716
    pub fn encode_bundle(b: &Bundle) -> Vec<u8> {
717
        cbor::encode(&bundle_value(b))
718
    }
719
    pub fn decode_bundle(bytes: &[u8]) -> Result<Bundle> {
720
        bundle_from_value(&cbor::decode(bytes).map_err(ObjectError::from)?)
721
    }
722
723
    pub fn encode_capabilities(id: [u8; 16]) -> Vec<u8> {
724
        cbor::encode(&Value::map([
725
            (0, Value::Array(vec![Value::Uint(PROTOCOL_VERSION)])),
726
            (1, Value::Bytes(id.to_vec())),
727
            (2, Value::Array(vec![Value::Text("seal".into())])),
728
        ]))
729
    }
730
    pub fn decode_capabilities(bytes: &[u8]) -> Result<[u8; 16]> {
731
        let v = cbor::decode(bytes).map_err(ObjectError::from)?;
732
        let m = map(&v, "capabilities")?;
733
        let versions = o::array(field(m, 0, "versions")?, "versions")?;
734
        if !versions
735
            .iter()
736
            .any(|x| x.as_uint() == Some(PROTOCOL_VERSION))
737
        {
738
            return Err(RepoError::Msg(
739
                "remote does not speak protocol version 1".into(),
740
            ));
741
        }
742
        o::bytes(field(m, 1, "repo id")?, "repo id")?
743
            .try_into()
744
            .map_err(|_| schema("repo id: expected 16 bytes"))
745
    }
746
747
    fn tags_value(tags: &[TagRef]) -> Value {
748
        Value::Array(
749
            tags.iter()
750
                .map(|t| {
751
                    Value::map([
752
                        (0, Value::Text(t.name.clone())),
753
                        (1, Value::Bytes(t.state.0.to_vec())),
754
                        (2, hashes_value(&t.heads)),
755
                        (3, opt_attest_value(&t.attestation)),
756
                    ])
757
                })
758
                .collect(),
759
        )
760
    }
761
    fn tags_from(v: &Value) -> Result<Vec<TagRef>> {
762
        let mut out = Vec::new();
763
        for t in o::array(v, "tags")? {
764
            let tm = map(t, "tag")?;
765
            out.push(TagRef {
766
                name: o::text(field(tm, 0, "name")?, "name")?.to_owned(),
767
                state: o::oref(field(tm, 1, "state")?, "state")?,
768
                heads: hashes_from(field(tm, 2, "heads")?)?,
769
                attestation: opt_attest_from(field(tm, 3, "attestation")?)?,
770
            });
771
        }
772
        Ok(out)
773
    }
774
775
    pub fn encode_refs(id: [u8; 16], resp: &RefsResponse) -> Vec<u8> {
776
        cbor::encode(&Value::map([
777
            (
778
                0,
779
                Value::Array(
780
                    resp.refs
781
                        .iter()
782
                        .map(|r| {
783
                            Value::map([
784
                                (0, Value::Text(r.name.clone())),
785
                                (1, Value::Bytes(r.state.0.to_vec())),
786
                                (2, hashes_value(&r.heads)),
787
                                (3, opt_attest_value(&r.attestation)),
788
                                (4, r.last_serial.map(Value::Uint).unwrap_or(Value::Null)),
789
                            ])
790
                        })
791
                        .collect(),
792
                ),
793
            ),
794
            (1, Value::Bytes(id.to_vec())),
795
            (2, opt_policy_value(resp.policy.as_ref())),
796
            (3, tags_value(&resp.tags)),
797
            (
798
                4,
799
                Value::Array(resp.history.iter().map(policy_value).collect()),
800
            ),
801
            (
802
                5,
803
                resp.default_bookmark
804
                    .clone()
805
                    .map(Value::Text)
806
                    .unwrap_or(Value::Null),
807
            ),
808
        ]))
809
    }
810
    pub fn decode_refs(bytes: &[u8]) -> Result<([u8; 16], RefsResponse)> {
811
        let v = cbor::decode(bytes).map_err(ObjectError::from)?;
812
        let m = map(&v, "refs")?;
813
        let id: [u8; 16] = o::bytes(field(m, 1, "repo id")?, "repo id")?
814
            .try_into()
815
            .map_err(|_| schema("repo id: expected 16 bytes"))?;
816
        let mut refs = Vec::new();
817
        for r in o::array(field(m, 0, "refs")?, "refs")? {
818
            let rm = map(r, "ref")?;
819
            let ls = field(rm, 4, "last serial")?;
820
            refs.push(RemoteRef {
821
                name: o::text(field(rm, 0, "name")?, "name")?.to_owned(),
822
                state: o::oref(field(rm, 1, "state")?, "state")?,
823
                heads: hashes_from(field(rm, 2, "heads")?)?,
824
                attestation: opt_attest_from(field(rm, 3, "attestation")?)?,
825
                last_serial: if ls.is_null() {
826
                    None
827
                } else {
828
                    Some(o::uint(ls, "last serial")?)
829
                },
830
            });
831
        }
832
        let policy = match m.get(&2) {
833
            Some(p) => opt_policy_from(p)?,
834
            None => None,
835
        };
836
        let tags = match m.get(&3) {
837
            Some(t) => tags_from(t)?,
838
            None => Vec::new(),
839
        };
840
        let mut history = Vec::new();
841
        if let Some(h) = m.get(&4) {
842
            for p in o::array(h, "history")? {
843
                history.push(policy_from(p)?);
844
            }
845
        }
846
        let default_bookmark = match m.get(&5) {
847
            Some(Value::Text(t)) => Some(t.clone()),
848
            _ => None,
849
        };
850
        Ok((
851
            id,
852
            RefsResponse {
853
                refs,
854
                policy,
855
                tags,
856
                history,
857
                default_bookmark,
858
            },
859
        ))
860
    }
861
862
    pub fn encode_negotiate(have: &[ChangeHash], want: &[ChangeHash]) -> Vec<u8> {
863
        cbor::encode(&Value::map([
864
            (0, hashes_value(have)),
865
            (1, hashes_value(want)),
866
        ]))
867
    }
868
    pub fn decode_negotiate(bytes: &[u8]) -> Result<(Vec<ChangeHash>, Vec<ChangeHash>)> {
869
        let v = cbor::decode(bytes).map_err(ObjectError::from)?;
870
        let m = map(&v, "negotiate")?;
871
        Ok((
872
            hashes_from(field(m, 0, "have")?)?,
873
            hashes_from(field(m, 1, "want")?)?,
874
        ))
875
    }
876
877
    /// `fetch` request: `{0: [changes], 1: max blob size or null}`.
878
    pub fn encode_fetch(hs: &[ChangeHash], max_blob: Option<u64>) -> Vec<u8> {
879
        cbor::encode(&Value::map([
880
            (0, hashes_value(hs)),
881
            (1, max_blob.map(Value::Uint).unwrap_or(Value::Null)),
882
        ]))
883
    }
884
    pub fn decode_fetch(bytes: &[u8]) -> Result<(Vec<ChangeHash>, Option<u64>)> {
885
        let v = cbor::decode(bytes).map_err(ObjectError::from)?;
886
        let m = map(&v, "fetch")?;
887
        let max = match m.get(&1) {
888
            Some(x) if !x.is_null() => Some(o::uint(x, "max blob")?),
889
            _ => None,
890
        };
891
        Ok((hashes_from(field(m, 0, "changes")?)?, max))
892
    }
893
894
    /// `shallow` request: `{0: [heads], 1: depth}`.
895
    pub fn encode_shallow(hs: &[ChangeHash], depth: u32) -> Vec<u8> {
896
        cbor::encode(&Value::map([
897
            (0, hashes_value(hs)),
898
            (1, Value::Uint(depth as u64)),
899
        ]))
900
    }
901
    pub fn decode_shallow(bytes: &[u8]) -> Result<(Vec<ChangeHash>, u32)> {
902
        let v = cbor::decode(bytes).map_err(ObjectError::from)?;
903
        let m = map(&v, "shallow")?;
904
        Ok((
905
            hashes_from(field(m, 0, "heads")?)?,
906
            o::u32_(field(m, 1, "depth")?, "depth")?,
907
        ))
908
    }
909
910
    pub fn encode_hashes(hs: &[ChangeHash]) -> Vec<u8> {
911
        cbor::encode(&Value::map([(0, hashes_value(hs))]))
912
    }
913
    pub fn decode_hashes(bytes: &[u8]) -> Result<Vec<ChangeHash>> {
914
        let v = cbor::decode(bytes).map_err(ObjectError::from)?;
915
        hashes_from(field(map(&v, "hashes")?, 0, "hashes")?)
916
    }
917
918
    pub fn encode_push(
919
        bundle: &Bundle,
920
        updates: &[RefUpdate],
921
        policy: Option<&SignedPolicy>,
922
        tags: &[TagRef],
923
    ) -> Vec<u8> {
924
        cbor::encode(&Value::map([
925
            (0, bundle_value(bundle)),
926
            (2, opt_policy_value(policy)),
927
            (3, tags_value(tags)),
928
            (
929
                1,
930
                Value::Array(
931
                    updates
932
                        .iter()
933
                        .map(|u| {
934
                            Value::map([
935
                                (0, Value::Text(u.name.clone())),
936
                                (1, Value::Bytes(u.old_state.0.to_vec())),
937
                                (2, Value::Bytes(u.new_state.0.to_vec())),
938
                                (3, Value::Bool(u.force)),
939
                                (4, opt_attest_value(&u.attestation)),
940
                            ])
941
                        })
942
                        .collect(),
943
                ),
944
            ),
945
        ]))
946
    }
947
    /// A decoded push: bundle, bookmark updates, policy, tags.
948
    pub type PushParts = (Bundle, Vec<RefUpdate>, Option<SignedPolicy>, Vec<TagRef>);
949
950
    pub fn decode_push(bytes: &[u8]) -> Result<PushParts> {
951
        let v = cbor::decode(bytes).map_err(ObjectError::from)?;
952
        let m = map(&v, "push")?;
953
        let bundle = bundle_from_value(field(m, 0, "bundle")?)?;
954
        let mut updates = Vec::new();
955
        for u in o::array(field(m, 1, "updates")?, "updates")? {
956
            let um = map(u, "update")?;
957
            updates.push(RefUpdate {
958
                name: o::text(field(um, 0, "name")?, "name")?.to_owned(),
959
                old_state: o::oref(field(um, 1, "old_state")?, "old_state")?,
960
                new_state: o::oref(field(um, 2, "new_state")?, "new_state")?,
961
                force: field(um, 3, "force")?
962
                    .as_bool()
963
                    .ok_or_else(|| schema("force: expected bool"))?,
964
                attestation: opt_attest_from(field(um, 4, "attestation")?)?,
965
            });
966
        }
967
        let policy = match m.get(&2) {
968
            Some(p) => opt_policy_from(p)?,
969
            None => None,
970
        };
971
        let tags = match m.get(&3) {
972
            Some(t) => tags_from(t)?,
973
            None => Vec::new(),
974
        };
975
        Ok((bundle, updates, policy, tags))
976
    }
977
978
    pub fn encode_push_response(results: &[RefResult]) -> Vec<u8> {
979
        cbor::encode(&Value::map([(
980
            0,
981
            Value::Array(
982
                results
983
                    .iter()
984
                    .map(|r| {
985
                        Value::map([
986
                            (0, Value::Text(r.name.clone())),
987
                            (1, Value::Bool(r.ok)),
988
                            (2, Value::Text(r.reason.clone())),
989
                        ])
990
                    })
991
                    .collect(),
992
            ),
993
        )]))
994
    }
995
    pub fn decode_push_response(bytes: &[u8]) -> Result<Vec<RefResult>> {
996
        let v = cbor::decode(bytes).map_err(ObjectError::from)?;
997
        let m = map(&v, "push response")?;
998
        let mut out = Vec::new();
999
        for r in o::array(field(m, 0, "results")?, "results")? {
1000
            let rm = map(r, "result")?;
1001
            out.push(RefResult {
1002
                name: o::text(field(rm, 0, "name")?, "name")?.to_owned(),
1003
                ok: field(rm, 1, "ok")?
1004
                    .as_bool()
1005
                    .ok_or_else(|| schema("ok: expected bool"))?,
1006
                reason: o::text(field(rm, 2, "reason")?, "reason")?.to_owned(),
1007
            });
1008
        }
1009
        Ok(out)
1010
    }
1011
}
1012
1013
#[cfg(test)]
1014
mod tests {
1015
    use super::*;
1016
    use crate::attest::AttestBody;
1017
    use crate::objects::Hlc;
1018
1019
    #[test]
1020
    fn wire_round_trips() {
1021
        let c = crate::objects::tests::sample_change();
1022
        let sk = crate::identity::SecretKey {
1023
            ed_seed: [1; 32],
1024
            ml_seed: [2; 32],
1025
        };
1026
        let id = sk.identity("a", "b", 0);
1027
        let key = id.hash();
1028
        let sig = sk.sign(crate::identity::Domain::Change, &key, &c.hash());
1029
        let seal = Attest::seal([3; 16], vec![c.hash()]);
1030
        let seal_sig = sk.sign(crate::identity::Domain::Attest, &key, &seal.hash());
1031
        let policy = Policy::single_root([3; 16], id.clone(), 99);
1032
        let psig = sk.sign(crate::identity::Domain::Policy, &key, &policy.hash());
1033
        let bundle = Bundle {
1034
            changes: vec![(c.clone(), vec![sig.clone()])],
1035
            attests: vec![SignedAttest {
1036
                attest: seal.clone(),
1037
                sigs: vec![seal_sig],
1038
            }],
1039
            objects: vec![
1040
                (ObjectType::Identity, id.encode()),
1041
                (ObjectType::Chunk, b"xyz".to_vec()),
1042
            ],
1043
            policies: vec![SignedPolicy {
1044
                policy: policy.clone(),
1045
                sigs: vec![psig.clone()],
1046
            }],
1047
            hashes: vec![],
1048
            raw_changes: vec![],
1049
        };
1050
        let d = wire::decode_bundle(&wire::encode_bundle(&bundle)).unwrap();
1051
        assert_eq!(
1052
            d.hashes,
1053
            vec![c.hash()],
1054
            "the receiver hashes what it was sent"
1055
        );
1056
        assert_eq!(d.changes, bundle.changes);
1057
        assert_eq!(d.attests, bundle.attests);
1058
        assert_eq!(d.objects, bundle.objects);
1059
        assert_eq!(d.policies, bundle.policies);
1060
1061
        let att = Attest {
1062
            repo: [3; 16],
1063
            hlc: Hlc::default(),
1064
            body: AttestBody::Ref {
1065
                name: "main".into(),
1066
                old_state: ObjectRef([1; 32]),
1067
                new_state: ObjectRef([2; 32]),
1068
                serial: 4,
1069
            },
1070
        };
1071
        let refs = vec![
1072
            RemoteRef {
1073
                name: "main".into(),
1074
                state: ObjectRef([2; 32]),
1075
                heads: vec![c.hash()],
1076
                attestation: Some(SignedAttest {
1077
                    attest: att.clone(),
1078
                    sigs: vec![sig.clone()],
1079
                }),
1080
                last_serial: Some(4),
1081
            },
1082
            RemoteRef {
1083
                name: "dev".into(),
1084
                state: ObjectRef([5; 32]),
1085
                heads: vec![],
1086
                attestation: None,
1087
                last_serial: None,
1088
            },
1089
        ];
1090
        let tag = TagRef {
1091
            name: "v1".into(),
1092
            state: ObjectRef([2; 32]),
1093
            heads: vec![c.hash()],
1094
            attestation: None,
1095
        };
1096
        let resp = RefsResponse {
1097
            refs,
1098
            policy: Some(SignedPolicy {
1099
                policy: policy.clone(),
1100
                sigs: vec![psig.clone()],
1101
            }),
1102
            tags: vec![tag.clone()],
1103
            history: vec![SignedPolicy {
1104
                policy: policy.clone(),
1105
                sigs: vec![],
1106
            }],
1107
            default_bookmark: Some("develop".into()),
1108
        };
1109
        let (id_back, back) = wire::decode_refs(&wire::encode_refs([3; 16], &resp)).unwrap();
1110
        assert_eq!(id_back, [3; 16]);
1111
        assert_eq!(back.refs.len(), 2);
1112
        assert_eq!(back.refs[0].attestation.as_ref().unwrap().attest, att);
1113
        assert_eq!(back.refs[1].last_serial, None);
1114
        assert_eq!(back.policy.as_ref().unwrap().policy, policy);
1115
        assert_eq!(back.history, resp.history);
1116
        assert_eq!(back.tags, vec![tag.clone()]);
1117
1118
        let (h, w) = wire::decode_negotiate(&wire::encode_negotiate(
1119
            &[ObjectRef([1; 32])],
1120
            &[ObjectRef([2; 32])],
1121
        ))
1122
        .unwrap();
1123
        assert_eq!((h, w), (vec![ObjectRef([1; 32])], vec![ObjectRef([2; 32])]));
1124
        assert_eq!(
1125
            wire::decode_hashes(&wire::encode_hashes(&[c.hash()])).unwrap(),
1126
            vec![c.hash()]
1127
        );
1128
        assert_eq!(
1129
            wire::decode_fetch(&wire::encode_fetch(&[c.hash()], Some(4096))).unwrap(),
1130
            (vec![c.hash()], Some(4096))
1131
        );
1132
        assert_eq!(
1133
            wire::decode_fetch(&wire::encode_fetch(&[], None)).unwrap(),
1134
            (vec![], None)
1135
        );
1136
        assert_eq!(
1137
            wire::decode_shallow(&wire::encode_shallow(&[c.hash()], 3)).unwrap(),
1138
            (vec![c.hash()], 3)
1139
        );
1140
        // An old-style fetch request without the filter field still decodes.
1141
        assert_eq!(
1142
            wire::decode_fetch(&wire::encode_hashes(&[c.hash()])).unwrap(),
1143
            (vec![c.hash()], None)
1144
        );
1145
1146
        let up = RefUpdate {
1147
            name: "main".into(),
1148
            old_state: ObjectRef([1; 32]),
1149
            new_state: ObjectRef([2; 32]),
1150
            force: true,
1151
            attestation: Some(SignedAttest {
1152
                attest: att,
1153
                sigs: vec![sig],
1154
            }),
1155
        };
1156
        let sp = SignedPolicy {
1157
            policy,
1158
            sigs: vec![psig],
1159
        };
1160
        let (b2, ups, pol, tags) = wire::decode_push(&wire::encode_push(
1161
            &bundle,
1162
            std::slice::from_ref(&up),
1163
            Some(&sp),
1164
            std::slice::from_ref(&tag),
1165
        ))
1166
        .unwrap();
1167
        assert_eq!(pol, Some(sp));
1168
        assert_eq!(tags, vec![tag]);
1169
        assert_eq!(b2.changes.len(), 1);
1170
        assert_eq!(ups[0].name, "main");
1171
        assert!(ups[0].force);
1172
        assert_eq!(ups[0].attestation, up.attestation);
1173
        let res = vec![RefResult {
1174
            name: "main".into(),
1175
            ok: false,
1176
            reason: "stale".into(),
1177
        }];
1178
        assert_eq!(
1179
            wire::decode_push_response(&wire::encode_push_response(&res)).unwrap(),
1180
            res
1181
        );
1182
        assert_eq!(
1183
            wire::decode_capabilities(&wire::encode_capabilities([7; 16])).unwrap(),
1184
            [7; 16]
1185
        );
1186
    }
1187
}