gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/pkg/cpuid/cpuid_test.go (about)

     1  // Copyright 2020 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package cpuid
    16  
    17  import (
    18  	"encoding/binary"
    19  	"math/rand"
    20  	"os"
    21  	"testing"
    22  )
    23  
    24  func TestFeatureFromString(t *testing.T) {
    25  	// Check that known features do match.
    26  	for feature := range allFeatures {
    27  		f, ok := FeatureFromString(feature.String())
    28  		if f != feature || !ok {
    29  			t.Errorf("got %v, %v want %v, true", f, ok, feature)
    30  		}
    31  	}
    32  
    33  	// Check that "bad" doesn't match.
    34  	f, ok := FeatureFromString("bad")
    35  	if ok {
    36  		t.Errorf("got %v, %v want false", f, ok)
    37  	}
    38  }
    39  
    40  func TestReadHwCap(t *testing.T) {
    41  	// Make an auxv with fake entries
    42  	const (
    43  		auxvEntries = 16
    44  		uint64Size  = 8
    45  	)
    46  	auxv := [auxvEntries * uint64Size * 2]byte{}
    47  
    48  	hwCap1Idx := rand.Intn(auxvEntries)
    49  	hwCap2Idx := rand.Intn(auxvEntries)
    50  	if hwCap1Idx == hwCap2Idx {
    51  		hwCap2Idx = (hwCap2Idx + 1 + rand.Intn(auxvEntries-2)) % auxvEntries
    52  	}
    53  	// Set the entries we are interested in to not 0.
    54  	hwCap1Val := 1 + uint64(rand.Int63())
    55  	hwCap2Val := 1 + uint64(rand.Int63())
    56  
    57  	binary.LittleEndian.PutUint64(auxv[hwCap1Idx*uint64Size*2:], _AT_HWCAP)
    58  	binary.LittleEndian.PutUint64(auxv[hwCap1Idx*uint64Size*2+8:], hwCap1Val)
    59  	binary.LittleEndian.PutUint64(auxv[hwCap2Idx*uint64Size*2:], _AT_HWCAP2)
    60  	binary.LittleEndian.PutUint64(auxv[hwCap2Idx*uint64Size*2+8:], hwCap2Val)
    61  
    62  	file, err := os.CreateTemp(t.TempDir(), "fake-self-auxv")
    63  	if err != nil {
    64  		t.Errorf("failed to create file: %v", err)
    65  	}
    66  	_, err = file.Write(auxv[:])
    67  	if err != nil {
    68  		t.Errorf("failed to write to file: %v", err)
    69  	}
    70  	err = file.Close()
    71  	if err != nil {
    72  		t.Errorf("failed to close file: %v", err)
    73  	}
    74  
    75  	c, err := readHWCap(file.Name())
    76  	if err != nil {
    77  		t.Errorf("readHwCap got err %v, want nil", err)
    78  	}
    79  	if c.hwCap1 != hwCap1Val {
    80  		t.Errorf("c.hwCap1 got %d, want %d", c.hwCap1, hwCap1Val)
    81  	}
    82  	if c.hwCap2 != hwCap2Val {
    83  		t.Errorf("c.hwCap2 got %d, want %d", c.hwCap1, hwCap1Val)
    84  	}
    85  }
    86  
    87  func TestReadingSelfProcAuxv(t *testing.T) {
    88  	_, err := readHWCap("/proc/self/auxv")
    89  	if err != nil {
    90  		t.Errorf("got %v, expected nil", err)
    91  	}
    92  }
    93  
    94  func TestMain(m *testing.M) {
    95  	Initialize()
    96  	os.Exit(m.Run())
    97  }