[client] handle order of check when checking order of files in isChecksEqual (#4219)

This commit is contained in:
Pascal Fischer
2025-07-24 21:00:51 +02:00
committed by GitHub
parent c435c2727f
commit cb1e437785
2 changed files with 92 additions and 13 deletions

View File

@ -1968,21 +1968,24 @@ func (e *Engine) toExcludedLazyPeers(rules []firewallManager.ForwardRule, peers
} }
// isChecksEqual checks if two slices of checks are equal. // isChecksEqual checks if two slices of checks are equal.
func isChecksEqual(checks []*mgmProto.Checks, oChecks []*mgmProto.Checks) bool { func isChecksEqual(checks1, checks2 []*mgmProto.Checks) bool {
for _, check := range checks { normalize := func(checks []*mgmProto.Checks) []string {
sort.Slice(check.Files, func(i, j int) bool { normalized := make([]string, len(checks))
return check.Files[i] < check.Files[j]
}) for i, check := range checks {
} sortedFiles := slices.Clone(check.Files)
for _, oCheck := range oChecks { sort.Strings(sortedFiles)
sort.Slice(oCheck.Files, func(i, j int) bool { normalized[i] = strings.Join(sortedFiles, "|")
return oCheck.Files[i] < oCheck.Files[j]
})
} }
return slices.EqualFunc(checks, oChecks, func(checks, oChecks *mgmProto.Checks) bool { sort.Strings(normalized)
return slices.Equal(checks.Files, oChecks.Files) return normalized
}) }
n1 := normalize(checks1)
n2 := normalize(checks2)
return slices.Equal(n1, n2)
} }
func getInterfacePrefixes() ([]netip.Prefix, error) { func getInterfacePrefixes() ([]netip.Prefix, error) {

View File

@ -1270,6 +1270,82 @@ func Test_CheckFilesEqual(t *testing.T) {
}, },
expectedBool: false, expectedBool: false,
}, },
{
name: "Compared Slices with same files but different order should return true",
inputChecks1: []*mgmtProto.Checks{
{
Files: []string{
"testfile1",
"testfile2",
},
},
{
Files: []string{
"testfile4",
"testfile3",
},
},
},
inputChecks2: []*mgmtProto.Checks{
{
Files: []string{
"testfile3",
"testfile4",
},
},
{
Files: []string{
"testfile2",
"testfile1",
},
},
},
expectedBool: true,
},
{
name: "Compared Slices with same files but different order while first is equal should return true",
inputChecks1: []*mgmtProto.Checks{
{
Files: []string{
"testfile0",
"testfile1",
},
},
{
Files: []string{
"testfile0",
"testfile2",
},
},
{
Files: []string{
"testfile0",
"testfile3",
},
},
},
inputChecks2: []*mgmtProto.Checks{
{
Files: []string{
"testfile0",
"testfile1",
},
},
{
Files: []string{
"testfile0",
"testfile3",
},
},
{
Files: []string{
"testfile0",
"testfile2",
},
},
},
expectedBool: true,
},
} }
for _, testCase := range testCases { for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) { t.Run(testCase.name, func(t *testing.T) {