github.com/linuxboot/fiano@v1.2.0/cmds/create-ffs/create-ffs_test.go (about) 1 // Copyright 2019 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 main 6 7 import ( 8 "fmt" 9 "testing" 10 11 "github.com/linuxboot/fiano/pkg/guid" 12 "github.com/linuxboot/fiano/pkg/uefi" 13 ) 14 15 func compareErrors(expected string, err error) error { 16 if expected != "" { 17 if err == nil { 18 return fmt.Errorf("error was not returned, expected %v", expected) 19 } 20 if expected != err.Error() { 21 return fmt.Errorf("mismatched error returned, expected \n%v\n got \n%v", 22 expected, err.Error()) 23 } 24 } else if err != nil { 25 return fmt.Errorf("error was not expected, got %v", err) 26 } 27 return nil 28 } 29 30 func compareOps(expectedOps []uefi.DepExOp, ops []uefi.DepExOp) error { 31 if expectedOps != nil { 32 if ops == nil { 33 return fmt.Errorf("expected ops: %v, got nil", expectedOps) 34 } 35 if elen, olen := len(expectedOps), len(ops); elen != olen { 36 return fmt.Errorf("different lenghts of depexes expected \n%v\n got \n%v", 37 elen, olen) 38 } 39 for i := range expectedOps { 40 if expectedOps[i].OpCode != ops[i].OpCode { 41 return fmt.Errorf("different opcodes! expected %v, got %v", 42 expectedOps[i].OpCode, ops[i].OpCode) 43 } 44 if expectedOps[i].GUID != nil { 45 if ops[i].GUID == nil { 46 return fmt.Errorf("expected GUID %v, got nil", 47 *expectedOps[i].GUID) 48 } 49 if *expectedOps[i].GUID != *ops[i].GUID { 50 return fmt.Errorf("mismatched GUID, expected %v, got %v", 51 *expectedOps[i].GUID, *ops[i].GUID) 52 } 53 } else if ops[i].GUID != nil { 54 return fmt.Errorf("expected no GUIDs, got %v", *ops[i].GUID) 55 } 56 } 57 } else if ops != nil { 58 return fmt.Errorf("expected no ops, got %v", ops) 59 } 60 return nil 61 } 62 63 func TestCreateDepExes(t *testing.T) { 64 var tests = []struct { 65 name string 66 input string 67 ops []uefi.DepExOp 68 msg string 69 }{ 70 {"trueDepEx", "TRUE", []uefi.DepExOp{{OpCode: "TRUE"}, {OpCode: "END"}}, ""}, 71 {"pushone", "01234567-89AB-CDEF-0123-456789ABCDEF", 72 []uefi.DepExOp{ 73 {OpCode: "PUSH", GUID: guid.MustParse("01234567-89AB-CDEF-0123-456789ABCDEF")}, 74 {OpCode: "END"}}, ""}, 75 {"pushtwo", "01234567-89AB-CDEF-0123-456789ABCDEF 01234567-89AB-CDEF-0123-456789ABCDEF", 76 []uefi.DepExOp{ 77 {OpCode: "PUSH", GUID: guid.MustParse("01234567-89AB-CDEF-0123-456789ABCDEF")}, 78 {OpCode: "PUSH", GUID: guid.MustParse("01234567-89AB-CDEF-0123-456789ABCDEF")}, 79 {OpCode: "AND"}, 80 {OpCode: "END"}}, ""}, 81 {"badGUID", "ABC", nil, "guid string not correct, need string of the format \n01234567-89AB-CDEF-0123-456789ABCDEF" + 82 "\n, got \nABC"}, 83 } 84 for _, test := range tests { 85 t.Run(test.name, func(t *testing.T) { 86 outputs, err := createDepExes(test.input) 87 if err = compareErrors(test.msg, err); err != nil { 88 t.Fatal(err) 89 } 90 if err = compareOps(test.ops, outputs); err != nil { 91 t.Fatal(err) 92 } 93 }) 94 } 95 }