OXYGENHUB
# a46f3e4f50b1 oxygen / crates / oxy-core / src / protocol.rs
43.7 KiB · Rust · last changed Clones and remote add store a local path absolute 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
/// A remote's URL as it is stored: an `http(s)://` URL as given, a local
497
/// path made absolute (with its `file://` kept), so it still names the same
498
/// repository from inside the clone (oxygen#57).
499
pub fn stored_url(url: &str) -> String {
500
    if url.starts_with("http://") || url.starts_with("https://") {
501
        return url.to_string();
502
    }
503
    let (prefix, path) = match url.strip_prefix("file://") {
504
        Some(p) => ("file://", p),
505
        None => ("", url),
506
    };
507
    match std::path::absolute(path) {
508
        Ok(abs) => format!("{prefix}{}", abs.display()),
509
        Err(_) => url.to_string(),
510
    }
511
}
512
513
/// Open a remote by URL: a local path (optionally `file://`), or `http(s)://`.
514
pub fn open_remote(url: &str, options: &RemoteOptions) -> Result<Box<dyn Remote>> {
515
    if url.starts_with("http://") || url.starts_with("https://") {
516
        return Ok(Box::new(HttpRemote::new(url, options)?));
517
    }
518
    let path = url.strip_prefix("file://").unwrap_or(url);
519
    if path.contains("://") {
520
        return Err(RepoError::Msg(format!("unsupported remote URL {url:?}")));
521
    }
522
    let mut local = LocalRemote::open(Path::new(path))?;
523
    local.claimed_id = options.repo_id;
524
    Ok(Box::new(local))
525
}
526
527
// ---------------------------------------------------------------- wire
528
529
/// CBOR encodings of the protocol messages (spec §16.1).
530
pub mod wire {
531
    use super::*;
532
533
    fn schema(msg: &str) -> RepoError {
534
        RepoError::Object(ObjectError::Schema(msg.into()))
535
    }
536
    fn sigs_value(sigs: &[Signature]) -> Value {
537
        Value::Array(sigs.iter().map(|s| s.to_value()).collect())
538
    }
539
    fn sigs_from(v: &Value) -> Result<Vec<Signature>> {
540
        Ok(o::array(v, "signatures")?
541
            .iter()
542
            .map(Signature::from_value)
543
            .collect::<std::result::Result<Vec<_>, _>>()?)
544
    }
545
    fn hashes_value(hs: &[ObjectRef]) -> Value {
546
        Value::Array(hs.iter().map(|h| Value::Bytes(h.0.to_vec())).collect())
547
    }
548
    fn hashes_from(v: &Value) -> Result<Vec<ObjectRef>> {
549
        Ok(o::array(v, "hashes")?
550
            .iter()
551
            .map(|h| o::oref(h, "hash"))
552
            .collect::<std::result::Result<Vec<_>, _>>()?)
553
    }
554
    fn attest_value(sa: &SignedAttest) -> Value {
555
        Value::Array(vec![Value::Bytes(sa.attest.encode()), sigs_value(&sa.sigs)])
556
    }
557
    fn attest_from(v: &Value) -> Result<SignedAttest> {
558
        let a = o::array(v, "signed attest")?;
559
        if a.len() != 2 {
560
            return Err(schema("signed attest: expected [attest, sigs]"));
561
        }
562
        Ok(SignedAttest {
563
            attest: Attest::decode(o::bytes(&a[0], "attest")?)?,
564
            sigs: sigs_from(&a[1])?,
565
        })
566
    }
567
    fn opt_attest_value(sa: &Option<SignedAttest>) -> Value {
568
        sa.as_ref().map(attest_value).unwrap_or(Value::Null)
569
    }
570
    fn opt_attest_from(v: &Value) -> Result<Option<SignedAttest>> {
571
        if v.is_null() {
572
            Ok(None)
573
        } else {
574
            attest_from(v).map(Some)
575
        }
576
    }
577
    /// `[policy bytes, [signature]]`: also the format of a policy proposal
578
    /// file exchanged between root holders (spec §14.5).
579
    pub fn policy_value(sp: &SignedPolicy) -> Value {
580
        Value::Array(vec![Value::Bytes(sp.policy.encode()), sigs_value(&sp.sigs)])
581
    }
582
    pub fn policy_from(v: &Value) -> Result<SignedPolicy> {
583
        let a = o::array(v, "signed policy")?;
584
        if a.len() != 2 {
585
            return Err(schema("signed policy: expected [policy, sigs]"));
586
        }
587
        Ok(SignedPolicy {
588
            policy: Policy::decode(o::bytes(&a[0], "policy")?)?,
589
            sigs: sigs_from(&a[1])?,
590
        })
591
    }
592
    fn opt_policy_value(sp: Option<&SignedPolicy>) -> Value {
593
        sp.map(policy_value).unwrap_or(Value::Null)
594
    }
595
    fn opt_policy_from(v: &Value) -> Result<Option<SignedPolicy>> {
596
        if v.is_null() {
597
            Ok(None)
598
        } else {
599
            policy_from(v).map(Some)
600
        }
601
    }
602
    fn map<'a>(v: &'a Value, what: &str) -> Result<&'a o::Map> {
