github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/pkg/acpi/acpi_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  // +build linux
     6  
     7  package acpi
     8  
     9  import (
    10  	"bytes"
    11  	"fmt"
    12  	"reflect"
    13  	"testing"
    14  )
    15  
    16  // TestTabWrite verifies that we can write a table and
    17  // read it back the same. For fun, we use the DSDT, which is
    18  // the important one. We don't keep tables here as data since we don't know
    19  // which ones we can copy, so we use what's it in sys or skip the test.
    20  func TestTabWrite(t *testing.T) {
    21  	tabs, err := RawTablesFromSys()
    22  	if err != nil || len(tabs) == 0 {
    23  		t.Logf("Skipping test, no ACPI tables in /sys")
    24  		return
    25  	}
    26  
    27  	var ttab Table
    28  	for _, tab := range tabs {
    29  		if tab.Sig() == "DSDT" {
    30  			ttab = tab
    31  			break
    32  		}
    33  	}
    34  	if ttab == nil {
    35  		ttab = tabs[0]
    36  	}
    37  	var b = &bytes.Buffer{}
    38  	if err := WriteTables(b, ttab); err != nil {
    39  		t.Fatal(err)
    40  	}
    41  	if !reflect.DeepEqual(b.Bytes(), ttab.Data()) {
    42  		t.Fatalf("Written table does not match")
    43  	}
    44  
    45  }
    46  
    47  // TestAddr tests the decoding of those stupid "use this 64-bit if set else 32 things"
    48  // that permeate ACPI
    49  func TestAddr(t *testing.T) {
    50  	var tests = []struct {
    51  		n   string
    52  		dat []byte
    53  		a64 int64
    54  		a32 int64
    55  		val int64
    56  		err error
    57  	}{
    58  		{n: "zero length data", dat: []byte{}, a64: 5, a32: 1, val: -1, err: fmt.Errorf("No 64-bit address at 5, no 32-bit address at 1, in 0-byte slice")},
    59  		{n: "32 bits at 1, no 64-bit", dat: []byte{1, 2, 3, 4, 5}, a64: 5, a32: 1, val: 84148994, err: nil},
    60  		{n: "64 bits at 5", dat: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}, a64: 5, a32: 1, val: 940138559942690566, err: nil},
    61  	}
    62  	Debug = t.Logf
    63  	for _, tt := range tests {
    64  		v, err := getaddr(tt.dat, tt.a64, tt.a32)
    65  		if v != tt.val {
    66  			t.Errorf("Test %s: got %d, want %d", tt.n, v, tt.val)
    67  		}
    68  		if !reflect.DeepEqual(err, tt.err) {
    69  			t.Errorf("Test %s: got %v, want %v", tt.n, err, tt.err)
    70  		}
    71  	}
    72  }