github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/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  //go:build linux
     6  // +build linux
     7  
     8  package acpi
     9  
    10  import (
    11  	"bytes"
    12  	"fmt"
    13  	"reflect"
    14  	"testing"
    15  )
    16  
    17  // TestTabWrite verifies that we can write a table and
    18  // read it back the same. For fun, we use the DSDT, which is
    19  // the important one. We don't keep tables here as data since we don't know
    20  // which ones we can copy, so we use what's it in sys or skip the test.
    21  func TestTabWrite(t *testing.T) {
    22  	tabs, err := RawTablesFromSys()
    23  	if err != nil || len(tabs) == 0 {
    24  		t.Logf("Skipping test, no ACPI tables in /sys")
    25  		return
    26  	}
    27  
    28  	var ttab Table
    29  	for _, tab := range tabs {
    30  		if tab.Sig() == "DSDT" {
    31  			ttab = tab
    32  			break
    33  		}
    34  	}
    35  	if ttab == nil {
    36  		ttab = tabs[0]
    37  	}
    38  	b := &bytes.Buffer{}
    39  	if err := WriteTables(b, ttab); err != nil {
    40  		t.Fatal(err)
    41  	}
    42  	if !reflect.DeepEqual(b.Bytes(), ttab.Data()) {
    43  		t.Fatalf("Written table does not match")
    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  	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  }