| 1 |
// Copyright 2024 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 crypto_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"go/build" |
| 9 |
"internal/testenv" |
| 10 |
"log" |
| 11 |
"os" |
| 12 |
"os/exec" |
| 13 |
"strings" |
| 14 |
"testing" |
| 15 |
) |
| 16 |
|
| 17 |
// TestPureGoTag checks that when built with the purego build tag, crypto |
| 18 |
// packages don't require any assembly. This is used by alternative compilers |
| 19 |
// such as TinyGo. See also the "crypto/...:purego" test in cmd/dist, which |
| 20 |
// ensures the packages build correctly. |
| 21 |
func TestPureGoTag(t *testing.T) { |
| 22 |
cmd := exec.Command(testenv.GoToolPath(t), "list", "-e", "crypto/...", "math/big") |
| 23 |
cmd.Env = append(cmd.Env, "GOOS=linux") |
| 24 |
cmd.Stderr = os.Stderr |
| 25 |
out, err := cmd.Output() |
| 26 |
if err != nil { |
| 27 |
log.Fatalf("loading package list: %v\n%s", err, out) |
| 28 |
} |
| 29 |
pkgs := strings.Split(strings.TrimSpace(string(out)), "\n") |
| 30 |
|
| 31 |
cmd = exec.Command(testenv.GoToolPath(t), "tool", "dist", "list") |
| 32 |
cmd.Stderr = os.Stderr |
| 33 |
out, err = cmd.Output() |
| 34 |
if err != nil { |
| 35 |
log.Fatalf("loading architecture list: %v\n%s", err, out) |
| 36 |
} |
| 37 |
allGOARCH := make(map[string]bool) |
| 38 |
for _, pair := range strings.Split(strings.TrimSpace(string(out)), "\n") { |
| 39 |
GOARCH := strings.Split(pair, "/")[1] |
| 40 |
allGOARCH[GOARCH] = true |
| 41 |
} |
| 42 |
|
| 43 |
for _, pkgName := range pkgs { |
| 44 |
if strings.Contains(pkgName, "/boring") { |
| 45 |
continue |
| 46 |
} |
| 47 |
|
| 48 |
for GOARCH := range allGOARCH { |
| 49 |
context := build.Context{ |
| 50 |
GOOS: "linux", // darwin has custom assembly |
| 51 |
GOARCH: GOARCH, |
| 52 |
GOROOT: testenv.GOROOT(t), |
| 53 |
Compiler: build.Default.Compiler, |
| 54 |
BuildTags: []string{"purego", "math_big_pure_go"}, |
| 55 |
} |
| 56 |
|
| 57 |
pkg, err := context.Import(pkgName, "", 0) |
| 58 |
if err != nil { |
| 59 |
t.Fatal(err) |
| 60 |
} |
| 61 |
if len(pkg.SFiles) == 0 { |
| 62 |
continue |
| 63 |
} |
| 64 |
t.Errorf("package %s has purego assembly files on %s: %v", pkgName, GOARCH, pkg.SFiles) |
| 65 |
} |
| 66 |
} |
| 67 |
} |