|
| 1 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +// file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 | + |
| 5 | +package pe_test |
| 6 | + |
| 7 | +import ( |
| 8 | + "io" |
| 9 | + "os" |
| 10 | + "path/filepath" |
| 11 | + "strings" |
| 12 | + "testing" |
| 13 | + |
| 14 | + "github.com/stretchr/testify/assert" |
| 15 | + |
| 16 | + "github.com/siderolabs/talos/internal/pkg/uki/internal/pe" |
| 17 | +) |
| 18 | + |
| 19 | +func TestUKIExtract(t *testing.T) { |
| 20 | + srcFile := "testdata/sd-stub-amd64.efi" |
| 21 | + |
| 22 | + destDir := t.TempDir() |
| 23 | + |
| 24 | + destFile := filepath.Join(destDir, "vmlinuz.efi") |
| 25 | + |
| 26 | + for _, section := range []string{"linux", "initrd", "cmdline"} { |
| 27 | + assert.NoError(t, os.WriteFile(filepath.Join(destDir, section), []byte(section), 0o644)) |
| 28 | + } |
| 29 | + |
| 30 | + assert.NoError(t, pe.AssembleNative(srcFile, destFile, []pe.Section{ |
| 31 | + { |
| 32 | + Name: ".linux", |
| 33 | + Path: filepath.Join(destDir, "linux"), |
| 34 | + Measure: false, |
| 35 | + Append: true, |
| 36 | + }, |
| 37 | + { |
| 38 | + Name: ".initrd", |
| 39 | + Path: filepath.Join(destDir, "initrd"), |
| 40 | + Measure: false, |
| 41 | + Append: true, |
| 42 | + }, |
| 43 | + { |
| 44 | + Name: ".cmdline", |
| 45 | + Path: filepath.Join(destDir, "cmdline"), |
| 46 | + Measure: false, |
| 47 | + Append: true, |
| 48 | + }, |
| 49 | + })) |
| 50 | + |
| 51 | + ukiData, err := pe.Extract(destFile) |
| 52 | + assert.NoError(t, err) |
| 53 | + |
| 54 | + t.Cleanup(func() { |
| 55 | + assert.NoError(t, ukiData.Close()) |
| 56 | + }) |
| 57 | + |
| 58 | + var kernel, initrd, cmdline strings.Builder |
| 59 | + |
| 60 | + _, err = io.Copy(&kernel, ukiData.Kernel) |
| 61 | + assert.NoError(t, err) |
| 62 | + |
| 63 | + assert.Equal(t, "linux", kernel.String()) |
| 64 | + |
| 65 | + _, err = io.Copy(&initrd, ukiData.Initrd) |
| 66 | + assert.NoError(t, err) |
| 67 | + |
| 68 | + assert.Equal(t, "initrd", initrd.String()) |
| 69 | + |
| 70 | + _, err = io.Copy(&cmdline, ukiData.Cmdline) |
| 71 | + assert.NoError(t, err) |
| 72 | + |
| 73 | + assert.Equal(t, "cmdline", cmdline.String()) |
| 74 | +} |
0 commit comments