github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/pkg/boot/boottest/same_image.go (about) 1 // Copyright 2020 the u-root 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 boottest 6 7 import ( 8 "fmt" 9 "io" 10 11 "github.com/u-root/u-root/pkg/boot" 12 "github.com/u-root/u-root/pkg/uio" 13 ) 14 15 func mustReadAll(r io.ReaderAt) string { 16 if r == nil { 17 return "" 18 } 19 b, err := uio.ReadAll(r) 20 if err != nil { 21 return fmt.Sprintf("read error: %s", err) 22 } 23 return string(b) 24 } 25 26 // SameBootImage compares the contents of given boot images, but not the 27 // underlying URLs. 28 // 29 // Works for Linux and Multiboot images. 30 func SameBootImage(got, want boot.OSImage) error { 31 if got.Label() != want.Label() { 32 return fmt.Errorf("got image label %s, want %s", got.Label(), want.Label()) 33 } 34 35 if gotLinux, ok := got.(*boot.LinuxImage); ok { 36 wantLinux, ok := want.(*boot.LinuxImage) 37 if !ok { 38 return fmt.Errorf("got image %s is Linux image, but %s is not", got, want) 39 } 40 41 // Same kernel? 42 if !uio.ReaderAtEqual(gotLinux.Kernel, wantLinux.Kernel) { 43 return fmt.Errorf("got kernel %s, want %s", mustReadAll(gotLinux.Kernel), mustReadAll(wantLinux.Kernel)) 44 } 45 46 // Same initrd? 47 if !uio.ReaderAtEqual(gotLinux.Initrd, wantLinux.Initrd) { 48 return fmt.Errorf("got initrd %s, want %s", mustReadAll(gotLinux.Initrd), mustReadAll(wantLinux.Initrd)) 49 } 50 51 // Same cmdline? 52 if gotLinux.Cmdline != wantLinux.Cmdline { 53 return fmt.Errorf("got cmdline %s, want %s", gotLinux.Cmdline, wantLinux.Cmdline) 54 } 55 return nil 56 } 57 58 if gotMB, ok := got.(*boot.MultibootImage); ok { 59 wantMB, ok := want.(*boot.MultibootImage) 60 if !ok { 61 return fmt.Errorf("got image %s is Multiboot image, but %s is not", got, want) 62 } 63 64 // Same kernel? 65 if !uio.ReaderAtEqual(gotMB.Kernel, wantMB.Kernel) { 66 return fmt.Errorf("got kernel %s, want %s", mustReadAll(gotMB.Kernel), mustReadAll(wantMB.Kernel)) 67 } 68 69 // Same cmdline? 70 if gotMB.Cmdline != wantMB.Cmdline { 71 return fmt.Errorf("got cmdline %s, want %s", gotMB.Cmdline, wantMB.Cmdline) 72 } 73 74 if len(gotMB.Modules) != len(wantMB.Modules) { 75 return fmt.Errorf("got %d modules, want %d modules", len(gotMB.Modules), len(wantMB.Modules)) 76 } 77 78 for i := range gotMB.Modules { 79 g := gotMB.Modules[i] 80 w := wantMB.Modules[i] 81 if g.Cmdline != w.Cmdline { 82 return fmt.Errorf("module %d got name %s, want %s", i, g.Cmdline, w.Cmdline) 83 } 84 if !uio.ReaderAtEqual(g.Module, w.Module) { 85 return fmt.Errorf("got kernel %s, want %s", mustReadAll(g.Module), mustReadAll(w.Module)) 86 } 87 } 88 return nil 89 } 90 91 return fmt.Errorf("image not supported") 92 }