-
Notifications
You must be signed in to change notification settings - Fork 601
/
Copy pathmodel_test.go
804 lines (744 loc) · 31 KB
/
model_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2016-2019 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package asserts_test
import (
"fmt"
"strings"
"time"
. "gopkg.in/check.v1"
"github.com/snapcore/snapd/asserts"
)
type modelSuite struct {
ts time.Time
tsLine string
}
var (
_ = Suite(&modelSuite{})
)
func (mods *modelSuite) SetUpSuite(c *C) {
mods.ts = time.Now().Truncate(time.Second).UTC()
mods.tsLine = "timestamp: " + mods.ts.Format(time.RFC3339) + "\n"
}
const (
reqSnaps = "required-snaps:\n - foo\n - bar\n"
sysUserAuths = "system-user-authority: *\n"
)
const (
modelExample = "type: model\n" +
"authority-id: brand-id1\n" +
"series: 16\n" +
"brand-id: brand-id1\n" +
"model: baz-3000\n" +
"display-name: Baz 3000\n" +
"architecture: amd64\n" +
"gadget: brand-gadget\n" +
"base: core18\n" +
"kernel: baz-linux\n" +
"store: brand-store\n" +
sysUserAuths +
reqSnaps +
"TSLINE" +
"body-length: 0\n" +
"sign-key-sha3-384: Jv8_JiHiIzJVcO9M55pPdqSDWUvuhfDIBJUS-3VW7F_idjix7Ffn5qMxB21ZQuij" +
"\n\n" +
"AXNpZw=="
classicModelExample = "type: model\n" +
"authority-id: brand-id1\n" +
"series: 16\n" +
"brand-id: brand-id1\n" +
"model: baz-3000\n" +
"display-name: Baz 3000\n" +
"classic: true\n" +
"architecture: amd64\n" +
"gadget: brand-gadget\n" +
"store: brand-store\n" +
reqSnaps +
"TSLINE" +
"body-length: 0\n" +
"sign-key-sha3-384: Jv8_JiHiIzJVcO9M55pPdqSDWUvuhfDIBJUS-3VW7F_idjix7Ffn5qMxB21ZQuij" +
"\n\n" +
"AXNpZw=="
core20ModelExample = `type: model
authority-id: brand-id1
series: 16
brand-id: brand-id1
model: baz-3000
display-name: Baz 3000
architecture: amd64
system-user-authority: *
base: core20
store: brand-store
snaps:
-
name: baz-linux
id: bazlinuxidididididididididididid
type: kernel
default-channel: 20
-
name: brand-gadget
id: brandgadgetdidididididididididid
type: gadget
-
name: other-base
id: otherbasedididididididididididid
type: base
modes:
- run
presence: required
-
name: nm
id: nmididididididididididididididid
modes:
- ephemeral
- run
default-channel: 1.0
-
name: myapp
id: myappdididididididididididididid
type: app
default-channel: 2.0
-
name: myappopt
id: myappoptidididididididididididid
type: app
presence: optional
OTHERgrade: secured
` + "TSLINE" +
"body-length: 0\n" +
"sign-key-sha3-384: Jv8_JiHiIzJVcO9M55pPdqSDWUvuhfDIBJUS-3VW7F_idjix7Ffn5qMxB21ZQuij" +
"\n\n" +
"AXNpZw=="
)
func (mods *modelSuite) TestDecodeOK(c *C) {
encoded := strings.Replace(modelExample, "TSLINE", mods.tsLine, 1)
a, err := asserts.Decode([]byte(encoded))
c.Assert(err, IsNil)
c.Check(a.Type(), Equals, asserts.ModelType)
model := a.(*asserts.Model)
c.Check(model.AuthorityID(), Equals, "brand-id1")
c.Check(model.Timestamp(), Equals, mods.ts)
c.Check(model.Series(), Equals, "16")
c.Check(model.BrandID(), Equals, "brand-id1")
c.Check(model.Model(), Equals, "baz-3000")
c.Check(model.DisplayName(), Equals, "Baz 3000")
c.Check(model.Architecture(), Equals, "amd64")
c.Check(model.GadgetSnap(), DeepEquals, &asserts.ModelSnap{
Name: "brand-gadget",
SnapType: "gadget",
Modes: []string{"run"},
Presence: "required",
})
c.Check(model.Gadget(), Equals, "brand-gadget")
c.Check(model.GadgetTrack(), Equals, "")
c.Check(model.KernelSnap(), DeepEquals, &asserts.ModelSnap{
Name: "baz-linux",
SnapType: "kernel",
Modes: []string{"run"},
Presence: "required",
})
c.Check(model.Kernel(), Equals, "baz-linux")
c.Check(model.KernelTrack(), Equals, "")
c.Check(model.Base(), Equals, "core18")
c.Check(model.BaseSnap(), DeepEquals, &asserts.ModelSnap{
Name: "core18",
SnapType: "base",
Modes: []string{"run"},
Presence: "required",
})
c.Check(model.Store(), Equals, "brand-store")
c.Check(model.Grade(), Equals, asserts.ModelGradeUnset)
allSnaps := model.AllSnaps()
c.Check(allSnaps, DeepEquals, []*asserts.ModelSnap{
model.KernelSnap(),
model.BaseSnap(),
model.GadgetSnap(),
{
Name: "foo",
Modes: []string{"run"},
Presence: "required",
},
{
Name: "bar",
Modes: []string{"run"},
Presence: "required",
},
})
// essential snaps included
reqSnaps := model.RequiredWithEssentialSnaps()
c.Check(reqSnaps, HasLen, len(allSnaps))
for i, r := range reqSnaps {
c.Check(r.SnapName(), Equals, allSnaps[i].Name)
c.Check(r.ID(), Equals, "")
}
// essential snaps excluded
c.Check(model.RequiredNoEssentialSnaps(), DeepEquals, reqSnaps[3:])
c.Check(model.SystemUserAuthority(), HasLen, 0)
}
func (mods *modelSuite) TestDecodeStoreIsOptional(c *C) {
withTimestamp := strings.Replace(modelExample, "TSLINE", mods.tsLine, 1)
encoded := strings.Replace(withTimestamp, "store: brand-store\n", "store: \n", 1)
a, err := asserts.Decode([]byte(encoded))
c.Assert(err, IsNil)
model := a.(*asserts.Model)
c.Check(model.Store(), Equals, "")
encoded = strings.Replace(withTimestamp, "store: brand-store\n", "", 1)
a, err = asserts.Decode([]byte(encoded))
c.Assert(err, IsNil)
model = a.(*asserts.Model)
c.Check(model.Store(), Equals, "")
}
func (mods *modelSuite) TestDecodeBaseIsOptional(c *C) {
withTimestamp := strings.Replace(modelExample, "TSLINE", mods.tsLine, 1)
encoded := strings.Replace(withTimestamp, "base: core18\n", "base: \n", 1)
a, err := asserts.Decode([]byte(encoded))
c.Assert(err, IsNil)
model := a.(*asserts.Model)
c.Check(model.Base(), Equals, "")
encoded = strings.Replace(withTimestamp, "base: core18\n", "", 1)
a, err = asserts.Decode([]byte(encoded))
c.Assert(err, IsNil)
model = a.(*asserts.Model)
c.Check(model.Base(), Equals, "")
c.Check(model.BaseSnap(), IsNil)
}
func (mods *modelSuite) TestDecodeDisplayNameIsOptional(c *C) {
withTimestamp := strings.Replace(modelExample, "TSLINE", mods.tsLine, 1)
encoded := strings.Replace(withTimestamp, "display-name: Baz 3000\n", "display-name: \n", 1)
a, err := asserts.Decode([]byte(encoded))
c.Assert(err, IsNil)
model := a.(*asserts.Model)
// optional but we fallback to Model
c.Check(model.DisplayName(), Equals, "baz-3000")
encoded = strings.Replace(withTimestamp, "display-name: Baz 3000\n", "", 1)
a, err = asserts.Decode([]byte(encoded))
c.Assert(err, IsNil)
model = a.(*asserts.Model)
// optional but we fallback to Model
c.Check(model.DisplayName(), Equals, "baz-3000")
}
func (mods *modelSuite) TestDecodeRequiredSnapsAreOptional(c *C) {
withTimestamp := strings.Replace(modelExample, "TSLINE", mods.tsLine, 1)
encoded := strings.Replace(withTimestamp, reqSnaps, "", 1)
a, err := asserts.Decode([]byte(encoded))
c.Assert(err, IsNil)
model := a.(*asserts.Model)
c.Check(model.RequiredNoEssentialSnaps(), HasLen, 0)
}
func (mods *modelSuite) TestDecodeValidatesSnapNames(c *C) {
withTimestamp := strings.Replace(modelExample, "TSLINE", mods.tsLine, 1)
encoded := strings.Replace(withTimestamp, reqSnaps, "required-snaps:\n - foo_bar\n - bar\n", 1)
a, err := asserts.Decode([]byte(encoded))
c.Assert(a, IsNil)
c.Assert(err, ErrorMatches, `assertion model: invalid snap name in "required-snaps" header: foo_bar`)
encoded = strings.Replace(withTimestamp, reqSnaps, "required-snaps:\n - foo\n - bar-;;''\n", 1)
a, err = asserts.Decode([]byte(encoded))
c.Assert(a, IsNil)
c.Assert(err, ErrorMatches, `assertion model: invalid snap name in "required-snaps" header: bar-;;''`)
encoded = strings.Replace(withTimestamp, "kernel: baz-linux\n", "kernel: baz-linux_instance\n", 1)
a, err = asserts.Decode([]byte(encoded))
c.Assert(a, IsNil)
c.Assert(err, ErrorMatches, `assertion model: invalid snap name in "kernel" header: baz-linux_instance`)
encoded = strings.Replace(withTimestamp, "gadget: brand-gadget\n", "gadget: brand-gadget_instance\n", 1)
a, err = asserts.Decode([]byte(encoded))
c.Assert(a, IsNil)
c.Assert(err, ErrorMatches, `assertion model: invalid snap name in "gadget" header: brand-gadget_instance`)
encoded = strings.Replace(withTimestamp, "base: core18\n", "base: core18_instance\n", 1)
a, err = asserts.Decode([]byte(encoded))
c.Assert(a, IsNil)
c.Assert(err, ErrorMatches, `assertion model: invalid snap name in "base" header: core18_instance`)
}
func (mods modelSuite) TestDecodeValidSnapNames(c *C) {
// reuse test cases for snap.ValidateName()
withTimestamp := strings.Replace(modelExample, "TSLINE", mods.tsLine, 1)
validNames := []string{
"aa", "aaa", "aaaa",
"a-a", "aa-a", "a-aa", "a-b-c",
"a0", "a-0", "a-0a",
"01game", "1-or-2",
// a regexp stresser
"u-94903713687486543234157734673284536758",
}
for _, name := range validNames {
encoded := strings.Replace(withTimestamp, "kernel: baz-linux\n", fmt.Sprintf("kernel: %s\n", name), 1)
a, err := asserts.Decode([]byte(encoded))
c.Assert(err, IsNil)
model := a.(*asserts.Model)
c.Check(model.Kernel(), Equals, name)
}
invalidNames := []string{
// name cannot be empty, never reaches snap name validation
"",
// too short (min 2 chars)
"a",
// names cannot be too long
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"xxxxxxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxx",
"1111111111111111111111111111111111111111x",
"x1111111111111111111111111111111111111111",
"x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x",
// a regexp stresser
"u-9490371368748654323415773467328453675-",
// dashes alone are not a name
"-", "--",
// double dashes in a name are not allowed
"a--a",
// name should not end with a dash
"a-",
// name cannot have any spaces in it
"a ", " a", "a a",
// a number alone is not a name
"0", "123",
// identifier must be plain ASCII
"日本語", "한글", "ру́сский язы́к",
// instance names are invalid too
"foo_bar", "x_1",
}
for _, name := range invalidNames {
encoded := strings.Replace(withTimestamp, "kernel: baz-linux\n", fmt.Sprintf("kernel: %s\n", name), 1)
a, err := asserts.Decode([]byte(encoded))
c.Assert(a, IsNil)
if name != "" {
c.Assert(err, ErrorMatches, `assertion model: invalid snap name in "kernel" header: .*`)
} else {
c.Assert(err, ErrorMatches, `assertion model: "kernel" header should not be empty`)
}
}
}
func (mods *modelSuite) TestDecodeSystemUserAuthorityIsOptional(c *C) {
withTimestamp := strings.Replace(modelExample, "TSLINE", mods.tsLine, 1)
encoded := strings.Replace(withTimestamp, sysUserAuths, "", 1)
a, err := asserts.Decode([]byte(encoded))
c.Assert(err, IsNil)
model := a.(*asserts.Model)
// the default is just to accept the brand itself
c.Check(model.SystemUserAuthority(), DeepEquals, []string{"brand-id1"})
encoded = strings.Replace(withTimestamp, sysUserAuths, "system-user-authority:\n - foo\n - bar\n", 1)
a, err = asserts.Decode([]byte(encoded))
c.Assert(err, IsNil)
model = a.(*asserts.Model)
c.Check(model.SystemUserAuthority(), DeepEquals, []string{"foo", "bar"})
}
func (mods *modelSuite) TestDecodeKernelTrack(c *C) {
withTimestamp := strings.Replace(modelExample, "TSLINE", mods.tsLine, 1)
encoded := strings.Replace(withTimestamp, "kernel: baz-linux\n", "kernel: baz-linux=18\n", 1)
a, err := asserts.Decode([]byte(encoded))
c.Assert(err, IsNil)
model := a.(*asserts.Model)
c.Check(model.KernelSnap(), DeepEquals, &asserts.ModelSnap{
Name: "baz-linux",
SnapType: "kernel",
Modes: []string{"run"},
PinnedTrack: "18",
Presence: "required",
})
c.Check(model.Kernel(), Equals, "baz-linux")
c.Check(model.KernelTrack(), Equals, "18")
}
func (mods *modelSuite) TestDecodeGadgetTrack(c *C) {
withTimestamp := strings.Replace(modelExample, "TSLINE", mods.tsLine, 1)
encoded := strings.Replace(withTimestamp, "gadget: brand-gadget\n", "gadget: brand-gadget=18\n", 1)
a, err := asserts.Decode([]byte(encoded))
c.Assert(err, IsNil)
model := a.(*asserts.Model)
c.Check(model.GadgetSnap(), DeepEquals, &asserts.ModelSnap{
Name: "brand-gadget",
SnapType: "gadget",
Modes: []string{"run"},
PinnedTrack: "18",
Presence: "required",
})
c.Check(model.Gadget(), Equals, "brand-gadget")
c.Check(model.GadgetTrack(), Equals, "18")
}
const (
modelErrPrefix = "assertion model: "
)
func (mods *modelSuite) TestDecodeInvalid(c *C) {
encoded := strings.Replace(modelExample, "TSLINE", mods.tsLine, 1)
invalidTests := []struct{ original, invalid, expectedErr string }{
{"series: 16\n", "", `"series" header is mandatory`},
{"series: 16\n", "series: \n", `"series" header should not be empty`},
{"brand-id: brand-id1\n", "", `"brand-id" header is mandatory`},
{"brand-id: brand-id1\n", "brand-id: \n", `"brand-id" header should not be empty`},
{"brand-id: brand-id1\n", "brand-id: random\n", `authority-id and brand-id must match, model assertions are expected to be signed by the brand: "brand-id1" != "random"`},
{"model: baz-3000\n", "", `"model" header is mandatory`},
{"model: baz-3000\n", "model: \n", `"model" header should not be empty`},
{"model: baz-3000\n", "model: baz/3000\n", `"model" primary key header cannot contain '/'`},
// lift this restriction at a later point
{"model: baz-3000\n", "model: BAZ-3000\n", `"model" header cannot contain uppercase letters`},
{"display-name: Baz 3000\n", "display-name:\n - xyz\n", `"display-name" header must be a string`},
{"architecture: amd64\n", "", `"architecture" header is mandatory`},
{"architecture: amd64\n", "architecture: \n", `"architecture" header should not be empty`},
{"gadget: brand-gadget\n", "", `"gadget" header is mandatory`},
{"gadget: brand-gadget\n", "gadget: \n", `"gadget" header should not be empty`},
{"gadget: brand-gadget\n", "gadget: brand-gadget=x/x/x\n", `"gadget" channel selector must be a track name only`},
{"gadget: brand-gadget\n", "gadget: brand-gadget=stable\n", `"gadget" channel selector must be a track name`},
{"gadget: brand-gadget\n", "gadget: brand-gadget=18/beta\n", `"gadget" channel selector must be a track name only`},
{"gadget: brand-gadget\n", "gadget:\n - xyz \n", `"gadget" header must be a string`},
{"kernel: baz-linux\n", "", `"kernel" header is mandatory`},
{"kernel: baz-linux\n", "kernel: \n", `"kernel" header should not be empty`},
{"kernel: baz-linux\n", "kernel: baz-linux=x/x/x\n", `"kernel" channel selector must be a track name only`},
{"kernel: baz-linux\n", "kernel: baz-linux=stable\n", `"kernel" channel selector must be a track name`},
{"kernel: baz-linux\n", "kernel: baz-linux=18/beta\n", `"kernel" channel selector must be a track name only`},
{"kernel: baz-linux\n", "kernel:\n - xyz \n", `"kernel" header must be a string`},
{"base: core18\n", "base:\n - xyz \n", `"base" header must be a string`},
{"store: brand-store\n", "store:\n - xyz\n", `"store" header must be a string`},
{mods.tsLine, "", `"timestamp" header is mandatory`},
{mods.tsLine, "timestamp: \n", `"timestamp" header should not be empty`},
{mods.tsLine, "timestamp: 12:30\n", `"timestamp" header is not a RFC3339 date: .*`},
{reqSnaps, "required-snaps: foo\n", `"required-snaps" header must be a list of strings`},
{reqSnaps, "required-snaps:\n -\n - nested\n", `"required-snaps" header must be a list of strings`},
{sysUserAuths, "system-user-authority:\n a: 1\n", `"system-user-authority" header must be '\*' or a list of account ids`},
{sysUserAuths, "system-user-authority:\n - 5_6\n", `"system-user-authority" header must be '\*' or a list of account ids`},
{reqSnaps, "grade: dangerous\n", `cannot specify a grade for model without the extended snaps header`},
}
for _, test := range invalidTests {
invalid := strings.Replace(encoded, test.original, test.invalid, 1)
_, err := asserts.Decode([]byte(invalid))
c.Check(err, ErrorMatches, modelErrPrefix+test.expectedErr)
}
}
func (mods *modelSuite) TestModelCheck(c *C) {
ex, err := asserts.Decode([]byte(strings.Replace(modelExample, "TSLINE", mods.tsLine, 1)))
c.Assert(err, IsNil)
storeDB, db := makeStoreAndCheckDB(c)
brandDB := setup3rdPartySigning(c, "brand-id1", storeDB, db)
headers := ex.Headers()
headers["brand-id"] = brandDB.AuthorityID
headers["timestamp"] = time.Now().Format(time.RFC3339)
model, err := brandDB.Sign(asserts.ModelType, headers, nil, "")
c.Assert(err, IsNil)
err = db.Check(model)
c.Assert(err, IsNil)
}
func (mods *modelSuite) TestModelCheckInconsistentTimestamp(c *C) {
ex, err := asserts.Decode([]byte(strings.Replace(modelExample, "TSLINE", mods.tsLine, 1)))
c.Assert(err, IsNil)
storeDB, db := makeStoreAndCheckDB(c)
brandDB := setup3rdPartySigning(c, "brand-id1", storeDB, db)
headers := ex.Headers()
headers["brand-id"] = brandDB.AuthorityID
headers["timestamp"] = "2011-01-01T14:00:00Z"
model, err := brandDB.Sign(asserts.ModelType, headers, nil, "")
c.Assert(err, IsNil)
err = db.Check(model)
c.Assert(err, ErrorMatches, `model assertion timestamp outside of signing key validity \(key valid since.*\)`)
}
func (mods *modelSuite) TestClassicDecodeOK(c *C) {
encoded := strings.Replace(classicModelExample, "TSLINE", mods.tsLine, 1)
a, err := asserts.Decode([]byte(encoded))
c.Assert(err, IsNil)
c.Check(a.Type(), Equals, asserts.ModelType)
model := a.(*asserts.Model)
c.Check(model.AuthorityID(), Equals, "brand-id1")
c.Check(model.Timestamp(), Equals, mods.ts)
c.Check(model.Series(), Equals, "16")
c.Check(model.BrandID(), Equals, "brand-id1")
c.Check(model.Model(), Equals, "baz-3000")
c.Check(model.DisplayName(), Equals, "Baz 3000")
c.Check(model.Classic(), Equals, true)
c.Check(model.Architecture(), Equals, "amd64")
c.Check(model.GadgetSnap(), DeepEquals, &asserts.ModelSnap{
Name: "brand-gadget",
SnapType: "gadget",
Modes: []string{"run"},
Presence: "required",
})
c.Check(model.Gadget(), Equals, "brand-gadget")
c.Check(model.KernelSnap(), IsNil)
c.Check(model.Kernel(), Equals, "")
c.Check(model.KernelTrack(), Equals, "")
c.Check(model.Base(), Equals, "")
c.Check(model.BaseSnap(), IsNil)
c.Check(model.Store(), Equals, "brand-store")
allSnaps := model.AllSnaps()
c.Check(allSnaps, DeepEquals, []*asserts.ModelSnap{
model.GadgetSnap(),
{
Name: "foo",
Modes: []string{"run"},
Presence: "required",
},
{
Name: "bar",
Modes: []string{"run"},
Presence: "required",
},
})
// gadget included
reqSnaps := model.RequiredWithEssentialSnaps()
c.Check(reqSnaps, HasLen, len(allSnaps))
for i, r := range reqSnaps {
c.Check(r.SnapName(), Equals, allSnaps[i].Name)
c.Check(r.ID(), Equals, "")
}
// gadget excluded
c.Check(model.RequiredNoEssentialSnaps(), DeepEquals, reqSnaps[1:])
}
func (mods *modelSuite) TestClassicDecodeInvalid(c *C) {
encoded := strings.Replace(classicModelExample, "TSLINE", mods.tsLine, 1)
invalidTests := []struct{ original, invalid, expectedErr string }{
{"classic: true\n", "classic: foo\n", `"classic" header must be 'true' or 'false'`},
{"architecture: amd64\n", "architecture:\n - foo\n", `"architecture" header must be a string`},
{"gadget: brand-gadget\n", "gadget:\n - foo\n", `"gadget" header must be a string`},
{"gadget: brand-gadget\n", "kernel: brand-kernel\n", `cannot specify a kernel with a classic model`},
{"gadget: brand-gadget\n", "base: some-base\n", `cannot specify a base with a classic model`},
{"gadget: brand-gadget\n", "gadget:\n - xyz\n", `"gadget" header must be a string`},
}
for _, test := range invalidTests {
invalid := strings.Replace(encoded, test.original, test.invalid, 1)
_, err := asserts.Decode([]byte(invalid))
c.Check(err, ErrorMatches, modelErrPrefix+test.expectedErr)
}
}
func (mods *modelSuite) TestClassicDecodeGadgetAndArchOptional(c *C) {
encoded := strings.Replace(classicModelExample, "TSLINE", mods.tsLine, 1)
encoded = strings.Replace(encoded, "gadget: brand-gadget\n", "", 1)
encoded = strings.Replace(encoded, "architecture: amd64\n", "", 1)
a, err := asserts.Decode([]byte(encoded))
c.Assert(err, IsNil)
c.Check(a.Type(), Equals, asserts.ModelType)
model := a.(*asserts.Model)
c.Check(model.Classic(), Equals, true)
c.Check(model.Architecture(), Equals, "")
c.Check(model.GadgetSnap(), IsNil)
c.Check(model.Gadget(), Equals, "")
c.Check(model.GadgetTrack(), Equals, "")
}
func (mods *modelSuite) TestCore20DecodeOK(c *C) {
encoded := strings.Replace(core20ModelExample, "TSLINE", mods.tsLine, 1)
encoded = strings.Replace(encoded, "OTHER", "", 1)
a, err := asserts.Decode([]byte(encoded))
c.Assert(err, IsNil)
c.Check(a.Type(), Equals, asserts.ModelType)
model := a.(*asserts.Model)
c.Check(model.AuthorityID(), Equals, "brand-id1")
c.Check(model.Timestamp(), Equals, mods.ts)
c.Check(model.Series(), Equals, "16")
c.Check(model.BrandID(), Equals, "brand-id1")
c.Check(model.Model(), Equals, "baz-3000")
c.Check(model.DisplayName(), Equals, "Baz 3000")
c.Check(model.Architecture(), Equals, "amd64")
c.Check(model.GadgetSnap(), DeepEquals, &asserts.ModelSnap{
Name: "brand-gadget",
SnapID: "brandgadgetdidididididididididid",
SnapType: "gadget",
Modes: []string{"run", "ephemeral"},
DefaultChannel: "latest/stable",
Presence: "required",
})
c.Check(model.Gadget(), Equals, "brand-gadget")
c.Check(model.GadgetTrack(), Equals, "")
c.Check(model.KernelSnap(), DeepEquals, &asserts.ModelSnap{
Name: "baz-linux",
SnapID: "bazlinuxidididididididididididid",
SnapType: "kernel",
Modes: []string{"run", "ephemeral"},
DefaultChannel: "20",
Presence: "required",
})
c.Check(model.Kernel(), Equals, "baz-linux")
c.Check(model.KernelTrack(), Equals, "")
c.Check(model.Base(), Equals, "core20")
c.Check(model.BaseSnap(), DeepEquals, &asserts.ModelSnap{
Name: "core20",
SnapType: "base",
Modes: []string{"run", "ephemeral"},
DefaultChannel: "latest/stable",
Presence: "required",
})
c.Check(model.Store(), Equals, "brand-store")
c.Check(model.Grade(), Equals, asserts.ModelSecured)
allSnaps := model.AllSnaps()
c.Check(allSnaps, DeepEquals, []*asserts.ModelSnap{
model.KernelSnap(),
model.BaseSnap(),
model.GadgetSnap(),
{
Name: "other-base",
SnapID: "otherbasedididididididididididid",
SnapType: "base",
Modes: []string{"run"},
DefaultChannel: "latest/stable",
Presence: "required",
},
{
Name: "nm",
SnapID: "nmididididididididididididididid",
SnapType: "app",
Modes: []string{"ephemeral", "run"},
DefaultChannel: "1.0",
Presence: "required",
},
{
Name: "myapp",
SnapID: "myappdididididididididididididid",
SnapType: "app",
Modes: []string{"run"},
DefaultChannel: "2.0",
Presence: "required",
},
{
Name: "myappopt",
SnapID: "myappoptidididididididididididid",
SnapType: "app",
Modes: []string{"run"},
DefaultChannel: "latest/stable",
Presence: "optional",
},
})
// essential snaps included
reqSnaps := model.RequiredWithEssentialSnaps()
c.Check(reqSnaps, HasLen, len(allSnaps)-1)
for i, r := range reqSnaps {
c.Check(r.SnapName(), Equals, allSnaps[i].Name)
c.Check(r.ID(), Equals, allSnaps[i].SnapID)
}
// essential snaps excluded
c.Check(model.RequiredNoEssentialSnaps(), DeepEquals, reqSnaps[3:])
c.Check(model.SystemUserAuthority(), HasLen, 0)
}
func (mods *modelSuite) TestCore20ExplictBootBase(c *C) {
encoded := strings.Replace(core20ModelExample, "TSLINE", mods.tsLine, 1)
encoded = strings.Replace(encoded, "OTHER", ` -
name: core20
id: core20ididididididididididididid
type: base
default-channel: latest/candidate
`, 1)
a, err := asserts.Decode([]byte(encoded))
c.Assert(err, IsNil)
c.Check(a.Type(), Equals, asserts.ModelType)
model := a.(*asserts.Model)
c.Check(model.BaseSnap(), DeepEquals, &asserts.ModelSnap{
Name: "core20",
SnapID: "core20ididididididididididididid",
SnapType: "base",
Modes: []string{"run", "ephemeral"},
DefaultChannel: "latest/candidate",
Presence: "required",
})
}
func (mods *modelSuite) TestCore20ExplictSnapd(c *C) {
encoded := strings.Replace(core20ModelExample, "TSLINE", mods.tsLine, 1)
encoded = strings.Replace(encoded, "OTHER", ` -
name: snapd
id: snapdidididididididididididididd
type: snapd
default-channel: latest/edge
`, 1)
a, err := asserts.Decode([]byte(encoded))
c.Assert(err, IsNil)
c.Check(a.Type(), Equals, asserts.ModelType)
model := a.(*asserts.Model)
snapdSnap := model.AllSnaps()[0]
c.Check(snapdSnap, DeepEquals, &asserts.ModelSnap{
Name: "snapd",
SnapID: "snapdidididididididididididididd",
SnapType: "snapd",
Modes: []string{"run", "ephemeral"},
DefaultChannel: "latest/edge",
Presence: "required",
})
}
func (mods *modelSuite) TestCore20GradeOptionalDefaultSigned(c *C) {
encoded := strings.Replace(core20ModelExample, "TSLINE", mods.tsLine, 1)
encoded = strings.Replace(encoded, "OTHER", "", 1)
encoded = strings.Replace(encoded, "grade: secured\n", "", 1)
a, err := asserts.Decode([]byte(encoded))
c.Assert(err, IsNil)
c.Check(a.Type(), Equals, asserts.ModelType)
model := a.(*asserts.Model)
c.Check(model.Grade(), Equals, asserts.ModelSigned)
}
func (mods *modelSuite) TestCore20ValidGrades(c *C) {
encoded := strings.Replace(core20ModelExample, "TSLINE", mods.tsLine, 1)
encoded = strings.Replace(encoded, "OTHER", "", 1)
for _, grade := range []string{"signed", "secured", "dangerous"} {
ex := strings.Replace(encoded, "grade: secured\n", fmt.Sprintf("grade: %s\n", grade), 1)
a, err := asserts.Decode([]byte(ex))
c.Assert(err, IsNil)
c.Check(a.Type(), Equals, asserts.ModelType)
model := a.(*asserts.Model)
c.Check(string(model.Grade()), Equals, grade)
}
}
func (mods *modelSuite) TestCore20GradeDangerous(c *C) {
encoded := strings.Replace(core20ModelExample, "TSLINE", mods.tsLine, 1)
encoded = strings.Replace(encoded, "OTHER", "", 1)
encoded = strings.Replace(encoded, "grade: secured\n", "grade: dangerous\n", 1)
// snap ids are optional with grade dangerous to allow working
// with local/not pushed yet to the store snaps
encoded = strings.Replace(encoded, " id: myappdididididididididididididid\n", "", 1)
a, err := asserts.Decode([]byte(encoded))
c.Assert(err, IsNil)
c.Check(a.Type(), Equals, asserts.ModelType)
model := a.(*asserts.Model)
c.Check(model.Grade(), Equals, asserts.ModelDangerous)
allSnaps := model.AllSnaps()
c.Check(allSnaps[len(allSnaps)-2], DeepEquals, &asserts.ModelSnap{
Name: "myapp",
SnapType: "app",
Modes: []string{"run"},
DefaultChannel: "2.0",
Presence: "required",
})
}
func (mods *modelSuite) TestCore20DecodeInvalid(c *C) {
encoded := strings.Replace(core20ModelExample, "TSLINE", mods.tsLine, 1)
snapsStanza := encoded[strings.Index(encoded, "snaps:"):strings.Index(encoded, "grade:")]
invalidTests := []struct{ original, invalid, expectedErr string }{
{"base: core20\n", "", `"base" header is mandatory`},
{"OTHER", "classic: true\n", `cannot use extended snaps header for a classic model \(yet\)`},
{snapsStanza, "snaps: snap\n", `"snaps" header must be a list of maps`},
{snapsStanza, "snaps:\n - snap\n", `"snaps" header must be a list of maps`},
{"name: myapp\n", "other: 1\n", `"name" of snap is mandatory`},
{"name: myapp\n", "name: myapp_2\n", `invalid snap name "myapp_2"`},
{"id: myappdididididididididididididid\n", "id: 2\n", `"id" of snap "myapp" contains invalid characters: "2"`},
{" id: myappdididididididididididididid\n", "", `"id" of snap "myapp" is mandatory for stable model`},
{"type: gadget\n", "type:\n - g\n", `"type" of snap "brand-gadget" must be a string`},
{"type: app\n", "type: thing\n", `"type" of snap "myappopt" must be one of must be one of app|base|gadget|kernel|core|snapd`},
{"modes:\n - run\n", "modes: run\n", `"modes" of snap "other-base" must be a list of strings`},
{"default-channel: 20\n", "default-channel: edge\n", `default channel for snap "baz-linux" must specify a track`},
{"default-channel: 2.0\n", "default-channel:\n - x\n", `"default-channel" of snap "myapp" must be a string`},
{"default-channel: 2.0\n", "default-channel: 2.0/xyz/z\n", `invalid default channel for snap "myapp": invalid risk in channel name: 2.0/xyz/z`},
{"presence: optional\n", "presence:\n - opt\n", `"presence" of snap "myappopt" must be a string`},
{"presence: optional\n", "presence: no\n", `"presence" of snap "myappopt" must be one of must be one of required|optional`},
{"OTHER", " -\n name: myapp\n id: myappdididididididididididididid\n", `cannot list the same snap "myapp" multiple times`},
{"OTHER", " -\n name: myapp2\n id: myappdididididididididididididid\n", `cannot specify the same snap id "myappdididididididididididididid" multiple times, specified for snaps "myapp" and "myapp2"`},
{"OTHER", " -\n name: kernel2\n id: kernel2didididididididididididid\n type: kernel\n", `cannot specify multiple kernel snaps: "baz-linux" and "kernel2"`},
{"OTHER", " -\n name: gadget2\n id: gadget2didididididididididididid\n type: gadget\n", `cannot specify multiple gadget snaps: "brand-gadget" and "gadget2"`},
{"type: gadget\n", "type: gadget\n presence: required\n", `essential snaps are always available, cannot specify modes or presence for snap "brand-gadget"`},
{"type: gadget\n", "type: gadget\n modes:\n - run\n", `essential snaps are always available, cannot specify modes or presence for snap "brand-gadget"`},
{"type: kernel\n", "type: kernel\n presence: required\n", `essential snaps are always available, cannot specify modes or presence for snap "baz-linux"`},
{"OTHER", " -\n name: core20\n id: core20ididididididididididididid\n type: base\n presence: optional\n", `essential snaps are always available, cannot specify modes or presence for snap "core20"`},
{"type: gadget\n", "type: app\n", `one "snaps" header entry must specify the model gadget`},
{"type: kernel\n", "type: app\n", `one "snaps" header entry must specify the model kernel`},
{"OTHER", " -\n name: core20\n id: core20ididididididididididididid\n type: app\n", `boot base "core20" must specify type "base", not "app"`},
{"OTHER", "kernel: foo\n", `cannot specify separate "kernel" header once using the extended snaps header`},
{"OTHER", "gadget: foo\n", `cannot specify separate "gadget" header once using the extended snaps header`},
{"OTHER", "required-snaps:\n - foo\n", `cannot specify separate "required-snaps" header once using the extended snaps header`},
{"grade: secured\n", "grade: foo\n", `grade for model must be secured|signed|dangerous`},
}
for _, test := range invalidTests {
invalid := strings.Replace(encoded, test.original, test.invalid, 1)
invalid = strings.Replace(invalid, "OTHER", "", 1)
_, err := asserts.Decode([]byte(invalid))
c.Check(err, ErrorMatches, modelErrPrefix+test.expectedErr)
}
}