util/syspolicy: add read boolean setting (#9592)

This commit is contained in:
Claire Wang
2023-09-29 21:27:04 -04:00
committed by GitHub
parent 324f0d5f80
commit a56e58c244
7 changed files with 88 additions and 2 deletions

View File

@@ -19,6 +19,8 @@ type Handler interface {
ReadString(key string) (string, error)
// ReadUInt64 reads the policy settings uint64 value given the key.
ReadUInt64(key string) (uint64, error)
// ReadBool reads the policy setting's boolean value, given the key.
ReadBoolean(key string) (bool, error)
}
// ErrNoSuchKey is returned when the specified key does not have a value set.
@@ -35,6 +37,10 @@ func (defaultHandler) ReadUInt64(_ string) (uint64, error) {
return 0, ErrNoSuchKey
}
func (defaultHandler) ReadBoolean(_ string) (bool, error) {
return false, ErrNoSuchKey
}
// markHandlerInUse is called before handler methods are called.
func markHandlerInUse() {
handlerUsed.Store(true)

View File

@@ -30,3 +30,11 @@ func (windowsHandler) ReadUInt64(key string) (uint64, error) {
}
return value, err
}
func (windowsHandler) ReadBoolean(key string) (bool, error) {
value, err := winutil.GetPolicyInteger(key)
if errors.Is(err, winutil.ErrNoValue) {
err = ErrNoSuchKey
}
return value != 0, err
}

View File

@@ -27,6 +27,15 @@ func GetUint64(key Key, defaultValue uint64) (uint64, error) {
return v, err
}
func GetBoolean(key Key, defaultValue bool) (bool, error) {
markHandlerInUse()
v, err := handler.ReadBoolean(string(key))
if errors.Is(err, ErrNoSuchKey) {
return defaultValue, nil
}
return v, err
}
// PreferenceOption is a policy that governs whether a boolean variable
// is forcibly assigned an administrator-defined value, or allowed to receive
// a user-defined value.

View File

@@ -17,6 +17,7 @@ type testHandler struct {
key Key
s string
u64 uint64
b bool
err error
}
@@ -43,6 +44,13 @@ func (th *testHandler) ReadUInt64(key string) (uint64, error) {
return th.u64, th.err
}
func (th *testHandler) ReadBoolean(key string) (bool, error) {
if key != string(th.key) {
th.t.Errorf("ReadBool(%q) want %q", key, th.key)
}
return th.b, th.err
}
func TestGetString(t *testing.T) {
tests := []struct {
name string
@@ -157,6 +165,58 @@ func TestGetUint64(t *testing.T) {
}
}
func TestGetBoolean(t *testing.T) {
tests := []struct {
name string
key Key
handlerValue bool
handlerError error
defaultValue bool
wantValue bool
wantError error
}{
{
name: "read existing value",
key: FlushDNSOnSessionUnlock,
handlerValue: true,
wantValue: true,
},
{
name: "read non-existing value",
key: LogSCMInteractions,
handlerValue: false,
handlerError: ErrNoSuchKey,
wantValue: false,
},
{
name: "reading value returns other error",
key: FlushDNSOnSessionUnlock,
handlerError: someOtherError,
wantError: someOtherError,
defaultValue: true,
wantValue: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
setHandlerForTest(t, &testHandler{
t: t,
key: tt.key,
b: tt.handlerValue,
err: tt.handlerError,
})
value, err := GetBoolean(tt.key, tt.defaultValue)
if err != tt.wantError {
t.Errorf("err=%q, want %q", err, tt.wantError)
}
if value != tt.wantValue {
t.Errorf("value=%v, want %v", value, tt.wantValue)
}
})
}
}
func TestGetPreferenceOption(t *testing.T) {
tests := []struct {
name string