@ -9,57 +9,15 @@ import (
"testing"
"testing"
"time"
"time"
"tailscale.com/types/logger"
"tailscale.com/util/syspolicy/internal/loggerx"
"tailscale.com/util/syspolicy/internal/metrics"
"tailscale.com/util/syspolicy/setting"
"tailscale.com/util/syspolicy/setting"
"tailscale.com/util/syspolicy/source"
)
)
// testHandler encompasses all data types returned when testing any of the syspolicy
// methods that involve getting a policy value.
// For keys and the corresponding values, check policy_keys.go.
type testHandler struct {
t * testing . T
key Key
s string
u64 uint64
b bool
sArr [ ] string
err error
calls int // used for testing reads from cache vs. handler
}
var someOtherError = errors . New ( "error other than not found" )
var someOtherError = errors . New ( "error other than not found" )
func ( th * testHandler ) ReadString ( key string ) ( string , error ) {
if key != string ( th . key ) {
th . t . Errorf ( "ReadString(%q) want %q" , key , th . key )
}
th . calls ++
return th . s , th . err
}
func ( th * testHandler ) ReadUInt64 ( key string ) ( uint64 , error ) {
if key != string ( th . key ) {
th . t . Errorf ( "ReadUint64(%q) want %q" , key , th . key )
}
th . calls ++
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 )
}
th . calls ++
return th . b , th . err
}
func ( th * testHandler ) ReadStringArray ( key string ) ( [ ] string , error ) {
if key != string ( th . key ) {
th . t . Errorf ( "ReadStringArray(%q) want %q" , key , th . key )
}
th . calls ++
return th . sArr , th . err
}
func TestGetString ( t * testing . T ) {
func TestGetString ( t * testing . T ) {
tests := [ ] struct {
tests := [ ] struct {
name string
name string
@ -69,23 +27,28 @@ func TestGetString(t *testing.T) {
defaultValue string
defaultValue string
wantValue string
wantValue string
wantError error
wantError error
wantMetrics [ ] metrics . TestState
} {
} {
{
{
name : "read existing value" ,
name : "read existing value" ,
key : AdminConsoleVisibility ,
key : AdminConsoleVisibility ,
handlerValue : "hide" ,
handlerValue : "hide" ,
wantValue : "hide" ,
wantValue : "hide" ,
wantMetrics : [ ] metrics . TestState {
{ Name : "$os_syspolicy_any" , Value : 1 } ,
{ Name : "$os_syspolicy_AdminConsole" , Value : 1 } ,
} ,
} ,
} ,
{
{
name : "read non-existing value" ,
name : "read non-existing value" ,
key : EnableServerMode ,
key : EnableServerMode ,
handlerError : ErrNo SuchKey ,
handlerError : ErrNo tConfigured ,
wantError : nil ,
wantError : nil ,
} ,
} ,
{
{
name : "read non-existing value, non-blank default" ,
name : "read non-existing value, non-blank default" ,
key : EnableServerMode ,
key : EnableServerMode ,
handlerError : ErrNo SuchKey ,
handlerError : ErrNo tConfigured ,
defaultValue : "test" ,
defaultValue : "test" ,
wantValue : "test" ,
wantValue : "test" ,
wantError : nil ,
wantError : nil ,
@ -95,24 +58,43 @@ func TestGetString(t *testing.T) {
key : NetworkDevicesVisibility ,
key : NetworkDevicesVisibility ,
handlerError : someOtherError ,
handlerError : someOtherError ,
wantError : someOtherError ,
wantError : someOtherError ,
wantMetrics : [ ] metrics . TestState {
{ Name : "$os_syspolicy_errors" , Value : 1 } ,
{ Name : "$os_syspolicy_NetworkDevices_error" , Value : 1 } ,
} ,
} ,
} ,
}
}
RegisterWellKnownSettingsForTest ( t )
for _ , tt := range tests {
for _ , tt := range tests {
t . Run ( tt . name , func ( t * testing . T ) {
t . Run ( tt . name , func ( t * testing . T ) {
SetHandlerForTest ( t , & testHandler {
h := metrics . NewTestHandler ( t )
t : t ,
metrics . SetHooksForTest ( t , h . AddMetric , h . SetMetric )
key : tt . key ,
s : tt . handlerValue ,
s := source . TestSetting [ string ] {
err : tt . handlerError ,
Key : tt . key ,
} )
Value : tt . handlerValue ,
Error : tt . handlerError ,
}
registerSingleSettingStoreForTest ( t , s )
value , err := GetString ( tt . key , tt . defaultValue )
value , err := GetString ( tt . key , tt . defaultValue )
if err != tt . wantError {
if ! errorsMatchForTest ( err , tt . wantError ) {
t . Errorf ( "err=%q, want %q" , err , tt . wantError )
t . Errorf ( "err=%q, want %q" , err , tt . wantError )
}
}
if value != tt . wantValue {
if value != tt . wantValue {
t . Errorf ( "value=%v, want %v" , value , tt . wantValue )
t . Errorf ( "value=%v, want %v" , value , tt . wantValue )
}
}
wantMetrics := tt . wantMetrics
if ! metrics . ShouldReport ( ) {
// Check that metrics are not reported on platforms
// where they shouldn't be reported.
// As of 2024-09-04, syspolicy only reports metrics
// on Windows and Android.
wantMetrics = nil
}
h . MustEqual ( wantMetrics ... )
} )
} )
}
}
}
}
@ -129,7 +111,7 @@ func TestGetUint64(t *testing.T) {
} {
} {
{
{
name : "read existing value" ,
name : "read existing value" ,
key : KeyExpirationNoticeTime ,
key : LogSCMInteractions ,
handlerValue : 1 ,
handlerValue : 1 ,
wantValue : 1 ,
wantValue : 1 ,
} ,
} ,
@ -137,14 +119,14 @@ func TestGetUint64(t *testing.T) {
name : "read non-existing value" ,
name : "read non-existing value" ,
key : LogSCMInteractions ,
key : LogSCMInteractions ,
handlerValue : 0 ,
handlerValue : 0 ,
handlerError : ErrNo SuchKey ,
handlerError : ErrNo tConfigured ,
wantValue : 0 ,
wantValue : 0 ,
} ,
} ,
{
{
name : "read non-existing value, non-zero default" ,
name : "read non-existing value, non-zero default" ,
key : LogSCMInteractions ,
key : LogSCMInteractions ,
defaultValue : 2 ,
defaultValue : 2 ,
handlerError : ErrNo SuchKey ,
handlerError : ErrNo tConfigured ,
wantValue : 2 ,
wantValue : 2 ,
} ,
} ,
{
{
@ -157,14 +139,23 @@ func TestGetUint64(t *testing.T) {
for _ , tt := range tests {
for _ , tt := range tests {
t . Run ( tt . name , func ( t * testing . T ) {
t . Run ( tt . name , func ( t * testing . T ) {
SetHandlerForTest ( t , & testHandler {
// None of the policy settings tested here are integers.
t : t ,
// In fact, we don't have any integer policies as of 2024-10-08.
key : tt . key ,
// However, we can register each of them as an integer policy setting
u64 : tt . handlerValue ,
// for the duration of the test, providing us with something to test against.
err : tt . handlerError ,
if err := setting . SetDefinitionsForTest ( t , setting . NewDefinition ( tt . key , setting . DeviceSetting , setting . IntegerValue ) ) ; err != nil {
} )
t . Fatalf ( "SetDefinitionsForTest failed: %v" , err )
}
s := source . TestSetting [ uint64 ] {
Key : tt . key ,
Value : tt . handlerValue ,
Error : tt . handlerError ,
}
registerSingleSettingStoreForTest ( t , s )
value , err := GetUint64 ( tt . key , tt . defaultValue )
value , err := GetUint64 ( tt . key , tt . defaultValue )
if err != tt . wantError {
if ! errorsMatchForTest ( err , tt . wantError ) {
t . Errorf ( "err=%q, want %q" , err , tt . wantError )
t . Errorf ( "err=%q, want %q" , err , tt . wantError )
}
}
if value != tt . wantValue {
if value != tt . wantValue {
@ -183,45 +174,69 @@ func TestGetBoolean(t *testing.T) {
defaultValue bool
defaultValue bool
wantValue bool
wantValue bool
wantError error
wantError error
wantMetrics [ ] metrics . TestState
} {
} {
{
{
name : "read existing value" ,
name : "read existing value" ,
key : FlushDNSOnSessionUnlock ,
key : FlushDNSOnSessionUnlock ,
handlerValue : true ,
handlerValue : true ,
wantValue : true ,
wantValue : true ,
wantMetrics : [ ] metrics . TestState {
{ Name : "$os_syspolicy_any" , Value : 1 } ,
{ Name : "$os_syspolicy_FlushDNSOnSessionUnlock" , Value : 1 } ,
} ,
} ,
} ,
{
{
name : "read non-existing value" ,
name : "read non-existing value" ,
key : LogSCMInteractions ,
key : LogSCMInteractions ,
handlerValue : false ,
handlerValue : false ,
handlerError : ErrNo SuchKey ,
handlerError : ErrNo tConfigured ,
wantValue : false ,
wantValue : false ,
} ,
} ,
{
{
name : "reading value returns other error" ,
name : "reading value returns other error" ,
key : FlushDNSOnSessionUnlock ,
key : FlushDNSOnSessionUnlock ,
handlerError : someOtherError ,
handlerError : someOtherError ,
wantError : someOtherError ,
wantError : someOtherError , // expect error...
defaultValue : true ,
defaultValue : true ,
wantValue : false ,
wantValue : true , // ...AND default value if the handler fails.
wantMetrics : [ ] metrics . TestState {
{ Name : "$os_syspolicy_errors" , Value : 1 } ,
{ Name : "$os_syspolicy_FlushDNSOnSessionUnlock_error" , Value : 1 } ,
} ,
} ,
} ,
}
}
RegisterWellKnownSettingsForTest ( t )
for _ , tt := range tests {
for _ , tt := range tests {
t . Run ( tt . name , func ( t * testing . T ) {
t . Run ( tt . name , func ( t * testing . T ) {
SetHandlerForTest ( t , & testHandler {
h := metrics . NewTestHandler ( t )
t : t ,
metrics . SetHooksForTest ( t , h . AddMetric , h . SetMetric )
key : tt . key ,
b : tt . handlerValue ,
s := source . TestSetting [ bool ] {
err : tt . handlerError ,
Key : tt . key ,
} )
Value : tt . handlerValue ,
Error : tt . handlerError ,
}
registerSingleSettingStoreForTest ( t , s )
value , err := GetBoolean ( tt . key , tt . defaultValue )
value , err := GetBoolean ( tt . key , tt . defaultValue )
if err != tt . wantError {
if ! errorsMatchForTest ( err , tt . wantError ) {
t . Errorf ( "err=%q, want %q" , err , tt . wantError )
t . Errorf ( "err=%q, want %q" , err , tt . wantError )
}
}
if value != tt . wantValue {
if value != tt . wantValue {
t . Errorf ( "value=%v, want %v" , value , tt . wantValue )
t . Errorf ( "value=%v, want %v" , value , tt . wantValue )
}
}
wantMetrics := tt . wantMetrics
if ! metrics . ShouldReport ( ) {
// Check that metrics are not reported on platforms
// where they shouldn't be reported.
// As of 2024-09-04, syspolicy only reports metrics
// on Windows and Android.
wantMetrics = nil
}
h . MustEqual ( wantMetrics ... )
} )
} )
}
}
}
}
@ -234,29 +249,42 @@ func TestGetPreferenceOption(t *testing.T) {
handlerError error
handlerError error
wantValue setting . PreferenceOption
wantValue setting . PreferenceOption
wantError error
wantError error
wantMetrics [ ] metrics . TestState
} {
} {
{
{
name : "always by policy" ,
name : "always by policy" ,
key : EnableIncomingConnections ,
key : EnableIncomingConnections ,
handlerValue : "always" ,
handlerValue : "always" ,
wantValue : setting . AlwaysByPolicy ,
wantValue : setting . AlwaysByPolicy ,
wantMetrics : [ ] metrics . TestState {
{ Name : "$os_syspolicy_any" , Value : 1 } ,
{ Name : "$os_syspolicy_AllowIncomingConnections" , Value : 1 } ,
} ,
} ,
} ,
{
{
name : "never by policy" ,
name : "never by policy" ,
key : EnableIncomingConnections ,
key : EnableIncomingConnections ,
handlerValue : "never" ,
handlerValue : "never" ,
wantValue : setting . NeverByPolicy ,
wantValue : setting . NeverByPolicy ,
wantMetrics : [ ] metrics . TestState {
{ Name : "$os_syspolicy_any" , Value : 1 } ,
{ Name : "$os_syspolicy_AllowIncomingConnections" , Value : 1 } ,
} ,
} ,
} ,
{
{
name : "use default" ,
name : "use default" ,
key : EnableIncomingConnections ,
key : EnableIncomingConnections ,
handlerValue : "" ,
handlerValue : "" ,
wantValue : setting . ShowChoiceByPolicy ,
wantValue : setting . ShowChoiceByPolicy ,
wantMetrics : [ ] metrics . TestState {
{ Name : "$os_syspolicy_any" , Value : 1 } ,
{ Name : "$os_syspolicy_AllowIncomingConnections" , Value : 1 } ,
} ,
} ,
} ,
{
{
name : "read non-existing value" ,
name : "read non-existing value" ,
key : EnableIncomingConnections ,
key : EnableIncomingConnections ,
handlerError : ErrNo SuchKey ,
handlerError : ErrNo tConfigured ,
wantValue : setting . ShowChoiceByPolicy ,
wantValue : setting . ShowChoiceByPolicy ,
} ,
} ,
{
{
@ -265,24 +293,43 @@ func TestGetPreferenceOption(t *testing.T) {
handlerError : someOtherError ,
handlerError : someOtherError ,
wantValue : setting . ShowChoiceByPolicy ,
wantValue : setting . ShowChoiceByPolicy ,
wantError : someOtherError ,
wantError : someOtherError ,
wantMetrics : [ ] metrics . TestState {
{ Name : "$os_syspolicy_errors" , Value : 1 } ,
{ Name : "$os_syspolicy_AllowIncomingConnections_error" , Value : 1 } ,
} ,
} ,
} ,
}
}
RegisterWellKnownSettingsForTest ( t )
for _ , tt := range tests {
for _ , tt := range tests {
t . Run ( tt . name , func ( t * testing . T ) {
t . Run ( tt . name , func ( t * testing . T ) {
SetHandlerForTest ( t , & testHandler {
h := metrics . NewTestHandler ( t )
t : t ,
metrics . SetHooksForTest ( t , h . AddMetric , h . SetMetric )
key : tt . key ,
s : tt . handlerValue ,
s := source . TestSetting [ string ] {
err : tt . handlerError ,
Key : tt . key ,
} )
Value : tt . handlerValue ,
Error : tt . handlerError ,
}
registerSingleSettingStoreForTest ( t , s )
option , err := GetPreferenceOption ( tt . key )
option , err := GetPreferenceOption ( tt . key )
if err != tt . wantError {
if ! errorsMatchForTest ( err , tt . wantError ) {
t . Errorf ( "err=%q, want %q" , err , tt . wantError )
t . Errorf ( "err=%q, want %q" , err , tt . wantError )
}
}
if option != tt . wantValue {
if option != tt . wantValue {
t . Errorf ( "option=%v, want %v" , option , tt . wantValue )
t . Errorf ( "option=%v, want %v" , option , tt . wantValue )
}
}
wantMetrics := tt . wantMetrics
if ! metrics . ShouldReport ( ) {
// Check that metrics are not reported on platforms
// where they shouldn't be reported.
// As of 2024-09-04, syspolicy only reports metrics
// on Windows and Android.
wantMetrics = nil
}
h . MustEqual ( wantMetrics ... )
} )
} )
}
}
}
}
@ -295,24 +342,33 @@ func TestGetVisibility(t *testing.T) {
handlerError error
handlerError error
wantValue setting . Visibility
wantValue setting . Visibility
wantError error
wantError error
wantMetrics [ ] metrics . TestState
} {
} {
{
{
name : "hidden by policy" ,
name : "hidden by policy" ,
key : AdminConsoleVisibility ,
key : AdminConsoleVisibility ,
handlerValue : "hide" ,
handlerValue : "hide" ,
wantValue : setting . HiddenByPolicy ,
wantValue : setting . HiddenByPolicy ,
wantMetrics : [ ] metrics . TestState {
{ Name : "$os_syspolicy_any" , Value : 1 } ,
{ Name : "$os_syspolicy_AdminConsole" , Value : 1 } ,
} ,
} ,
} ,
{
{
name : "visibility default" ,
name : "visibility default" ,
key : AdminConsoleVisibility ,
key : AdminConsoleVisibility ,
handlerValue : "show" ,
handlerValue : "show" ,
wantValue : setting . VisibleByPolicy ,
wantValue : setting . VisibleByPolicy ,
wantMetrics : [ ] metrics . TestState {
{ Name : "$os_syspolicy_any" , Value : 1 } ,
{ Name : "$os_syspolicy_AdminConsole" , Value : 1 } ,
} ,
} ,
} ,
{
{
name : "read non-existing value" ,
name : "read non-existing value" ,
key : AdminConsoleVisibility ,
key : AdminConsoleVisibility ,
handlerValue : "show" ,
handlerValue : "show" ,
handlerError : ErrNo SuchKey ,
handlerError : ErrNo tConfigured ,
wantValue : setting . VisibleByPolicy ,
wantValue : setting . VisibleByPolicy ,
} ,
} ,
{
{
@ -322,24 +378,43 @@ func TestGetVisibility(t *testing.T) {
handlerError : someOtherError ,
handlerError : someOtherError ,
wantValue : setting . VisibleByPolicy ,
wantValue : setting . VisibleByPolicy ,
wantError : someOtherError ,
wantError : someOtherError ,
wantMetrics : [ ] metrics . TestState {
{ Name : "$os_syspolicy_errors" , Value : 1 } ,
{ Name : "$os_syspolicy_AdminConsole_error" , Value : 1 } ,
} ,
} ,
} ,
}
}
RegisterWellKnownSettingsForTest ( t )
for _ , tt := range tests {
for _ , tt := range tests {
t . Run ( tt . name , func ( t * testing . T ) {
t . Run ( tt . name , func ( t * testing . T ) {
SetHandlerForTest ( t , & testHandler {
h := metrics . NewTestHandler ( t )
t : t ,
metrics . SetHooksForTest ( t , h . AddMetric , h . SetMetric )
key : tt . key ,
s : tt . handlerValue ,
s := source . TestSetting [ string ] {
err : tt . handlerError ,
Key : tt . key ,
} )
Value : tt . handlerValue ,
Error : tt . handlerError ,
}
registerSingleSettingStoreForTest ( t , s )
visibility , err := GetVisibility ( tt . key )
visibility , err := GetVisibility ( tt . key )
if err != tt . wantError {
if ! errorsMatchForTest ( err , tt . wantError ) {
t . Errorf ( "err=%q, want %q" , err , tt . wantError )
t . Errorf ( "err=%q, want %q" , err , tt . wantError )
}
}
if visibility != tt . wantValue {
if visibility != tt . wantValue {
t . Errorf ( "visibility=%v, want %v" , visibility , tt . wantValue )
t . Errorf ( "visibility=%v, want %v" , visibility , tt . wantValue )
}
}
wantMetrics := tt . wantMetrics
if ! metrics . ShouldReport ( ) {
// Check that metrics are not reported on platforms
// where they shouldn't be reported.
// As of 2024-09-04, syspolicy only reports metrics
// on Windows and Android.
wantMetrics = nil
}
h . MustEqual ( wantMetrics ... )
} )
} )
}
}
}
}
@ -353,6 +428,7 @@ func TestGetDuration(t *testing.T) {
defaultValue time . Duration
defaultValue time . Duration
wantValue time . Duration
wantValue time . Duration
wantError error
wantError error
wantMetrics [ ] metrics . TestState
} {
} {
{
{
name : "read existing value" ,
name : "read existing value" ,
@ -360,25 +436,34 @@ func TestGetDuration(t *testing.T) {
handlerValue : "2h" ,
handlerValue : "2h" ,
wantValue : 2 * time . Hour ,
wantValue : 2 * time . Hour ,
defaultValue : 24 * time . Hour ,
defaultValue : 24 * time . Hour ,
wantMetrics : [ ] metrics . TestState {
{ Name : "$os_syspolicy_any" , Value : 1 } ,
{ Name : "$os_syspolicy_KeyExpirationNotice" , Value : 1 } ,
} ,
} ,
} ,
{
{
name : "invalid duration value" ,
name : "invalid duration value" ,
key : KeyExpirationNoticeTime ,
key : KeyExpirationNoticeTime ,
handlerValue : "-20" ,
handlerValue : "-20" ,
wantValue : 24 * time . Hour ,
wantValue : 24 * time . Hour ,
wantError : errors . New ( ` time: missing unit in duration "-20" ` ) ,
defaultValue : 24 * time . Hour ,
defaultValue : 24 * time . Hour ,
wantMetrics : [ ] metrics . TestState {
{ Name : "$os_syspolicy_errors" , Value : 1 } ,
{ Name : "$os_syspolicy_KeyExpirationNotice_error" , Value : 1 } ,
} ,
} ,
} ,
{
{
name : "read non-existing value" ,
name : "read non-existing value" ,
key : KeyExpirationNoticeTime ,
key : KeyExpirationNoticeTime ,
handlerError : ErrNo SuchKey ,
handlerError : ErrNo tConfigured ,
wantValue : 24 * time . Hour ,
wantValue : 24 * time . Hour ,
defaultValue : 24 * time . Hour ,
defaultValue : 24 * time . Hour ,
} ,
} ,
{
{
name : "read non-existing value different default" ,
name : "read non-existing value different default" ,
key : KeyExpirationNoticeTime ,
key : KeyExpirationNoticeTime ,
handlerError : ErrNo SuchKey ,
handlerError : ErrNo tConfigured ,
wantValue : 0 * time . Second ,
wantValue : 0 * time . Second ,
defaultValue : 0 * time . Second ,
defaultValue : 0 * time . Second ,
} ,
} ,
@ -389,24 +474,43 @@ func TestGetDuration(t *testing.T) {
wantValue : 24 * time . Hour ,
wantValue : 24 * time . Hour ,
wantError : someOtherError ,
wantError : someOtherError ,
defaultValue : 24 * time . Hour ,
defaultValue : 24 * time . Hour ,
wantMetrics : [ ] metrics . TestState {
{ Name : "$os_syspolicy_errors" , Value : 1 } ,
{ Name : "$os_syspolicy_KeyExpirationNotice_error" , Value : 1 } ,
} ,
} ,
} ,
}
}
RegisterWellKnownSettingsForTest ( t )
for _ , tt := range tests {
for _ , tt := range tests {
t . Run ( tt . name , func ( t * testing . T ) {
t . Run ( tt . name , func ( t * testing . T ) {
SetHandlerForTest ( t , & testHandler {
h := metrics . NewTestHandler ( t )
t : t ,
metrics . SetHooksForTest ( t , h . AddMetric , h . SetMetric )
key : tt . key ,
s : tt . handlerValue ,
s := source . TestSetting [ string ] {
err : tt . handlerError ,
Key : tt . key ,
} )
Value : tt . handlerValue ,
Error : tt . handlerError ,
}
registerSingleSettingStoreForTest ( t , s )
duration , err := GetDuration ( tt . key , tt . defaultValue )
duration , err := GetDuration ( tt . key , tt . defaultValue )
if err != tt . wantError {
if ! errorsMatchForTest ( err , tt . wantError ) {
t . Errorf ( "err=%q, want %q" , err , tt . wantError )
t . Errorf ( "err=%q, want %q" , err , tt . wantError )
}
}
if duration != tt . wantValue {
if duration != tt . wantValue {
t . Errorf ( "duration=%v, want %v" , duration , tt . wantValue )
t . Errorf ( "duration=%v, want %v" , duration , tt . wantValue )
}
}
wantMetrics := tt . wantMetrics
if ! metrics . ShouldReport ( ) {
// Check that metrics are not reported on platforms
// where they shouldn't be reported.
// As of 2024-09-04, syspolicy only reports metrics
// on Windows and Android.
wantMetrics = nil
}
h . MustEqual ( wantMetrics ... )
} )
} )
}
}
}
}
@ -420,23 +524,28 @@ func TestGetStringArray(t *testing.T) {
defaultValue [ ] string
defaultValue [ ] string
wantValue [ ] string
wantValue [ ] string
wantError error
wantError error
wantMetrics [ ] metrics . TestState
} {
} {
{
{
name : "read existing value" ,
name : "read existing value" ,
key : AllowedSuggestedExitNodes ,
key : AllowedSuggestedExitNodes ,
handlerValue : [ ] string { "foo" , "bar" } ,
handlerValue : [ ] string { "foo" , "bar" } ,
wantValue : [ ] string { "foo" , "bar" } ,
wantValue : [ ] string { "foo" , "bar" } ,
wantMetrics : [ ] metrics . TestState {
{ Name : "$os_syspolicy_any" , Value : 1 } ,
{ Name : "$os_syspolicy_AllowedSuggestedExitNodes" , Value : 1 } ,
} ,
} ,
} ,
{
{
name : "read non-existing value" ,
name : "read non-existing value" ,
key : AllowedSuggestedExitNodes ,
key : AllowedSuggestedExitNodes ,
handlerError : ErrNo SuchKey ,
handlerError : ErrNo tConfigured ,
wantError : nil ,
wantError : nil ,
} ,
} ,
{
{
name : "read non-existing value, non nil default" ,
name : "read non-existing value, non nil default" ,
key : AllowedSuggestedExitNodes ,
key : AllowedSuggestedExitNodes ,
handlerError : ErrNo SuchKey ,
handlerError : ErrNo tConfigured ,
defaultValue : [ ] string { "foo" , "bar" } ,
defaultValue : [ ] string { "foo" , "bar" } ,
wantValue : [ ] string { "foo" , "bar" } ,
wantValue : [ ] string { "foo" , "bar" } ,
wantError : nil ,
wantError : nil ,
@ -446,28 +555,68 @@ func TestGetStringArray(t *testing.T) {
key : AllowedSuggestedExitNodes ,
key : AllowedSuggestedExitNodes ,
handlerError : someOtherError ,
handlerError : someOtherError ,
wantError : someOtherError ,
wantError : someOtherError ,
wantMetrics : [ ] metrics . TestState {
{ Name : "$os_syspolicy_errors" , Value : 1 } ,
{ Name : "$os_syspolicy_AllowedSuggestedExitNodes_error" , Value : 1 } ,
} ,
} ,
} ,
}
}
RegisterWellKnownSettingsForTest ( t )
for _ , tt := range tests {
for _ , tt := range tests {
t . Run ( tt . name , func ( t * testing . T ) {
t . Run ( tt . name , func ( t * testing . T ) {
SetHandlerForTest ( t , & testHandler {
h := metrics . NewTestHandler ( t )
t : t ,
metrics . SetHooksForTest ( t , h . AddMetric , h . SetMetric )
key : tt . key ,
sArr : tt . handlerValue ,
s := source . TestSetting [ [ ] string ] {
err : tt . handlerError ,
Key : tt . key ,
} )
Value : tt . handlerValue ,
Error : tt . handlerError ,
}
registerSingleSettingStoreForTest ( t , s )
value , err := GetStringArray ( tt . key , tt . defaultValue )
value , err := GetStringArray ( tt . key , tt . defaultValue )
if err != tt . wantError {
if ! errorsMatchForTest ( err , tt . wantError ) {
t . Errorf ( "err=%q, want %q" , err , tt . wantError )
t . Errorf ( "err=%q, want %q" , err , tt . wantError )
}
}
if ! slices . Equal ( tt . wantValue , value ) {
if ! slices . Equal ( tt . wantValue , value ) {
t . Errorf ( "value=%v, want %v" , value , tt . wantValue )
t . Errorf ( "value=%v, want %v" , value , tt . wantValue )
}
}
wantMetrics := tt . wantMetrics
if ! metrics . ShouldReport ( ) {
// Check that metrics are not reported on platforms
// where they shouldn't be reported.
// As of 2024-09-04, syspolicy only reports metrics
// on Windows and Android.
wantMetrics = nil
}
h . MustEqual ( wantMetrics ... )
} )
} )
}
}
}
}
func registerSingleSettingStoreForTest [ T source . TestValueType ] ( tb TB , s source . TestSetting [ T ] ) {
policyStore := source . NewTestStoreOf ( tb , s )
MustRegisterStoreForTest ( tb , "TestStore" , setting . DeviceScope , policyStore )
}
func BenchmarkGetString ( b * testing . B ) {
loggerx . SetForTest ( b , logger . Discard , logger . Discard )
RegisterWellKnownSettingsForTest ( b )
wantControlURL := "https://login.tailscale.com"
registerSingleSettingStoreForTest ( b , source . TestSettingOf ( ControlURL , wantControlURL ) )
b . ResetTimer ( )
for i := 0 ; i < b . N ; i ++ {
gotControlURL , _ := GetString ( ControlURL , "https://controlplane.tailscale.com" )
if gotControlURL != wantControlURL {
b . Fatalf ( "got %v; want %v" , gotControlURL , wantControlURL )
}
}
}
func TestSelectControlURL ( t * testing . T ) {
func TestSelectControlURL ( t * testing . T ) {
tests := [ ] struct {
tests := [ ] struct {
reg , disk , want string
reg , disk , want string
@ -499,3 +648,13 @@ func TestSelectControlURL(t *testing.T) {
}
}
}
}
}
}
func errorsMatchForTest ( got , want error ) bool {
if got == nil && want == nil {
return true
}
if got == nil || want == nil {
return false
}
return errors . Is ( got , want ) || got . Error ( ) == want . Error ( )
}