github.com/chipaca/snappy@v0.0.0-20210104084008-1f06296fe8ad/asserts/export_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2015-2020 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 21 22 import ( 23 "io" 24 "time" 25 26 "github.com/snapcore/snapd/asserts/internal" 27 ) 28 29 // expose test-only things here 30 31 var NumAssertionType = len(typeRegistry) 32 33 // v1FixedTimestamp exposed for tests 34 var V1FixedTimestamp = v1FixedTimestamp 35 36 // assembleAndSign exposed for tests 37 var AssembleAndSignInTest = assembleAndSign 38 39 // decodePrivateKey exposed for tests 40 var DecodePrivateKeyInTest = decodePrivateKey 41 42 // NewDecoderStressed makes a Decoder with a stressed setup with the given buffer and maximum sizes. 43 func NewDecoderStressed(r io.Reader, bufSize, maxHeadersSize, maxBodySize, maxSigSize int) *Decoder { 44 return (&Decoder{ 45 rd: r, 46 initialBufSize: bufSize, 47 maxHeadersSize: maxHeadersSize, 48 maxSigSize: maxSigSize, 49 defaultMaxBodySize: maxBodySize, 50 }).initBuffer() 51 } 52 53 func BootstrapAccountForTest(authorityID string) *Account { 54 return &Account{ 55 assertionBase: assertionBase{ 56 headers: map[string]interface{}{ 57 "type": "account", 58 "authority-id": authorityID, 59 "account-id": authorityID, 60 "validation": "verified", 61 }, 62 }, 63 timestamp: time.Now().UTC(), 64 } 65 } 66 67 func makeAccountKeyForTest(authorityID string, openPGPPubKey PublicKey, validYears int) *AccountKey { 68 return &AccountKey{ 69 assertionBase: assertionBase{ 70 headers: map[string]interface{}{ 71 "type": "account-key", 72 "authority-id": authorityID, 73 "account-id": authorityID, 74 "public-key-sha3-384": openPGPPubKey.ID(), 75 }, 76 }, 77 since: time.Time{}, 78 until: time.Time{}.UTC().AddDate(validYears, 0, 0), 79 pubKey: openPGPPubKey, 80 } 81 } 82 83 func BootstrapAccountKeyForTest(authorityID string, pubKey PublicKey) *AccountKey { 84 return makeAccountKeyForTest(authorityID, pubKey, 9999) 85 } 86 87 func ExpiredAccountKeyForTest(authorityID string, pubKey PublicKey) *AccountKey { 88 return makeAccountKeyForTest(authorityID, pubKey, 1) 89 } 90 91 // define dummy assertion types to use in the tests 92 93 type TestOnly struct { 94 assertionBase 95 } 96 97 func assembleTestOnly(assert assertionBase) (Assertion, error) { 98 // for testing error cases 99 if _, err := checkIntWithDefault(assert.headers, "count", 0); err != nil { 100 return nil, err 101 } 102 return &TestOnly{assert}, nil 103 } 104 105 var TestOnlyType = &AssertionType{"test-only", []string{"primary-key"}, assembleTestOnly, 0} 106 107 type TestOnly2 struct { 108 assertionBase 109 } 110 111 func assembleTestOnly2(assert assertionBase) (Assertion, error) { 112 return &TestOnly2{assert}, nil 113 } 114 115 var TestOnly2Type = &AssertionType{"test-only-2", []string{"pk1", "pk2"}, assembleTestOnly2, 0} 116 117 // TestOnlyDecl is a test-only assertion that mimics snap-declaration 118 // relations with other assertions. 119 type TestOnlyDecl struct { 120 assertionBase 121 } 122 123 func (dcl *TestOnlyDecl) ID() string { 124 return dcl.HeaderString("id") 125 } 126 127 func (dcl *TestOnlyDecl) DevID() string { 128 return dcl.HeaderString("dev-id") 129 } 130 131 func (dcl *TestOnlyDecl) Prerequisites() []*Ref { 132 return []*Ref{ 133 {Type: AccountType, PrimaryKey: []string{dcl.DevID()}}, 134 } 135 } 136 137 func assembleTestOnlyDecl(assert assertionBase) (Assertion, error) { 138 return &TestOnlyDecl{assert}, nil 139 } 140 141 var TestOnlyDeclType = &AssertionType{"test-only-decl", []string{"id"}, assembleTestOnlyDecl, 0} 142 143 // TestOnlyRev is a test-only assertion that mimics snap-revision 144 // relations with other assertions. 145 type TestOnlyRev struct { 146 assertionBase 147 } 148 149 func (rev *TestOnlyRev) H() string { 150 return rev.HeaderString("h") 151 } 152 153 func (rev *TestOnlyRev) ID() string { 154 return rev.HeaderString("id") 155 } 156 157 func (rev *TestOnlyRev) DevID() string { 158 return rev.HeaderString("dev-id") 159 } 160 161 func (rev *TestOnlyRev) Prerequisites() []*Ref { 162 return []*Ref{ 163 {Type: TestOnlyDeclType, PrimaryKey: []string{rev.ID()}}, 164 {Type: AccountType, PrimaryKey: []string{rev.DevID()}}, 165 } 166 } 167 168 func assembleTestOnlyRev(assert assertionBase) (Assertion, error) { 169 return &TestOnlyRev{assert}, nil 170 } 171 172 var TestOnlyRevType = &AssertionType{"test-only-rev", []string{"h"}, assembleTestOnlyRev, 0} 173 174 // TestOnlySeq is a test-only assertion that is sequence-forming. 175 type TestOnlySeq struct { 176 assertionBase 177 seq int 178 } 179 180 func (seq *TestOnlyRev) N() string { 181 return seq.HeaderString("n") 182 } 183 184 func (seq *TestOnlySeq) Sequence() int { 185 return seq.seq 186 } 187 188 func assembleTestOnlySeq(assert assertionBase) (Assertion, error) { 189 seq, err := checkSequence(assert.headers, "sequence") 190 if err != nil { 191 return nil, err 192 } 193 return &TestOnlySeq{ 194 assertionBase: assert, 195 seq: seq, 196 }, nil 197 } 198 199 var TestOnlySeqType = &AssertionType{"test-only-seq", []string{"n", "sequence"}, assembleTestOnlySeq, sequenceForming} 200 201 type TestOnlyNoAuthority struct { 202 assertionBase 203 } 204 205 func assembleTestOnlyNoAuthority(assert assertionBase) (Assertion, error) { 206 if _, err := checkNotEmptyString(assert.headers, "hdr"); err != nil { 207 return nil, err 208 } 209 return &TestOnlyNoAuthority{assert}, nil 210 } 211 212 var TestOnlyNoAuthorityType = &AssertionType{"test-only-no-authority", nil, assembleTestOnlyNoAuthority, noAuthority} 213 214 type TestOnlyNoAuthorityPK struct { 215 assertionBase 216 } 217 218 func assembleTestOnlyNoAuthorityPK(assert assertionBase) (Assertion, error) { 219 return &TestOnlyNoAuthorityPK{assert}, nil 220 } 221 222 var TestOnlyNoAuthorityPKType = &AssertionType{"test-only-no-authority-pk", []string{"pk"}, assembleTestOnlyNoAuthorityPK, noAuthority} 223 224 func init() { 225 typeRegistry[TestOnlyType.Name] = TestOnlyType 226 maxSupportedFormat[TestOnlyType.Name] = 1 227 typeRegistry[TestOnly2Type.Name] = TestOnly2Type 228 typeRegistry[TestOnlyNoAuthorityType.Name] = TestOnlyNoAuthorityType 229 typeRegistry[TestOnlyNoAuthorityPKType.Name] = TestOnlyNoAuthorityPKType 230 formatAnalyzer[TestOnlyType] = func(headers map[string]interface{}, _ []byte) (int, error) { 231 if _, ok := headers["format-1-feature"]; ok { 232 return 1, nil 233 } 234 return 0, nil 235 } 236 typeRegistry[TestOnlyDeclType.Name] = TestOnlyDeclType 237 typeRegistry[TestOnlyRevType.Name] = TestOnlyRevType 238 typeRegistry[TestOnlySeqType.Name] = TestOnlySeqType 239 maxSupportedFormat[TestOnlySeqType.Name] = 2 240 } 241 242 // AccountKeyIsKeyValidAt exposes isKeyValidAt on AccountKey for tests 243 func AccountKeyIsKeyValidAt(ak *AccountKey, when time.Time) bool { 244 return ak.isKeyValidAt(when) 245 } 246 247 type GPGRunner func(input []byte, args ...string) ([]byte, error) 248 249 func MockRunGPG(mock func(prev GPGRunner, input []byte, args ...string) ([]byte, error)) (restore func()) { 250 prevRunGPG := runGPG 251 runGPG = func(input []byte, args ...string) ([]byte, error) { 252 return mock(prevRunGPG, input, args...) 253 } 254 return func() { 255 runGPG = prevRunGPG 256 } 257 } 258 259 // Headers helpers to test 260 var ( 261 ParseHeaders = parseHeaders 262 AppendEntry = appendEntry 263 ) 264 265 // ParametersForGenerate exposes parametersForGenerate for tests. 266 func (gkm *GPGKeypairManager) ParametersForGenerate(passphrase string, name string) string { 267 return gkm.parametersForGenerate(passphrase, name) 268 } 269 270 // ifacedecls tests 271 var ( 272 CompileAttributeConstraints = compileAttributeConstraints 273 CompileNameConstraints = compileNameConstraints 274 CompilePlugRule = compilePlugRule 275 CompileSlotRule = compileSlotRule 276 ) 277 278 type featureExposer interface { 279 feature(flabel string) bool 280 } 281 282 func RuleFeature(rule featureExposer, flabel string) bool { 283 return rule.feature(flabel) 284 } 285 286 func (b *Batch) DoPrecheck(db *Database) error { 287 return b.precheck(db) 288 } 289 290 // pool tests 291 292 func MakePoolGrouping(elems ...uint16) Grouping { 293 return Grouping(internal.Serialize(elems)) 294 }