github.com/Lephar/snapd@v0.0.0-20210825215435-c7fba9cef4d2/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, since time.Time, 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: since.UTC(), 78 until: since.UTC().AddDate(validYears, 0, 0), 79 pubKey: openPGPPubKey, 80 } 81 } 82 83 func BootstrapAccountKeyForTest(authorityID string, pubKey PublicKey) *AccountKey { 84 return MakeAccountKeyForTest(authorityID, pubKey, time.Time{}, 9999) 85 } 86 87 func ExpiredAccountKeyForTest(authorityID string, pubKey PublicKey) *AccountKey { 88 return MakeAccountKeyForTest(authorityID, pubKey, time.Time{}, 1) 89 } 90 91 func MockTimeNow(t time.Time) (restore func()) { 92 oldTimeNow := timeNow 93 timeNow = func() time.Time { 94 return t 95 } 96 return func() { 97 timeNow = oldTimeNow 98 } 99 } 100 101 // define dummy assertion types to use in the tests 102 103 type TestOnly struct { 104 assertionBase 105 } 106 107 func assembleTestOnly(assert assertionBase) (Assertion, error) { 108 // for testing error cases 109 if _, err := checkIntWithDefault(assert.headers, "count", 0); err != nil { 110 return nil, err 111 } 112 return &TestOnly{assert}, nil 113 } 114 115 var TestOnlyType = &AssertionType{"test-only", []string{"primary-key"}, assembleTestOnly, 0} 116 117 type TestOnly2 struct { 118 assertionBase 119 } 120 121 func assembleTestOnly2(assert assertionBase) (Assertion, error) { 122 return &TestOnly2{assert}, nil 123 } 124 125 var TestOnly2Type = &AssertionType{"test-only-2", []string{"pk1", "pk2"}, assembleTestOnly2, 0} 126 127 // TestOnlyDecl is a test-only assertion that mimics snap-declaration 128 // relations with other assertions. 129 type TestOnlyDecl struct { 130 assertionBase 131 } 132 133 func (dcl *TestOnlyDecl) ID() string { 134 return dcl.HeaderString("id") 135 } 136 137 func (dcl *TestOnlyDecl) DevID() string { 138 return dcl.HeaderString("dev-id") 139 } 140 141 func (dcl *TestOnlyDecl) Prerequisites() []*Ref { 142 return []*Ref{ 143 {Type: AccountType, PrimaryKey: []string{dcl.DevID()}}, 144 } 145 } 146 147 func assembleTestOnlyDecl(assert assertionBase) (Assertion, error) { 148 return &TestOnlyDecl{assert}, nil 149 } 150 151 var TestOnlyDeclType = &AssertionType{"test-only-decl", []string{"id"}, assembleTestOnlyDecl, 0} 152 153 // TestOnlyRev is a test-only assertion that mimics snap-revision 154 // relations with other assertions. 155 type TestOnlyRev struct { 156 assertionBase 157 } 158 159 func (rev *TestOnlyRev) H() string { 160 return rev.HeaderString("h") 161 } 162 163 func (rev *TestOnlyRev) ID() string { 164 return rev.HeaderString("id") 165 } 166 167 func (rev *TestOnlyRev) DevID() string { 168 return rev.HeaderString("dev-id") 169 } 170 171 func (rev *TestOnlyRev) Prerequisites() []*Ref { 172 return []*Ref{ 173 {Type: TestOnlyDeclType, PrimaryKey: []string{rev.ID()}}, 174 {Type: AccountType, PrimaryKey: []string{rev.DevID()}}, 175 } 176 } 177 178 func assembleTestOnlyRev(assert assertionBase) (Assertion, error) { 179 return &TestOnlyRev{assert}, nil 180 } 181 182 var TestOnlyRevType = &AssertionType{"test-only-rev", []string{"h"}, assembleTestOnlyRev, 0} 183 184 // TestOnlySeq is a test-only assertion that is sequence-forming. 185 type TestOnlySeq struct { 186 assertionBase 187 seq int 188 } 189 190 func (seq *TestOnlySeq) N() string { 191 return seq.HeaderString("n") 192 } 193 194 func (seq *TestOnlySeq) Sequence() int { 195 return seq.seq 196 } 197 198 func assembleTestOnlySeq(assert assertionBase) (Assertion, error) { 199 seq, err := checkSequence(assert.headers, "sequence") 200 if err != nil { 201 return nil, err 202 } 203 return &TestOnlySeq{ 204 assertionBase: assert, 205 seq: seq, 206 }, nil 207 } 208 209 var TestOnlySeqType = &AssertionType{"test-only-seq", []string{"n", "sequence"}, assembleTestOnlySeq, sequenceForming} 210 211 type TestOnlyNoAuthority struct { 212 assertionBase 213 } 214 215 func assembleTestOnlyNoAuthority(assert assertionBase) (Assertion, error) { 216 if _, err := checkNotEmptyString(assert.headers, "hdr"); err != nil { 217 return nil, err 218 } 219 return &TestOnlyNoAuthority{assert}, nil 220 } 221 222 var TestOnlyNoAuthorityType = &AssertionType{"test-only-no-authority", nil, assembleTestOnlyNoAuthority, noAuthority} 223 224 type TestOnlyNoAuthorityPK struct { 225 assertionBase 226 } 227 228 func assembleTestOnlyNoAuthorityPK(assert assertionBase) (Assertion, error) { 229 return &TestOnlyNoAuthorityPK{assert}, nil 230 } 231 232 var TestOnlyNoAuthorityPKType = &AssertionType{"test-only-no-authority-pk", []string{"pk"}, assembleTestOnlyNoAuthorityPK, noAuthority} 233 234 func init() { 235 typeRegistry[TestOnlyType.Name] = TestOnlyType 236 maxSupportedFormat[TestOnlyType.Name] = 1 237 typeRegistry[TestOnly2Type.Name] = TestOnly2Type 238 typeRegistry[TestOnlyNoAuthorityType.Name] = TestOnlyNoAuthorityType 239 typeRegistry[TestOnlyNoAuthorityPKType.Name] = TestOnlyNoAuthorityPKType 240 formatAnalyzer[TestOnlyType] = func(headers map[string]interface{}, _ []byte) (int, error) { 241 if _, ok := headers["format-1-feature"]; ok { 242 return 1, nil 243 } 244 return 0, nil 245 } 246 typeRegistry[TestOnlyDeclType.Name] = TestOnlyDeclType 247 typeRegistry[TestOnlyRevType.Name] = TestOnlyRevType 248 typeRegistry[TestOnlySeqType.Name] = TestOnlySeqType 249 maxSupportedFormat[TestOnlySeqType.Name] = 2 250 } 251 252 // AccountKeyIsKeyValidAt exposes isKeyValidAt on AccountKey for tests 253 func AccountKeyIsKeyValidAt(ak *AccountKey, when time.Time) bool { 254 return ak.isKeyValidAt(when) 255 } 256 257 // AccountKeyIsKeyValidAssumingCurTimeWithin exposes isKeyValidAssumingCurTimeWithin on AccountKey for tests 258 func AccountKeyIsKeyValidAssumingCurTimeWithin(ak *AccountKey, earliest, latest time.Time) bool { 259 return ak.isKeyValidAssumingCurTimeWithin(earliest, latest) 260 } 261 262 type GPGRunner func(input []byte, args ...string) ([]byte, error) 263 264 func MockRunGPG(mock func(prev GPGRunner, input []byte, args ...string) ([]byte, error)) (restore func()) { 265 prevRunGPG := runGPG 266 runGPG = func(input []byte, args ...string) ([]byte, error) { 267 return mock(prevRunGPG, input, args...) 268 } 269 return func() { 270 runGPG = prevRunGPG 271 } 272 } 273 274 // Headers helpers to test 275 var ( 276 ParseHeaders = parseHeaders 277 AppendEntry = appendEntry 278 ) 279 280 // ParametersForGenerate exposes parametersForGenerate for tests. 281 func (gkm *GPGKeypairManager) ParametersForGenerate(passphrase string, name string) string { 282 return gkm.parametersForGenerate(passphrase, name) 283 } 284 285 // ifacedecls tests 286 var ( 287 CompileAttributeConstraints = compileAttributeConstraints 288 CompileNameConstraints = compileNameConstraints 289 CompilePlugRule = compilePlugRule 290 CompileSlotRule = compileSlotRule 291 ) 292 293 type featureExposer interface { 294 feature(flabel string) bool 295 } 296 297 func RuleFeature(rule featureExposer, flabel string) bool { 298 return rule.feature(flabel) 299 } 300 301 func (b *Batch) DoPrecheck(db *Database) error { 302 return b.precheck(db) 303 } 304 305 // pool tests 306 307 func MakePoolGrouping(elems ...uint16) Grouping { 308 return Grouping(internal.Serialize(elems)) 309 }