603
        Ok(o::map(v, what)?)
604
    }
605
    fn field<'a>(m: &'a o::Map, k: u64, what: &str) -> Result<&'a Value> {
606
        Ok(o::field(m, k, what)?)
607
    }
608
609
    /// A bundle's value. Each change's text blobs and their chunks (spec
610
    /// §6.1) are added to its objects when they are not already there, so a
611
    /// bundle can always be read on its own.
612
    pub fn bundle_value(b: &Bundle) -> Value {
613
        let mut changes = Vec::with_capacity(b.raw_changes.len() + b.changes.len());
614
        // Changes as stored are already their encoding, their texts among
615
        // the objects.
616
        for (bytes, s) in &b.raw_changes {
617
            changes.push(Value::Array(vec![
618
                Value::Bytes(bytes.clone()),
619
                sigs_value(s),
620
            ]));
621
        }
622
        let mut texts: Vec<(ObjectType, Vec<u8>)> = Vec::new();
623
        let mut listed: Option<std::collections::HashSet<ObjectRef>> = None;
624
        for (c, s) in &b.changes {
625
            let (bytes, stored) = c.encode_parts();
626
            for t in stored {
627
                let listed = listed.get_or_insert_with(|| {
628
                    b.objects
629
                        .iter()
630
                        .map(|(ty, body)| crate::hash::hash_object(*ty, body))
631
                        .collect()
632
                });
633
                if !listed.insert(t.blob.hash()) {
634
                    continue;
635
                }
636
                for chunk in crate::chunk::chunks(&t.content) {
637
                    if listed.insert(crate::hash::hash_object(ObjectType::Chunk, chunk)) {
638
                        texts.push((ObjectType::Chunk, chunk.to_vec()));
639
                    }
640
                }
641
                texts.push((ObjectType::Blob, t.blob.encode()));
642
            }
643
            changes.push(Value::Array(vec![Value::Bytes(bytes), sigs_value(s)]));
644
        }
645
        Value::map([
646
            (0, Value::Array(changes)),
647
            (
648
                1,
649
                Value::Array(b.attests.iter().map(attest_value).collect()),
650
            ),
651
            (
652
                2,
653
                Value::Array(
654
                    b.objects
655
                        .iter()
656
                        .chain(&texts)
657
                        .map(|(t, body)| {
658
                            Value::Array(vec![
659
                                Value::Text(t.name().into()),
660
                                Value::Bytes(body.clone()),
661
                            ])
662
                        })
663
                        .collect(),
664
                ),
665
            ),
666
            (
667
                3,
668
                Value::Array(b.policies.iter().map(policy_value).collect()),
669
            ),
670
        ])
671
    }
