Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkg/visitors: use testing.TempDir and embed test file #440

Merged
merged 2 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ workflows:
jobs:
clean-code:
docker:
- image: cimg/go:1.17
- image: cimg/go:1.22
steps:
- checkout
- run:
Expand All @@ -38,7 +38,7 @@ jobs:
command: test -z "$(gofmt -s -l cmds pkg)"
test:
docker:
- image: cimg/go:1.17
- image: cimg/go:1.22
steps:
- checkout
- run:
Expand All @@ -51,15 +51,15 @@ jobs:
resource_class: medium
race:
docker:
- image: cimg/go:1.17
- image: cimg/go:1.22
steps:
- checkout
- run:
name: Race detector
command: go test -race ./...
coverage:
docker:
- image: cimg/go:1.17
- image: cimg/go:1.22
steps:
- checkout
- run:
Expand All @@ -80,7 +80,7 @@ jobs:
command: bash <(curl -s https://codecov.io/bash)
check_licenses:
docker:
- image: cimg/go:1.17
- image: cimg/go:1.22
steps:
- checkout
- run:
Expand Down
10 changes: 10 additions & 0 deletions integration/files.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright 2024 the LinuxBoot Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package utk_test

import _ "embed"

//go:embed roms/ovmfSECFV.fv
var OVMFSecFV []byte
2 changes: 1 addition & 1 deletion pkg/uefi/fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"github.com/ulikunitz/xz"
)

//no-op writer to minimize logging overhead
// no-op writer to minimize logging overhead
type nopWriter struct{}

func (n *nopWriter) Write(_ []byte) (int, error) { return 0, nil }
Expand Down
2 changes: 1 addition & 1 deletion pkg/uefi/section.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ func (s *Section) GenSecHeader() error {
return nil
}

//ErrOversizeHdr is the error returned by NewSection when the header is oversize.
// ErrOversizeHdr is the error returned by NewSection when the header is oversize.
type ErrOversizeHdr struct {
hdrsiz uintptr
bufsiz int
Expand Down
39 changes: 5 additions & 34 deletions pkg/visitors/extract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,12 @@
package visitors

import (
"os"
"testing"

"github.com/linuxboot/fiano/pkg/log"
utk_test "github.com/linuxboot/fiano/integration"
"github.com/linuxboot/fiano/pkg/uefi"
)

var (
// FV examples
sampleFV []byte // Sample FV from OVMF
)

func init() {
var err error
sampleFV, err = os.ReadFile("../../integration/roms/ovmfSECFV.fv")
if err != nil {
log.Fatalf("%v", err)
}
}

var (
// File headers
// Hardcoded checksums for testing :(
Expand Down Expand Up @@ -93,12 +79,7 @@ func TestExtractAssembleFile(t *testing.T) {
uefi.Attributes.ErasePolarity = 0xFF
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "section-test")

if err != nil {
t.Fatalf("could not create temp dir: %v", err)
}
defer os.RemoveAll(tmpDir)
tmpDir := t.TempDir()

f, err := uefi.NewFile(test.origBuf)
if err != nil {
Expand Down Expand Up @@ -133,18 +114,13 @@ func TestExtractAssembleFV(t *testing.T) {
origBuf []byte
newBuf []byte
}{
{"sampleFV", sampleFV, sampleFV},
{"sampleFV", utk_test.OVMFSecFV, utk_test.OVMFSecFV},
}
// Set erasepolarity to FF
uefi.Attributes.ErasePolarity = 0xFF
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "section-test")

if err != nil {
t.Fatalf("could not create temp dir: %v", err)
}
defer os.RemoveAll(tmpDir)
tmpDir := t.TempDir()

fv, err := uefi.NewFirmwareVolume(test.origBuf, 0, false)
if err != nil {
Expand Down Expand Up @@ -188,12 +164,7 @@ func TestExtractAssembleSection(t *testing.T) {
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "section-test")

if err != nil {
t.Fatalf("could not create temp dir: %v", err)
}
defer os.RemoveAll(tmpDir)
tmpDir := t.TempDir()

s, err := uefi.NewSection(test.buf, test.fileOrder)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion pkg/visitors/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package visitors
import (
"testing"

utk_test "github.com/linuxboot/fiano/integration"
"github.com/linuxboot/fiano/pkg/uefi"
)

Expand All @@ -16,7 +17,7 @@ func TestValidateFV(t *testing.T) {
buf []byte
msgs []string
}{
{"sampleFV", sampleFV, nil},
{"sampleFV", utk_test.OVMFSecFV, nil},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
Expand Down