|
| 1 | +package dns |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/netip" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + "golang.org/x/sys/windows/registry" |
| 11 | +) |
| 12 | + |
| 13 | +// TestNRPTEntriesCleanupOnConfigChange tests that old NRPT entries are properly cleaned up |
| 14 | +// when the number of match domains decreases between configuration changes. |
| 15 | +func TestNRPTEntriesCleanupOnConfigChange(t *testing.T) { |
| 16 | + if testing.Short() { |
| 17 | + t.Skip("skipping registry integration test in short mode") |
| 18 | + } |
| 19 | + |
| 20 | + defer cleanupRegistryKeys(t) |
| 21 | + cleanupRegistryKeys(t) |
| 22 | + |
| 23 | + testIP := netip.MustParseAddr("100.64.0.1") |
| 24 | + |
| 25 | + // Create a test interface registry key so updateSearchDomains doesn't fail |
| 26 | + testGUID := "{12345678-1234-1234-1234-123456789ABC}" |
| 27 | + interfacePath := `SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\` + testGUID |
| 28 | + testKey, _, err := registry.CreateKey(registry.LOCAL_MACHINE, interfacePath, registry.SET_VALUE) |
| 29 | + require.NoError(t, err, "Should create test interface registry key") |
| 30 | + testKey.Close() |
| 31 | + defer func() { |
| 32 | + _ = registry.DeleteKey(registry.LOCAL_MACHINE, interfacePath) |
| 33 | + }() |
| 34 | + |
| 35 | + cfg := ®istryConfigurator{ |
| 36 | + guid: testGUID, |
| 37 | + gpo: false, |
| 38 | + } |
| 39 | + |
| 40 | + config5 := HostDNSConfig{ |
| 41 | + ServerIP: testIP, |
| 42 | + Domains: []DomainConfig{ |
| 43 | + {Domain: "domain1.com", MatchOnly: true}, |
| 44 | + {Domain: "domain2.com", MatchOnly: true}, |
| 45 | + {Domain: "domain3.com", MatchOnly: true}, |
| 46 | + {Domain: "domain4.com", MatchOnly: true}, |
| 47 | + {Domain: "domain5.com", MatchOnly: true}, |
| 48 | + }, |
| 49 | + } |
| 50 | + |
| 51 | + err = cfg.applyDNSConfig(config5, nil) |
| 52 | + require.NoError(t, err) |
| 53 | + |
| 54 | + // Verify all 5 entries exist |
| 55 | + for i := 0; i < 5; i++ { |
| 56 | + exists, err := registryKeyExists(fmt.Sprintf("%s-%d", dnsPolicyConfigMatchPath, i)) |
| 57 | + require.NoError(t, err) |
| 58 | + assert.True(t, exists, "Entry %d should exist after first config", i) |
| 59 | + } |
| 60 | + |
| 61 | + config2 := HostDNSConfig{ |
| 62 | + ServerIP: testIP, |
| 63 | + Domains: []DomainConfig{ |
| 64 | + {Domain: "domain1.com", MatchOnly: true}, |
| 65 | + {Domain: "domain2.com", MatchOnly: true}, |
| 66 | + }, |
| 67 | + } |
| 68 | + |
| 69 | + err = cfg.applyDNSConfig(config2, nil) |
| 70 | + require.NoError(t, err) |
| 71 | + |
| 72 | + // Verify first 2 entries exist |
| 73 | + for i := 0; i < 2; i++ { |
| 74 | + exists, err := registryKeyExists(fmt.Sprintf("%s-%d", dnsPolicyConfigMatchPath, i)) |
| 75 | + require.NoError(t, err) |
| 76 | + assert.True(t, exists, "Entry %d should exist after second config", i) |
| 77 | + } |
| 78 | + |
| 79 | + // Verify entries 2-4 are cleaned up |
| 80 | + for i := 2; i < 5; i++ { |
| 81 | + exists, err := registryKeyExists(fmt.Sprintf("%s-%d", dnsPolicyConfigMatchPath, i)) |
| 82 | + require.NoError(t, err) |
| 83 | + assert.False(t, exists, "Entry %d should NOT exist after reducing to 2 domains", i) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +func registryKeyExists(path string) (bool, error) { |
| 88 | + k, err := registry.OpenKey(registry.LOCAL_MACHINE, path, registry.QUERY_VALUE) |
| 89 | + if err != nil { |
| 90 | + if err == registry.ErrNotExist { |
| 91 | + return false, nil |
| 92 | + } |
| 93 | + return false, err |
| 94 | + } |
| 95 | + k.Close() |
| 96 | + return true, nil |
| 97 | +} |
| 98 | + |
| 99 | +func cleanupRegistryKeys(*testing.T) { |
| 100 | + cfg := ®istryConfigurator{nrptEntryCount: 10} |
| 101 | + _ = cfg.removeDNSMatchPolicies() |
| 102 | +} |
0 commit comments