672
673
    pub fn bundle_from_value(v: &Value) -> Result<Bundle> {
674
        let m = map(v, "bundle")?;
675
        let mut b = Bundle::default();
676
        for ob in o::array(field(m, 2, "objects")?, "objects")? {
677
            let pair = o::array(ob, "object")?;
678
            if pair.len() != 2 {
679
                return Err(schema("object: expected [type, body]"));
680
            }
681
            let ty = ObjectType::from_name(o::text(&pair[0], "object type")?)
682
                .ok_or_else(|| schema("unknown object type"))?;
683
            b.objects
684
                .push((ty, o::bytes(&pair[1], "object body")?.to_vec()));
685
        }
686
        // Stored inserts are read from the bundle's own objects.
687
        let mut by_ref: Option<std::collections::HashMap<ObjectRef, usize>> = None;
688
        for c in o::array(field(m, 0, "changes")?, "changes")? {
689
            let pair = o::array(c, "change entry")?;
690
            if pair.len() != 2 {
691
                return Err(schema("change entry: expected [change, sigs]"));
692
            }
693
            let encoded = o::bytes(&pair[0], "change")?;
694
            b.hashes
695
                .push(crate::hash::hash_object(ObjectType::Change, encoded));
696
            let change = Change::decode_with(encoded, &mut |text, count, form| {
697
                let by_ref = by_ref.get_or_insert_with(|| {
698
                    b.objects
699
                        .iter()
700
                        .enumerate()
701
                        .map(|(i, (ty, body))| (crate::hash::hash_object(*ty, body), i))
702
                        .collect()
703
                });
704
                let get =
705
                    |r: &ObjectRef, want: ObjectType| match by_ref.get(r).map(|i| &b.objects[*i]) {
706
                        Some((ty, body)) if *ty == want => Ok(body),
707
                        _ => Err(ObjectError::Schema(format!(
708
                            "the bundle lacks {} {}",
709
                            want.name(),
710
                            r.short(12)
711
                        ))),
712
                    };
713
                let blob = crate::objects::Blob::decode(get(text, ObjectType::Blob)?)?;
714
                let mut content = Vec::with_capacity(blob.length as usize);
715
                for chunk in &blob.chunks {
716
                    content.extend_from_slice(get(chunk, ObjectType::Chunk)?);
717
                }
718
                crate::objects::text_lines(&content, count, form)
719
            })?;
720
            b.changes.push((change, sigs_from(&pair[1])?));
721
        }
722
        for a in o::array(field(m, 1, "attests")?, "attests")? {
723
            b.attests.push(attest_from(a)?);
724
        }
725
        if let Some(ps) = m.get(&3) {
726
            for p in o::array(ps, "policies")? {
727
                b.policies.push(policy_from(p)?);
728
            }
729
        }
730
        Ok(b)
731
    }
732
733
    pub fn encode_bundle(b: &Bundle) -> Vec<u8> {
734
        cbor::encode(&bundle_value(b))
735
    }
736
    pub fn decode_bundle(bytes: &[u8]) -> Result<Bundle> {
737
        bundle_from_value(&cbor::decode(bytes).map_err(ObjectError::from)?)
738
    }
739
740
    pub fn encode_capabilities(id: [u8; 16]) -> Vec<u8> {
741
        cbor::encode(&Value::map([
742
            (0, Value::Array(vec![Value::Uint(PROTOCOL_VERSION)])),
743
            (1, Value::Bytes(id.to_vec())),
744
            (2, Value::Array(vec![Value::Text("seal".into())])),
745
        ]))
746
    }
747
    pub fn decode_capabilities(bytes: &[u8]) -> Result<[u8; 16]> {
748
        let v = cbor::decode(bytes).map_err(ObjectError::from)?;
749
        let m = map(&v, "capabilities")?;
750
        let versions = o::array(field(m, 0, "versions")?, "versions")?;
751
        if !versions
752
            .iter()
753
            .any(|x| x.as_uint() == Some(PROTOCOL_VERSION))
754
        {
755
            return Err(RepoError::Msg(
756
                "remote does not speak protocol version 1".into(),
757
            ));
758
        }
759
        o::bytes(field(m, 1, "repo id")?, "repo id")?
760
            .try_into()
761
            .map_err(|_| schema("repo id: expected 16 bytes"))
762
    }
763
764
    fn tags_value(tags: &[TagRef]) -> Value {
765
        Value::Array(
766
            tags.iter()
767
                .map(|t| {
768
                    Value::map([
769
                        (0, Value::Text(t.name.clone())),
770
                        (1, Value::Bytes(t.state.0.to_vec())),
771
                        (2, hashes_value(&t.heads)),
772
                        (3, opt_attest_value(&t.attestation)),
773
                    ])
774
                })
775
                .collect(),
776
        )
777
    }
778
    fn tags_from(v: &Value) -> Result<Vec<TagRef>> {
779
        let mut out = Vec::new();
780
        for t in o::array(v, "tags")? {
781
            let tm = map(t, "tag")?;
782
            out.push(TagRef {
783
                name: o::text(field(tm, 0, "name")?, "name")?.to_owned(),
784
                state: o::oref(field(tm, 1, "state")?, "state")?,
785
                heads: hashes_from(field(tm, 2, "heads")?)?,
786
                attestation: opt_attest_from(field(tm, 3, "attestation")?)?,
787
            });
788
        }
789
        Ok(out)
790
    }
