github.com/icodeface/tls@v0.0.0-20230910023335-34df9250cd12/internal/cpu/cpu_test.go (about)

     1  // Copyright 2017 The Go 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 cpu_test
     6  
     7  import (
     8  	"github.com/icodeface/tls/internal/testenv"
     9  	"os"
    10  	"os/exec"
    11  	"runtime"
    12  	"strings"
    13  	"testing"
    14  )
    15  
    16  func TestMinimalFeatures(t *testing.T) {
    17  	if runtime.GOARCH == "arm64" {
    18  		switch runtime.GOOS {
    19  		case "linux", "android":
    20  		default:
    21  			t.Skipf("%s/%s is not supported", runtime.GOOS, runtime.GOARCH)
    22  		}
    23  	}
    24  
    25  	for _, o := range Options {
    26  		if o.Required && !*o.Feature {
    27  			t.Errorf("%v expected true, got false", o.Name)
    28  		}
    29  	}
    30  }
    31  
    32  func MustHaveDebugOptionsSupport(t *testing.T) {
    33  	if !DebugOptions {
    34  		t.Skipf("skipping test: cpu feature options not supported by OS")
    35  	}
    36  }
    37  
    38  func runDebugOptionsTest(t *testing.T, test string, options string) {
    39  	MustHaveDebugOptionsSupport(t)
    40  
    41  	testenv.MustHaveExec(t)
    42  
    43  	env := "GODEBUG=" + options
    44  
    45  	cmd := exec.Command(os.Args[0], "-test.run="+test)
    46  	cmd.Env = append(cmd.Env, env)
    47  
    48  	output, err := cmd.CombinedOutput()
    49  	lines := strings.Fields(string(output))
    50  	lastline := lines[len(lines)-1]
    51  
    52  	got := strings.TrimSpace(lastline)
    53  	want := "PASS"
    54  	if err != nil || got != want {
    55  		t.Fatalf("%s with %s: want %s, got %v", test, env, want, got)
    56  	}
    57  }
    58  
    59  func TestDisableAllCapabilities(t *testing.T) {
    60  	runDebugOptionsTest(t, "TestAllCapabilitiesDisabled", "cpu.all=off")
    61  }
    62  
    63  func TestAllCapabilitiesDisabled(t *testing.T) {
    64  	MustHaveDebugOptionsSupport(t)
    65  
    66  	if os.Getenv("GODEBUG") != "cpu.all=off" {
    67  		t.Skipf("skipping test: GODEBUG=cpu.all=off not set")
    68  	}
    69  
    70  	for _, o := range Options {
    71  		want := o.Required
    72  		if got := *o.Feature; got != want {
    73  			t.Errorf("%v: expected %v, got %v", o.Name, want, got)
    74  		}
    75  	}
    76  }