github.com/linuxboot/fiano@v1.2.0/pkg/uefi/file_test.go (about)

     1  // Copyright 2018 the LinuxBoot Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package uefi
     6  
     7  import (
     8  	"testing"
     9  )
    10  
    11  var (
    12  	// File headers
    13  	// Hardcoded checksums for testing :(
    14  	// I don't know how to do it better without rewriting or calling code under test.
    15  	emptyPadHeader = append(FFGUID[:],
    16  		[]byte{8, EmptyBodyChecksum, byte(FVFileTypePad), 0, FileHeaderMinLength, 0x00, 0x00, 0xF8}...) // Empty pad file header with no data
    17  	goodFreeFormHeader = append(FFGUID[:],
    18  		[]byte{202, EmptyBodyChecksum, byte(FVFileTypeFreeForm), 0, FileHeaderMinLength, 0x00, 0x00, 0xF8}...) // Empty freeform file header with no data
    19  )
    20  
    21  var (
    22  	// File examples
    23  	emptyFile        = []byte{}       // nolint, Empty file
    24  	emptyPadFile     = emptyPadHeader // Empty pad file with no data
    25  	badFreeFormFile  []byte           // File with bad checksum. Should construct fine, but not validate
    26  	goodFreeFormFile []byte           // Good file
    27  )
    28  
    29  func init() {
    30  	goodFreeFormFile = append(goodFreeFormHeader, linuxSec...)
    31  	goodFreeFormFile = append(goodFreeFormFile, smallSec...)
    32  	goodFreeFormFile = append(goodFreeFormFile, []byte{0, 0}...) // Alignment
    33  	goodFreeFormFile = append(goodFreeFormFile, tinySec...)
    34  	goodFreeFormFile[20] = byte(FileHeaderMinLength + len(tinySec) + 2 + len(linuxSec) + len(smallSec))
    35  
    36  	badFreeFormFile = make([]byte, len(goodFreeFormFile))
    37  	copy(badFreeFormFile, goodFreeFormFile)
    38  	badFreeFormFile[16] = 0 // Zero out checksum
    39  }
    40  
    41  func TestNewFile(t *testing.T) {
    42  	var tests = []struct {
    43  		name string
    44  		buf  []byte
    45  		msg  string
    46  	}{
    47  		{"emptyFile", emptyFile, "EOF"},
    48  		{"emptyPadFile", emptyPadFile, ""},
    49  		{"badFreeFormFile", badFreeFormFile, ""},
    50  		{"goodFreeFormFile", goodFreeFormFile, ""},
    51  	}
    52  	for _, test := range tests {
    53  		t.Run(test.name, func(t *testing.T) {
    54  			_, err := NewFile(test.buf)
    55  			if err == nil && test.msg != "" {
    56  				t.Errorf("Error was not returned, expected %v", test.msg)
    57  			} else if err != nil && err.Error() != test.msg {
    58  				t.Errorf("Mismatched Error returned, expected \n%v\n got \n%v\n", test.msg, err.Error())
    59  			}
    60  		})
    61  	}
    62  }