791
792
    pub fn encode_refs(id: [u8; 16], resp: &RefsResponse) -> Vec<u8> {
793
        cbor::encode(&Value::map([
794
            (
795
                0,
796
                Value::Array(
797
                    resp.refs
798
                        .iter()
799
                        .map(|r| {
800
                            Value::map([
801
                                (0, Value::Text(r.name.clone())),
802
                                (1, Value::Bytes(r.state.0.to_vec())),
803
                                (2, hashes_value(&r.heads)),
804
                                (3, opt_attest_value(&r.attestation)),
805
                                (4, r.last_serial.map(Value::Uint).unwrap_or(Value::Null)),
806
                            ])
807
                        })
808
                        .collect(),
809
                ),
810
            ),
811
            (1, Value::Bytes(id.to_vec())),
812
            (2, opt_policy_value(resp.policy.as_ref())),
813
            (3, tags_value(&resp.tags)),
814
            (
815
                4,
816
                Value::Array(resp.history.iter().map(policy_value).collect()),
817
            ),
818
            (
819
                5,
820
                resp.default_bookmark
821
                    .clone()
822
                    .map(Value::Text)
823
                    .unwrap_or(Value::Null),
824
            ),
825
        ]))
826
    }
827
    pub fn decode_refs(bytes: &[u8]) -> Result<([u8; 16], RefsResponse)> {
828
        let v = cbor::decode(bytes).map_err(ObjectError::from)?;
829
        let m = map(&v, "refs")?;
830
        let id: [u8; 16] = o::bytes(field(m, 1, "repo id")?, "repo id")?
831
            .try_into()
832
            .map_err(|_| schema("repo id: expected 16 bytes"))?;
833
        let mut refs = Vec::new();
834
        for r in o::array(field(m, 0, "refs")?, "refs")? {
835
            let rm = map(r, "ref")?;
836
            let ls = field(rm, 4, "last serial")?;
837
            refs.push(RemoteRef {
838
                name: o::text(field(rm, 0, "name")?, "name")?.to_owned(),
839
                state: o::oref(field(rm, 1, "state")?, "state")?,
840
                heads: hashes_from(field(rm, 2, "heads")?)?,
841
                attestation: opt_attest_from(field(rm, 3, "attestation")?)?,
842
                last_serial: if ls.is_null() {
843
                    None
844
                } else {
845
                    Some(o::uint(ls, "last serial")?)
846
                },
847
            });
848
        }
849
        let policy = match m.get(&2) {
850
            Some(p) => opt_policy_from(p)?,
851
            None => None,
852
        };
853
        let tags = match m.get(&3) {
854
            Some(t) => tags_from(t)?,
855
            None => Vec::new(),
856
        };
857
        let mut history = Vec::new();
858
        if let Some(h) = m.get(&4) {
859
            for p in o::array(h, "history")? {
860
                history.push(policy_from(p)?);
861
            }
862
        }
863
        let default_bookmark = match m.get(&5) {
864
            Some(Value::Text(t)) => Some(t.clone()),
865
            _ => None,
866
        };
867
        Ok((
868
            id,
869
            RefsResponse {
870
                refs,
871
                policy,
872
                tags,
873
                history,
874
                default_bookmark,
875
            },
876
        ))
877
    }
878
879
    pub fn encode_negotiate(have: &[ChangeHash], want: &[ChangeHash]) -> Vec<u8> {
880
        cbor::encode(&Value::map([
881
            (0, hashes_value(have)),
882
            (1, hashes_value(want)),
883
        ]))
884
    }
885
    pub fn decode_negotiate(bytes: &[u8]) -> Result<(Vec<ChangeHash>, Vec<ChangeHash>)> {
886
        let v = cbor::decode(bytes).map_err(ObjectError::from)?;
887
        let m = map(&v, "negotiate")?;
888
        Ok((
889
            hashes_from(field(m, 0, "have")?)?,
890
            hashes_from(field(m, 1, "want")?)?,
891
        ))
892
    }
893
894
    /// `fetch` request: `{0: [changes], 1: max blob size or null}`.
895
    pub fn encode_fetch(hs: &[ChangeHash], max_blob: Option<u64>) -> Vec<u8> {
896
        cbor::encode(&Value::map([
897
            (0, hashes_value(hs)),
898
            (1, max_blob.map(Value::Uint).unwrap_or(Value::Null)),
899
        ]))
900
    }
