github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/pkg/msr/msr_linux_test.go (about)

     1  // Copyright 2020 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  package msr
     5  
     6  import (
     7  	"os"
     8  	"testing"
     9  
    10  	"github.com/u-root/u-root/pkg/testutil"
    11  )
    12  
    13  func TestParseCPUs(t *testing.T) {
    14  	var tests = []struct {
    15  		name   string
    16  		input  string
    17  		cpus   CPUs
    18  		errStr string
    19  	}{
    20  		{
    21  			name:   "one core",
    22  			input:  "0",
    23  			cpus:   []uint64{0},
    24  			errStr: "",
    25  		},
    26  		{
    27  			name:   "eight cores",
    28  			input:  "0-7",
    29  			cpus:   []uint64{0, 1, 2, 3, 4, 5, 6, 7},
    30  			errStr: "",
    31  		},
    32  		{
    33  			name:   "split cores",
    34  			input:  "0-2,4-7",
    35  			cpus:   []uint64{0, 1, 2, 4, 5, 6, 7},
    36  			errStr: "",
    37  		},
    38  		{
    39  			name:   "duplicates",
    40  			input:  "0-2,0-2",
    41  			cpus:   []uint64{0, 1, 2},
    42  			errStr: "",
    43  		},
    44  		{
    45  			name:   "invalid range",
    46  			input:  "7-0",
    47  			cpus:   nil,
    48  			errStr: "invalid cpu range, upper bound greater than lower: 7-0",
    49  		},
    50  		{
    51  			name:   "non numbers",
    52  			input:  "a",
    53  			cpus:   nil,
    54  			errStr: "unknown cpu range: a, failed to parse strconv.ParseUint: parsing \"a\": invalid syntax",
    55  		},
    56  		{
    57  			name:   "non numbers 2",
    58  			input:  "a-b",
    59  			cpus:   nil,
    60  			errStr: "unknown cpu range: a-b, failed to parse strconv.ParseUint: parsing \"a\": invalid syntax",
    61  		},
    62  		{
    63  			name:   "weird range",
    64  			input:  "1-2-3",
    65  			cpus:   nil,
    66  			errStr: "unknown cpu range: 1-2-3",
    67  		},
    68  		{
    69  			name:   "weirder range",
    70  			input:  "-1",
    71  			cpus:   nil,
    72  			errStr: "unknown cpu range: -1, failed to parse strconv.ParseUint: parsing \"\": invalid syntax",
    73  		},
    74  		{
    75  			name:   "empty string",
    76  			input:  "",
    77  			cpus:   nil,
    78  			errStr: "no cpus found, input was ",
    79  		},
    80  		{
    81  			name:   "comma string",
    82  			input:  ",,,,",
    83  			cpus:   nil,
    84  			errStr: "no cpus found, input was ,,,,",
    85  		},
    86  	}
    87  	for _, test := range tests {
    88  		t.Run(test.name, func(t *testing.T) {
    89  			o, err := parseCPUs(test.input)
    90  			if e := testutil.CheckError(err, test.errStr); e != nil {
    91  				t.Error(e)
    92  			}
    93  			if test.cpus != nil && o == nil {
    94  				t.Errorf("Expected cpus %v, got nil", test.cpus)
    95  			} else if test.cpus == nil && o != nil {
    96  				t.Errorf("Expected nil cpus, got %v", o)
    97  			} else if test.cpus != nil && o != nil {
    98  				if len(test.cpus) != len(o) {
    99  					t.Errorf("Mismatched output: got %v, want %v", o, test.cpus)
   100  				} else {
   101  					for i := range o {
   102  						if test.cpus[i] != o[i] {
   103  							t.Errorf("Mismatched output, got %v, want %v", o, test.cpus)
   104  						}
   105  					}
   106  				}
   107  			}
   108  		})
   109  	}
   110  }
   111  
   112  func TestCPUsString(t *testing.T) {
   113  	var tests = []struct {
   114  		name   string
   115  		cpus   CPUs
   116  		output string
   117  	}{
   118  		{
   119  			name:   "no cpus",
   120  			cpus:   []uint64{},
   121  			output: "nil",
   122  		},
   123  		{
   124  			name:   "1 cpu",
   125  			cpus:   []uint64{1},
   126  			output: "1",
   127  		},
   128  		{
   129  			name:   "2 cpu",
   130  			cpus:   []uint64{1, 2},
   131  			output: "1-2",
   132  		},
   133  		{
   134  			name:   "3 cpu",
   135  			cpus:   []uint64{1, 2},
   136  			output: "1-2",
   137  		},
   138  		{
   139  			name:   "3 cpu",
   140  			cpus:   []uint64{1, 2, 3},
   141  			output: "1-3",
   142  		},
   143  		{
   144  			name:   "3 noncontinuous cpu",
   145  			cpus:   []uint64{1, 2, 4},
   146  			output: "1-2,4",
   147  		},
   148  		{
   149  			name:   "large sequence",
   150  			cpus:   []uint64{1, 2, 4, 6, 7, 8, 9, 10, 11, 12, 13},
   151  			output: "1-2,4,6-13",
   152  		},
   153  	}
   154  	for _, test := range tests {
   155  		t.Run(test.name, func(t *testing.T) {
   156  			s := test.cpus.String()
   157  			if s != test.output {
   158  				t.Errorf("CPUs(%v).String() == %q; want %q", []uint64(test.cpus), s, test.output)
   159  			}
   160  		})
   161  	}
   162  }
   163  
   164  // This is a hard one to test. But for many systems, 0x3a is a good bet.
   165  // but this is of necessity not a complete test! We don't want to set an MSR
   166  // as part of a test, it might cause real trouble.
   167  func TestTestAndSet(t *testing.T) {
   168  	if os.Getuid() != 0 {
   169  		t.Skipf("Skipping test since we are not root")
   170  	}
   171  	c, err := AllCPUs()
   172  	if err != nil {
   173  		t.Fatalf("AllCPUs: got %v,want nil", err)
   174  	}
   175  	r := IntelIA32FeatureControl
   176  	vals, errs := r.Read(c)
   177  	if errs != nil {
   178  		t.Skipf("Skipping test, can't read %s", r)
   179  	}
   180  
   181  	if errs = r.Test(c, 0, 0); errs != nil {
   182  		t.Errorf("Test with good val: got %v, want nil", errs)
   183  	}
   184  
   185  	// clear every set bit, set every clear bit, this should not go well.
   186  	if errs = r.Test(c, vals[0], ^vals[0]); errs == nil {
   187  		t.Errorf("Test with bad clear/set/val 0x%#x: got nil, want err", vals[0])
   188  	}
   189  }
   190  
   191  func TestLocked(t *testing.T) {
   192  	// Passing this is basically optional, but at least try.
   193  	if err := Locked(); err != nil {
   194  		t.Logf("(warning only) Verify GenuineIntel: got %v, want nil", err)
   195  	}
   196  }