|
| 1 | +package generators |
| 2 | + |
| 3 | +import ( |
| 4 | + "sync" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/projectdiscovery/goflags" |
| 8 | + "github.com/projectdiscovery/nuclei/v3/pkg/types" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | +) |
| 11 | + |
| 12 | +func TestBuildPayloadFromOptionsConcurrency(t *testing.T) { |
| 13 | + // Test that BuildPayloadFromOptions is safe for concurrent use |
| 14 | + // and returns independent copies that can be modified without races |
| 15 | + vars := goflags.RuntimeMap{} |
| 16 | + _ = vars.Set("key=value") |
| 17 | + |
| 18 | + opts := &types.Options{ |
| 19 | + Vars: vars, |
| 20 | + } |
| 21 | + |
| 22 | + const numGoroutines = 100 |
| 23 | + var wg sync.WaitGroup |
| 24 | + wg.Add(numGoroutines) |
| 25 | + |
| 26 | + // Each goroutine gets a map and modifies it |
| 27 | + for i := 0; i < numGoroutines; i++ { |
| 28 | + go func(id int) { |
| 29 | + defer wg.Done() |
| 30 | + |
| 31 | + // Get the map (should be a copy of cached data) |
| 32 | + m := BuildPayloadFromOptions(opts) |
| 33 | + |
| 34 | + // Modify it - this should not cause races |
| 35 | + m["goroutine_id"] = id |
| 36 | + m["test_key"] = "test_value" |
| 37 | + |
| 38 | + // Verify original cached value is present |
| 39 | + require.Equal(t, "value", m["key"]) |
| 40 | + }(i) |
| 41 | + } |
| 42 | + |
| 43 | + wg.Wait() |
| 44 | +} |
| 45 | + |
| 46 | +func TestBuildPayloadFromOptionsCaching(t *testing.T) { |
| 47 | + // Test that caching actually works |
| 48 | + vars := goflags.RuntimeMap{} |
| 49 | + _ = vars.Set("cached=yes") |
| 50 | + |
| 51 | + opts := &types.Options{ |
| 52 | + Vars: vars, |
| 53 | + EnvironmentVariables: false, |
| 54 | + } |
| 55 | + |
| 56 | + // First call - builds and caches |
| 57 | + m1 := BuildPayloadFromOptions(opts) |
| 58 | + require.Equal(t, "yes", m1["cached"]) |
| 59 | + |
| 60 | + // Second call - should return copy of cached result |
| 61 | + m2 := BuildPayloadFromOptions(opts) |
| 62 | + require.Equal(t, "yes", m2["cached"]) |
| 63 | + |
| 64 | + // Modify m1 - should not affect m2 since they're copies |
| 65 | + m1["modified"] = "in_m1" |
| 66 | + require.NotContains(t, m2, "modified") |
| 67 | + |
| 68 | + // Modify m2 - should not affect future calls |
| 69 | + m2["modified"] = "in_m2" |
| 70 | + m3 := BuildPayloadFromOptions(opts) |
| 71 | + require.NotContains(t, m3, "modified") |
| 72 | +} |
| 73 | + |
| 74 | +func TestClearOptionsPayloadMap(t *testing.T) { |
| 75 | + vars := goflags.RuntimeMap{} |
| 76 | + _ = vars.Set("temp=data") |
| 77 | + |
| 78 | + opts := &types.Options{ |
| 79 | + Vars: vars, |
| 80 | + } |
| 81 | + |
| 82 | + // Build and cache |
| 83 | + m1 := BuildPayloadFromOptions(opts) |
| 84 | + require.Equal(t, "data", m1["temp"]) |
| 85 | + |
| 86 | + // Clear the cache |
| 87 | + ClearOptionsPayloadMap(opts) |
| 88 | + |
| 89 | + // Verify it still works (rebuilds) |
| 90 | + m2 := BuildPayloadFromOptions(opts) |
| 91 | + require.Equal(t, "data", m2["temp"]) |
| 92 | +} |
0 commit comments