901
    pub fn decode_fetch(bytes: &[u8]) -> Result<(Vec<ChangeHash>, Option<u64>)> {
902
        let v = cbor::decode(bytes).map_err(ObjectError::from)?;
903
        let m = map(&v, "fetch")?;
904
        let max = match m.get(&1) {
905
            Some(x) if !x.is_null() => Some(o::uint(x, "max blob")?),
906
            _ => None,
907
        };
908
        Ok((hashes_from(field(m, 0, "changes")?)?, max))
909
    }
910
911
    /// `shallow` request: `{0: [heads], 1: depth}`.
912
    pub fn encode_shallow(hs: &[ChangeHash], depth: u32) -> Vec<u8> {
913
        cbor::encode(&Value::map([
914
            (0, hashes_value(hs)),
915
            (1, Value::Uint(depth as u64)),
916
        ]))
917
    }
918
    pub fn decode_shallow(bytes: &[u8]) -> Result<(Vec<ChangeHash>, u32)> {
919
        let v = cbor::decode(bytes).map_err(ObjectError::from)?;
920
        let m = map(&v, "shallow")?;
921
        Ok((
922
            hashes_from(field(m, 0, "heads")?)?,
923
            o::u32_(field(m, 1, "depth")?, "depth")?,
924
        ))
925
    }
926
927
    pub fn encode_hashes(hs: &[ChangeHash]) -> Vec<u8> {
928
        cbor::encode(&Value::map([(0, hashes_value(hs))]))
929
    }
930
    pub fn decode_hashes(bytes: &[u8]) -> Result<Vec<ChangeHash>> {
931
        let v = cbor::decode(bytes).map_err(ObjectError::from)?;
932
        hashes_from(field(map(&v, "hashes")?, 0, "hashes")?)
933
    }
934
935
    pub fn encode_push(
936
        bundle: &Bundle,
937
        updates: &[RefUpdate],
938
        policy: Option<&SignedPolicy>,
939
        tags: &[TagRef],
940
    ) -> Vec<u8> {
941
        cbor::encode(&Value::map([
942
            (0, bundle_value(bundle)),
943
            (2, opt_policy_value(policy)),
944
            (3, tags_value(tags)),
945
            (
946
                1,
947
                Value::Array(
948
                    updates
949
                        .iter()
950
                        .map(|u| {
951
                            Value::map([
952
                                (0, Value::Text(u.name.clone())),
953
                                (1, Value::Bytes(u.old_state.0.to_vec())),
954
                                (2, Value::Bytes(u.new_state.0.to_vec())),
955
                                (3, Value::Bool(u.force)),
956
                                (4, opt_attest_value(&u.attestation)),
957
                            ])
958
                        })
959
                        .collect(),
960
                ),
961
            ),
962
        ]))
963
    }
964
    /// A decoded push: bundle, bookmark updates, policy, tags.
965
    pub type PushParts = (Bundle, Vec<RefUpdate>, Option<SignedPolicy>, Vec<TagRef>);
966
967
    pub fn decode_push(bytes: &[u8]) -> Result<PushParts> {
968
        let v = cbor::decode(bytes).map_err(ObjectError::from)?;
969
        let m = map(&v, "push")?;
970
        let bundle = bundle_from_value(field(m, 0, "bundle")?)?;
971
        let mut updates = Vec::new();
972
        for u in o::array(field(m, 1, "updates")?, "updates")? {
973
            let um = map(u, "update")?;
974
            updates.push(RefUpdate {
975
                name: o::text(field(um, 0, "name")?, "name")?.to_owned(),
976
                old_state: o::oref(field(um, 1, "old_state")?, "old_state")?,
977
                new_state: o::oref(field(um, 2, "new_state")?, "new_state")?,
978
                force: field(um, 3, "force")?
979
                    .as_bool()
980
                    .ok_or_else(|| schema("force: expected bool"))?,
981
                attestation: opt_attest_from(field(um, 4, "attestation")?)?,
982
            });
983
        }
984
        let policy = match m.get(&2) {
985
            Some(p) => opt_policy_from(p)?,
986
            None => None,
987
        };
988
        let tags = match m.get(&3) {
989
            Some(t) => tags_from(t)?,
990
            None => Vec::new(),
991
        };
992
        Ok((bundle, updates, policy, tags))
993
    }
