|
| 1 | +package rules |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | +) |
| 11 | + |
| 12 | +func writeGoMod(t *testing.T, dir, content string) string { |
| 13 | + t.Helper() |
| 14 | + path := filepath.Join(dir, "go.mod") |
| 15 | + require.NoError(t, os.WriteFile(path, []byte(content), 0o644)) |
| 16 | + return path |
| 17 | +} |
| 18 | + |
| 19 | +func TestCheckModuleVersionSync_Aligned(t *testing.T) { |
| 20 | + dir := t.TempDir() |
| 21 | + path := writeGoMod(t, dir, `module example.com/app |
| 22 | +
|
| 23 | +go 1.22 |
| 24 | +
|
| 25 | +require ( |
| 26 | + github.com/AikidoSec/firewall-go v1.2.0 |
| 27 | + github.com/AikidoSec/firewall-go/instrumentation/sources/gin-gonic/gin v1.2.0 |
| 28 | + github.com/AikidoSec/firewall-go/instrumentation/sinks/pgx.v5 v1.2.0 |
| 29 | +) |
| 30 | +`) |
| 31 | + require.NoError(t, CheckModuleVersionSync(path)) |
| 32 | +} |
| 33 | + |
| 34 | +func TestCheckModuleVersionSync_Mismatch(t *testing.T) { |
| 35 | + dir := t.TempDir() |
| 36 | + path := writeGoMod(t, dir, `module example.com/app |
| 37 | +
|
| 38 | +go 1.22 |
| 39 | +
|
| 40 | +require ( |
| 41 | + github.com/AikidoSec/firewall-go v1.2.0 |
| 42 | + github.com/AikidoSec/firewall-go/instrumentation/sources/gin-gonic/gin v1.1.1 |
| 43 | +) |
| 44 | +`) |
| 45 | + err := CheckModuleVersionSync(path) |
| 46 | + require.Error(t, err) |
| 47 | + assert.Contains(t, err.Error(), "version mismatch") |
| 48 | + assert.Contains(t, err.Error(), "v1.2.0") |
| 49 | + assert.Contains(t, err.Error(), "gin is at v1.1.1") |
| 50 | + assert.Contains(t, err.Error(), "go get") |
| 51 | + assert.Contains(t, err.Error(), "gin@v1.2.0") |
| 52 | +} |
| 53 | + |
| 54 | +func TestCheckModuleVersionSync_MultipleMismatches(t *testing.T) { |
| 55 | + dir := t.TempDir() |
| 56 | + path := writeGoMod(t, dir, `module example.com/app |
| 57 | +
|
| 58 | +go 1.22 |
| 59 | +
|
| 60 | +require ( |
| 61 | + github.com/AikidoSec/firewall-go v1.2.0 |
| 62 | + github.com/AikidoSec/firewall-go/instrumentation/sources/gin-gonic/gin v1.1.1 |
| 63 | + github.com/AikidoSec/firewall-go/instrumentation/sinks/pgx.v5 v1.0.0 |
| 64 | +) |
| 65 | +`) |
| 66 | + err := CheckModuleVersionSync(path) |
| 67 | + require.Error(t, err) |
| 68 | + assert.Contains(t, err.Error(), "gin is at v1.1.1") |
| 69 | + assert.Contains(t, err.Error(), "pgx.v5 is at v1.0.0") |
| 70 | + assert.Contains(t, err.Error(), "gin@v1.2.0") |
| 71 | + assert.Contains(t, err.Error(), "pgx.v5@v1.2.0") |
| 72 | +} |
| 73 | + |
| 74 | +func TestCheckModuleVersionSync_NoFirewallModule(t *testing.T) { |
| 75 | + dir := t.TempDir() |
| 76 | + path := writeGoMod(t, dir, `module example.com/app |
| 77 | +
|
| 78 | +go 1.22 |
| 79 | +
|
| 80 | +require ( |
| 81 | + github.com/gin-gonic/gin v1.9.0 |
| 82 | +) |
| 83 | +`) |
| 84 | + require.NoError(t, CheckModuleVersionSync(path)) |
| 85 | +} |
| 86 | + |
| 87 | +func TestCheckModuleVersionSync_MainModuleOnly(t *testing.T) { |
| 88 | + dir := t.TempDir() |
| 89 | + path := writeGoMod(t, dir, `module example.com/app |
| 90 | +
|
| 91 | +go 1.22 |
| 92 | +
|
| 93 | +require ( |
| 94 | + github.com/AikidoSec/firewall-go v1.2.0 |
| 95 | +) |
| 96 | +`) |
| 97 | + require.NoError(t, CheckModuleVersionSync(path)) |
| 98 | +} |
| 99 | + |
| 100 | +func TestCheckModuleVersionSync_UnreadableFile(t *testing.T) { |
| 101 | + require.NoError(t, CheckModuleVersionSync("/nonexistent/go.mod")) |
| 102 | +} |
| 103 | + |
| 104 | +func TestCheckModuleVersionSync_ReplacedSubmodule(t *testing.T) { |
| 105 | + dir := t.TempDir() |
| 106 | + path := writeGoMod(t, dir, `module example.com/app |
| 107 | +
|
| 108 | +go 1.22 |
| 109 | +
|
| 110 | +require ( |
| 111 | + github.com/AikidoSec/firewall-go v1.2.0 |
| 112 | + github.com/AikidoSec/firewall-go/instrumentation/sources/gin-gonic/gin v0.0.0-00010101000000-000000000000 |
| 113 | +) |
| 114 | +
|
| 115 | +replace github.com/AikidoSec/firewall-go/instrumentation/sources/gin-gonic/gin => ../../instrumentation/sources/gin-gonic/gin |
| 116 | +`) |
| 117 | + require.NoError(t, CheckModuleVersionSync(path)) |
| 118 | +} |
| 119 | + |
| 120 | +func TestCheckModuleVersionSync_ReplacedMainModule(t *testing.T) { |
| 121 | + dir := t.TempDir() |
| 122 | + path := writeGoMod(t, dir, `module example.com/app |
| 123 | +
|
| 124 | +go 1.22 |
| 125 | +
|
| 126 | +require ( |
| 127 | + github.com/AikidoSec/firewall-go v1.2.0 |
| 128 | + github.com/AikidoSec/firewall-go/instrumentation/sources/gin-gonic/gin v1.1.1 |
| 129 | +) |
| 130 | +
|
| 131 | +replace github.com/AikidoSec/firewall-go => ../../ |
| 132 | +`) |
| 133 | + require.NoError(t, CheckModuleVersionSync(path)) |
| 134 | +} |
| 135 | + |
| 136 | +func TestFindGoMod_Found(t *testing.T) { |
| 137 | + root := t.TempDir() |
| 138 | + gomodPath := filepath.Join(root, "go.mod") |
| 139 | + require.NoError(t, os.WriteFile(gomodPath, []byte("module example.com/app\n"), 0o644)) |
| 140 | + |
| 141 | + subdir := filepath.Join(root, "pkg", "handlers") |
| 142 | + require.NoError(t, os.MkdirAll(subdir, 0o755)) |
| 143 | + |
| 144 | + assert.Equal(t, gomodPath, FindGoMod(subdir)) |
| 145 | + assert.Equal(t, gomodPath, FindGoMod(root)) |
| 146 | +} |
| 147 | + |
| 148 | +func TestFindGoMod_NotFound(t *testing.T) { |
| 149 | + dir := t.TempDir() |
| 150 | + assert.Equal(t, "", FindGoMod(dir)) |
| 151 | +} |
0 commit comments