github.com/stolowski/snapd@v0.0.0-20210407085831-115137ce5a22/boot/export_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2014-2019 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 boot
    21  
    22  import (
    23  	"fmt"
    24  
    25  	"github.com/snapcore/snapd/asserts"
    26  	"github.com/snapcore/snapd/bootloader"
    27  	"github.com/snapcore/snapd/secboot"
    28  	"github.com/snapcore/snapd/seed"
    29  	"github.com/snapcore/snapd/snap"
    30  	"github.com/snapcore/snapd/timings"
    31  )
    32  
    33  func NewCoreBootParticipant(s snap.PlaceInfo, t snap.Type, dev Device) *coreBootParticipant {
    34  	bs, err := bootStateFor(t, dev)
    35  	if err != nil {
    36  		panic(err)
    37  	}
    38  	return &coreBootParticipant{s: s, bs: bs}
    39  }
    40  
    41  func NewCoreKernel(s snap.PlaceInfo, d Device) *coreKernel {
    42  	return &coreKernel{s, bootloaderOptionsForDeviceKernel(d)}
    43  }
    44  
    45  type Trivial = trivial
    46  
    47  func (m *Modeenv) WasRead() bool {
    48  	return m.read
    49  }
    50  
    51  func (m *Modeenv) DeepEqual(m2 *Modeenv) bool {
    52  	return m.deepEqual(m2)
    53  }
    54  
    55  var (
    56  	ModeenvKnownKeys = modeenvKnownKeys
    57  
    58  	MarshalModeenvEntryTo        = marshalModeenvEntryTo
    59  	UnmarshalModeenvValueFromCfg = unmarshalModeenvValueFromCfg
    60  
    61  	NewTrustedAssetsCache = newTrustedAssetsCache
    62  
    63  	ObserveSuccessfulBootWithAssets = observeSuccessfulBootAssets
    64  	SealKeyToModeenv                = sealKeyToModeenv
    65  	ResealKeyToModeenv              = resealKeyToModeenv
    66  	RecoveryBootChainsForSystems    = recoveryBootChainsForSystems
    67  	SealKeyModelParams              = sealKeyModelParams
    68  )
    69  
    70  type BootAssetsMap = bootAssetsMap
    71  type BootCommandLines = bootCommandLines
    72  type TrackedAsset = trackedAsset
    73  
    74  func (t *TrackedAsset) Equals(blName, name, hash string) error {
    75  	equal := t.hash == hash &&
    76  		t.name == name &&
    77  		t.blName == blName
    78  	if !equal {
    79  		return fmt.Errorf("not equal to bootloader %q tracked asset %v:%v", t.blName, t.name, t.hash)
    80  	}
    81  	return nil
    82  }
    83  
    84  func (o *TrustedAssetsInstallObserver) CurrentTrustedBootAssetsMap() BootAssetsMap {
    85  	return o.currentTrustedBootAssetsMap()
    86  }
    87  
    88  func (o *TrustedAssetsInstallObserver) CurrentTrustedRecoveryBootAssetsMap() BootAssetsMap {
    89  	return o.currentTrustedRecoveryBootAssetsMap()
    90  }
    91  
    92  func (o *TrustedAssetsInstallObserver) CurrentDataEncryptionKey() secboot.EncryptionKey {
    93  	return o.dataEncryptionKey
    94  }
    95  
    96  func (o *TrustedAssetsInstallObserver) CurrentSaveEncryptionKey() secboot.EncryptionKey {
    97  	return o.saveEncryptionKey
    98  }
    99  
   100  func MockSecbootSealKeys(f func(keys []secboot.SealKeyRequest, params *secboot.SealKeysParams) error) (restore func()) {
   101  	old := secbootSealKeys
   102  	secbootSealKeys = f
   103  	return func() {
   104  		secbootSealKeys = old
   105  	}
   106  }
   107  
   108  func MockSecbootResealKeys(f func(params *secboot.ResealKeysParams) error) (restore func()) {
   109  	old := secbootResealKeys
   110  	secbootResealKeys = f
   111  	return func() {
   112  		secbootResealKeys = old
   113  	}
   114  }
   115  
   116  func MockSeedReadSystemEssential(f func(seedDir, label string, essentialTypes []snap.Type, tm timings.Measurer) (*asserts.Model, []*seed.Snap, error)) (restore func()) {
   117  	old := seedReadSystemEssential
   118  	seedReadSystemEssential = f
   119  	return func() {
   120  		seedReadSystemEssential = old
   121  	}
   122  }
   123  
   124  func (o *TrustedAssetsUpdateObserver) InjectChangedAsset(blName, assetName, hash string, recovery bool) {
   125  	ta := &trackedAsset{
   126  		blName: blName,
   127  		name:   assetName,
   128  		hash:   hash,
   129  	}
   130  	if !recovery {
   131  		o.changedAssets = append(o.changedAssets, ta)
   132  	} else {
   133  		o.seedChangedAssets = append(o.seedChangedAssets, ta)
   134  	}
   135  }
   136  
   137  type BootAsset = bootAsset
   138  type BootChain = bootChain
   139  type PredictableBootChains = predictableBootChains
   140  
   141  const (
   142  	BootChainEquivalent   = bootChainEquivalent
   143  	BootChainDifferent    = bootChainDifferent
   144  	BootChainUnrevisioned = bootChainUnrevisioned
   145  )
   146  
   147  var (
   148  	ToPredictableBootAsset              = toPredictableBootAsset
   149  	ToPredictableBootChain              = toPredictableBootChain
   150  	ToPredictableBootChains             = toPredictableBootChains
   151  	PredictableBootChainsEqualForReseal = predictableBootChainsEqualForReseal
   152  	BootAssetsToLoadChains              = bootAssetsToLoadChains
   153  	BootAssetLess                       = bootAssetLess
   154  	WriteBootChains                     = writeBootChains
   155  	ReadBootChains                      = readBootChains
   156  	IsResealNeeded                      = isResealNeeded
   157  )
   158  
   159  func (b *bootChain) SetModelAssertion(model *asserts.Model) {
   160  	b.model = model
   161  }
   162  
   163  func (b *bootChain) SetKernelBootFile(kbf bootloader.BootFile) {
   164  	b.kernelBootFile = kbf
   165  }
   166  
   167  func (b *bootChain) KernelBootFile() bootloader.BootFile {
   168  	return b.kernelBootFile
   169  }
   170  
   171  func MockHasFDESetupHook(f func() (bool, error)) (restore func()) {
   172  	oldHasFDESetupHook := HasFDESetupHook
   173  	HasFDESetupHook = f
   174  	return func() {
   175  		HasFDESetupHook = oldHasFDESetupHook
   176  	}
   177  }
   178  
   179  func MockRunFDESetupHook(f func(string, *FDESetupHookParams) ([]byte, error)) (restore func()) {
   180  	oldRunFDESetupHook := RunFDESetupHook
   181  	RunFDESetupHook = f
   182  	return func() { RunFDESetupHook = oldRunFDESetupHook }
   183  }
   184  
   185  func MockResealKeyToModeenvUsingFDESetupHook(f func(string, *asserts.Model, *Modeenv, bool) error) (restore func()) {
   186  	old := resealKeyToModeenvUsingFDESetupHook
   187  	resealKeyToModeenvUsingFDESetupHook = f
   188  	return func() {
   189  		resealKeyToModeenvUsingFDESetupHook = old
   190  	}
   191  }