github.com/linuxboot/fiano@v1.2.0/cmds/fmap/fmap_test.go (about) 1 // Copyright 2017-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 main 6 7 import ( 8 "bytes" 9 "os" 10 "path/filepath" 11 "testing" 12 13 "github.com/u-root/u-root/pkg/testutil" 14 ) 15 16 const testFlash = "fake_test.flash" 17 18 var tests = []struct { 19 cmd string 20 out string 21 }{ 22 // Test summary 23 { 24 cmd: "summary", 25 out: `Fmap found at 0x5f74: 26 Signature: __FMAP__ 27 VerMajor: 1 28 VerMinor: 0 29 Base: 0xcafebabedeadbeef 30 Size: 0x44332211 31 Name: Fake flash 32 NAreas: 2 33 Areas[0]: 34 Offset: 0xdeadbeef 35 Size: 0x11111111 36 Name: Area Number 1Hello 37 Flags: 0x1013 (STATIC|COMPRESSED|0x1010) 38 Areas[1]: 39 Offset: 0xcafebabe 40 Size: 0x22222222 41 Name: Area Number 2xxxxxxxxxxxxxxxxxxx 42 Flags: 0x0 (0x0) 43 `, 44 }, 45 // Test usage 46 { 47 cmd: "usage", 48 out: `Legend: '.' - full (0xff), '0' - zero (0x00), '#' - mixed 49 0x00000000: 0..### 50 Blocks: 6 (100.0%) 51 Full (0xff): 2 (33.3%) 52 Empty (0x00): 1 (16.7%) 53 Mixed: 3 (50.0%) 54 `, 55 }, 56 } 57 58 // Table driven testing 59 func TestFmap(t *testing.T) { 60 for _, tt := range tests { 61 out, err := testutil.Command(t, tt.cmd, testFlash).CombinedOutput() 62 if err != nil { 63 t.Error(err) 64 } 65 66 // Filter out null characters which may be present in fmap strings. 67 out = bytes.Replace(out, []byte{0}, []byte{}, -1) 68 if string(out) != tt.out { 69 t.Errorf("expected:\n%s\ngot:\n%s", tt.out, string(out)) 70 } 71 } 72 } 73 74 func TestJson(t *testing.T) { 75 tmpDir, err := os.MkdirTemp("", "fmap_json") 76 if err != nil { 77 t.Fatal(err) 78 } 79 defer os.RemoveAll(tmpDir) 80 81 jsonFile := filepath.Join(tmpDir, "tmp.json") 82 if err := testutil.Command(t, "jget", jsonFile, testFlash).Run(); err != nil { 83 t.Fatal(err) 84 } 85 got, err := os.ReadFile(jsonFile) 86 if err != nil { 87 t.Fatal(err) 88 } 89 want := `{ 90 "FMap": { 91 "Signature": [ 92 95, 93 95, 94 70, 95 77, 96 65, 97 80, 98 95, 99 95 100 ], 101 "VerMajor": 1, 102 "VerMinor": 0, 103 "Base": 14627333968688430831, 104 "Size": 1144201745, 105 "Name": "Fake flash", 106 "NAreas": 2, 107 "Areas": [ 108 { 109 "Offset": 3735928559, 110 "Size": 286331153, 111 "Name": "Area Number 1\u0000\u0000\u0000Hello", 112 "Flags": 4115 113 }, 114 { 115 "Offset": 3405691582, 116 "Size": 572662306, 117 "Name": "Area Number 2xxxxxxxxxxxxxxxxxxx", 118 "Flags": 0 119 } 120 ] 121 }, 122 "Metadata": { 123 "Start": 24436 124 } 125 } 126 ` 127 if string(got) != want { 128 t.Errorf("want:%s; got:%s", string(want), got) 129 } 130 } 131 132 func TestMain(m *testing.M) { 133 testutil.Run(m, main) 134 }