5b9c6ad4 Andy McCall 1 month ago ↶
1
2
750251b2 Andy McCall 1 month ago ↶
3
4
5
6
7
5b9c6ad4 Andy McCall 1 month ago ↶
8
9
use crate :: attest:: Attest;
750251b2 Andy McCall 1 month ago ↶
10
use crate :: cbor:: { self , Value} ;
5b9c6ad4 Andy McCall 1 month ago ↶
11
use crate :: hash:: { ChangeHash, ObjectRef, ObjectType} ;
12
use crate :: identity:: Signature;
750251b2 Andy McCall 1 month ago ↶
13
use crate :: objects:: { self as o, Change, ObjectError} ;
83e0677b Andy McCall 1 month ago ↶
14
use crate :: policy:: Policy;
5b9c6ad4 Andy McCall 1 month ago ↶
15
use crate :: repo:: { Repo, RepoError, Result } ;
2b5ee371 Andy McCall 1 month ago ↶
16
use std:: path:: { Path, PathBuf} ;
17
use std:: sync:: Arc;
5b9c6ad4 Andy McCall 1 month ago ↶
18
750251b2 Andy McCall 1 month ago ↶
19
pub const CONTENT_TYPE : & str = " application/oxy-cbor" ;
20
pub const PROTOCOL_VERSION : u64 = 1 ;
21
5b9c6ad4 Andy McCall 1 month ago ↶
22
23
# [ derive ( Clone, Debug, PartialEq, Eq ) ]
24
pub struct SignedAttest {
25
pub attest : Attest,
26
pub sigs : Vec< Signature> ,
27
}
28
83e0677b Andy McCall 1 month ago ↶
29
30
# [ derive ( Clone, Debug, PartialEq, Eq ) ]
31
pub struct SignedPolicy {
32
pub policy : Policy,
33
pub sigs : Vec< Signature> ,
34
}
35
28fc23d7 Andy McCall 1 month ago ↶
36
impl SignedPolicy {
37
38
pub fn encode ( & self ) -> Vec< u8 > {
39
crate :: cbor:: encode( & wire:: policy_value( self ) )
40
}
41
pub fn decode ( b : & [u8 ]) -> Result< SignedPolicy> {
75167672 Andy McCall 1 month ago
42
wire:: policy_from(
43
& crate :: cbor:: decode( b) .map_err ( | e | RepoError:: Object( ObjectError:: from( e) ) ) ? ,
44
)
28fc23d7 Andy McCall 1 month ago ↶
45
}
46
}
47
132b884b Andy McCall 1 month ago ↶
48
83e0677b Andy McCall 1 month ago ↶
49
# [ derive ( Clone, Debug, Default ) ]
50
pub struct RefsResponse {
51
pub refs : Vec< RemoteRef> ,
52
pub policy : Option< SignedPolicy> ,
132b884b Andy McCall 1 month ago ↶
53
pub tags : Vec< TagRef> ,
28fc23d7 Andy McCall 1 month ago ↶
54
55
56
pub history : Vec< SignedPolicy> ,
83e0677b Andy McCall 1 month ago ↶
57
}
58
132b884b Andy McCall 1 month ago ↶
59
60
# [ derive ( Clone, Debug, PartialEq, Eq ) ]
61
pub struct TagRef {
62
pub name : String,
63
pub state : ObjectRef,
64
pub heads : Vec< ChangeHash> ,
65
pub attestation : Option< SignedAttest> ,
66
}
67
5b9c6ad4 Andy McCall 1 month ago ↶
68
69
# [ derive ( Clone, Debug ) ]
70
pub struct RemoteRef {
71
pub name : String,
72
pub state : ObjectRef,
73
pub heads : Vec< ChangeHash> ,
74
pub attestation : Option< SignedAttest> ,
75
}
76
77
78
# [ derive ( Clone, Debug, Default ) ]
79
pub struct Bundle {
80
81
pub changes : Vec< ( Change, Vec< Signature> ) > ,
82
83
pub attests : Vec< SignedAttest> ,
84
85
pub objects : Vec< ( ObjectType, Vec< u8 > ) > ,
28fc23d7 Andy McCall 1 month ago ↶
86
87
88
pub policies : Vec< SignedPolicy> ,
5b9c6ad4 Andy McCall 1 month ago ↶
89
}
90
91
impl Bundle {
92
pub fn is_empty ( & self ) -> bool {
93
self .changes.is_empty ( ) & & self .attests.is_empty ( ) & & self .objects.is_empty ( )
94
}
95
}
96
97
98
# [ derive ( Clone, Debug ) ]
99
pub struct RefUpdate {
100
pub name : String,
101
pub old_state : ObjectRef,
102
pub new_state : ObjectRef,
103
pub force : bool ,
104
pub attestation : Option< SignedAttest> ,
105
}
106
107
# [ derive ( Clone, Debug, PartialEq, Eq ) ]
108
pub struct RefResult {
109
pub name : String,
110
pub ok : bool ,
111
pub reason : String,
112
}
113
114
pub trait Remote {
115
fn repo_id ( & self ) -> Result< [ u8 ; 16 ] > ;
83e0677b Andy McCall 1 month ago ↶
116
fn refs ( & self ) -> Result< RefsResponse> ;
5b9c6ad4 Andy McCall 1 month ago ↶
117
118
fn negotiate ( & self , have : & [ChangeHash], want : & [ChangeHash]) -> Result< Vec< ChangeHash> > ;
b3c5a59a Andy McCall 1 month ago ↶
119
120
121
fn fetch ( & self , changes : & [ChangeHash], max_blob : Option< u64 > ) -> Result< Bundle> ;
122
123
124
fn objects ( & self , refs : & [ObjectRef]) -> Result< Vec< ( ObjectType, Vec< u8 > ) > > ;
2863456a Andy McCall 1 month ago ↶
125
126
127
fn checkpoint ( & self , heads : & [ChangeHash]) -> Result< Bundle> ;
128
129
fn shallow ( & self , heads : & [ChangeHash], depth : u32 ) -> Result< Vec< ChangeHash> > ;
132b884b Andy McCall 1 month ago ↶
130
131
132
75167672 Andy McCall 1 month ago
133
fn push (
134
& self ,
135
bundle : Bundle,
136
updates : Vec< RefUpdate> ,
137
policy : Option< SignedPolicy> ,
138
tags : Vec< TagRef> ,
139
) -> Result< Vec< RefResult> > ;
5b9c6ad4 Andy McCall 1 month ago ↶
140
}
141
750251b2 Andy McCall 1 month ago ↶
142
143
5b9c6ad4 Andy McCall 1 month ago ↶
144
145
pub struct LocalRemote {
146
pub repo : Repo,
84b9e9ac Andy McCall 1 month ago ↶
147
148
149
pub claimed_id : Option< [ u8 ; 16 ] > ,
5b9c6ad4 Andy McCall 1 month ago ↶
150
}
151
152
impl LocalRemote {
153
pub fn open ( path : & Path) -> Result< LocalRemote> {
75167672 Andy McCall 1 month ago
154
Ok ( LocalRemote {
155
repo: Repo:: open( path) ? ,
84b9e9ac Andy McCall 1 month ago ↶
156
claimed_id: None ,
75167672 Andy McCall 1 month ago
157
} )
5b9c6ad4 Andy McCall 1 month ago ↶
158
}
159
}
160
161
impl Remote for LocalRemote {
162
fn repo_id ( & self ) -> Result< [ u8 ; 16 ] > {
163
self .repo.id ( )
164
}
83e0677b Andy McCall 1 month ago ↶
165
fn refs ( & self ) -> Result< RefsResponse> {
5b9c6ad4 Andy McCall 1 month ago ↶
166
self .repo.remote_refs ( )
167
}
168
fn negotiate ( & self , have : & [ChangeHash], want : & [ChangeHash]) -> Result< Vec< ChangeHash> > {
169
self .repo.negotiate ( have, want)
170
}
b3c5a59a Andy McCall 1 month ago ↶
171
fn fetch ( & self , changes : & [ChangeHash], max_blob : Option< u64 > ) -> Result< Bundle> {
172
self .repo.bundle_for_filtered ( changes, max_blob)
173
}
174
fn objects ( & self , refs : & [ObjectRef]) -> Result< Vec< ( ObjectType, Vec< u8 > ) > > {
175
self .repo.objects_for ( refs)
5b9c6ad4 Andy McCall 1 month ago ↶
176
}
2863456a Andy McCall 1 month ago ↶
177
fn checkpoint ( & self , heads : & [ChangeHash]) -> Result< Bundle> {
178
self .repo.checkpoint_bundle_for ( heads)
179
}
180
fn shallow ( & self , heads : & [ChangeHash], depth : u32 ) -> Result< Vec< ChangeHash> > {
181
self .repo.shallow_changes ( heads, depth)
182
}
75167672 Andy McCall 1 month ago
183
fn push (
184
& self ,
185
bundle : Bundle,
186
updates : Vec< RefUpdate> ,
187
policy : Option< SignedPolicy> ,
188
tags : Vec< TagRef> ,
189
) -> Result< Vec< RefResult> > {
84b9e9ac Andy McCall 1 month ago ↶
190
if self .repo.is_unbound ( ) {
191
match self .claimed_id {
192
Some ( id) => self .repo.bind_id ( id) ? ,
193
None => return Err ( RepoError:: Msg( " the remote is unbound and this client did not say which repository it is" .into ( ) ) ) ,
194
}
195
}
132b884b Andy McCall 1 month ago ↶
196
self .repo.receive_push ( bundle, updates, policy, tags)
5b9c6ad4 Andy McCall 1 month ago ↶
197
}
198
}
199
750251b2 Andy McCall 1 month ago ↶
200
201
2b5ee371 Andy McCall 1 month ago ↶
202
203
# [ derive ( Clone, Debug, Default ) ]
204
pub struct RemoteOptions {
205
206
pub token : Option< String> ,
207
208
209
pub ca : Option< PathBuf> ,
75167672 Andy McCall 1 month ago
210
211
212
pub repo_id : Option< [ u8 ; 16 ] > ,
2b5ee371 Andy McCall 1 month ago ↶
213
}
214
75167672 Andy McCall 1 month ago
215
216
pub const REPO_ID_HEADER : & str = " Oxy-Repo-Id" ;
217
218
219
pub const UNBOUND_ID : [ u8 ; 16 ] = [ 0 ; 16 ] ;
220
750251b2 Andy McCall 1 month ago ↶
221
222
pub struct HttpRemote {
223
base : String,
224
token : Option< String> ,
75167672 Andy McCall 1 month ago
225
repo_id : Option< [ u8 ; 16 ] > ,
750251b2 Andy McCall 1 month ago ↶
226
agent : ureq:: Agent,
227
}
228
2b5ee371 Andy McCall 1 month ago ↶
229
230
fn tls_client_config ( ca : Option< & Path> ) -> Result< Arc< rustls:: ClientConfig> > {
231
let mut roots = rustls:: RootCertStore:: empty( ) ;
232
roots.extend ( webpki_roots:: TLS_SERVER_ROOTS .iter ( ) .cloned ( ) ) ;
233
if let Some ( path) = ca {
75167672 Andy McCall 1 month ago
234
let pem = std:: fs:: read( path)
235
.map_err ( | e | RepoError:: Msg( format! ( " reading CA file {} : {e} " , path.display ( ) ) ) ) ? ;
2b5ee371 Andy McCall 1 month ago ↶
236
let certs = rustls_pemfile:: certs( & mut pem.as_slice ( ) )
237
.collect:: < std:: result:: Result< Vec< _ > , _ > > ( )
238
.map_err ( | e | RepoError:: Msg( format! ( " parsing CA file {} : {e} " , path.display ( ) ) ) ) ? ;
239
if certs.is_empty ( ) {
75167672 Andy McCall 1 month ago
240
return Err ( RepoError:: Msg( format! (
241
" CA file {} holds no certificates" ,
242
path.display ( )
243
) ) ) ;
2b5ee371 Andy McCall 1 month ago ↶
244
}
245
for c in certs {
75167672 Andy McCall 1 month ago
246
roots
247
.add ( c)
248
.map_err ( | e | RepoError:: Msg( format! ( " CA file {} : {e} " , path.display ( ) ) ) ) ? ;
2b5ee371 Andy McCall 1 month ago ↶
249
}
250
}
75167672 Andy McCall 1 month ago
251
let cfg = rustls:: ClientConfig:: builder_with_provider( Arc:: new(
252
rustls:: crypto:: ring:: default_provider( ) ,
253
) )
254
.with_safe_default_protocol_versions ( )
255
.map_err ( | e | RepoError:: Msg( e.to_string ( ) ) ) ?
256
.with_root_certificates ( roots)
257
.with_no_client_auth ( ) ;
2b5ee371 Andy McCall 1 month ago ↶
258
Ok ( Arc:: new( cfg) )
259
}
260
750251b2 Andy McCall 1 month ago ↶
261
impl HttpRemote {
2b5ee371 Andy McCall 1 month ago ↶
262
pub fn new ( base : & str , options : & RemoteOptions) -> Result< HttpRemote> {
75167672 Andy McCall 1 month ago
263
let agent = ureq:: AgentBuilder:: new( )
264
.tls_config ( tls_client_config ( options.ca.as_deref ( ) ) ? )
265
.build ( ) ;
266
Ok ( HttpRemote {
267
base: base.trim_end_matches ( ' /' ) .to_string ( ) ,
268
token: options.token.clone ( ) ,
269
repo_id: options.repo_id,
270
agent,
271
} )
750251b2 Andy McCall 1 month ago ↶
272
}
273
274
fn call ( & self , post : bool , endpoint : & str , body : Option< & [ u8 ] > ) -> Result< Vec< u8 > > {
275
let url = format! ( " {} /oxy/v1/{endpoint} " , self .base) ;
75167672 Andy McCall 1 month ago
276
let mut req = if post {
277
self .agent.post ( & url)
278
} else {
279
self .agent.get ( & url)
280
} ;
750251b2 Andy McCall 1 month ago ↶
281
req = req.set ( " Accept" , CONTENT_TYPE ) ;
282
if let Some ( t) = & self .token {
283
req = req.set ( " Authorization" , & format! ( " Bearer {t} " ) ) ;
284
}
75167672 Andy McCall 1 month ago
285
if let Some ( id) = & self .repo_id {
286
req = req.set ( REPO_ID_HEADER , & hex:: encode( id) ) ;
287
}
750251b2 Andy McCall 1 month ago ↶
288
let resp = match body {
289
Some ( b) => req.set ( " Content-Type" , CONTENT_TYPE ) .send_bytes ( b) ,
290
None => req.call ( ) ,
291
} ;
292
match resp {
293
Ok ( r) => {
294
let mut out = Vec :: new( ) ;
75167672 Andy McCall 1 month ago
295
r.into_reader ( )
296
.read_to_end ( & mut out)
297
.map_err ( | e | RepoError:: Msg( format! ( " {url} : {e} " ) ) ) ? ;
750251b2 Andy McCall 1 month ago ↶
298
Ok ( out)
299
}
300
Err ( ureq:: Error:: Status( code, r) ) => {
301
let text = r.into_string ( ) .unwrap_or_default ( ) ;
3e5772cc Andy McCall 1 month ago ↶
302
let msg = format! ( " {url} : HTTP {code} : {} " , text.trim ( ) ) ;
303
Err ( RepoError:: Msg( with_hint ( msg) ) )
750251b2 Andy McCall 1 month ago ↶
304
}
3e5772cc Andy McCall 1 month ago ↶
305
306
Err ( e) => Err ( RepoError:: Msg( with_hint ( e.to_string ( ) ) ) ) ,
750251b2 Andy McCall 1 month ago ↶
307
}
308
}
309
}
310
3e5772cc Andy McCall 1 month ago ↶
311
312
pub fn hint_for ( message : & str ) -> Option< & 'static str > {
313
let m = message.to_ascii_lowercase ( ) ;
314
let hint = if m.contains ( " http 401" ) {
315
" the server did not accept the token: set one with `oxy config remote.<remote>.token TOKEN` (a personal access token; pushing needs repo:write)"
316
} else if m.contains ( " http 403" ) {
317
" the token or account lacks permission: pushing needs a token with repo:write and write access to the repository"
318
} else if m.contains ( " http 404" ) {
319
" nothing at this URL: check the owner and repository name, or create the repository on the hub first"
320
} else if m.contains ( " unknownissuer" )
321
| | m.contains ( " unknown issuer" )
322
| | m.contains ( " certificate" )
323
| | m.contains ( " invalidcertificate" )
324
{
325
" 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`"
326
} else if m.contains ( " connection refused" ) {
327
" nothing is listening there: check the URL and port, and that the server is running"
328
} else if m.contains ( " dns" )
329
| | m.contains ( " failed to lookup" )
330
| | m.contains ( " name or service not known" )
331
{
332
" the host name does not resolve: check the URL"
333
} else if m.contains ( " timed out" ) | | m.contains ( " timeout" ) {
334
" the server did not answer in time: check the network, or try again"
335
} else {
336
return None ;
337
} ;
338
Some ( hint)
339
}
340
341
342
pub fn with_hint ( message : String) -> String {
343
match hint_for ( & message) {
344
Some ( h) => format! ( " {message} \n hint: {h} " ) ,
345
None => message,
346
}
347
}
348
d82e4409 Andy McCall 1 month ago ↶
349
350
351
pub fn http_get ( url : & str , options : & RemoteOptions) -> Result< Vec< u8 > > {
352
let agent = ureq:: AgentBuilder:: new( )
353
.tls_config ( tls_client_config ( options.ca.as_deref ( ) ) ? )
354
.build ( ) ;
355
let mut req = agent.get ( url) .set ( " Accept" , " application/json" ) ;
356
if let Some ( t) = & options.token {
357
req = req.set ( " Authorization" , & format! ( " Bearer {t} " ) ) ;
358
}
359
match req.call ( ) {
360
Ok ( r) => {
361
let mut out = Vec :: new( ) ;
362
r.into_reader ( )
363
.read_to_end ( & mut out)
364
.map_err ( | e | RepoError:: Msg( format! ( " {url} : {e} " ) ) ) ? ;
365
Ok ( out)
366
}
367
Err ( ureq:: Error:: Status( code, r) ) => {
368
let text = r.into_string ( ) .unwrap_or_default ( ) ;
3e5772cc Andy McCall 1 month ago ↶
369
Err ( RepoError:: Msg( with_hint ( format! (
4213c41d Andy McCall 1 month ago ↶
370
" {url} : HTTP {code} : {} " ,
371
text.trim ( )
3e5772cc Andy McCall 1 month ago ↶
372
) ) ) )
d82e4409 Andy McCall 1 month ago ↶
373
}
3e5772cc Andy McCall 1 month ago ↶
374
375
Err ( e) => Err ( RepoError:: Msg( with_hint ( e.to_string ( ) ) ) ) ,
d82e4409 Andy McCall 1 month ago ↶
376
}
377
}
378
750251b2 Andy McCall 1 month ago ↶
379
impl Remote for HttpRemote {
380
fn repo_id ( & self ) -> Result< [ u8 ; 16 ] > {
381
wire:: decode_capabilities( & self .call ( false , " capabilities" , None ) ? )
382
}
83e0677b Andy McCall 1 month ago ↶
383
fn refs ( & self ) -> Result< RefsResponse> {
750251b2 Andy McCall 1 month ago ↶
384
Ok ( wire:: decode_refs( & self .call ( false , " refs" , None ) ? ) ? .1 )
385
}
386
fn negotiate ( & self , have : & [ChangeHash], want : & [ChangeHash]) -> Result< Vec< ChangeHash> > {
75167672 Andy McCall 1 month ago
387
wire:: decode_hashes( & self .call (
388
true ,
389
" negotiate" ,
390
Some ( & wire:: encode_negotiate( have, want) ) ,
391
) ? )
750251b2 Andy McCall 1 month ago ↶
392
}
b3c5a59a Andy McCall 1 month ago ↶
393
fn fetch ( & self , changes : & [ChangeHash], max_blob : Option< u64 > ) -> Result< Bundle> {
75167672 Andy McCall 1 month ago
394
wire:: decode_bundle( & self .call (
395
true ,
396
" fetch" ,
397
Some ( & wire:: encode_fetch( changes, max_blob) ) ,
398
) ? )
750251b2 Andy McCall 1 month ago ↶
399
}
b3c5a59a Andy McCall 1 month ago ↶
400
fn objects ( & self , refs : & [ObjectRef]) -> Result< Vec< ( ObjectType, Vec< u8 > ) > > {
75167672 Andy McCall 1 month ago
401
Ok (
402
wire:: decode_bundle( & self .call ( true , " objects" , Some ( & wire:: encode_hashes( refs) ) ) ? ) ?
403
.objects,
404
)
b3c5a59a Andy McCall 1 month ago ↶
405
}
2863456a Andy McCall 1 month ago ↶
406
fn checkpoint ( & self , heads : & [ChangeHash]) -> Result< Bundle> {
407
wire:: decode_bundle( & self .call ( true , " checkpoint" , Some ( & wire:: encode_hashes( heads) ) ) ? )
408
}
409
fn shallow ( & self , heads : & [ChangeHash], depth : u32 ) -> Result< Vec< ChangeHash> > {
75167672 Andy McCall 1 month ago
410
wire:: decode_hashes( & self .call (
411
true ,
412
" shallow" ,
413
Some ( & wire:: encode_shallow( heads, depth) ) ,
414
) ? )
2863456a Andy McCall 1 month ago ↶
415
}
75167672 Andy McCall 1 month ago
416
fn push (
417
& self ,
418
bundle : Bundle,
419
updates : Vec< RefUpdate> ,
420
policy : Option< SignedPolicy> ,
421
tags : Vec< TagRef> ,
422
) -> Result< Vec< RefResult> > {
423
wire:: decode_push_response( & self .call (
424
true ,
425
" push" ,
426
Some ( & wire:: encode_push(
427
& bundle,
428
& updates,
429
policy.as_ref ( ) ,
430
& tags,
431
) ) ,
432
) ? )
750251b2 Andy McCall 1 month ago ↶
433
}
434
}
435
436
2b5ee371 Andy McCall 1 month ago ↶
437
pub fn open_remote ( url : & str , options : & RemoteOptions) -> Result< Box< dyn Remote> > {
750251b2 Andy McCall 1 month ago ↶
438
if url.starts_with ( " http://" ) | | url.starts_with ( " https://" ) {
2b5ee371 Andy McCall 1 month ago ↶
439
return Ok ( Box :: new( HttpRemote:: new( url, options) ? ) ) ;
750251b2 Andy McCall 1 month ago ↶
440
}
5b9c6ad4 Andy McCall 1 month ago ↶
441
let path = url.strip_prefix ( " file://" ) .unwrap_or ( url) ;
442
if path.contains ( " ://" ) {
750251b2 Andy McCall 1 month ago ↶
443
return Err ( RepoError:: Msg( format! ( " unsupported remote URL {url:?} " ) ) ) ;
5b9c6ad4 Andy McCall 1 month ago ↶
444
}
84b9e9ac Andy McCall 1 month ago ↶
445
let mut local = LocalRemote:: open( Path:: new( path) ) ? ;
446
local.claimed_id = options.repo_id;
447
Ok ( Box :: new( local) )
5b9c6ad4 Andy McCall 1 month ago ↶
448
}
750251b2 Andy McCall 1 month ago ↶
449
450
451
452
453
pub mod wire {
454
use super :: * ;
455
456
fn schema ( msg : & str ) -> RepoError {
457
RepoError:: Object( ObjectError:: Schema( msg.into ( ) ) )
458
}
459
fn sigs_value ( sigs : & [Signature]) -> Value {
460
Value:: Array( sigs.iter ( ) .map ( | s | s.to_value ( ) ) .collect ( ) )
461
}
462
fn sigs_from ( v : & Value) -> Result< Vec< Signature> > {
75167672 Andy McCall 1 month ago
463
Ok ( o:: array( v, " signatures" ) ?
464
.iter ( )
465
.map ( Signature:: from_value)
466
.collect:: < std:: result:: Result< Vec< _ > , _ > > ( ) ? )
750251b2 Andy McCall 1 month ago ↶
467
}
468
fn hashes_value ( hs : & [ObjectRef]) -> Value {
469
Value:: Array( hs.iter ( ) .map ( | h | Value:: Bytes( h.0. to_vec ( ) ) ) .collect ( ) )
470
}
471
fn hashes_from ( v : & Value) -> Result< Vec< ObjectRef> > {
75167672 Andy McCall 1 month ago
472
Ok ( o:: array( v, " hashes" ) ?
473
.iter ( )
474
.map ( | h | o:: oref( h, " hash" ) )
475
.collect:: < std:: result:: Result< Vec< _ > , _ > > ( ) ? )
750251b2 Andy McCall 1 month ago ↶
476
}
477
fn attest_value ( sa : & SignedAttest) -> Value {
478
Value:: Array( vec! [ Value:: Bytes( sa.attest.encode ( ) ) , sigs_value ( & sa.sigs) ] )
479
}
480
fn attest_from ( v : & Value) -> Result< SignedAttest> {
481
let a = o:: array( v, " signed attest" ) ? ;
482
if a.len ( ) ! = 2 {
483
return Err ( schema ( " signed attest: expected [attest, sigs]" ) ) ;
484
}
75167672 Andy McCall 1 month ago
485
Ok ( SignedAttest {
486
attest: Attest:: decode( o:: bytes( & a[ 0 ] , " attest" ) ? ) ? ,
487
sigs: sigs_from ( & a[ 1 ] ) ? ,
488
} )
750251b2 Andy McCall 1 month ago ↶
489
}
490
fn opt_attest_value ( sa : & Option< SignedAttest> ) -> Value {
491
sa.as_ref ( ) .map ( attest_value) .unwrap_or ( Value:: Null)
492
}
493
fn opt_attest_from ( v : & Value) -> Result< Option< SignedAttest> > {
75167672 Andy McCall 1 month ago
494
if v.is_null ( ) {
495
Ok ( None )
496
} else {
497
attest_from ( v) .map ( Some )
498
}
750251b2 Andy McCall 1 month ago ↶
499
}
28fc23d7 Andy McCall 1 month ago ↶
500
501
502
pub fn policy_value ( sp : & SignedPolicy) -> Value {
503
Value:: Array( vec! [ Value:: Bytes( sp.policy.encode ( ) ) , sigs_value ( & sp.sigs) ] )
83e0677b Andy McCall 1 month ago ↶
504
}
28fc23d7 Andy McCall 1 month ago ↶
505
pub fn policy_from ( v : & Value) -> Result< SignedPolicy> {
83e0677b Andy McCall 1 month ago ↶
506
let a = o:: array( v, " signed policy" ) ? ;
507
if a.len ( ) ! = 2 {
508
return Err ( schema ( " signed policy: expected [policy, sigs]" ) ) ;
509
}
75167672 Andy McCall 1 month ago
510
Ok ( SignedPolicy {
511
policy: Policy:: decode( o:: bytes( & a[ 0 ] , " policy" ) ? ) ? ,
512
sigs: sigs_from ( & a[ 1 ] ) ? ,
513
} )
28fc23d7 Andy McCall 1 month ago ↶
514
}
515
fn opt_policy_value ( sp : Option< & SignedPolicy> ) -> Value {
516
sp.map ( policy_value) .unwrap_or ( Value:: Null)
517
}
518
fn opt_policy_from ( v : & Value) -> Result< Option< SignedPolicy> > {
75167672 Andy McCall 1 month ago
519
if v.is_null ( ) {
520
Ok ( None )
521
} else {
522
policy_from ( v) .map ( Some )
523
}
83e0677b Andy McCall 1 month ago ↶
524
}
750251b2 Andy McCall 1 month ago ↶
525
fn map < 'a > ( v : & 'a Value, what : & str ) -> Result< & 'a o:: Map> {
526
Ok ( o:: map( v, what) ? )
527
}
528
fn field < 'a > ( m : & 'a o:: Map, k : u64 , what : & str ) -> Result< & 'a Value> {
529
Ok ( o:: field( m, k, what) ? )
530
}
531
532
pub fn bundle_value ( b : & Bundle) -> Value {
533
Value:: map( [
534
(
535
0 ,
75167672 Andy McCall 1 month ago
536
Value:: Array(
537
b.changes
538
.iter ( )
539
.map ( | ( c , s ) | Value:: Array( vec! [ Value:: Bytes( c.encode ( ) ) , sigs_value ( s) ] ) )
540
.collect ( ) ,
541
) ,
750251b2 Andy McCall 1 month ago ↶
542
) ,
543
(
75167672 Andy McCall 1 month ago
544
1 ,
545
Value:: Array( b.attests.iter ( ) .map ( attest_value) .collect ( ) ) ,
546
) ,
547
(
750251b2 Andy McCall 1 month ago ↶
548
2 ,
549
Value:: Array(
75167672 Andy McCall 1 month ago
550
b.objects
551
.iter ( )
552
.map ( | ( t , body ) | {
553
Value:: Array( vec! [
554
Value:: Text( t.name ( ) .into ( ) ) ,
555
Value:: Bytes( body.clone ( ) ) ,
556
] )
557
} )
558
.collect ( ) ,
750251b2 Andy McCall 1 month ago ↶
559
) ,
560
) ,
75167672 Andy McCall 1 month ago
561
(
562
3 ,
563
Value:: Array( b.policies.iter ( ) .map ( policy_value) .collect ( ) ) ,
564
) ,
750251b2 Andy McCall 1 month ago ↶
565
] )
566
}
567
568
pub fn bundle_from_value ( v : & Value) -> Result< Bundle> {
569
let m = map ( v, " bundle" ) ? ;
570
let mut b = Bundle:: default( ) ;
571
for c in o:: array( field ( m, 0 , " changes" ) ? , " changes" ) ? {
572
let pair = o:: array( c, " change entry" ) ? ;
573
if pair.len ( ) ! = 2 {
574
return Err ( schema ( " change entry: expected [change, sigs]" ) ) ;
575
}
75167672 Andy McCall 1 month ago
576
b.changes.push ( (
577
Change:: decode( o:: bytes( & pair[ 0 ] , " change" ) ? ) ? ,
578
sigs_from ( & pair[ 1 ] ) ? ,
579
) ) ;
750251b2 Andy McCall 1 month ago ↶
580
}
581
for a in o:: array( field ( m, 1 , " attests" ) ? , " attests" ) ? {
582
b.attests.push ( attest_from ( a) ? ) ;
583
}
584
for ob in o:: array( field ( m, 2 , " objects" ) ? , " objects" ) ? {
585
let pair = o:: array( ob, " object" ) ? ;
586
if pair.len ( ) ! = 2 {
587
return Err ( schema ( " object: expected [type, body]" ) ) ;
588
}
75167672 Andy McCall 1 month ago
589
let ty = ObjectType:: from_name( o:: text( & pair[ 0 ] , " object type" ) ? )
590
.ok_or_else ( | | schema ( " unknown object type" ) ) ? ;
591
b.objects
592
.push ( ( ty, o:: bytes( & pair[ 1 ] , " object body" ) ? .to_vec ( ) ) ) ;
750251b2 Andy McCall 1 month ago ↶
593
}
28fc23d7 Andy McCall 1 month ago ↶
594
if let Some ( ps) = m.get ( & 3 ) {
595
for p in o:: array( ps, " policies" ) ? {
596
b.policies.push ( policy_from ( p) ? ) ;
597
}
598
}
750251b2 Andy McCall 1 month ago ↶
599
Ok ( b)
600
}
601
602
pub fn encode_bundle ( b : & Bundle) -> Vec< u8 > {
603
cbor:: encode( & bundle_value ( b) )
604
}
605
pub fn decode_bundle ( bytes : & [u8 ]) -> Result< Bundle> {
606
bundle_from_value ( & cbor:: decode( bytes) .map_err ( ObjectError:: from) ? )
607
}
608
609
pub fn encode_capabilities ( id : [u8 ; 16]) -> Vec< u8 > {
610
cbor:: encode( & Value:: map( [
611
( 0 , Value:: Array( vec! [ Value:: Uint( PROTOCOL_VERSION ) ] ) ) ,
612
( 1 , Value:: Bytes( id.to_vec ( ) ) ) ,
613
( 2 , Value:: Array( vec! [ Value:: Text( " seal" .into ( ) ) ] ) ) ,
614
] ) )
615
}
616
pub fn decode_capabilities ( bytes : & [u8 ]) -> Result< [ u8 ; 16 ] > {
617
let v = cbor:: decode( bytes) .map_err ( ObjectError:: from) ? ;
618
let m = map ( & v, " capabilities" ) ? ;
619
let versions = o:: array( field ( m, 0 , " versions" ) ? , " versions" ) ? ;
75167672 Andy McCall 1 month ago
620
if ! versions
621
.iter ( )
622
.any ( | x | x.as_uint ( ) = = Some ( PROTOCOL_VERSION ) )
623
{
624
return Err ( RepoError:: Msg(
625
" remote does not speak protocol version 1" .into ( ) ,
626
) ) ;
750251b2 Andy McCall 1 month ago ↶
627
}
75167672 Andy McCall 1 month ago
628
o:: bytes( field ( m, 1 , " repo id" ) ? , " repo id" ) ?
629
.try_into ( )
630
.map_err ( | _| schema ( " repo id: expected 16 bytes" ) )
750251b2 Andy McCall 1 month ago ↶
631
}
632
132b884b Andy McCall 1 month ago ↶
633
fn tags_value ( tags : & [TagRef]) -> Value {
634
Value:: Array(
635
tags.iter ( )
636
.map ( | t | {
637
Value:: map( [
638
( 0 , Value:: Text( t.name.clone ( ) ) ) ,
639
( 1 , Value:: Bytes( t.state.0. to_vec ( ) ) ) ,
640
( 2 , hashes_value ( & t.heads) ) ,
641
( 3 , opt_attest_value ( & t.attestation) ) ,
642
] )
643
} )
644
.collect ( ) ,
645
)
646
}
647
fn tags_from ( v : & Value) -> Result< Vec< TagRef> > {
648
let mut out = Vec :: new( ) ;
649
for t in o:: array( v, " tags" ) ? {
650
let tm = map ( t, " tag" ) ? ;
651
out.push ( TagRef {
652
name: o:: text( field ( tm, 0 , " name" ) ? , " name" ) ? .to_owned ( ) ,
653
state: o:: oref( field ( tm, 1 , " state" ) ? , " state" ) ? ,
654
heads: hashes_from ( field ( tm, 2 , " heads" ) ? ) ? ,
655
attestation: opt_attest_from ( field ( tm, 3 , " attestation" ) ? ) ? ,
656
} ) ;
657
}
658
Ok ( out)
659
}
660
83e0677b Andy McCall 1 month ago ↶
661
pub fn encode_refs ( id : [u8 ; 16], resp : & RefsResponse) -> Vec< u8 > {
750251b2 Andy McCall 1 month ago ↶
662
cbor:: encode( & Value:: map( [
663
(
664
0 ,
665
Value:: Array(
83e0677b Andy McCall 1 month ago ↶
666
resp.refs
667
.iter ( )
750251b2 Andy McCall 1 month ago ↶
668
.map ( | r | {
669
Value:: map( [
670
( 0 , Value:: Text( r.name.clone ( ) ) ) ,
671
( 1 , Value:: Bytes( r.state.0. to_vec ( ) ) ) ,
672
( 2 , hashes_value ( & r.heads) ) ,
673
( 3 , opt_attest_value ( & r.attestation) ) ,
674
( 4 , r.last_serial.map ( Value:: Uint) .unwrap_or ( Value:: Null) ) ,
675
] )
676
} )
677
.collect ( ) ,
678
) ,
679
) ,
680
( 1 , Value:: Bytes( id.to_vec ( ) ) ) ,
83e0677b Andy McCall 1 month ago ↶
681
( 2 , opt_policy_value ( resp.policy.as_ref ( ) ) ) ,
132b884b Andy McCall 1 month ago ↶
682
( 3 , tags_value ( & resp.tags) ) ,
75167672 Andy McCall 1 month ago
683
(
684
4 ,
685
Value:: Array( resp.history.iter ( ) .map ( policy_value) .collect ( ) ) ,
686
) ,
750251b2 Andy McCall 1 month ago ↶
687
] ) )
688
}
83e0677b Andy McCall 1 month ago ↶
689
pub fn decode_refs ( bytes : & [u8 ]) -> Result< ( [u8 ; 16], RefsResponse) > {
750251b2 Andy McCall 1 month ago ↶
690
let v = cbor:: decode( bytes) .map_err ( ObjectError:: from) ? ;
691
let m = map ( & v, " refs" ) ? ;
75167672 Andy McCall 1 month ago
692
let id: [ u8 ; 16 ] = o:: bytes( field ( m, 1 , " repo id" ) ? , " repo id" ) ?
693
.try_into ( )
694
.map_err ( | _| schema ( " repo id: expected 16 bytes" ) ) ? ;
750251b2 Andy McCall 1 month ago ↶
695
let mut refs = Vec :: new( ) ;
696
for r in o:: array( field ( m, 0 , " refs" ) ? , " refs" ) ? {
697
let rm = map ( r, " ref" ) ? ;
698
let ls = field ( rm, 4 , " last serial" ) ? ;
699
refs.push ( RemoteRef {
700
name: o:: text( field ( rm, 0 , " name" ) ? , " name" ) ? .to_owned ( ) ,
701
state: o:: oref( field ( rm, 1 , " state" ) ? , " state" ) ? ,
702
heads: hashes_from ( field ( rm, 2 , " heads" ) ? ) ? ,
703
attestation: opt_attest_from ( field ( rm, 3 , " attestation" ) ? ) ? ,
75167672 Andy McCall 1 month ago
704
last_serial: if ls.is_null ( ) {
705
None
706
} else {
707
Some ( o:: uint( ls, " last serial" ) ? )
708
} ,
750251b2 Andy McCall 1 month ago ↶
709
} ) ;
710
}
83e0677b Andy McCall 1 month ago ↶
711
let policy = match m.get ( & 2 ) {
712
Some ( p) => opt_policy_from ( p) ? ,
713
None => None ,
714
} ;
132b884b Andy McCall 1 month ago ↶
715
let tags = match m.get ( & 3 ) {
716
Some ( t) => tags_from ( t) ? ,
717
None => Vec :: new( ) ,
718
} ;
28fc23d7 Andy McCall 1 month ago ↶
719
let mut history = Vec :: new( ) ;
720
if let Some ( h) = m.get ( & 4 ) {
721
for p in o:: array( h, " history" ) ? {
722
history.push ( policy_from ( p) ? ) ;
723
}
724
}
75167672 Andy McCall 1 month ago
725
Ok ( (
726
id,
727
RefsResponse {
728
refs,
729
policy,
730
tags,
731
history,
732
} ,
733
) )
750251b2 Andy McCall 1 month ago ↶
734
}
735
736
pub fn encode_negotiate ( have : & [ChangeHash], want : & [ChangeHash]) -> Vec< u8 > {
75167672 Andy McCall 1 month ago
737
cbor:: encode( & Value:: map( [
738
( 0 , hashes_value ( have) ) ,
739
( 1 , hashes_value ( want) ) ,
740
] ) )
750251b2 Andy McCall 1 month ago ↶
741
}
742
pub fn decode_negotiate ( bytes : & [u8 ]) -> Result< ( Vec< ChangeHash> , Vec< ChangeHash> ) > {
743
let v = cbor:: decode( bytes) .map_err ( ObjectError:: from) ? ;
744
let m = map ( & v, " negotiate" ) ? ;
75167672 Andy McCall 1 month ago
745
Ok ( (
746
hashes_from ( field ( m, 0 , " have" ) ? ) ? ,
747
hashes_from ( field ( m, 1 , " want" ) ? ) ? ,
748
) )
750251b2 Andy McCall 1 month ago ↶
749
}
750
b3c5a59a Andy McCall 1 month ago ↶
751
752
pub fn encode_fetch ( hs : & [ChangeHash], max_blob : Option< u64 > ) -> Vec< u8 > {
75167672 Andy McCall 1 month ago
753
cbor:: encode( & Value:: map( [
754
( 0 , hashes_value ( hs) ) ,
755
( 1 , max_blob.map ( Value:: Uint) .unwrap_or ( Value:: Null) ) ,
756
] ) )
b3c5a59a Andy McCall 1 month ago ↶
757
}
758
pub fn decode_fetch ( bytes : & [u8 ]) -> Result< ( Vec< ChangeHash> , Option< u64 > ) > {
759
let v = cbor:: decode( bytes) .map_err ( ObjectError:: from) ? ;
760
let m = map ( & v, " fetch" ) ? ;
761
let max = match m.get ( & 1 ) {
762
Some ( x) if ! x.is_null ( ) => Some ( o:: uint( x, " max blob" ) ? ) ,
763
_ => None ,
764
} ;
765
Ok ( ( hashes_from ( field ( m, 0 , " changes" ) ? ) ? , max) )
766
}
767
2863456a Andy McCall 1 month ago ↶
768
769
pub fn encode_shallow ( hs : & [ChangeHash], depth : u32 ) -> Vec< u8 > {
75167672 Andy McCall 1 month ago
770
cbor:: encode( & Value:: map( [
771
( 0 , hashes_value ( hs) ) ,
772
( 1 , Value:: Uint( depth as u64 ) ) ,
773
] ) )
2863456a Andy McCall 1 month ago ↶
774
}
775
pub fn decode_shallow ( bytes : & [u8 ]) -> Result< ( Vec< ChangeHash> , u32 ) > {
776
let v = cbor:: decode( bytes) .map_err ( ObjectError:: from) ? ;
777
let m = map ( & v, " shallow" ) ? ;
75167672 Andy McCall 1 month ago
778
Ok ( (
779
hashes_from ( field ( m, 0 , " heads" ) ? ) ? ,
780
o:: u32_( field ( m, 1 , " depth" ) ? , " depth" ) ? ,
781
) )
2863456a Andy McCall 1 month ago ↶
782
}
783
750251b2 Andy McCall 1 month ago ↶
784
pub fn encode_hashes ( hs : & [ChangeHash]) -> Vec< u8 > {
785
cbor:: encode( & Value:: map( [ ( 0 , hashes_value ( hs) ) ] ) )
786
}
787
pub fn decode_hashes ( bytes : & [u8 ]) -> Result< Vec< ChangeHash> > {
788
let v = cbor:: decode( bytes) .map_err ( ObjectError:: from) ? ;
789
hashes_from ( field ( map ( & v, " hashes" ) ? , 0 , " hashes" ) ? )
790
}
791
75167672 Andy McCall 1 month ago
792
pub fn encode_push (
793
bundle : & Bundle,
794
updates : & [RefUpdate],
795
policy : Option< & SignedPolicy> ,
796
tags : & [TagRef],
797
) -> Vec< u8 > {
750251b2 Andy McCall 1 month ago ↶
798
cbor:: encode( & Value:: map( [
799
( 0 , bundle_value ( bundle) ) ,
83e0677b Andy McCall 1 month ago ↶
800
( 2 , opt_policy_value ( policy) ) ,
132b884b Andy McCall 1 month ago ↶
801
( 3 , tags_value ( tags) ) ,
750251b2 Andy McCall 1 month ago ↶
802
(
803
1 ,
804
Value:: Array(
805
updates
806
.iter ( )
807
.map ( | u | {
808
Value:: map( [
809
( 0 , Value:: Text( u.name.clone ( ) ) ) ,
810
( 1 , Value:: Bytes( u.old_state.0. to_vec ( ) ) ) ,
811
( 2 , Value:: Bytes( u.new_state.0. to_vec ( ) ) ) ,
812
( 3 , Value:: Bool( u.force) ) ,
813
( 4 , opt_attest_value ( & u.attestation) ) ,
814
] )
815
} )
816
.collect ( ) ,
817
) ,
818
) ,
819
] ) )
820
}
132b884b Andy McCall 1 month ago ↶
821
822
pub type PushParts = ( Bundle, Vec< RefUpdate> , Option< SignedPolicy> , Vec< TagRef> ) ;
823
824
pub fn decode_push ( bytes : & [u8 ]) -> Result< PushParts> {
750251b2 Andy McCall 1 month ago ↶
825
let v = cbor:: decode( bytes) .map_err ( ObjectError:: from) ? ;
826
let m = map ( & v, " push" ) ? ;
827
let bundle = bundle_from_value ( field ( m, 0 , " bundle" ) ? ) ? ;
828
let mut updates = Vec :: new( ) ;
829
for u in o:: array( field ( m, 1 , " updates" ) ? , " updates" ) ? {
830
let um = map ( u, " update" ) ? ;
831
updates.push ( RefUpdate {
832
name: o:: text( field ( um, 0 , " name" ) ? , " name" ) ? .to_owned ( ) ,
833
old_state: o:: oref( field ( um, 1 , " old_state" ) ? , " old_state" ) ? ,
834
new_state: o:: oref( field ( um, 2 , " new_state" ) ? , " new_state" ) ? ,
75167672 Andy McCall 1 month ago
835
force: field ( um, 3 , " force" ) ?
836
.as_bool ( )
837
.ok_or_else ( | | schema ( " force: expected bool" ) ) ? ,
750251b2 Andy McCall 1 month ago ↶
838
attestation: opt_attest_from ( field ( um, 4 , " attestation" ) ? ) ? ,
839
} ) ;
840
}
83e0677b Andy McCall 1 month ago ↶
841
let policy = match m.get ( & 2 ) {
842
Some ( p) => opt_policy_from ( p) ? ,
843
None => None ,
844
} ;
132b884b Andy McCall 1 month ago ↶
845
let tags = match m.get ( & 3 ) {
846
Some ( t) => tags_from ( t) ? ,
847
None => Vec :: new( ) ,
848
} ;
849
Ok ( ( bundle, updates, policy, tags) )
750251b2 Andy McCall 1 month ago ↶
850
}
851
852
pub fn encode_push_response ( results : & [RefResult]) -> Vec< u8 > {
853
cbor:: encode( & Value:: map( [ (
854
0 ,
855
Value:: Array(
856
results
857
.iter ( )
75167672 Andy McCall 1 month ago
858
.map ( | r | {
859
Value:: map( [
860
( 0 , Value:: Text( r.name.clone ( ) ) ) ,
861
( 1 , Value:: Bool( r.ok) ) ,
862
( 2 , Value:: Text( r.reason.clone ( ) ) ) ,
863
] )
864
} )
750251b2 Andy McCall 1 month ago ↶
865
.collect ( ) ,
866
) ,
867
) ] ) )
868
}
869
pub fn decode_push_response ( bytes : & [u8 ]) -> Result< Vec< RefResult> > {
870
let v = cbor:: decode( bytes) .map_err ( ObjectError:: from) ? ;
871
let m = map ( & v, " push response" ) ? ;
872
let mut out = Vec :: new( ) ;
873
for r in o:: array( field ( m, 0 , " results" ) ? , " results" ) ? {
874
let rm = map ( r, " result" ) ? ;
875
out.push ( RefResult {
876
name: o:: text( field ( rm, 0 , " name" ) ? , " name" ) ? .to_owned ( ) ,
75167672 Andy McCall 1 month ago
877
ok: field ( rm, 1 , " ok" ) ?
878
.as_bool ( )
879
.ok_or_else ( | | schema ( " ok: expected bool" ) ) ? ,
750251b2 Andy McCall 1 month ago ↶
880
reason: o:: text( field ( rm, 2 , " reason" ) ? , " reason" ) ? .to_owned ( ) ,
881
} ) ;
882
}
883
Ok ( out)
884
}
885
}
886
887
# [ cfg ( test ) ]
888
mod tests {
889
use super :: * ;
890
use crate :: attest:: AttestBody;
891
use crate :: objects:: Hlc;
892
893
# [ test ]
894
fn wire_round_trips ( ) {
895
let c = crate :: objects:: tests:: sample_change( ) ;
75167672 Andy McCall 1 month ago
896
let sk = crate :: identity:: SecretKey {
897
ed_seed: [ 1 ; 32 ] ,
898
ml_seed: [ 2 ; 32 ] ,
899
} ;
750251b2 Andy McCall 1 month ago ↶
900
let id = sk.identity ( " a" , " b" , 0 ) ;
901
let key = id.hash ( ) ;
902
let sig = sk.sign ( crate :: identity:: Domain:: Change, & key, & c.hash ( ) ) ;
903
let seal = Attest:: seal( [ 3 ; 16 ] , vec! [ c.hash ( ) ] ) ;
904
let seal_sig = sk.sign ( crate :: identity:: Domain:: Attest, & key, & seal.hash ( ) ) ;
28fc23d7 Andy McCall 1 month ago ↶
905
let policy = Policy:: single_root( [ 3 ; 16 ] , id.clone ( ) , 99 ) ;
906
let psig = sk.sign ( crate :: identity:: Domain:: Policy, & key, & policy.hash ( ) ) ;
750251b2 Andy McCall 1 month ago ↶
907
let bundle = Bundle {
908
changes: vec! [ ( c.clone ( ) , vec! [ sig.clone ( ) ] ) ] ,
75167672 Andy McCall 1 month ago
909
attests: vec! [ SignedAttest {
910
attest: seal.clone ( ) ,
911
sigs: vec! [ seal_sig] ,
912
} ] ,
913
objects: vec! [
914
( ObjectType:: Identity, id.encode ( ) ) ,
915
( ObjectType:: Chunk, b " xyz" .to_vec ( ) ) ,
916
] ,
917
policies: vec! [ SignedPolicy {
918
policy: policy.clone ( ) ,
919
sigs: vec! [ psig.clone ( ) ] ,
920
} ] ,
750251b2 Andy McCall 1 month ago ↶
921
} ;
922
let d = wire:: decode_bundle( & wire:: encode_bundle( & bundle) ) .unwrap ( ) ;
923
assert_eq! ( d.changes, bundle.changes) ;
924
assert_eq! ( d.attests, bundle.attests) ;
925
assert_eq! ( d.objects, bundle.objects) ;
28fc23d7 Andy McCall 1 month ago ↶
926
assert_eq! ( d.policies, bundle.policies) ;
750251b2 Andy McCall 1 month ago ↶
927
75167672 Andy McCall 1 month ago
928
let att = Attest {
929
repo: [ 3 ; 16 ] ,
930
hlc: Hlc:: default( ) ,
931
body: AttestBody:: Ref {
932
name: " main" .into ( ) ,
933
old_state: ObjectRef( [ 1 ; 32 ] ) ,
934
new_state: ObjectRef( [ 2 ; 32 ] ) ,
935
serial: 4 ,
936
} ,
937
} ;
750251b2 Andy McCall 1 month ago ↶
938
let refs = vec! [
75167672 Andy McCall 1 month ago
939
RemoteRef {
940
name: " main" .into ( ) ,
941
state: ObjectRef( [ 2 ; 32 ] ) ,
942
heads: vec! [ c.hash ( ) ] ,
943
attestation: Some ( SignedAttest {
944
attest: att.clone ( ) ,
945
sigs: vec! [ sig.clone ( ) ] ,
946
} ) ,
947
last_serial: Some ( 4 ) ,
948
} ,
949
RemoteRef {
950
name: " dev" .into ( ) ,
951
state: ObjectRef( [ 5 ; 32 ] ) ,
952
heads: vec! [ ] ,
953
attestation: None ,
954
last_serial: None ,
955
} ,
750251b2 Andy McCall 1 month ago ↶
956
] ;
75167672 Andy McCall 1 month ago
957
let tag = TagRef {
958
name: " v1" .into ( ) ,
959
state: ObjectRef( [ 2 ; 32 ] ) ,
960
heads: vec! [ c.hash ( ) ] ,
961
attestation: None ,
962
} ;
963
let resp = RefsResponse {
964
refs,
965
policy: Some ( SignedPolicy {
966
policy: policy.clone ( ) ,
967
sigs: vec! [ psig.clone ( ) ] ,
968
} ) ,
969
tags: vec! [ tag.clone ( ) ] ,
970
history: vec! [ SignedPolicy {
971
policy: policy.clone ( ) ,
972
sigs: vec! [ ] ,
973
} ] ,
974
} ;
83e0677b Andy McCall 1 month ago ↶
975
let ( id_back, back) = wire:: decode_refs( & wire:: encode_refs( [ 3 ; 16 ] , & resp) ) .unwrap ( ) ;
750251b2 Andy McCall 1 month ago ↶
976
assert_eq! ( id_back, [ 3 ; 16 ] ) ;
83e0677b Andy McCall 1 month ago ↶
977
assert_eq! ( back.refs.len ( ) , 2 ) ;
978
assert_eq! ( back.refs[ 0 ] .attestation.as_ref ( ) .unwrap ( ) .attest, att) ;
979
assert_eq! ( back.refs[ 1 ] .last_serial, None ) ;
980
assert_eq! ( back.policy.as_ref ( ) .unwrap ( ) .policy, policy) ;
28fc23d7 Andy McCall 1 month ago ↶
981
assert_eq! ( back.history, resp.history) ;
132b884b Andy McCall 1 month ago ↶
982
assert_eq! ( back.tags, vec! [ tag.clone ( ) ] ) ;
750251b2 Andy McCall 1 month ago ↶
983
75167672 Andy McCall 1 month ago
984
let ( h, w) = wire:: decode_negotiate( & wire:: encode_negotiate(
985
& [ ObjectRef( [ 1 ; 32 ] ) ] ,
986
& [ ObjectRef( [ 2 ; 32 ] ) ] ,
987
) )
988
.unwrap ( ) ;
750251b2 Andy McCall 1 month ago ↶
989
assert_eq! ( ( h, w) , ( vec! [ ObjectRef( [ 1 ; 32 ] ) ] , vec! [ ObjectRef( [ 2 ; 32 ] ) ] ) ) ;
75167672 Andy McCall 1 month ago
990
assert_eq! (
991
wire:: decode_hashes( & wire:: encode_hashes( & [ c.hash ( ) ] ) ) .unwrap ( ) ,
992
vec! [ c.hash ( ) ]
993
) ;
994
assert_eq! (
995
wire:: decode_fetch( & wire:: encode_fetch( & [ c.hash ( ) ] , Some ( 4096 ) ) ) .unwrap ( ) ,
996
( vec! [ c.hash ( ) ] , Some ( 4096 ) )
997
) ;
998
assert_eq! (
999
wire:: decode_fetch( & wire:: encode_fetch( & [ ] , None ) ) .unwrap ( ) ,
1000
( vec! [ ] , None )
1001
) ;
1002
assert_eq! (
1003
wire:: decode_shallow( & wire:: encode_shallow( & [ c.hash ( ) ] , 3 ) ) .unwrap ( ) ,
1004
( vec! [ c.hash ( ) ] , 3 )
1005
) ;
b3c5a59a Andy McCall 1 month ago ↶
1006
75167672 Andy McCall 1 month ago
1007
assert_eq! (
1008
wire:: decode_fetch( & wire:: encode_hashes( & [ c.hash ( ) ] ) ) .unwrap ( ) ,
1009
( vec! [ c.hash ( ) ] , None )
1010
) ;
750251b2 Andy McCall 1 month ago ↶
1011
75167672 Andy McCall 1 month ago
1012
let up = RefUpdate {
1013
name: " main" .into ( ) ,
1014
old_state: ObjectRef( [ 1 ; 32 ] ) ,
1015
new_state: ObjectRef( [ 2 ; 32 ] ) ,
1016
force: true ,
1017
attestation: Some ( SignedAttest {
1018
attest: att,
1019
sigs: vec! [ sig] ,
1020
} ) ,
1021
} ;
1022
let sp = SignedPolicy {
1023
policy,
1024
sigs: vec! [ psig] ,
1025
} ;
1026
let ( b2, ups, pol, tags) = wire:: decode_push( & wire:: encode_push(
1027
& bundle,
1028
std:: slice:: from_ref( & up) ,
1029
Some ( & sp) ,
1030
std:: slice:: from_ref( & tag) ,
1031
) )
1032
.unwrap ( ) ;
83e0677b Andy McCall 1 month ago ↶
1033
assert_eq! ( pol, Some ( sp) ) ;
132b884b Andy McCall 1 month ago ↶
1034
assert_eq! ( tags, vec! [ tag] ) ;
750251b2 Andy McCall 1 month ago ↶
1035
assert_eq! ( b2.changes.len ( ) , 1 ) ;
1036
assert_eq! ( ups[ 0 ] .name, " main" ) ;
1037
assert! ( ups[ 0 ] .force) ;
1038
assert_eq! ( ups[ 0 ] .attestation, up.attestation) ;
75167672 Andy McCall 1 month ago
1039
let res = vec! [ RefResult {
1040
name: " main" .into ( ) ,
1041
ok: false ,
1042
reason: " stale" .into ( ) ,
1043
} ] ;
1044
assert_eq! (
1045
wire:: decode_push_response( & wire:: encode_push_response( & res) ) .unwrap ( ) ,
1046
res
1047
) ;
1048
assert_eq! (
1049
wire:: decode_capabilities( & wire:: encode_capabilities( [ 7 ; 16 ] ) ) .unwrap ( ) ,
1050
[ 7 ; 16 ]
1051
) ;
750251b2 Andy McCall 1 month ago ↶
1052
}
1053
}