mirror of
https://github.com/netbirdio/netbird.git
synced 2026-09-20 13:49:07 +02:00
Merge remote-tracking branch 'origin/main' into fix_update_settings_value_aware
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
package mdm
|
||||
|
||||
import "net/url"
|
||||
import (
|
||||
"net/url"
|
||||
|
||||
"github.com/netbirdio/netbird/util"
|
||||
)
|
||||
|
||||
// PreSharedKeyRedactedSentinel is the redaction mask returned in place of a
|
||||
// real pre-shared key; an incoming value equal to it is a round-trip echo,
|
||||
@@ -44,8 +48,9 @@ func ConflictStringPtr(key string, p *string) ConflictCheck {
|
||||
}
|
||||
}
|
||||
|
||||
// ConflictURL builds a ConflictCheck for a URL-typed MDM key; both sides are
|
||||
// normalized via CanonicalURL before comparison.
|
||||
// ConflictURL builds a ConflictCheck for a URL-typed MDM key. The two sides are
|
||||
// compared as the endpoints they address, not as strings: see
|
||||
// util.SameServiceURL.
|
||||
func ConflictURL(key, got string) ConflictCheck {
|
||||
return ConflictCheck{
|
||||
Key: key,
|
||||
@@ -54,7 +59,7 @@ func ConflictURL(key, got string) ConflictCheck {
|
||||
return true
|
||||
}
|
||||
want, ok := pol.GetString(key)
|
||||
return ok && CanonicalURL(want) == CanonicalURL(got)
|
||||
return ok && util.SameServiceURLStrings(want, got)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package mdm
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// The same spellings, through the conflict check that decides whether a request
|
||||
// is refused. An enforced URL restated in another spelling addresses the very
|
||||
// server the policy names, so it must not be reported as a conflict.
|
||||
func TestConflictURLComparesEndpoints(t *testing.T) {
|
||||
policy := NewPolicy(map[string]any{KeyManagementURL: "https://mgmt.example.com"})
|
||||
require.True(t, policy.HasKey(KeyManagementURL))
|
||||
|
||||
for _, restated := range []string{
|
||||
"https://mgmt.example.com",
|
||||
"https://mgmt.example.com:443",
|
||||
"https://mgmt.example.com/",
|
||||
"https://MGMT.example.com",
|
||||
"https://mgmt.example.com:0443",
|
||||
} {
|
||||
conflicts := ResolveConflicts(policy, []ConflictCheck{ConflictURL(KeyManagementURL, restated)})
|
||||
assert.Empty(t, conflicts, "%q is the enforced endpoint written differently", restated)
|
||||
}
|
||||
|
||||
for _, diverging := range []string{
|
||||
"https://other.example.com",
|
||||
"http://mgmt.example.com",
|
||||
"https://mgmt.example.com:8443",
|
||||
"https://mgmt.example.com/other",
|
||||
} {
|
||||
conflicts := ResolveConflicts(policy, []ConflictCheck{ConflictURL(KeyManagementURL, diverging)})
|
||||
assert.Equal(t, []string{KeyManagementURL}, conflicts, "%q addresses another endpoint", diverging)
|
||||
}
|
||||
|
||||
// An unset field is not a request to change anything.
|
||||
assert.Empty(t, ResolveConflicts(policy, []ConflictCheck{ConflictURL(KeyManagementURL, "")}))
|
||||
}
|
||||
@@ -235,6 +235,8 @@ func (p *Policy) GetBool(key string) (bool, bool) {
|
||||
return t != 0, true
|
||||
case int64:
|
||||
return t != 0, true
|
||||
case float64:
|
||||
return t != 0, true
|
||||
}
|
||||
return false, false
|
||||
}
|
||||
|
||||
@@ -96,7 +96,8 @@ func TestPolicy_GetBool(t *testing.T) {
|
||||
{"int64 nonzero", int64(2), true, true},
|
||||
{"int64 zero", int64(0), false, true},
|
||||
{"string garbage", "maybe", false, false},
|
||||
{"float unsupported", 1.0, false, false},
|
||||
{"float nonzero", 1.0, true, true},
|
||||
{"float zero", 0.0, false, true},
|
||||
}
|
||||
for _, c := range cases {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
@@ -156,6 +157,20 @@ func TestPolicy_GetStringSlice(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
// encoding/json decodes every JSON number into float64, so the mobile
|
||||
// loaders never see int.
|
||||
func TestJSONLoader_BoolFromNumber(t *testing.T) {
|
||||
p := NewJSONLoader(func() string { return `{"blockInbound":1,"disableProfiles":0}` }).Load()
|
||||
|
||||
got, ok := p.GetBool(KeyBlockInbound)
|
||||
assert.True(t, ok)
|
||||
assert.True(t, got)
|
||||
|
||||
got, ok = p.GetBool(KeyDisableProfiles)
|
||||
assert.True(t, ok)
|
||||
assert.False(t, got)
|
||||
}
|
||||
|
||||
func TestLoader_NilFetcherReturnsEmpty(t *testing.T) {
|
||||
// Loader.Load with no fetcher (desktop construction) must degrade
|
||||
// gracefully and never return nil; on linux loadPlatform is a stub
|
||||
|
||||
Reference in New Issue
Block a user