github.com/linuxboot/fiano@v1.2.0/pkg/visitors/remove_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 visitors 6 7 import ( 8 "testing" 9 ) 10 11 func TestRemoveNoPad(t *testing.T) { 12 f := parseImage(t) 13 14 count := &Count{} 15 if err := count.Run(f); err != nil { 16 t.Fatal(err) 17 } 18 padCount := count.FileTypeCount["EFI_FV_FILETYPE_FFS_PAD"] 19 // Apply the visitor. 20 remove := &Remove{ 21 Predicate: FindFileGUIDPredicate(*testGUID), 22 Pad: false, 23 } 24 if err := remove.Run(f); err != nil { 25 t.Fatal(err) 26 } 27 28 // We expect one match. 29 if len(remove.Matches) != 1 { 30 t.Fatalf("got %d matches; expected 1", len(remove.Matches)) 31 } 32 33 // We expect no match. 34 results := find(t, f, testGUID) 35 if len(results) != 0 { 36 t.Errorf("got %d matches; expected 0", len(results)) 37 } 38 // We expect the same number of pad files 39 if err := count.Run(f); err != nil { 40 t.Fatal(err) 41 } 42 if newPadCount := count.FileTypeCount["EFI_FV_FILETYPE_FFS_PAD"]; padCount != newPadCount { 43 t.Errorf("differing number of pad files: originally had %v, after removal got %v", 44 padCount, newPadCount) 45 } 46 } 47 48 func TestRemovePad(t *testing.T) { 49 f := parseImage(t) 50 51 count := &Count{} 52 if err := count.Run(f); err != nil { 53 t.Fatal(err) 54 } 55 padCount := count.FileTypeCount["EFI_FV_FILETYPE_FFS_PAD"] 56 // Apply the visitor. 57 remove := &Remove{ 58 Predicate: FindFileGUIDPredicate(*testGUID), 59 Pad: true, 60 } 61 if err := remove.Run(f); err != nil { 62 t.Fatal(err) 63 } 64 65 // We expect one match. 66 if len(remove.Matches) != 1 { 67 t.Fatalf("got %d matches; expected 1", len(remove.Matches)) 68 } 69 70 // We expect no match. 71 results := find(t, f, testGUID) 72 if len(results) != 0 { 73 t.Fatalf("got %d matches; expected 0", len(results)) 74 } 75 // We expect one more pad file 76 if err := count.Run(f); err != nil { 77 t.Fatal(err) 78 } 79 if newPadCount := count.FileTypeCount["EFI_FV_FILETYPE_FFS_PAD"]; padCount+1 != newPadCount { 80 t.Errorf("differing number of pad files: expected %v, got %v", 81 padCount+1, newPadCount) 82 } 83 } 84 85 func TestRemoveExcept(t *testing.T) { 86 f := parseImage(t) 87 88 pred, err := FindFilePredicate(dxeCoreGUID.String()) 89 if err != nil { 90 t.Fatal(err) 91 } 92 remove := &Remove{ 93 Predicate: pred, 94 RemoveDxes: true, 95 } 96 if err := remove.Run(f); err != nil { 97 t.Fatal(err) 98 } 99 100 // We expect no more dxe drivers since we only kept the core. 101 count := &Count{} 102 if err := count.Run(f); err != nil { 103 t.Fatal(err) 104 } 105 dxeCount := count.FileTypeCount["EFI_FV_FILETYPE_DRIVER"] 106 coreCount := count.FileTypeCount["EFI_FV_FILETYPE_DXE_CORE"] 107 if dxeCount != 0 { 108 t.Errorf("expected no more drivers, got %v", dxeCount) 109 } 110 if coreCount != 1 { 111 t.Errorf("expected one dxecore remaining, got %v", coreCount) 112 } 113 114 } 115 116 func TestRemoveExceptBlackList(t *testing.T) { 117 // The following blacklist contains corner case: 118 // - start, mid or end of existing names but no full name 119 // - start, mid or end of existing GUID but no full GUID 120 // so it should behave as an empty blacklist, ie remove all 121 var blacklists = []string{ 122 "INEXISTING_FILENAME\nINEXISTING_FILENAME2", 123 "Isa\nDisk\nDxe", 124 "D6A2CB7F\n11E3\n9920A733700A", 125 } 126 for _, blacklist := range blacklists { 127 f := parseImage(t) 128 129 blackListRegex, err := parseBlackList("(embedded)", blacklist) 130 if err != nil { 131 t.Fatal(err) 132 } 133 134 pred, err := FindFilePredicate(blackListRegex) 135 if err != nil { 136 t.Fatal(err) 137 } 138 remove := &Remove{ 139 Predicate: pred, 140 RemoveDxes: true, 141 } 142 if err := remove.Run(f); err != nil { 143 t.Fatal(err) 144 } 145 146 // We expect no more dxe drivers since we only kept the core. 147 count := &Count{} 148 if err := count.Run(f); err != nil { 149 t.Fatal(err) 150 } 151 dxeCount := count.FileTypeCount["EFI_FV_FILETYPE_DRIVER"] 152 coreCount := count.FileTypeCount["EFI_FV_FILETYPE_DXE_CORE"] 153 if dxeCount != 0 { 154 t.Errorf("expected no more drivers, got %v", dxeCount) 155 } 156 if coreCount != 0 { 157 t.Errorf("expected no more dxecore, got %v", coreCount) 158 } 159 } 160 }