-
Notifications
You must be signed in to change notification settings - Fork 601
/
Copy pathstore_asserts_test.go
236 lines (204 loc) · 7.69 KB
/
store_asserts_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
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2017 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"
"github.com/snapcore/snapd/asserts/assertstest"
)
var _ = Suite(&storeSuite{})
type storeSuite struct {
ts time.Time
tsLine string
validExample string
}
func (s *storeSuite) SetUpSuite(c *C) {
s.ts = time.Now().Truncate(time.Second).UTC()
s.tsLine = "timestamp: " + s.ts.Format(time.RFC3339) + "\n"
s.validExample = "type: store\n" +
"authority-id: canonical\n" +
"store: store1\n" +
"operator-id: op-id1\n" +
"url: https://store.example.com\n" +
"location: upstairs\n" +
s.tsLine +
"sign-key-sha3-384: Jv8_JiHiIzJVcO9M55pPdqSDWUvuhfDIBJUS-3VW7F_idjix7Ffn5qMxB21ZQuij\n" +
"\n" +
"AXNpZw=="
}
func (s *storeSuite) TestDecodeOK(c *C) {
a, err := asserts.Decode([]byte(s.validExample))
c.Assert(err, IsNil)
c.Check(a.Type(), Equals, asserts.StoreType)
store := a.(*asserts.Store)
c.Check(store.OperatorID(), Equals, "op-id1")
c.Check(store.Store(), Equals, "store1")
c.Check(store.URL().String(), Equals, "https://store.example.com")
c.Check(store.Location(), Equals, "upstairs")
c.Check(store.Timestamp().Equal(s.ts), Equals, true)
c.Check(store.FriendlyStores(), HasLen, 0)
}
var storeErrPrefix = "assertion store: "
func (s *storeSuite) TestDecodeInvalidHeaders(c *C) {
tests := []struct{ original, invalid, expectedErr string }{
{"store: store1\n", "", `"store" header is mandatory`},
{"store: store1\n", "store: \n", `"store" header should not be empty`},
{"operator-id: op-id1\n", "", `"operator-id" header is mandatory`},
{"operator-id: op-id1\n", "operator-id: \n", `"operator-id" header should not be empty`},
{"url: https://store.example.com\n", "url:\n - foo\n", `"url" header must be a string`},
{"location: upstairs\n", "location:\n - foo\n", `"location" header must be a string`},
{s.tsLine, "", `"timestamp" header is mandatory`},
{s.tsLine, "timestamp: \n", `"timestamp" header should not be empty`},
{s.tsLine, "timestamp: 12:30\n", `"timestamp" header is not a RFC3339 date: .*`},
{"url: https://store.example.com\n", "friendly-stores: foo\n", `"friendly-stores" header must be a list of strings`},
}
for _, test := range tests {
invalid := strings.Replace(s.validExample, test.original, test.invalid, 1)
_, err := asserts.Decode([]byte(invalid))
c.Check(err, ErrorMatches, storeErrPrefix+test.expectedErr)
}
}
func (s *storeSuite) TestURLOptional(c *C) {
tests := []string{"", "url: \n"}
for _, test := range tests {
encoded := strings.Replace(s.validExample, "url: https://store.example.com\n", test, 1)
assert, err := asserts.Decode([]byte(encoded))
c.Assert(err, IsNil)
store := assert.(*asserts.Store)
c.Check(store.URL(), IsNil)
}
}
func (s *storeSuite) TestURL(c *C) {
tests := []struct {
url string
err string
}{
// Valid URLs.
{"http://example.com/", ""},
{"https://example.com/", ""},
{"https://example.com/some/path/", ""},
{"https://example.com:443/", ""},
{"https://example.com:1234/", ""},
{"https://user:[email protected]/", ""},
{"https://[email protected]/", ""},
// Invalid URLs.
{"://example.com", `"url" header must be a valid URL`},
{"example.com", `"url" header scheme must be "https" or "http"`},
{"//example.com", `"url" header scheme must be "https" or "http"`},
{"ftp://example.com", `"url" header scheme must be "https" or "http"`},
{"mailto:[email protected]", `"url" header scheme must be "https" or "http"`},
{"https://", `"url" header must have a host`},
{"https:///", `"url" header must have a host`},
{"https:///some/path", `"url" header must have a host`},
{"https://example.com/?foo=bar", `"url" header must not have a query`},
{"https://example.com/#fragment", `"url" header must not have a fragment`},
}
for _, test := range tests {
encoded := strings.Replace(
s.validExample, "url: https://store.example.com\n",
fmt.Sprintf("url: %s\n", test.url), 1)
assert, err := asserts.Decode([]byte(encoded))
if test.err != "" {
c.Assert(err, NotNil)
c.Check(err.Error(), Equals, storeErrPrefix+test.err+": "+test.url)
} else {
c.Assert(err, IsNil)
c.Check(assert.(*asserts.Store).URL().String(), Equals, test.url)
}
}
}
func (s *storeSuite) TestLocationOptional(c *C) {
encoded := strings.Replace(s.validExample, "location: upstairs\n", "", 1)
_, err := asserts.Decode([]byte(encoded))
c.Check(err, IsNil)
}
func (s *storeSuite) TestLocation(c *C) {
for _, test := range []string{"foo", "bar", ""} {
encoded := strings.Replace(
s.validExample, "location: upstairs\n",
fmt.Sprintf("location: %s\n", test), 1)
assert, err := asserts.Decode([]byte(encoded))
c.Assert(err, IsNil)
store := assert.(*asserts.Store)
c.Check(store.Location(), Equals, test)
}
}
func (s *storeSuite) TestCheckAuthority(c *C) {
storeDB, db := makeStoreAndCheckDB(c)
// Add account for operator.
operator := assertstest.NewAccount(storeDB, "op-id1", nil, "")
err := db.Add(operator)
c.Assert(err, IsNil)
otherDB := setup3rdPartySigning(c, "other", storeDB, db)
storeHeaders := map[string]interface{}{
"store": "store1",
"operator-id": operator.HeaderString("account-id"),
"timestamp": time.Now().Format(time.RFC3339),
}
// store signed by some other account fails.
store, err := otherDB.Sign(asserts.StoreType, storeHeaders, nil, "")
c.Assert(err, IsNil)
err = db.Check(store)
c.Assert(err, ErrorMatches, `store assertion "store1" is not signed by a directly trusted authority: other`)
// but succeeds when signed by a trusted authority.
store, err = storeDB.Sign(asserts.StoreType, storeHeaders, nil, "")
c.Assert(err, IsNil)
err = db.Check(store)
c.Assert(err, IsNil)
}
func (s *storeSuite) TestFriendlyStores(c *C) {
encoded := strings.Replace(s.validExample, "url: https://store.example.com\n", `friendly-stores:
- store1
- store2
- store3
`, 1)
assert, err := asserts.Decode([]byte(encoded))
c.Assert(err, IsNil)
store := assert.(*asserts.Store)
c.Check(store.URL(), IsNil)
c.Check(store.FriendlyStores(), DeepEquals, []string{"store1", "store2", "store3"})
}
func (s *storeSuite) TestCheckOperatorAccount(c *C) {
storeDB, db := makeStoreAndCheckDB(c)
store, err := storeDB.Sign(asserts.StoreType, map[string]interface{}{
"store": "store1",
"operator-id": "op-id1",
"timestamp": time.Now().Format(time.RFC3339),
}, nil, "")
c.Assert(err, IsNil)
// No account for operator op-id1 yet, so Check fails.
err = db.Check(store)
c.Assert(err, ErrorMatches, `store assertion "store1" does not have a matching account assertion for the operator "op-id1"`)
// Add the op-id1 account.
operator := assertstest.NewAccount(storeDB, "op-id1", map[string]interface{}{"account-id": "op-id1"}, "")
err = db.Add(operator)
c.Assert(err, IsNil)
// Now the operator exists so Check succeeds.
err = db.Check(store)
c.Assert(err, IsNil)
}
func (s *storeSuite) TestPrerequisites(c *C) {
assert, err := asserts.Decode([]byte(s.validExample))
c.Assert(err, IsNil)
c.Assert(assert.Prerequisites(), DeepEquals, []*asserts.Ref{
{Type: asserts.AccountType, PrimaryKey: []string{"op-id1"}},
})
}