github.com/rigado/snapd@v2.42.5-go-mod+incompatible/asserts/export_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2015-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 21 22 import ( 23 "io" 24 "time" 25 ) 26 27 // expose test-only things here 28 29 var NumAssertionType = len(typeRegistry) 30 31 // v1FixedTimestamp exposed for tests 32 var V1FixedTimestamp = v1FixedTimestamp 33 34 // assembleAndSign exposed for tests 35 var AssembleAndSignInTest = assembleAndSign 36 37 // decodePrivateKey exposed for tests 38 var DecodePrivateKeyInTest = decodePrivateKey 39 40 // NewDecoderStressed makes a Decoder with a stressed setup with the given buffer and maximum sizes. 41 func NewDecoderStressed(r io.Reader, bufSize, maxHeadersSize, maxBodySize, maxSigSize int) *Decoder { 42 return (&Decoder{ 43 rd: r, 44 initialBufSize: bufSize, 45 maxHeadersSize: maxHeadersSize, 46 maxSigSize: maxSigSize, 47 defaultMaxBodySize: maxBodySize, 48 }).initBuffer() 49 } 50 51 func BootstrapAccountForTest(authorityID string) *Account { 52 return &Account{ 53 assertionBase: assertionBase{ 54 headers: map[string]interface{}{ 55 "type": "account", 56 "authority-id": authorityID, 57 "account-id": authorityID, 58 "validation": "verified", 59 }, 60 }, 61 timestamp: time.Now().UTC(), 62 } 63 } 64 65 func makeAccountKeyForTest(authorityID string, openPGPPubKey PublicKey, validYears int) *AccountKey { 66 return &AccountKey{ 67 assertionBase: assertionBase{ 68 headers: map[string]interface{}{ 69 "type": "account-key", 70 "authority-id": authorityID, 71 "account-id": authorityID, 72 "public-key-sha3-384": openPGPPubKey.ID(), 73 }, 74 }, 75 since: time.Time{}, 76 until: time.Time{}.UTC().AddDate(validYears, 0, 0), 77 pubKey: openPGPPubKey, 78 } 79 } 80 81 func BootstrapAccountKeyForTest(authorityID string, pubKey PublicKey) *AccountKey { 82 return makeAccountKeyForTest(authorityID, pubKey, 9999) 83 } 84 85 func ExpiredAccountKeyForTest(authorityID string, pubKey PublicKey) *AccountKey { 86 return makeAccountKeyForTest(authorityID, pubKey, 1) 87 } 88 89 // define dummy assertion types to use in the tests 90 91 type TestOnly struct { 92 assertionBase 93 } 94 95 func assembleTestOnly(assert assertionBase) (Assertion, error) { 96 // for testing error cases 97 if _, err := checkIntWithDefault(assert.headers, "count", 0); err != nil { 98 return nil, err 99 } 100 return &TestOnly{assert}, nil 101 } 102 103 var TestOnlyType = &AssertionType{"test-only", []string{"primary-key"}, assembleTestOnly, 0} 104 105 type TestOnly2 struct { 106 assertionBase 107 } 108 109 func assembleTestOnly2(assert assertionBase) (Assertion, error) { 110 return &TestOnly2{assert}, nil 111 } 112 113 var TestOnly2Type = &AssertionType{"test-only-2", []string{"pk1", "pk2"}, assembleTestOnly2, 0} 114 115 type TestOnlyNoAuthority struct { 116 assertionBase 117 } 118 119 func assembleTestOnlyNoAuthority(assert assertionBase) (Assertion, error) { 120 if _, err := checkNotEmptyString(assert.headers, "hdr"); err != nil { 121 return nil, err 122 } 123 return &TestOnlyNoAuthority{assert}, nil 124 } 125 126 var TestOnlyNoAuthorityType = &AssertionType{"test-only-no-authority", nil, assembleTestOnlyNoAuthority, noAuthority} 127 128 type TestOnlyNoAuthorityPK struct { 129 assertionBase 130 } 131 132 func assembleTestOnlyNoAuthorityPK(assert assertionBase) (Assertion, error) { 133 return &TestOnlyNoAuthorityPK{assert}, nil 134 } 135 136 var TestOnlyNoAuthorityPKType = &AssertionType{"test-only-no-authority-pk", []string{"pk"}, assembleTestOnlyNoAuthorityPK, noAuthority} 137 138 func init() { 139 typeRegistry[TestOnlyType.Name] = TestOnlyType 140 maxSupportedFormat[TestOnlyType.Name] = 1 141 typeRegistry[TestOnly2Type.Name] = TestOnly2Type 142 typeRegistry[TestOnlyNoAuthorityType.Name] = TestOnlyNoAuthorityType 143 typeRegistry[TestOnlyNoAuthorityPKType.Name] = TestOnlyNoAuthorityPKType 144 formatAnalyzer[TestOnlyType] = func(headers map[string]interface{}, _ []byte) (int, error) { 145 if _, ok := headers["format-1-feature"]; ok { 146 return 1, nil 147 } 148 return 0, nil 149 } 150 } 151 152 // AccountKeyIsKeyValidAt exposes isKeyValidAt on AccountKey for tests 153 func AccountKeyIsKeyValidAt(ak *AccountKey, when time.Time) bool { 154 return ak.isKeyValidAt(when) 155 } 156 157 type GPGRunner func(input []byte, args ...string) ([]byte, error) 158 159 func MockRunGPG(mock func(prev GPGRunner, input []byte, args ...string) ([]byte, error)) (restore func()) { 160 prevRunGPG := runGPG 161 runGPG = func(input []byte, args ...string) ([]byte, error) { 162 return mock(prevRunGPG, input, args...) 163 } 164 return func() { 165 runGPG = prevRunGPG 166 } 167 } 168 169 // Headers helpers to test 170 var ( 171 ParseHeaders = parseHeaders 172 AppendEntry = appendEntry 173 ) 174 175 // ParametersForGenerate exposes parametersForGenerate for tests. 176 func (gkm *GPGKeypairManager) ParametersForGenerate(passphrase string, name string) string { 177 return gkm.parametersForGenerate(passphrase, name) 178 } 179 180 // ifacedecls tests 181 var ( 182 CompileAttributeConstraints = compileAttributeConstraints 183 CompilePlugRule = compilePlugRule 184 CompileSlotRule = compileSlotRule 185 ) 186 187 type featureExposer interface { 188 feature(flabel string) bool 189 } 190 191 func RuleFeature(rule featureExposer, flabel string) bool { 192 return rule.feature(flabel) 193 } 194 195 func (b *Batch) DoPrecheck(db *Database) error { 196 return b.precheck(db) 197 }