| // Copyright 2026 Google LLC |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| |
| package cosboot |
| |
| import ( |
| "testing" |
| ) |
| |
| // createPE creates a minimal PE/COFF file that can be interpreted by debug/pe. |
| // This includes a minimal MS-DOS stub, Signature, and the part of the COFF file |
| // header necessary to determine architecture. |
| // |
| // More details: https://learn.microsoft.com/en-us/windows/win32/debug/pe-format#ms-dos-stub-image-only |
| func createPE(machine uint16) []byte { |
| // Minimal PE header |
| data := make([]byte, 0x80) |
| // DOS header |
| copy(data[0:], "MZ") |
| // Set file offset to PE signature |
| data[0x3c] = 0x40 |
| // PE signature |
| copy(data[0x40:], "PE\x00\x00") |
| // COFF header starts at 0x44 |
| // Machine (little endian) |
| data[0x44] = byte(machine) |
| data[0x45] = byte(machine >> 8) |
| return data |
| } |
| |
| func TestEFIArch_Success(t *testing.T) { |
| tests := []struct { |
| name string |
| img []byte |
| want string |
| }{ |
| { |
| name: "x86_64", |
| img: createPE(0x8664), |
| want: "x86_64", |
| }, |
| { |
| name: "arm64", |
| img: createPE(0xAA64), |
| want: "arm64", |
| }, |
| } |
| for _, test := range tests { |
| test := test |
| t.Run(test.name, func(t *testing.T) { |
| got, err := EFIArch(test.img) |
| if err != nil { |
| t.Fatalf("archFromImage() error = %v, want nil", err) |
| return |
| } |
| if got != test.want { |
| t.Errorf("archFromImage() = %v, want %v", got, test.want) |
| } |
| }) |
| } |
| } |
| |
| func TestEFIArch_Failure(t *testing.T) { |
| if got, err := EFIArch([]byte("invalid")); err == nil { |
| t.Fatalf("archFromImage() = %v, want error", got) |
| } |
| } |