| 1 |
// Copyright 2021 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 buildcfg |
| 6 |
|
| 7 |
import ( |
| 8 |
"os" |
| 9 |
"testing" |
| 10 |
) |
| 11 |
|
| 12 |
func TestConfigFlags(t *testing.T) { |
| 13 |
os.Setenv("GOAMD64", "v1") |
| 14 |
if goamd64() != 1 { |
| 15 |
t.Errorf("Wrong parsing of GOAMD64=v1") |
| 16 |
} |
| 17 |
os.Setenv("GOAMD64", "v4") |
| 18 |
if goamd64() != 4 { |
| 19 |
t.Errorf("Wrong parsing of GOAMD64=v4") |
| 20 |
} |
| 21 |
Error = nil |
| 22 |
os.Setenv("GOAMD64", "1") |
| 23 |
if goamd64(); Error == nil { |
| 24 |
t.Errorf("Wrong parsing of GOAMD64=1") |
| 25 |
} |
| 26 |
|
| 27 |
os.Setenv("GORISCV64", "rva20u64") |
| 28 |
if goriscv64() != 20 { |
| 29 |
t.Errorf("Wrong parsing of RISCV64=rva20u64") |
| 30 |
} |
| 31 |
os.Setenv("GORISCV64", "rva22u64") |
| 32 |
if goriscv64() != 22 { |
| 33 |
t.Errorf("Wrong parsing of RISCV64=rva22u64") |
| 34 |
} |
| 35 |
os.Setenv("GORISCV64", "rva23u64") |
| 36 |
if goriscv64() != 23 { |
| 37 |
t.Errorf("Wrong parsing of RISCV64=rva23u64") |
| 38 |
} |
| 39 |
Error = nil |
| 40 |
os.Setenv("GORISCV64", "rva22") |
| 41 |
if _ = goriscv64(); Error == nil { |
| 42 |
t.Errorf("Wrong parsing of RISCV64=rva22") |
| 43 |
} |
| 44 |
Error = nil |
| 45 |
os.Setenv("GOARM64", "v7.0") |
| 46 |
if _ = goarm64(); Error == nil { |
| 47 |
t.Errorf("Wrong parsing of GOARM64=7.0") |
| 48 |
} |
| 49 |
Error = nil |
| 50 |
os.Setenv("GOARM64", "8.0") |
| 51 |
if _ = goarm64(); Error == nil { |
| 52 |
t.Errorf("Wrong parsing of GOARM64=8.0") |
| 53 |
} |
| 54 |
Error = nil |
| 55 |
os.Setenv("GOARM64", "v8.0,lsb") |
| 56 |
if _ = goarm64(); Error == nil { |
| 57 |
t.Errorf("Wrong parsing of GOARM64=v8.0,lsb") |
| 58 |
} |
| 59 |
os.Setenv("GOARM64", "v8.0,lse") |
| 60 |
if goarm64().Version != "v8.0" || goarm64().LSE != true || goarm64().Crypto != false { |
| 61 |
t.Errorf("Wrong parsing of GOARM64=v8.0,lse") |
| 62 |
} |
| 63 |
os.Setenv("GOARM64", "v8.0,crypto") |
| 64 |
if goarm64().Version != "v8.0" || goarm64().LSE != false || goarm64().Crypto != true { |
| 65 |
t.Errorf("Wrong parsing of GOARM64=v8.0,crypto") |
| 66 |
} |
| 67 |
os.Setenv("GOARM64", "v8.0,crypto,lse") |
| 68 |
if goarm64().Version != "v8.0" || goarm64().LSE != true || goarm64().Crypto != true { |
| 69 |
t.Errorf("Wrong parsing of GOARM64=v8.0,crypto,lse") |
| 70 |
} |
| 71 |
os.Setenv("GOARM64", "v8.0,lse,crypto") |
| 72 |
if goarm64().Version != "v8.0" || goarm64().LSE != true || goarm64().Crypto != true { |
| 73 |
t.Errorf("Wrong parsing of GOARM64=v8.0,lse,crypto") |
| 74 |
} |
| 75 |
os.Setenv("GOARM64", "v9.0") |
| 76 |
if goarm64().Version != "v9.0" || goarm64().LSE != true || goarm64().Crypto != false { |
| 77 |
t.Errorf("Wrong parsing of GOARM64=v9.0") |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
func TestGoarm64FeaturesSupports(t *testing.T) { |
| 82 |
g, _ := ParseGoarm64("v9.3") |
| 83 |
|
| 84 |
if !g.Supports("v9.3") { |
| 85 |
t.Errorf("Wrong goarm64Features.Supports for v9.3, v9.3") |
| 86 |
} |
| 87 |
|
| 88 |
if g.Supports("v9.4") { |
| 89 |
t.Errorf("Wrong goarm64Features.Supports for v9.3, v9.4") |
| 90 |
} |
| 91 |
|
| 92 |
if !g.Supports("v8.8") { |
| 93 |
t.Errorf("Wrong goarm64Features.Supports for v9.3, v8.8") |
| 94 |
} |
| 95 |
|
| 96 |
if g.Supports("v8.9") { |
| 97 |
t.Errorf("Wrong goarm64Features.Supports for v9.3, v8.9") |
| 98 |
} |
| 99 |
|
| 100 |
if g.Supports(",lse") { |
| 101 |
t.Errorf("Wrong goarm64Features.Supports for v9.3, ,lse") |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
func TestGogoarchTags(t *testing.T) { |
| 106 |
old_goarch := GOARCH |
| 107 |
old_goarm64 := GOARM64 |
| 108 |
|
| 109 |
GOARCH = "arm64" |
| 110 |
|
| 111 |
os.Setenv("GOARM64", "v9.5") |
| 112 |
GOARM64 = goarm64() |
| 113 |
tags := gogoarchTags() |
| 114 |
want := []string{"arm64.v9.0", "arm64.v9.1", "arm64.v9.2", "arm64.v9.3", "arm64.v9.4", "arm64.v9.5", |
| 115 |
"arm64.v8.0", "arm64.v8.1", "arm64.v8.2", "arm64.v8.3", "arm64.v8.4", "arm64.v8.5", "arm64.v8.6", "arm64.v8.7", "arm64.v8.8", "arm64.v8.9"} |
| 116 |
if len(tags) != len(want) { |
| 117 |
t.Errorf("Wrong number of tags for GOARM64=v9.5") |
| 118 |
} else { |
| 119 |
for i, v := range tags { |
| 120 |
if v != want[i] { |
| 121 |
t.Error("Wrong tags for GOARM64=v9.5") |
| 122 |
break |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
GOARCH = old_goarch |
| 128 |
GOARM64 = old_goarm64 |
| 129 |
} |
| 130 |
|
| 131 |
var goodFIPS = []string{ |
| 132 |
"v1.0.0", |
| 133 |
"v1.0.1", |
| 134 |
"v1.2.0", |
| 135 |
"v1.2.3", |
| 136 |
} |
| 137 |
|
| 138 |
var badFIPS = []string{ |
| 139 |
"v1.0.0-fips", |
| 140 |
"v1.0.0+fips", |
| 141 |
"1.0.0", |
| 142 |
"x1.0.0", |
| 143 |
} |
| 144 |
|
| 145 |
func TestIsFIPSVersion(t *testing.T) { |
| 146 |
// good |
| 147 |
for _, s := range goodFIPS { |
| 148 |
if !isFIPSVersion(s) { |
| 149 |
t.Errorf("isFIPSVersion(%q) = false, want true", s) |
| 150 |
} |
| 151 |
} |
| 152 |
// truncated |
| 153 |
const v = "v1.2.3" |
| 154 |
for i := 0; i < len(v); i++ { |
| 155 |
if isFIPSVersion(v[:i]) { |
| 156 |
t.Errorf("isFIPSVersion(%q) = true, want false", v[:i]) |
| 157 |
} |
| 158 |
} |
| 159 |
// bad |
| 160 |
for _, s := range badFIPS { |
| 161 |
if isFIPSVersion(s) { |
| 162 |
t.Errorf("isFIPSVersion(%q) = true, want false", s) |
| 163 |
} |
| 164 |
} |
| 165 |
} |