github.com/ratrocket/u-root@v0.0.0-20180201221235-1cf9f48ee2cf/cmds/fmap/fmap_test.go (about)

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