Skip to content

Commit

Permalink
bootenv: add tests for keyDataScope
Browse files Browse the repository at this point in the history
  • Loading branch information
sespiros committed Oct 27, 2023
1 parent fb2fa76 commit 3642470
Show file tree
Hide file tree
Showing 5 changed files with 567 additions and 8 deletions.
28 changes: 28 additions & 0 deletions bootenv/bootenv_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
* Copyright (C) 2023 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 bootenv

import (
"testing"

. "gopkg.in/check.v1"
)

func Test(t *testing.T) { TestingT(t) }
4 changes: 2 additions & 2 deletions bootenv/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ var (
currentBootMode atomic.Value
)

func SetModel(model secboot.SnapModel) bool {
var SetModel = func(model secboot.SnapModel) bool {
return currentModel.CompareAndSwap(nil, model)
}

func SetBootMode(mode string) bool {
var SetBootMode = func(mode string) bool {
return currentBootMode.CompareAndSwap(nil, mode)
}
58 changes: 58 additions & 0 deletions bootenv/export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
* Copyright (C) 2023 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 bootenv

import "github.com/snapcore/secboot"

var (
ComputeSnapModelHash = computeSnapModelHash
)

func MockSetModel(f func(secboot.SnapModel) bool) (restore func()) {
origSetModel := SetModel
SetModel = f
return func() {
SetModel = origSetModel
}
}

func MockSetBootMode(f func(string) bool) (restore func()) {
origSetBootMode := SetBootMode
SetBootMode = f
return func() {
SetBootMode = origSetBootMode
}
}

func MockLoadCurrentModel(f func() (secboot.SnapModel, error)) (restore func()) {
origLoadCurrentModel := loadCurrentModel
loadCurrentModel = f
return func() {
loadCurrentModel = origLoadCurrentModel
}
}

func MockLoadCurrenBootMode(f func() (string, error)) (restore func()) {
origLoadCurrentBootMode := loadCurrentBootMode
loadCurrentBootMode = f
return func() {
loadCurrentBootMode = origLoadCurrentBootMode
}
}
28 changes: 22 additions & 6 deletions bootenv/keydata.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,22 @@ func (d *KeyDataScope) SetAuthorizedBootModes(key secboot.PrimaryKey, role strin
return d.authorize(key, role)
}

var loadCurrentModel = func() (secboot.SnapModel, error) {
model, ok := currentModel.Load().(secboot.SnapModel)
if !ok {
return nil, errors.New("SetModel hasn't been called yet")
}
return model, nil
}

var loadCurrentBootMode = func() (string, error) {
mode, ok := currentBootMode.Load().(string)
if !ok {
return "", errors.New("SetBootMode hasn't been called yet")
}
return mode, nil
}

func (d *KeyDataScope) IsBootEnvironmentAuthorized() error {
ok, err := d.isAuthorized()
if err != nil {
Expand All @@ -399,9 +415,9 @@ func (d *KeyDataScope) IsBootEnvironmentAuthorized() error {
}

if len(d.data.Params.ModelDigests.Digests) > 0 {
model, ok := currentModel.Load().(secboot.SnapModel)
if !ok {
return errors.New("SetModel hasn't been called yet")
model, err := loadCurrentModel()
if err != nil {
return err
}

currentModelDigest, err := computeSnapModelHash(crypto.Hash(alg), model)
Expand All @@ -422,9 +438,9 @@ func (d *KeyDataScope) IsBootEnvironmentAuthorized() error {
}

if len(d.data.Params.Modes) > 0 {
mode, ok := currentBootMode.Load().(string)
if !ok {
return errors.New("SetBootMode hasn't been called yet")
mode, err := loadCurrentBootMode()
if err != nil {
return err
}

found := false
Expand Down
Loading

0 comments on commit 3642470

Please sign in to comment.