| // 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 ( | |
| "compress/gzip" | |
| "io" | |
| "os" | |
| "path/filepath" | |
| "testing" | |
| "cos.googlesource.com/cos/tools.git/src/pkg/utils" | |
| ) | |
| func TestEditUKICmdLine(t *testing.T) { | |
| tests := []struct { | |
| name string | |
| input string | |
| vmlinux string | |
| sedCmd string | |
| want string | |
| arch string | |
| }{ | |
| { | |
| name: "BootA", | |
| input: "testdata/kernelefi.img", | |
| vmlinux: "testdata/vmlinux.gz", | |
| sedCmd: "s|.*|hello world|g", | |
| want: "hello world", | |
| arch: "x86_64", | |
| }, | |
| { | |
| name: "BootA_arm64", | |
| input: "testdata/kernelefi_arm64.img", | |
| vmlinux: "testdata/vmlinux_arm64.gz", | |
| sedCmd: "s|.*|hello world|g", | |
| want: "hello world", | |
| arch: "arm64", | |
| }, | |
| } | |
| var vmlinuxDecompressed string | |
| for _, test := range tests { | |
| test := test | |
| t.Run(test.name, func(t *testing.T) { | |
| tmpDir, err := os.MkdirTemp("", "cos-ukiconfigtest-") | |
| if err != nil { | |
| t.Fatal(err) | |
| } | |
| defer os.RemoveAll(tmpDir) | |
| vmlinuxDecompressed = filepath.Join(tmpDir, "vmlinux") | |
| f, err := os.Create(vmlinuxDecompressed) | |
| if err != nil { | |
| t.Fatal(err) | |
| } | |
| defer f.Close() | |
| r, err := os.Open(test.vmlinux) | |
| if err != nil { | |
| t.Fatal(err) | |
| } | |
| defer r.Close() | |
| gzRead, err := gzip.NewReader(r) | |
| if err != nil { | |
| t.Fatal(err) | |
| } | |
| defer gzRead.Close() | |
| if _, err := io.Copy(f, gzRead); err != nil { | |
| t.Fatal(err) | |
| } | |
| if err := f.Close(); err != nil { | |
| t.Fatal(err) | |
| } | |
| copiedInput := filepath.Join(tmpDir, "input") | |
| if err := utils.CopyFile(test.input, copiedInput); err != nil { | |
| t.Fatal(err) | |
| } | |
| if err := EditUKICmdLine(copiedInput, vmlinuxDecompressed, test.sedCmd); err != nil { | |
| t.Fatalf("EditUKICmdLine(%q, %q, %q) = %v; want nil", test.input, vmlinuxDecompressed, test.sedCmd, err) | |
| } | |
| got, err := NextUKICmdLine(copiedInput, vmlinuxDecompressed) | |
| if got != test.want { | |
| t.Errorf("NextUKICmdLine(%q, %q) = %q; want %q", test.input, vmlinuxDecompressed, got, test.want) | |
| } | |
| }) | |
| } | |
| } |