994
995
    pub fn encode_push_response(results: &[RefResult]) -> Vec<u8> {
996
        cbor::encode(&Value::map([(
997
            0,
998
            Value::Array(
999
                results
1000
                    .iter()
1001
                    .map(|r| {
1002
                        Value::map([
1003
                            (0, Value::Text(r.name.clone())),
1004
                            (1, Value::Bool(r.ok)),
1005
                            (2, Value::Text(r.reason.clone())),
1006
                        ])
1007
                    })
1008
                    .collect(),
1009
            ),
1010
        )]))
1011
    }
1012
    pub fn decode_push_response(bytes: &[u8]) -> Result<Vec<RefResult>> {
1013
        let v = cbor::decode(bytes).map_err(ObjectError::from)?;
1014
        let m = map(&v, "push response")?;
1015
        let mut out = Vec::new();
1016
        for r in o::array(field(m, 0, "results")?, "results")? {
1017
            let rm = map(r, "result")?;
1018
            out.push(RefResult {
1019
                name: o::text(field(rm, 0, "name")?, "name")?.to_owned(),
1020
                ok: field(rm, 1, "ok")?
1021
                    .as_bool()
1022
                    .ok_or_else(|| schema("ok: expected bool"))?,
1023
                reason: o::text(field(rm, 2, "reason")?, "reason")?.to_owned(),
1024
            });
1025
        }
1026
        Ok(out)
1027
    }
