|
5 | 5 | package fips140_test |
6 | 6 |
|
7 | 7 | import ( |
| 8 | + "crypto/des" |
| 9 | + "crypto/fips140" |
8 | 10 | "crypto/internal/cryptotest" |
9 | 11 | "internal/testenv" |
10 | | - "path/filepath" |
11 | | - "strings" |
12 | 12 | "testing" |
13 | 13 | ) |
14 | 14 |
|
| 15 | +func expectAllowed(t *testing.T, why string, expected bool) { |
| 16 | + t.Helper() |
| 17 | + result := isAllowed() |
| 18 | + if result != expected { |
| 19 | + t.Fatalf("%v: expected: %v, got: %v", why, expected, result) |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +func isAllowed() bool { |
| 24 | + _, err := des.NewCipher(make([]byte, 8)) |
| 25 | + return err == nil |
| 26 | +} |
| 27 | + |
15 | 28 | func TestWithoutEnforcement(t *testing.T) { |
16 | | - testenv.MustHaveExec(t) |
17 | | - testenv.MustHaveGoBuild(t) |
18 | 29 | cryptotest.MustSupportFIPS140(t) |
19 | | - |
20 | | - tool, _ := testenv.GoTool() |
21 | | - tmpdir := t.TempDir() |
22 | | - binFile := filepath.Join(tmpdir, "fips140.test") |
23 | | - cmd := testenv.Command(t, tool, "test", "-c", "-o", binFile, "./testdata") |
24 | | - out, err := cmd.CombinedOutput() |
25 | | - if err != nil { |
26 | | - t.Log(string(out)) |
27 | | - t.Errorf("Could not build enforcement tests") |
28 | | - } |
29 | | - cmd = testenv.Command(t, binFile, "-test.list", ".") |
30 | | - list, err := cmd.CombinedOutput() |
31 | | - if err != nil { |
32 | | - t.Log(string(out)) |
33 | | - t.Errorf("Could not get enforcement test list") |
| 30 | + if !fips140.Enforced() { |
| 31 | + cmd := testenv.Command(t, testenv.Executable(t), "-test.run=^TestWithoutEnforcement$", "-test.v") |
| 32 | + cmd.Env = append(cmd.Environ(), "GODEBUG=fips140=only") |
| 33 | + out, err := cmd.CombinedOutput() |
| 34 | + t.Logf("running with GODEBUG=fips140=only:\n%s", out) |
| 35 | + if err != nil { |
| 36 | + t.Errorf("fips140=only subprocess failed: %v", err) |
| 37 | + } |
| 38 | + return |
34 | 39 | } |
35 | | - for test := range strings.Lines(string(list)) { |
36 | | - test = strings.TrimSpace(test) |
37 | | - t.Run(test, func(t *testing.T) { |
38 | | - cmd = testenv.Command(t, binFile, "-test.run", "^"+test+"$") |
39 | | - cmd.Env = append(cmd.Env, "GODEBUG=fips140=only") |
40 | | - out, err := cmd.CombinedOutput() |
41 | | - if err != nil { |
42 | | - t.Error(string(out)) |
43 | | - } |
| 40 | + |
| 41 | + t.Run("Disabled", func(t *testing.T) { |
| 42 | + expectAllowed(t, "before enforcement disabled", false) |
| 43 | + fips140.WithoutEnforcement(func() { |
| 44 | + expectAllowed(t, "inside WithoutEnforcement", true) |
44 | 45 | }) |
45 | | - } |
| 46 | + // make sure that bypass doesn't live on after returning |
| 47 | + expectAllowed(t, "after WithoutEnforcement", false) |
| 48 | + }) |
| 49 | + |
| 50 | + t.Run("Nested", func(t *testing.T) { |
| 51 | + expectAllowed(t, "before enforcement bypass", false) |
| 52 | + fips140.WithoutEnforcement(func() { |
| 53 | + fips140.WithoutEnforcement(func() { |
| 54 | + expectAllowed(t, "inside nested WithoutEnforcement", true) |
| 55 | + }) |
| 56 | + expectAllowed(t, "inside nested WithoutEnforcement", true) |
| 57 | + }) |
| 58 | + expectAllowed(t, "after enforcement bypass", false) |
| 59 | + }) |
| 60 | + |
| 61 | + t.Run("GoroutineInherit", func(t *testing.T) { |
| 62 | + ch := make(chan bool, 2) |
| 63 | + expectAllowed(t, "before enforcement bypass", false) |
| 64 | + fips140.WithoutEnforcement(func() { |
| 65 | + go func() { |
| 66 | + ch <- isAllowed() |
| 67 | + }() |
| 68 | + }) |
| 69 | + allowed := <-ch |
| 70 | + if !allowed { |
| 71 | + t.Fatal("goroutine didn't inherit enforcement bypass") |
| 72 | + } |
| 73 | + go func() { |
| 74 | + ch <- isAllowed() |
| 75 | + }() |
| 76 | + allowed = <-ch |
| 77 | + if allowed { |
| 78 | + t.Fatal("goroutine inherited bypass after WithoutEnforcement return") |
| 79 | + } |
| 80 | + }) |
46 | 81 | } |
0 commit comments