github.com/meulengracht/snapd@v0.0.0-20210719210640-8bde69bcc84e/asserts/fetcher_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 asserts_test 21 22 import ( 23 "crypto" 24 "fmt" 25 "time" 26 27 "golang.org/x/crypto/sha3" 28 . "gopkg.in/check.v1" 29 30 "github.com/snapcore/snapd/asserts" 31 "github.com/snapcore/snapd/asserts/assertstest" 32 ) 33 34 type fetcherSuite struct { 35 storeSigning *assertstest.StoreStack 36 } 37 38 var _ = Suite(&fetcherSuite{}) 39 40 func (s *fetcherSuite) SetUpTest(c *C) { 41 s.storeSigning = assertstest.NewStoreStack("can0nical", nil) 42 } 43 44 func fakeSnap(rev int) []byte { 45 fake := fmt.Sprintf("hsqs________________%d", rev) 46 return []byte(fake) 47 } 48 49 func fakeHash(rev int) []byte { 50 h := sha3.Sum384(fakeSnap(rev)) 51 return h[:] 52 } 53 54 func makeDigest(rev int) string { 55 d, err := asserts.EncodeDigest(crypto.SHA3_384, fakeHash(rev)) 56 if err != nil { 57 panic(err) 58 } 59 return string(d) 60 } 61 62 func (s *fetcherSuite) prereqSnapAssertions(c *C, revisions ...int) { 63 dev1Acct := assertstest.NewAccount(s.storeSigning, "developer1", nil, "") 64 err := s.storeSigning.Add(dev1Acct) 65 c.Assert(err, IsNil) 66 67 headers := map[string]interface{}{ 68 "series": "16", 69 "snap-id": "snap-id-1", 70 "snap-name": "foo", 71 "publisher-id": dev1Acct.AccountID(), 72 "timestamp": time.Now().Format(time.RFC3339), 73 } 74 snapDecl, err := s.storeSigning.Sign(asserts.SnapDeclarationType, headers, nil, "") 75 c.Assert(err, IsNil) 76 err = s.storeSigning.Add(snapDecl) 77 c.Assert(err, IsNil) 78 79 for _, rev := range revisions { 80 headers = map[string]interface{}{ 81 "series": "16", 82 "snap-id": "snap-id-1", 83 "snap-sha3-384": makeDigest(rev), 84 "snap-size": "1000", 85 "snap-revision": fmt.Sprintf("%d", rev), 86 "developer-id": dev1Acct.AccountID(), 87 "timestamp": time.Now().Format(time.RFC3339), 88 } 89 snapRev, err := s.storeSigning.Sign(asserts.SnapRevisionType, headers, nil, "") 90 c.Assert(err, IsNil) 91 err = s.storeSigning.Add(snapRev) 92 c.Assert(err, IsNil) 93 } 94 } 95 96 func (s *fetcherSuite) TestFetch(c *C) { 97 s.prereqSnapAssertions(c, 10) 98 99 db, err := asserts.OpenDatabase(&asserts.DatabaseConfig{ 100 Backstore: asserts.NewMemoryBackstore(), 101 Trusted: s.storeSigning.Trusted, 102 }) 103 c.Assert(err, IsNil) 104 105 ref := &asserts.Ref{ 106 Type: asserts.SnapRevisionType, 107 PrimaryKey: []string{makeDigest(10)}, 108 } 109 110 retrieve := func(ref *asserts.Ref) (asserts.Assertion, error) { 111 return ref.Resolve(s.storeSigning.Find) 112 } 113 114 f := asserts.NewFetcher(db, retrieve, db.Add) 115 116 err = f.Fetch(ref) 117 c.Assert(err, IsNil) 118 119 snapRev, err := ref.Resolve(db.Find) 120 c.Assert(err, IsNil) 121 c.Check(snapRev.(*asserts.SnapRevision).SnapRevision(), Equals, 10) 122 123 snapDecl, err := db.Find(asserts.SnapDeclarationType, map[string]string{ 124 "series": "16", 125 "snap-id": "snap-id-1", 126 }) 127 c.Assert(err, IsNil) 128 c.Check(snapDecl.(*asserts.SnapDeclaration).SnapName(), Equals, "foo") 129 } 130 131 func (s *fetcherSuite) TestSave(c *C) { 132 s.prereqSnapAssertions(c, 10) 133 134 db, err := asserts.OpenDatabase(&asserts.DatabaseConfig{ 135 Backstore: asserts.NewMemoryBackstore(), 136 Trusted: s.storeSigning.Trusted, 137 }) 138 c.Assert(err, IsNil) 139 140 retrieve := func(ref *asserts.Ref) (asserts.Assertion, error) { 141 return ref.Resolve(s.storeSigning.Find) 142 } 143 144 f := asserts.NewFetcher(db, retrieve, db.Add) 145 146 ref := &asserts.Ref{ 147 Type: asserts.SnapRevisionType, 148 PrimaryKey: []string{makeDigest(10)}, 149 } 150 rev, err := ref.Resolve(s.storeSigning.Find) 151 c.Assert(err, IsNil) 152 153 err = f.Save(rev) 154 c.Assert(err, IsNil) 155 156 snapRev, err := ref.Resolve(db.Find) 157 c.Assert(err, IsNil) 158 c.Check(snapRev.(*asserts.SnapRevision).SnapRevision(), Equals, 10) 159 160 snapDecl, err := db.Find(asserts.SnapDeclarationType, map[string]string{ 161 "series": "16", 162 "snap-id": "snap-id-1", 163 }) 164 c.Assert(err, IsNil) 165 c.Check(snapDecl.(*asserts.SnapDeclaration).SnapName(), Equals, "foo") 166 }