1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
|
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2015-2022 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package asserts
import (
"io"
"time"
"github.com/snapcore/snapd/asserts/internal"
"github.com/snapcore/snapd/testutil"
)
// expose test-only things here
var NumAssertionType int
func init() {
NumAssertionType = len(typeRegistry)
}
// v1FixedTimestamp exposed for tests
var V1FixedTimestamp = v1FixedTimestamp
// assembleAndSign exposed for tests
var AssembleAndSignInTest = assembleAndSign
// decodePrivateKey exposed for tests
var DecodePrivateKeyInTest = decodePrivateKey
// NewDecoderStressed makes a Decoder with a stressed setup with the given buffer and maximum sizes.
func NewDecoderStressed(r io.Reader, bufSize, maxHeadersSize, maxBodySize, maxSigSize int) *Decoder {
return (&Decoder{
rd: r,
initialBufSize: bufSize,
maxHeadersSize: maxHeadersSize,
maxSigSize: maxSigSize,
defaultMaxBodySize: maxBodySize,
}).initBuffer()
}
func BootstrapAccountForTest(authorityID string) *Account {
return &Account{
assertionBase: assertionBase{
headers: map[string]any{
"type": "account",
"authority-id": authorityID,
"account-id": authorityID,
"validation": "verified",
},
},
timestamp: time.Now().UTC(),
}
}
func MakeAccountKeyForTest(authorityID string, openPGPPubKey PublicKey, since time.Time, validYears int) *AccountKey {
return MakeAccountKeyForTestWithUntil(authorityID, openPGPPubKey, since, since.AddDate(validYears, 0, 0), validYears)
}
func MakeAccountKeyForTestWithUntil(authorityID string, openPGPPubKey PublicKey, since, until time.Time, validYears int) *AccountKey {
return &AccountKey{
assertionBase: assertionBase{
headers: map[string]any{
"type": "account-key",
"authority-id": authorityID,
"account-id": authorityID,
"public-key-sha3-384": openPGPPubKey.ID(),
},
},
sinceUntil: sinceUntil{
since: since.UTC(),
until: until.UTC(),
},
pubKey: openPGPPubKey,
}
}
func BootstrapAccountKeyForTest(authorityID string, pubKey PublicKey) *AccountKey {
return MakeAccountKeyForTest(authorityID, pubKey, time.Time{}, 9999)
}
func ExpiredAccountKeyForTest(authorityID string, pubKey PublicKey) *AccountKey {
return MakeAccountKeyForTest(authorityID, pubKey, time.Time{}, 1)
}
func MockTimeNow(t time.Time) (restore func()) {
oldTimeNow := timeNow
timeNow = func() time.Time {
return t
}
return func() {
timeNow = oldTimeNow
}
}
// define test assertion types to use in the tests
type TestOnly struct {
assertionBase
}
func assembleTestOnly(assert assertionBase) (Assertion, error) {
// for testing error cases
if _, err := checkIntWithDefault(assert.headers, "count", 0); err != nil {
return nil, err
}
return &TestOnly{assert}, nil
}
var TestOnlyType = &AssertionType{"test-only", []string{"primary-key"}, nil, assembleTestOnly, 0}
type TestOnly2 struct {
assertionBase
}
func assembleTestOnly2(assert assertionBase) (Assertion, error) {
return &TestOnly2{assert}, nil
}
var TestOnly2Type = &AssertionType{"test-only-2", []string{"pk1", "pk2"}, nil, assembleTestOnly2, 0}
// TestOnlyDecl is a test-only assertion that mimics snap-declaration
// relations with other assertions.
type TestOnlyDecl struct {
assertionBase
}
func (dcl *TestOnlyDecl) ID() string {
return dcl.HeaderString("id")
}
func (dcl *TestOnlyDecl) DevID() string {
return dcl.HeaderString("dev-id")
}
func (dcl *TestOnlyDecl) Prerequisites() []*Ref {
return []*Ref{
{Type: AccountType, PrimaryKey: []string{dcl.DevID()}},
}
}
func assembleTestOnlyDecl(assert assertionBase) (Assertion, error) {
return &TestOnlyDecl{assert}, nil
}
var TestOnlyDeclType = &AssertionType{"test-only-decl", []string{"id"}, nil, assembleTestOnlyDecl, 0}
// TestOnlyRev is a test-only assertion that mimics snap-revision
// relations with other assertions.
type TestOnlyRev struct {
assertionBase
}
func (rev *TestOnlyRev) H() string {
return rev.HeaderString("h")
}
func (rev *TestOnlyRev) ID() string {
return rev.HeaderString("id")
}
func (rev *TestOnlyRev) DevID() string {
return rev.HeaderString("dev-id")
}
func (rev *TestOnlyRev) Prerequisites() []*Ref {
return []*Ref{
{Type: TestOnlyDeclType, PrimaryKey: []string{rev.ID()}},
{Type: AccountType, PrimaryKey: []string{rev.DevID()}},
}
}
func assembleTestOnlyRev(assert assertionBase) (Assertion, error) {
return &TestOnlyRev{assert}, nil
}
var TestOnlyRevType = &AssertionType{"test-only-rev", []string{"h"}, nil, assembleTestOnlyRev, 0}
// TestOnlySeq is a test-only assertion that is sequence-forming.
type TestOnlySeq struct {
assertionBase
seq int
}
func (seq *TestOnlySeq) N() string {
return seq.HeaderString("n")
}
func (seq *TestOnlySeq) Sequence() int {
return seq.seq
}
func assembleTestOnlySeq(assert assertionBase) (Assertion, error) {
seq, err := checkSequence(assert.headers, "sequence")
if err != nil {
return nil, err
}
return &TestOnlySeq{
assertionBase: assert,
seq: seq,
}, nil
}
var TestOnlySeqType = &AssertionType{"test-only-seq", []string{"n", "sequence"}, nil, assembleTestOnlySeq, sequenceForming}
type TestOnlyNoAuthority struct {
assertionBase
}
func assembleTestOnlyNoAuthority(assert assertionBase) (Assertion, error) {
if _, err := checkNotEmptyString(assert.headers, "hdr"); err != nil {
return nil, err
}
return &TestOnlyNoAuthority{assert}, nil
}
var TestOnlyNoAuthorityType = &AssertionType{"test-only-no-authority", nil, nil, assembleTestOnlyNoAuthority, noAuthority}
type TestOnlyNoAuthorityPK struct {
assertionBase
}
func assembleTestOnlyNoAuthorityPK(assert assertionBase) (Assertion, error) {
return &TestOnlyNoAuthorityPK{assert}, nil
}
var TestOnlyNoAuthorityPKType = &AssertionType{"test-only-no-authority-pk", []string{"pk"}, nil, assembleTestOnlyNoAuthorityPK, noAuthority}
func init() {
typeRegistry[TestOnlyType.Name] = TestOnlyType
maxSupportedFormat[TestOnlyType.Name] = 1
typeRegistry[TestOnly2Type.Name] = TestOnly2Type
typeRegistry[TestOnlyNoAuthorityType.Name] = TestOnlyNoAuthorityType
typeRegistry[TestOnlyNoAuthorityPKType.Name] = TestOnlyNoAuthorityPKType
formatAnalyzer[TestOnlyType] = func(headers map[string]any, _ []byte) (int, error) {
if _, ok := headers["format-1-feature"]; ok {
return 1, nil
}
return 0, nil
}
typeRegistry[TestOnlyDeclType.Name] = TestOnlyDeclType
typeRegistry[TestOnlyRevType.Name] = TestOnlyRevType
typeRegistry[TestOnlySeqType.Name] = TestOnlySeqType
maxSupportedFormat[TestOnlySeqType.Name] = 2
}
func (ak *AccountKey) CanSign(a Assertion) bool {
return ak.canSign(a)
}
// AccountKeyIsKeyValidAt exposes isKeyValidAt on AccountKey for tests
func AccountKeyIsKeyValidAt(ak *AccountKey, when time.Time) bool {
return ak.isValidAt(when)
}
type sinceUntilLike interface {
isValidAssumingCurTimeWithin(earliest, latest time.Time) bool
}
// IsValidAssumingCurTimeWithin exposes sinceUntil.isValidAssumingCurTimeWithin
func IsValidAssumingCurTimeWithin(su sinceUntilLike, earliest, latest time.Time) bool {
return su.isValidAssumingCurTimeWithin(earliest, latest)
}
type GPGRunner func(input []byte, args ...string) ([]byte, error)
func MockRunGPG(mock func(prev GPGRunner, input []byte, args ...string) ([]byte, error)) (restore func()) {
prevRunGPG := runGPG
runGPG = func(input []byte, args ...string) ([]byte, error) {
return mock(prevRunGPG, input, args...)
}
return func() {
runGPG = prevRunGPG
}
}
func GPGBatchYes() (restore func()) {
gpgBatchYes = true
return func() {
gpgBatchYes = false
}
}
// Headers helpers to test
var (
ParseHeaders = parseHeaders
AppendEntry = appendEntry
)
// ParametersForGenerate exposes parametersForGenerate for tests.
func (gkm *GPGKeypairManager) ParametersForGenerate(passphrase string, name string) string {
return gkm.parametersForGenerate(passphrase, name)
}
// constraint tests
func CompileAttrMatcher(constraints any, allowedOperations, allowedRefs []string) (func(attrs map[string]any, helper AttrMatchContext) error, error) {
// XXX adjust
cc := compileContext{
opts: &compileAttrMatcherOptions{
allowedOperations: allowedOperations,
allowedRefs: allowedRefs,
},
}
matcher, err := compileAttrMatcher(cc, constraints)
if err != nil {
return nil, err
}
domatch := func(attrs map[string]any, helper AttrMatchContext) error {
return matcher.match("", attrs, &attrMatchingContext{
attrWord: "field",
helper: helper,
})
}
return domatch, nil
}
var (
CompileDeviceScopeConstraint = compileDeviceScopeConstraint
)
// ifacedecls tests
var (
CompileAttributeConstraints = compileAttributeConstraints
CompileNameConstraints = compileNameConstraints
CompilePlugRule = compilePlugRule
CompileSlotRule = compileSlotRule
)
type featureExposer interface {
feature(flabel string) bool
}
func RuleFeature(rule featureExposer, flabel string) bool {
return rule.feature(flabel)
}
func (b *Batch) DoPrecheck(db *Database) error {
return b.precheck(db)
}
// pool tests
func MakePoolGrouping(elems ...uint16) Grouping {
return Grouping(internal.Serialize(elems))
}
// fetcher tests
func MockAssertionPrereqs(f func(a Assertion) []*Ref) func() {
r := testutil.Backup(&assertionPrereqs)
assertionPrereqs = f
return r
}
|