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
|
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 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 client_test
import (
"encoding/json"
"errors"
"io"
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
"golang.org/x/xerrors"
. "gopkg.in/check.v1"
"github.com/snapcore/snapd/asserts"
"github.com/snapcore/snapd/client"
"github.com/snapcore/snapd/dirs"
)
const happyModelAssertionResponse = `type: model
authority-id: mememe
series: 16
brand-id: mememe
model: test-model
architecture: amd64
base: core18
gadget: pc=18
kernel: pc-kernel=18
required-snaps:
- core
- hello-world
timestamp: 2017-07-27T00:00:00.0Z
sign-key-sha3-384: 8B3Wmemeu3H6i4dEV4Q85Q4gIUCHIBCNMHq49e085QeLGHi7v27l3Cqmemer4__t
AcLBcwQAAQoAHRYhBMbX+t6MbKGH5C3nnLZW7+q0g6ELBQJdTdwTAAoJELZW7+q0g6ELEvgQAI3j
jXTqR6kKOqvw94pArwdMDUaZ++tebASAZgso8ejrW2DQGWSc0Q7SQICIR8bvHxqS1GtupQswOzwS
U8hjDTv7WEchH1jylyTj/1W1GernmitTKycecRlEkSOE+EpuqBFgTtj6PdA1Fj3CiCRi1rLMhgF2
luCOitBLaP+E8P3fuATsLqqDLYzt1VY4Y14MU75hMn+CxAQdnOZTI+NzGMasPsldmOYCPNaN/b3N
6/fDLU47RtNlMJ3K0Tz8kj0bqRbegKlD0RdNbAgo9iZwNmrr5E9WCu9f/0rUor/NIxO77H2ExIll
zhmsZ7E6qlxvAgBmzKgAXrn68gGrBkIb0eXKiCaKy/i2ApvjVZ9HkOzA6Ldd+SwNJv/iA8rdiMsq
p2BfKV5f3ju5b6+WktHxAakJ8iqQmj9Yh7piHjsOAUf1PEJd2s2nqQ+pEEn1F0B23gVCY/Fa9YRQ
iKtWVeL3rBw4dSAaK9rpTMqlNcr+yrdXfTK5YzkCC6RU4yzc5MW0hKeseeSiEDSaRYxvftjFfVNa
ZaVXKg8Lu+cHtCJDeYXEkPIDQzXswdBO1M8Mb9D0mYxQwHxwvsWv1DByB+Otq08EYgPh4kyHo7ag
85yK2e/NQ/fxSwQJMhBF74jM1z9arq6RMiE/KOleFAOraKn2hcROKnEeinABW+sOn6vNuMVv
`
// note: this serial assertion was generated by adding print statements to the
// test in api_model_test.go that generate a fake serial assertion
const happySerialAssertionResponse = `type: serial
authority-id: my-brand
brand-id: my-brand
model: my-old-model
serial: serialserial
device-key:
AcZrBFaFwYABAvCgEOrrLA6FKcreHxCcOoTgBUZ+IRG7Nb8tzmEAklaQPGpv7skapUjwD1luE2go
mTcoTssVHrfLpBoSDV1aBs44rg3NK40ZKPJP7d2zkds1GxUo1Ea5vfet3SJ4h3aRABEBAAE=
device-key-sha3-384: iqLo9doLzK8De9925UrdUyuvPbBad72OTWVE9YJXqd6nz9dKvwJ_lHP5bVxrl3VO
timestamp: 2019-08-26T16:34:21-05:00
sign-key-sha3-384: anCEGC2NYq7DzDEi6y7OafQCVeVLS90XlLt9PNjrRl9sim5rmRHDDNFNO7ODcWQW
AcJwBAABCgAGBQJdZFBdAADCLALwR6Sy24wm9PffwbvUhOEXneyY3BnxKC0+NgdHu1gU8go9vEP1
i+Flh5uoS70+MBIO+nmF8T+9JWIx2QWFDDxvcuFosnIhvUajCEQohauys5FMz/H/WvB0vrbTBpvK
eg==`
const noModelAssertionYetResponse = `
{
"type": "error",
"status-code": 404,
"status": "Not Found",
"result": {
"message": "no model assertion yet",
"kind": "assertion-not-found",
"value": "model"
}
}`
const noSerialAssertionYetResponse = `
{
"type": "error",
"status-code": 404,
"status": "Not Found",
"result": {
"message": "no serial assertion yet",
"kind": "assertion-not-found",
"value": "serial"
}
}`
func (cs *clientSuite) TestClientRemodelEndpoint(c *C) {
cs.cli.Remodel([]byte(`{"new-model": "some-model"}`), client.RemodelOpts{})
c.Check(cs.req.Method, Equals, "POST")
c.Check(cs.req.URL.Path, Equals, "/v2/model")
}
func (cs *clientSuite) TestClientRemodel(c *C) {
cs.status = 202
cs.rsp = `{
"type": "async",
"status-code": 202,
"result": {},
"change": "d728"
}`
remodelJsonData := []byte(`{"new-model": "some-model"}`)
id, err := cs.cli.Remodel(remodelJsonData, client.RemodelOpts{})
c.Assert(err, IsNil)
c.Check(id, Equals, "d728")
c.Assert(cs.req.Header.Get("Content-Type"), Equals, "application/json")
body, err := io.ReadAll(cs.req.Body)
c.Assert(err, IsNil)
jsonBody := make(map[string]any)
err = json.Unmarshal(body, &jsonBody)
c.Assert(err, IsNil)
c.Check(jsonBody, HasLen, 1)
c.Check(jsonBody["new-model"], Equals, string(remodelJsonData))
c.Check(jsonBody["offline"], IsNil)
}
func (cs *clientSuite) TestClientRemodelOffline(c *C) {
cs.status = 202
cs.rsp = `{
"type": "async",
"status-code": 202,
"result": {},
"change": "d728"
}`
remodelJsonData := []byte(`{"new-model": "some-model"}`)
id, err := cs.cli.Remodel(remodelJsonData, client.RemodelOpts{Offline: true})
c.Assert(err, IsNil)
c.Check(id, Equals, "d728")
c.Assert(cs.req.Header.Get("Content-Type"), Equals, "application/json")
body, err := io.ReadAll(cs.req.Body)
c.Assert(err, IsNil)
jsonBody := make(map[string]any)
err = json.Unmarshal(body, &jsonBody)
c.Assert(err, IsNil)
c.Check(jsonBody, HasLen, 2)
c.Check(jsonBody["new-model"], Equals, string(remodelJsonData))
c.Check(jsonBody["offline"], Equals, true)
}
func (cs *clientSuite) TestClientGetModelHappy(c *C) {
cs.status = 200
cs.rsp = happyModelAssertionResponse
modelAssertion, err := cs.cli.CurrentModelAssertion()
c.Assert(err, IsNil)
expectedAssert, err := asserts.Decode([]byte(happyModelAssertionResponse))
c.Assert(err, IsNil)
c.Assert(modelAssertion, DeepEquals, expectedAssert)
}
func (cs *clientSuite) TestClientGetModelNoModel(c *C) {
cs.status = 404
cs.rsp = noModelAssertionYetResponse
cs.header = http.Header{}
cs.header.Add("Content-Type", "application/json")
_, err := cs.cli.CurrentModelAssertion()
c.Assert(err, ErrorMatches, "no model assertion yet")
}
func (cs *clientSuite) TestClientGetModelNoSerial(c *C) {
cs.status = 404
cs.rsp = noSerialAssertionYetResponse
cs.header = http.Header{}
cs.header.Add("Content-Type", "application/json")
_, err := cs.cli.CurrentSerialAssertion()
c.Assert(err, ErrorMatches, "no serial assertion yet")
}
func (cs *clientSuite) TestClientGetSerialHappy(c *C) {
cs.status = 200
cs.rsp = happySerialAssertionResponse
serialAssertion, err := cs.cli.CurrentSerialAssertion()
c.Assert(err, IsNil)
expectedAssert, err := asserts.Decode([]byte(happySerialAssertionResponse))
c.Assert(err, IsNil)
c.Assert(serialAssertion, DeepEquals, expectedAssert)
}
func (cs *clientSuite) TestClientCurrentModelAssertionErrIsWrapped(c *C) {
cs.err = errors.New("boom")
_, err := cs.cli.CurrentModelAssertion()
var e xerrors.Wrapper
c.Assert(err, Implements, &e)
}
func (cs *clientSuite) TestClientOfflineRemodel(c *C) {
cs.status = 202
cs.rsp = `{
"type": "async",
"status-code": 202,
"result": {},
"change": "d728"
}`
rawModel := []byte(`some-model`)
var err error
snapPaths := []string{filepath.Join(dirs.GlobalRootDir, "snap1.snap")}
err = os.WriteFile(snapPaths[0], []byte("snap1"), 0644)
c.Assert(err, IsNil)
assertsPaths := []string{filepath.Join(dirs.GlobalRootDir, "f1.asserts")}
err = os.WriteFile(assertsPaths[0], []byte("asserts1"), 0644)
c.Assert(err, IsNil)
id, err := cs.cli.RemodelWithLocalSnaps(rawModel, snapPaths, assertsPaths)
c.Assert(err, IsNil)
c.Check(id, Equals, "d728")
contentTypeReStr := "^multipart/form-data; boundary=([A-Za-z0-9]*)$"
contentType := cs.req.Header.Get("Content-Type")
c.Assert(contentType, Matches, contentTypeReStr)
contentTypeRe := regexp.MustCompile(contentTypeReStr)
matches := contentTypeRe.FindStringSubmatch(contentType)
c.Assert(len(matches), Equals, 2)
boundary := "--" + matches[1]
body, err := io.ReadAll(cs.req.Body)
c.Assert(err, IsNil)
expected := boundary + `
Content-Disposition: form-data; name="new-model"
Content-Type: application/x.ubuntu.assertion
some-model
` + boundary + `
Content-Disposition: form-data; name="assertion"
Content-Type: application/x.ubuntu.assertion
asserts1
` + boundary + `
Content-Disposition: form-data; name="snap"; filename="snap1.snap"
Content-Type: application/octet-stream
snap1
` + boundary + `--
`
expected = strings.Replace(expected, "\n", "\r\n", -1)
c.Assert(string(body), Equals, expected)
}
func (cs *clientSuite) TestClientOfflineRemodelServerError(c *C) {
cs.status = 404
cs.rsp = noSerialAssertionYetResponse
rawModel := []byte(`some-model`)
var err error
snapPaths := []string{filepath.Join(dirs.GlobalRootDir, "snap1.snap")}
err = os.WriteFile(snapPaths[0], []byte("snap1"), 0644)
c.Assert(err, IsNil)
assertsPaths := []string{filepath.Join(dirs.GlobalRootDir, "f1.asserts")}
err = os.WriteFile(assertsPaths[0], []byte("asserts1"), 0644)
c.Assert(err, IsNil)
id, err := cs.cli.RemodelWithLocalSnaps(rawModel, snapPaths, assertsPaths)
c.Assert(err.Error(), Equals, "no serial assertion yet")
c.Check(id, Equals, "")
}
func (cs *clientSuite) TestClientOfflineRemodelNoFile(c *C) {
rawModel := []byte(`some-model`)
paths := []string{filepath.Join(dirs.GlobalRootDir, "snap1.snap")}
// No snap file
id, err := cs.cli.RemodelWithLocalSnaps(rawModel, paths, nil)
c.Assert(err, ErrorMatches, `cannot open .*: no such file or directory`)
c.Assert(id, Equals, "")
// No assertions file
id, err = cs.cli.RemodelWithLocalSnaps(rawModel, nil, paths)
c.Assert(err, ErrorMatches, `cannot open .*: no such file or directory`)
c.Assert(id, Equals, "")
}
|