github.com/kubiko/snapd@v0.0.0-20201013125620-d4f3094d9ddf/client/asserts_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2016 Canonical Ltd 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 3 as 8 * published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * 18 */ 19 20 package client_test 21 22 import ( 23 "errors" 24 "io/ioutil" 25 "net/http" 26 "net/url" 27 28 "golang.org/x/xerrors" 29 30 . "gopkg.in/check.v1" 31 32 "github.com/snapcore/snapd/asserts" 33 "github.com/snapcore/snapd/client" 34 "github.com/snapcore/snapd/snap" 35 ) 36 37 func (cs *clientSuite) TestClientAssert(c *C) { 38 cs.rsp = `{ 39 "type": "sync", 40 "result": {} 41 }` 42 a := []byte("Assertion.") 43 err := cs.cli.Ack(a) 44 c.Assert(err, IsNil) 45 body, err := ioutil.ReadAll(cs.req.Body) 46 c.Assert(err, IsNil) 47 c.Check(body, DeepEquals, a) 48 c.Check(cs.req.Method, Equals, "POST") 49 c.Check(cs.req.URL.Path, Equals, "/v2/assertions") 50 } 51 52 func (cs *clientSuite) TestClientAssertsTypes(c *C) { 53 cs.rsp = `{ 54 "result": { 55 "types": ["one", "two"] 56 }, 57 "status": "OK", 58 "status-code": 200, 59 "type": "sync" 60 }` 61 typs, err := cs.cli.AssertionTypes() 62 c.Assert(err, IsNil) 63 c.Check(typs, DeepEquals, []string{"one", "two"}) 64 } 65 66 func (cs *clientSuite) TestClientAssertsCallsEndpoint(c *C) { 67 _, _ = cs.cli.Known("snap-revision", nil, nil) 68 c.Check(cs.req.Method, Equals, "GET") 69 c.Check(cs.req.URL.Path, Equals, "/v2/assertions/snap-revision") 70 } 71 72 func (cs *clientSuite) TestClientAssertsOptsCallsEndpoint(c *C) { 73 _, _ = cs.cli.Known("snap-revision", nil, &client.KnownOptions{Remote: true}) 74 c.Check(cs.req.Method, Equals, "GET") 75 c.Check(cs.req.URL.Path, Equals, "/v2/assertions/snap-revision") 76 c.Check(cs.req.URL.Query()["remote"], DeepEquals, []string{"true"}) 77 } 78 79 func (cs *clientSuite) TestClientAssertsCallsEndpointWithFilter(c *C) { 80 _, _ = cs.cli.Known("snap-revision", map[string]string{ 81 "snap-id": "snap-id-1", 82 "snap-sha3-384": "sha3-384...", 83 }, nil) 84 u, err := url.ParseRequestURI(cs.req.URL.String()) 85 c.Assert(err, IsNil) 86 c.Check(u.Path, Equals, "/v2/assertions/snap-revision") 87 c.Check(u.Query(), DeepEquals, url.Values{ 88 "snap-sha3-384": []string{"sha3-384..."}, 89 "snap-id": []string{"snap-id-1"}, 90 }) 91 } 92 93 func (cs *clientSuite) TestClientAssertsHttpError(c *C) { 94 cs.err = errors.New("fail") 95 _, err := cs.cli.Known("snap-build", nil, nil) 96 c.Assert(err, ErrorMatches, "failed to query assertions: cannot communicate with server: fail") 97 } 98 99 func (cs *clientSuite) TestClientAssertsJSONError(c *C) { 100 cs.status = 400 101 cs.header = http.Header{} 102 cs.header.Add("Content-type", "application/json") 103 cs.rsp = `{ 104 "status-code": 400, 105 "type": "error", 106 "result": { 107 "message": "invalid" 108 } 109 }` 110 _, err := cs.cli.Known("snap-build", nil, nil) 111 c.Assert(err, ErrorMatches, "invalid") 112 } 113 114 func (cs *clientSuite) TestClientAsserts(c *C) { 115 cs.header = http.Header{} 116 cs.header.Add("X-Ubuntu-Assertions-Count", "2") 117 cs.rsp = `type: snap-revision 118 authority-id: store-id1 119 snap-sha3-384: P1wNUk5O_5tO5spqOLlqUuAk7gkNYezIMHp5N9hMUg1a6YEjNeaCc4T0BaYz7IWs 120 snap-id: snap-id-1 121 snap-size: 123 122 snap-revision: 1 123 developer-id: dev-id1 124 revision: 1 125 timestamp: 2015-11-25T20:00:00Z 126 body-length: 0 127 sign-key-sha3-384: Jv8_JiHiIzJVcO9M55pPdqSDWUvuhfDIBJUS-3VW7F_idjix7Ffn5qMxB21ZQuij 128 129 openpgp ... 130 131 type: snap-revision 132 authority-id: store-id1 133 snap-sha3-384: 0Yt6-GXQeTZWUAHo1IKDpS9kqO6zMaizY6vGEfGM-aSfpghPKir1Ic7teQ5Zadaj 134 snap-id: snap-id-2 135 snap-size: 456 136 snap-revision: 1 137 developer-id: dev-id1 138 revision: 1 139 timestamp: 2015-11-30T20:00:00Z 140 body-length: 0 141 sign-key-sha3-384: Jv8_JiHiIzJVcO9M55pPdqSDWUvuhfDIBJUS-3VW7F_idjix7Ffn5qMxB21ZQuij 142 143 openpgp ... 144 ` 145 146 a, err := cs.cli.Known("snap-revision", nil, nil) 147 c.Assert(err, IsNil) 148 c.Check(a, HasLen, 2) 149 150 c.Check(a[0].Type(), Equals, asserts.SnapRevisionType) 151 } 152 153 func (cs *clientSuite) TestClientAssertsNoAssertions(c *C) { 154 cs.header = http.Header{} 155 cs.header.Add("X-Ubuntu-Assertions-Count", "0") 156 cs.rsp = "" 157 cs.status = 200 158 a, err := cs.cli.Known("snap-revision", nil, nil) 159 c.Assert(err, IsNil) 160 c.Check(a, HasLen, 0) 161 } 162 163 func (cs *clientSuite) TestClientAssertsMissingAssertions(c *C) { 164 cs.header = http.Header{} 165 cs.header.Add("X-Ubuntu-Assertions-Count", "4") 166 cs.rsp = "" 167 cs.status = 200 168 _, err := cs.cli.Known("snap-build", nil, nil) 169 c.Assert(err, ErrorMatches, "response did not have the expected number of assertions") 170 } 171 172 func (cs *clientSuite) TestStoreAccount(c *C) { 173 cs.header = http.Header{} 174 cs.header.Add("X-Ubuntu-Assertions-Count", "1") 175 cs.rsp = `type: account 176 authority-id: canonical 177 account-id: canonicalID 178 display-name: canonicalDisplay 179 timestamp: 2016-04-01T00:00:00.0Z 180 username: canonicalUser 181 validation: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TzufIXFni7+nEuOK0SQEzO3/WaRedK1SGOOtTDjB8/3OJeW96AUYK5 197 oTIynkYkEyHWMNCXALg+WQW6L4/YO7aUjZ97zOWIugd7Xy63aT3r/EHafqaY2nacOhLfkeKZ830b 198 o/ezjoZQAxbh6ce7JnXRgE9ELxjdAhBTpGjmmmN2sYrJ7zP9bOgly0BnEPXGSQfFA+NNNw1FADx1 199 MUY8q9DBjmVtgqY+1KGTV5X8KvQCBMODZIf/XJPHdCRAHxMd8COypcwgL2vDIIXpOFbi1J/B0GF+ 200 eklxk9wzBA8AecBMCwCzIRHDNpD1oa2we38bVFrOug6e/VId1k1jYFJjiLyLCDmV8IMYwEllHSXp 201 LQAdm3xZ7t4WnxYC8YSCk9mXf3CZg59SpmnV5Q5Z6A5Pl7Nc3sj7hcsMBZEsOMPzNC9dPsBnZvjs 202 WpPUffJzEdhHBFhvYMuD4Vqj6ejUv9l3oTrjQWVC 203 ` 204 205 account, err := cs.cli.StoreAccount("canonicalID") 206 c.Assert(err, IsNil) 207 c.Check(cs.req.Method, Equals, "GET") 208 c.Check(cs.req.URL.Query(), HasLen, 1) 209 c.Check(cs.req.URL.Query().Get("account-id"), Equals, "canonicalID") 210 c.Assert(account, DeepEquals, &snap.StoreAccount{ 211 ID: "canonicalID", 212 Username: "canonicalUser", 213 DisplayName: "canonicalDisplay", 214 Validation: "verified", 215 }) 216 } 217 218 func (cs *clientSuite) TestStoreAccountNoAssertionFound(c *C) { 219 cs.header = http.Header{} 220 cs.header.Add("X-Ubuntu-Assertions-Count", "0") 221 cs.rsp = "" 222 223 _, err := cs.cli.StoreAccount("canonicalID") 224 c.Assert(err, ErrorMatches, "no assertion found for account-id canonicalID") 225 } 226 227 func (cs *clientSuite) TestClientAssertTypesErrIsWrapped(c *C) { 228 cs.err = errors.New("boom") 229 _, err := cs.cli.AssertionTypes() 230 var e xerrors.Wrapper 231 c.Assert(err, Implements, &e) 232 } 233 234 func (cs *clientSuite) TestClientKnownErrIsWrapped(c *C) { 235 cs.err = errors.New("boom") 236 _, err := cs.cli.Known("foo", nil, nil) 237 var e xerrors.Wrapper 238 c.Assert(err, Implements, &e) 239 }