Path: blob/dev/pkg/protocols/common/generators/options_test.go
2843 views
package generators12import (3"sync"4"testing"56"github.com/projectdiscovery/goflags"7"github.com/projectdiscovery/nuclei/v3/pkg/types"8"github.com/stretchr/testify/require"9)1011func TestBuildPayloadFromOptionsConcurrency(t *testing.T) {12// Test that BuildPayloadFromOptions is safe for concurrent use13// and returns independent copies that can be modified without races14vars := goflags.RuntimeMap{}15_ = vars.Set("key=value")1617opts := &types.Options{18Vars: vars,19}2021const numGoroutines = 10022var wg sync.WaitGroup23wg.Add(numGoroutines)2425// Each goroutine gets a map and modifies it26for i := 0; i < numGoroutines; i++ {27go func(id int) {28defer wg.Done()2930// Get the map (should be a copy of cached data)31m := BuildPayloadFromOptions(opts)3233// Modify it - this should not cause races34m["goroutine_id"] = id35m["test_key"] = "test_value"3637// Verify original cached value is present38require.Equal(t, "value", m["key"])39}(i)40}4142wg.Wait()43}4445func TestBuildPayloadFromOptionsCaching(t *testing.T) {46// Test that caching actually works47vars := goflags.RuntimeMap{}48_ = vars.Set("cached=yes")4950opts := &types.Options{51Vars: vars,52EnvironmentVariables: false,53}5455// First call - builds and caches56m1 := BuildPayloadFromOptions(opts)57require.Equal(t, "yes", m1["cached"])5859// Second call - should return copy of cached result60m2 := BuildPayloadFromOptions(opts)61require.Equal(t, "yes", m2["cached"])6263// Modify m1 - should not affect m2 since they're copies64m1["modified"] = "in_m1"65require.NotContains(t, m2, "modified")6667// Modify m2 - should not affect future calls68m2["modified"] = "in_m2"69m3 := BuildPayloadFromOptions(opts)70require.NotContains(t, m3, "modified")71}7273func TestClearOptionsPayloadMap(t *testing.T) {74vars := goflags.RuntimeMap{}75_ = vars.Set("temp=data")7677opts := &types.Options{78Vars: vars,79}8081// Build and cache82m1 := BuildPayloadFromOptions(opts)83require.Equal(t, "data", m1["temp"])8485// Clear the cache86ClearOptionsPayloadMap(opts)8788// Verify it still works (rebuilds)89m2 := BuildPayloadFromOptions(opts)90require.Equal(t, "data", m2["temp"])91}929394