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

     1  // Copyright 2018 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  	"errors"
     9  	"io/ioutil"
    10  	"regexp"
    11  	"testing"
    12  )
    13  
    14  func getFeatureList() ([]string, error) {
    15  	cpuinfo, err := ioutil.ReadFile("/proc/cpuinfo")
    16  	if err != nil {
    17  		return nil, err
    18  	}
    19  	r := regexp.MustCompile("features\\s*:\\s*(.*)")
    20  	b := r.FindSubmatch(cpuinfo)
    21  	if len(b) < 2 {
    22  		return nil, errors.New("no feature list in /proc/cpuinfo")
    23  	}
    24  	return regexp.MustCompile("\\s+").Split(string(b[1]), -1), nil
    25  }
    26  
    27  func TestS390XAgainstCPUInfo(t *testing.T) {
    28  	// mapping of linux feature strings to S390X fields
    29  	mapping := make(map[string]*bool)
    30  	for _, option := range Options {
    31  		mapping[option.Name] = option.Feature
    32  	}
    33  
    34  	// these must be true on the machines Go supports
    35  	mandatory := make(map[string]bool)
    36  	mandatory["zarch"] = false
    37  	mandatory["eimm"] = false
    38  	mandatory["ldisp"] = false
    39  	mandatory["stfle"] = false
    40  
    41  	features, err := getFeatureList()
    42  	if err != nil {
    43  		t.Error(err)
    44  	}
    45  	for _, feature := range features {
    46  		if _, ok := mandatory[feature]; ok {
    47  			mandatory[feature] = true
    48  		}
    49  		if flag, ok := mapping[feature]; ok {
    50  			if !*flag {
    51  				t.Errorf("feature '%v' not detected", feature)
    52  			}
    53  		} else {
    54  			t.Logf("no entry for '%v'", feature)
    55  		}
    56  	}
    57  	for k, v := range mandatory {
    58  		if !v {
    59  			t.Errorf("mandatory feature '%v' not detected", k)
    60  		}
    61  	}
    62  }