gitee.com/mirrors_u-root/u-root@v7.0.0+incompatible/pkg/dt/fdt_test.go (about)

     1  // Copyright 2019 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 dt
     6  
     7  import (
     8  	"encoding/json"
     9  	"io/ioutil"
    10  	"os"
    11  	"os/exec"
    12  	"reflect"
    13  	"testing"
    14  )
    15  
    16  func TestRead(t *testing.T) {
    17  	f, err := os.Open("testdata/fdt.dtb")
    18  	if err != nil {
    19  		t.Fatal(err)
    20  	}
    21  	defer f.Close()
    22  
    23  	jsonData, err := ioutil.ReadFile("testdata/fdt.json")
    24  	if err != nil {
    25  		t.Fatal(err)
    26  	}
    27  	testData := &FDT{}
    28  	if err := json.Unmarshal(jsonData, testData); err != nil {
    29  		t.Fatal(err)
    30  	}
    31  
    32  	fdt, err := ReadFDT(f)
    33  	if err != nil {
    34  		t.Fatal(err)
    35  	}
    36  
    37  	if !reflect.DeepEqual(fdt, testData) {
    38  		got, err := json.MarshalIndent(fdt, "", "    ")
    39  		if err != nil {
    40  			t.Fatal(err)
    41  		}
    42  		t.Errorf(`Read("fdt.dtb") = %s`, got)
    43  		t.Errorf(`want %s`, jsonData)
    44  	}
    45  }
    46  
    47  // TestParity tests that the fdt Read+Write operations are compatible with your
    48  // system's fdtdump command.
    49  func TestParity(t *testing.T) {
    50  	// TODO: I'm convinced my system's fdtdump command is broken.
    51  	t.Skip()
    52  
    53  	// Read and write the fdt.
    54  	f, err := os.Open("testdata/fdt.dtb")
    55  	if err != nil {
    56  		t.Fatal(err)
    57  	}
    58  	fdt, err := ReadFDT(f)
    59  	f.Close()
    60  	if err != nil {
    61  		t.Fatal(err)
    62  	}
    63  	f, err = os.Create("testdata/fdt2.dtb")
    64  	if err != nil {
    65  		t.Fatal(err)
    66  	}
    67  	_, err = fdt.Write(f)
    68  	f.Close()
    69  	if err != nil {
    70  		t.Fatal(err)
    71  	}
    72  
    73  	// Run your system's fdtdump command.
    74  	f, err = os.Create("testdata/fdt2.dts")
    75  	if err != nil {
    76  		t.Fatal(err)
    77  	}
    78  	cmd := exec.Command("fdtdump", "testdata/fdt2.dtb")
    79  	cmd.Stdout = f
    80  	err = cmd.Run()
    81  	f.Close()
    82  	if err != nil {
    83  		t.Fatal(err) // TODO: skip if system does not have fdtdump
    84  	}
    85  
    86  	cmd = exec.Command("diff", "testdata/fdt.dts", "testdata/fdt2.dts")
    87  	cmd.Stdout = os.Stdout
    88  	cmd.Stderr = os.Stderr
    89  	cmd.Run()
    90  }