1028
}
1029
1030
#[cfg(test)]
1031
mod tests {
1032
    use super::*;
1033
    use crate::attest::AttestBody;
1034
    use crate::objects::Hlc;
1035
1036
    #[test]
1037
    fn wire_round_trips() {
1038
        let c = crate::objects::tests::sample_change();
1039
        let sk = crate::identity::SecretKey {
1040
            ed_seed: [1; 32],
1041
            ml_seed: [2; 32],
1042
        };
1043
        let id = sk.identity("a", "b", 0);
1044
        let key = id.hash();
1045
        let sig = sk.sign(crate::identity::Domain::Change, &key, &c.hash());
1046
        let seal = Attest::seal([3; 16], vec![c.hash()]);
1047
        let seal_sig = sk.sign(crate::identity::Domain::Attest, &key, &seal.hash());
1048
        let policy = Policy::single_root([3; 16], id.clone(), 99);
1049
        let psig = sk.sign(crate::identity::Domain::Policy, &key, &policy.hash());
1050
        let bundle = Bundle {
1051
            changes: vec![(c.clone(), vec![sig.clone()])],
1052
            attests: vec![SignedAttest {
1053
                attest: seal.clone(),
1054
                sigs: vec![seal_sig],
1055
            }],
1056
            objects: vec![
1057
                (ObjectType::Identity, id.encode()),
1058
                (ObjectType::Chunk, b"xyz".to_vec()),
1059
            ],
1060
            policies: vec![SignedPolicy {
1061
                policy: policy.clone(),
1062
                sigs: vec![psig.clone()],
1063
            }],
1064
            hashes: vec![],
1065
            raw_changes: vec![],
1066
        };
1067
        let d = wire::decode_bundle(&wire::encode_bundle(&bundle)).unwrap();
1068
        assert_eq!(
1069
            d.hashes,
1070
            vec![c.hash()],
1071
            "the receiver hashes what it was sent"
1072
        );
1073
        assert_eq!(d.changes, bundle.changes);
1074
        assert_eq!(d.attests, bundle.attests);
1075
        assert_eq!(d.objects, bundle.objects);
1076
        assert_eq!(d.policies, bundle.policies);
1077
1078
        let att = Attest {
1079
            repo: [3; 16],
1080
            hlc: Hlc::default(),
1081
            body: AttestBody::Ref {
1082
                name: "main".into(),
1083
                old_state: ObjectRef([1; 32]),
1084
                new_state: ObjectRef([2; 32]),
1085
                serial: 4,
1086
            },
1087
        };
1088
        let refs = vec![
1089
            RemoteRef {
1090
                name: "main".into(),
1091
                state: ObjectRef([2; 32]),
1092
                heads: vec![c.hash()],
1093
                attestation: Some(SignedAttest {
1094
                    attest: att.clone(),
1095
                    sigs: vec![sig.clone()],
1096
                }),
1097
                last_serial: Some(4),
1098
            },
1099
            RemoteRef {
1100
                name: "dev".into(),
1101
                state: ObjectRef([5; 32]),
1102
                heads: vec![],
1103
                attestation: None,
1104
                last_serial: None,
1105
            },
1106
        ];
1107
        let tag = TagRef {
1108
            name: "v1".into(),
1109
            state: ObjectRef([2; 32]),
1110
            heads: vec![c.hash()],
1111
            attestation: None,
1112
        };
1113
        let resp = RefsResponse {
1114
            refs,
1115
            policy: Some(SignedPolicy {
1116
                policy: policy.clone(),
1117
                sigs: vec![psig.clone()],
1118
            }),
1119
            tags: vec![tag.clone()],
1120
            history: vec![SignedPolicy {
1121
                policy: policy.clone(),
1122
                sigs: vec![],
1123
            }],
1124
            default_bookmark: Some("develop".into()),
1125
        };
1126
        let (id_back, back) = wire::decode_refs(&wire::encode_refs([3; 16], &resp)).unwrap();
1127
        assert_eq!(id_back, [3; 16]);
1128
        assert_eq!(back.refs.len(), 2);
1129
        assert_eq!(back.refs[0].attestation.as_ref().unwrap().attest, att);
1130
        assert_eq!(back.refs[1].last_serial, None);
1131
        assert_eq!(back.policy.as_ref().unwrap().policy, policy);
1132
        assert_eq!(back.history, resp.history);
1133
        assert_eq!(back.tags, vec![tag.clone()]);
1134
1135
        let (h, w) = wire::decode_negotiate(&wire::encode_negotiate(
1136
            &[ObjectRef([1; 32])],
1137
            &[ObjectRef([2; 32])],
1138
        ))
1139
        .unwrap();
1140
        assert_eq!((h, w), (vec![ObjectRef([1; 32])], vec![ObjectRef([2; 32])]));
1141
        assert_eq!(
1142
            wire::decode_hashes(&wire::encode_hashes(&[c.hash()])).unwrap(),
1143
            vec![c.hash()]
1144
        );
1145
        assert_eq!(
1146
            wire::decode_fetch(&wire::encode_fetch(&[c.hash()], Some(4096))).unwrap(),
1147
            (vec![c.hash()], Some(4096))
1148
        );
1149
        assert_eq!(
1150
            wire::decode_fetch(&wire::encode_fetch(&[], None)).unwrap(),
1151
            (vec![], None)
1152
        );
1153
        assert_eq!(
1154
            wire::decode_shallow(&wire::encode_shallow(&[c.hash()], 3)).unwrap(),
1155
            (vec![c.hash()], 3)
1156
        );
1157
        // An old-style fetch request without the filter field still decodes.
1158
        assert_eq!(
1159
            wire::decode_fetch(&wire::encode_hashes(&[c.hash()])).unwrap(),
1160
            (vec![c.hash()], None)
1161
        );
1162
1163
        let up = RefUpdate {
1164
            name: "main".into(),
1165
            old_state: ObjectRef([1; 32]),
1166
            new_state: ObjectRef([2; 32]),
1167
            force: true,
1168
            attestation: Some(SignedAttest {
1169
                attest: att,
1170
                sigs: vec![sig],
1171
            }),
1172
        };
1173
        let sp = SignedPolicy {
1174
            policy,
1175
            sigs: vec![psig],
1176
        };
1177
        let (b2, ups, pol, tags) = wire::decode_push(&wire::encode_push(
1178
            &bundle,
1179
            std::slice::from_ref(&up),
1180
            Some(&sp),
1181
            std::slice::from_ref(&tag),
1182
        ))
1183
        .unwrap();
1184
        assert_eq!(pol, Some(sp));
1185
        assert_eq!(tags, vec![tag]);
1186
        assert_eq!(b2.changes.len(), 1);
1187
        assert_eq!(ups[0].name, "main");
1188
        assert!(ups[0].force);
1189
        assert_eq!(ups[0].attestation, up.attestation);
1190
        let res = vec![RefResult {
1191
            name: "main".into(),
1192
            ok: false,
1193
            reason: "stale".into(),
1194
        }];
1195
        assert_eq!(
1196
            wire::decode_push_response(&wire::encode_push_response(&res)).unwrap(),
1197
            res
1198
        );
1199
        assert_eq!(
1200
            wire::decode_capabilities(&wire::encode_capabilities([7; 16])).unwrap(),
1201
            [7; 16]
1202
        );
1203
    }
1204
}