Skip to content

Commit

Permalink
efi: add tests for grubHasPrefix
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisccoulson committed Nov 28, 2023
1 parent 313b997 commit d35fb15
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
3 changes: 3 additions & 0 deletions efi/efi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,9 @@ type mockGrubImageHandle struct {
}

func (h *mockGrubImageHandle) Prefix() (string, error) {
if h.grubPrefix == "" {
return "", errors.New("no prefix")
}
return h.grubPrefix, nil
}

Expand Down
1 change: 1 addition & 0 deletions efi/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ var (
// unexported members of some unexported types.
type FwContext = fwContext
type GrubFlags = grubFlags
type GrubHasPrefix = grubHasPrefix
type GrubImageHandle = grubImageHandle
type GrubLoadHandler = grubLoadHandler
type ImageLoadHandler = imageLoadHandler
Expand Down
1 change: 1 addition & 0 deletions efi/image_rules_defs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ func (s *imageRulesDefsSuite) TestFallbackNewImageLoadHandlerGrub(c *C) {
c.Assert(handler, testutil.ConvertibleTo, &GrubLoadHandler{})
c.Check(handler.(*GrubLoadHandler), DeepEquals, new(GrubLoadHandler))
}

func (s *imageRulesDefsSuite) TestFallbackNewImageLoadHandlerNull(c *C) {
// verify that an unrecognized leaf image is recognized by the fallback rules
image := newMockImage()
Expand Down
46 changes: 46 additions & 0 deletions efi/image_rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ func (p *mockImagePredicate) Matches(image PeImageHandle) (bool, error) {

type imageRulesSuite struct {
mockShimImageHandleMixin
mockGrubImageHandleMixin
}

func (s *imageRulesSuite) SetUpTest(c *C) {
s.mockShimImageHandleMixin.SetUpTest(c)
s.mockGrubImageHandleMixin.SetUpTest(c)
}

func (s *imageRulesSuite) TearDownTest(c *C) {
s.mockShimImageHandleMixin.TearDownTest(c)
s.mockGrubImageHandleMixin.TearDownTest(c)
}

var _ = Suite(&imageRulesSuite{})
Expand Down Expand Up @@ -492,6 +503,41 @@ func (s *imageRulesSuite) TestShimVersionErr(c *C) {
c.Check(err, ErrorMatches, `cannot obtain shim version: no version`)
}

func (s *imageRulesSuite) TestGrubHasPrefixTrue1(c *C) {
image := newMockImage().withGrubPrefix("/EFI/ubuntu").newPeImageHandle()

pred := GrubHasPrefix("/EFI/ubuntu")
match, err := pred.Matches(image)
c.Check(err, IsNil)
c.Check(match, testutil.IsTrue)
}

func (s *imageRulesSuite) TestGrubHasPrefixTrue2(c *C) {
image := newMockImage().withGrubPrefix("/EFI/debian").newPeImageHandle()

pred := GrubHasPrefix("/EFI/debian")
match, err := pred.Matches(image)
c.Check(err, IsNil)
c.Check(match, testutil.IsTrue)
}

func (s *imageRulesSuite) TestGrubHasPrefixFalse(c *C) {
image := newMockImage().withGrubPrefix("/EFI/debian").newPeImageHandle()

pred := GrubHasPrefix("/EFI/ubuntu")
match, err := pred.Matches(image)
c.Check(err, IsNil)
c.Check(match, testutil.IsFalse)
}

func (s *imageRulesSuite) TestGrubHasPrefixErr(c *C) {
image := newMockImage().newPeImageHandle()

pred := GrubHasPrefix("/EFI/ubuntu")
_, err := pred.Matches(image)
c.Check(err, ErrorMatches, `cannot obtain grub prefix: no prefix`)
}

func (s *imageRulesSuite) TestImageRulesMatch1(c *C) {
image := newMockImage().newPeImageHandle()

Expand Down

0 comments on commit d35fb15

Please sign in to comment.