| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565 |
- // Copyright (c) Tailscale Inc & AUTHORS
- // SPDX-License-Identifier: BSD-3-Clause
- package setting
- import (
- "reflect"
- "testing"
- jsonv2 "github.com/go-json-experiment/json"
- )
- func TestPolicyScopeIsApplicableSetting(t *testing.T) {
- tests := []struct {
- name string
- scope PolicyScope
- setting *Definition
- wantApplicable bool
- }{
- {
- name: "DeviceScope/DeviceSetting",
- scope: DeviceScope,
- setting: NewDefinition("TestSetting", DeviceSetting, IntegerValue),
- wantApplicable: true,
- },
- {
- name: "DeviceScope/ProfileSetting",
- scope: DeviceScope,
- setting: NewDefinition("TestSetting", ProfileSetting, IntegerValue),
- wantApplicable: false,
- },
- {
- name: "DeviceScope/UserSetting",
- scope: DeviceScope,
- setting: NewDefinition("TestSetting", UserSetting, IntegerValue),
- wantApplicable: false,
- },
- {
- name: "ProfileScope/DeviceSetting",
- scope: CurrentProfileScope,
- setting: NewDefinition("TestSetting", DeviceSetting, IntegerValue),
- wantApplicable: true,
- },
- {
- name: "ProfileScope/ProfileSetting",
- scope: CurrentProfileScope,
- setting: NewDefinition("TestSetting", ProfileSetting, IntegerValue),
- wantApplicable: true,
- },
- {
- name: "ProfileScope/UserSetting",
- scope: CurrentProfileScope,
- setting: NewDefinition("TestSetting", UserSetting, IntegerValue),
- wantApplicable: false,
- },
- {
- name: "UserScope/DeviceSetting",
- scope: CurrentUserScope,
- setting: NewDefinition("TestSetting", DeviceSetting, IntegerValue),
- wantApplicable: true,
- },
- {
- name: "UserScope/ProfileSetting",
- scope: CurrentUserScope,
- setting: NewDefinition("TestSetting", ProfileSetting, IntegerValue),
- wantApplicable: true,
- },
- {
- name: "UserScope/UserSetting",
- scope: CurrentUserScope,
- setting: NewDefinition("TestSetting", UserSetting, IntegerValue),
- wantApplicable: true,
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- gotApplicable := tt.scope.IsApplicableSetting(tt.setting)
- if gotApplicable != tt.wantApplicable {
- t.Fatalf("got %v, want %v", gotApplicable, tt.wantApplicable)
- }
- })
- }
- }
- func TestPolicyScopeIsConfigurableSetting(t *testing.T) {
- tests := []struct {
- name string
- scope PolicyScope
- setting *Definition
- wantConfigurable bool
- }{
- {
- name: "DeviceScope/DeviceSetting",
- scope: DeviceScope,
- setting: NewDefinition("TestSetting", DeviceSetting, IntegerValue),
- wantConfigurable: true,
- },
- {
- name: "DeviceScope/ProfileSetting",
- scope: DeviceScope,
- setting: NewDefinition("TestSetting", ProfileSetting, IntegerValue),
- wantConfigurable: true,
- },
- {
- name: "DeviceScope/UserSetting",
- scope: DeviceScope,
- setting: NewDefinition("TestSetting", UserSetting, IntegerValue),
- wantConfigurable: true,
- },
- {
- name: "ProfileScope/DeviceSetting",
- scope: CurrentProfileScope,
- setting: NewDefinition("TestSetting", DeviceSetting, IntegerValue),
- wantConfigurable: false,
- },
- {
- name: "ProfileScope/ProfileSetting",
- scope: CurrentProfileScope,
- setting: NewDefinition("TestSetting", ProfileSetting, IntegerValue),
- wantConfigurable: true,
- },
- {
- name: "ProfileScope/UserSetting",
- scope: CurrentProfileScope,
- setting: NewDefinition("TestSetting", UserSetting, IntegerValue),
- wantConfigurable: true,
- },
- {
- name: "UserScope/DeviceSetting",
- scope: CurrentUserScope,
- setting: NewDefinition("TestSetting", DeviceSetting, IntegerValue),
- wantConfigurable: false,
- },
- {
- name: "UserScope/ProfileSetting",
- scope: CurrentUserScope,
- setting: NewDefinition("TestSetting", ProfileSetting, IntegerValue),
- wantConfigurable: false,
- },
- {
- name: "UserScope/UserSetting",
- scope: CurrentUserScope,
- setting: NewDefinition("TestSetting", UserSetting, IntegerValue),
- wantConfigurable: true,
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- gotConfigurable := tt.scope.IsConfigurableSetting(tt.setting)
- if gotConfigurable != tt.wantConfigurable {
- t.Fatalf("got %v, want %v", gotConfigurable, tt.wantConfigurable)
- }
- })
- }
- }
- func TestPolicyScopeContains(t *testing.T) {
- tests := []struct {
- name string
- scopeA PolicyScope
- scopeB PolicyScope
- wantAContainsB bool
- wantAStrictlyContainsB bool
- }{
- {
- name: "DeviceScope/DeviceScope",
- scopeA: DeviceScope,
- scopeB: DeviceScope,
- wantAContainsB: true,
- wantAStrictlyContainsB: false,
- },
- {
- name: "DeviceScope/CurrentProfileScope",
- scopeA: DeviceScope,
- scopeB: CurrentProfileScope,
- wantAContainsB: true,
- wantAStrictlyContainsB: true,
- },
- {
- name: "DeviceScope/UserScope",
- scopeA: DeviceScope,
- scopeB: CurrentUserScope,
- wantAContainsB: true,
- wantAStrictlyContainsB: true,
- },
- {
- name: "ProfileScope/DeviceScope",
- scopeA: CurrentProfileScope,
- scopeB: DeviceScope,
- wantAContainsB: false,
- wantAStrictlyContainsB: false,
- },
- {
- name: "ProfileScope/ProfileScope",
- scopeA: CurrentProfileScope,
- scopeB: CurrentProfileScope,
- wantAContainsB: true,
- wantAStrictlyContainsB: false,
- },
- {
- name: "ProfileScope/UserScope",
- scopeA: CurrentProfileScope,
- scopeB: CurrentUserScope,
- wantAContainsB: true,
- wantAStrictlyContainsB: true,
- },
- {
- name: "UserScope/DeviceScope",
- scopeA: CurrentUserScope,
- scopeB: DeviceScope,
- wantAContainsB: false,
- wantAStrictlyContainsB: false,
- },
- {
- name: "UserScope/ProfileScope",
- scopeA: CurrentUserScope,
- scopeB: CurrentProfileScope,
- wantAContainsB: false,
- wantAStrictlyContainsB: false,
- },
- {
- name: "UserScope/UserScope",
- scopeA: CurrentUserScope,
- scopeB: CurrentUserScope,
- wantAContainsB: true,
- wantAStrictlyContainsB: false,
- },
- {
- name: "UserScope(1234)/UserScope(1234)",
- scopeA: UserScopeOf("1234"),
- scopeB: UserScopeOf("1234"),
- wantAContainsB: true,
- wantAStrictlyContainsB: false,
- },
- {
- name: "UserScope(1234)/UserScope(5678)",
- scopeA: UserScopeOf("1234"),
- scopeB: UserScopeOf("5678"),
- wantAContainsB: false,
- wantAStrictlyContainsB: false,
- },
- {
- name: "ProfileScope(A)/UserScope(A/1234)",
- scopeA: PolicyScope{kind: ProfileSetting, profileID: "A"},
- scopeB: PolicyScope{kind: UserSetting, userID: "1234", profileID: "A"},
- wantAContainsB: true,
- wantAStrictlyContainsB: true,
- },
- {
- name: "ProfileScope(A)/UserScope(B/1234)",
- scopeA: PolicyScope{kind: ProfileSetting, profileID: "A"},
- scopeB: PolicyScope{kind: UserSetting, userID: "1234", profileID: "B"},
- wantAContainsB: false,
- wantAStrictlyContainsB: false,
- },
- {
- name: "UserScope(1234)/UserScope(A/1234)",
- scopeA: PolicyScope{kind: UserSetting, userID: "1234"},
- scopeB: PolicyScope{kind: UserSetting, userID: "1234", profileID: "A"},
- wantAContainsB: true,
- wantAStrictlyContainsB: true,
- },
- {
- name: "UserScope(1234)/UserScope(A/5678)",
- scopeA: PolicyScope{kind: UserSetting, userID: "1234"},
- scopeB: PolicyScope{kind: UserSetting, userID: "5678", profileID: "A"},
- wantAContainsB: false,
- wantAStrictlyContainsB: false,
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- gotContains := tt.scopeA.Contains(tt.scopeB)
- if gotContains != tt.wantAContainsB {
- t.Fatalf("WithinOf: got %v, want %v", gotContains, tt.wantAContainsB)
- }
- gotStrictlyContains := tt.scopeA.StrictlyContains(tt.scopeB)
- if gotStrictlyContains != tt.wantAStrictlyContainsB {
- t.Fatalf("StrictlyWithinOf: got %v, want %v", gotStrictlyContains, tt.wantAStrictlyContainsB)
- }
- })
- }
- }
- func TestPolicyScopeMarshalUnmarshal(t *testing.T) {
- tests := []struct {
- name string
- in any
- wantJSON string
- wantError bool
- }{
- {
- name: "null-scope",
- in: &struct {
- Scope PolicyScope
- }{},
- wantJSON: `{"Scope":"Device"}`,
- },
- {
- name: "null-scope-omit-zero",
- in: &struct {
- Scope PolicyScope `json:",omitzero"`
- }{},
- wantJSON: `{}`,
- },
- {
- name: "device-scope",
- in: &struct {
- Scope PolicyScope
- }{DeviceScope},
- wantJSON: `{"Scope":"Device"}`,
- },
- {
- name: "current-profile-scope",
- in: &struct {
- Scope PolicyScope
- }{CurrentProfileScope},
- wantJSON: `{"Scope":"Profile"}`,
- },
- {
- name: "current-user-scope",
- in: &struct {
- Scope PolicyScope
- }{CurrentUserScope},
- wantJSON: `{"Scope":"User"}`,
- },
- {
- name: "specific-user-scope",
- in: &struct {
- Scope PolicyScope
- }{UserScopeOf("_")},
- wantJSON: `{"Scope":"User(_)"}`,
- },
- {
- name: "specific-user-scope",
- in: &struct {
- Scope PolicyScope
- }{UserScopeOf("S-1-5-21-3698941153-1525015703-2649197413-1001")},
- wantJSON: `{"Scope":"User(S-1-5-21-3698941153-1525015703-2649197413-1001)"}`,
- },
- {
- name: "specific-profile-scope",
- in: &struct {
- Scope PolicyScope
- }{PolicyScope{kind: ProfileSetting, profileID: "1234"}},
- wantJSON: `{"Scope":"Profile(1234)"}`,
- },
- {
- name: "specific-profile-and-user-scope",
- in: &struct {
- Scope PolicyScope
- }{PolicyScope{
- kind: UserSetting,
- profileID: "1234",
- userID: "S-1-5-21-3698941153-1525015703-2649197413-1001",
- }},
- wantJSON: `{"Scope":"Profile(1234)/User(S-1-5-21-3698941153-1525015703-2649197413-1001)"}`,
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- gotJSON, err := jsonv2.Marshal(tt.in)
- if err != nil {
- t.Fatalf("Marshal failed: %v", err)
- }
- if string(gotJSON) != tt.wantJSON {
- t.Fatalf("Marshal got %s, want %s", gotJSON, tt.wantJSON)
- }
- wantBack := tt.in
- gotBack := reflect.New(reflect.TypeOf(tt.in).Elem()).Interface()
- err = jsonv2.Unmarshal(gotJSON, gotBack)
- if err != nil {
- t.Fatalf("Unmarshal failed: %v", err)
- }
- if !reflect.DeepEqual(gotBack, wantBack) {
- t.Fatalf("Unmarshal got %+v, want %+v", gotBack, wantBack)
- }
- })
- }
- }
- func TestPolicyScopeUnmarshalSpecial(t *testing.T) {
- tests := []struct {
- name string
- json string
- want any
- wantError bool
- }{
- {
- name: "empty",
- json: "{}",
- want: &struct {
- Scope PolicyScope
- }{},
- },
- {
- name: "too-many-scopes",
- json: `{"Scope":"Device/Profile/User"}`,
- wantError: true,
- },
- {
- name: "user/profile", // incorrect order
- json: `{"Scope":"User/Profile"}`,
- wantError: true,
- },
- {
- name: "profile-user-no-params",
- json: `{"Scope":"Profile/User"}`,
- want: &struct {
- Scope PolicyScope
- }{CurrentUserScope},
- },
- {
- name: "unknown-scope",
- json: `{"Scope":"Unknown"}`,
- wantError: true,
- },
- {
- name: "unknown-scope/unknown-scope",
- json: `{"Scope":"Unknown/Unknown"}`,
- wantError: true,
- },
- {
- name: "device-scope/unknown-scope",
- json: `{"Scope":"Device/Unknown"}`,
- wantError: true,
- },
- {
- name: "unknown-scope/device-scope",
- json: `{"Scope":"Unknown/Device"}`,
- wantError: true,
- },
- {
- name: "slash",
- json: `{"Scope":"/"}`,
- wantError: true,
- },
- {
- name: "empty",
- json: `{"Scope": ""`,
- wantError: true,
- },
- {
- name: "no-closing-bracket",
- json: `{"Scope": "user(1234"`,
- wantError: true,
- },
- {
- name: "device-with-id",
- json: `{"Scope": "device(123)"`,
- wantError: true,
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- got := &struct {
- Scope PolicyScope
- }{}
- err := jsonv2.Unmarshal([]byte(tt.json), got)
- if (err != nil) != tt.wantError {
- t.Errorf("Marshal error: got %v, want %v", err, tt.wantError)
- }
- if err != nil {
- return
- }
- if !reflect.DeepEqual(got, tt.want) {
- t.Fatalf("Unmarshal got %+v, want %+v", got, tt.want)
- }
- })
- }
- }
- func TestExtractScopeAndParams(t *testing.T) {
- tests := []struct {
- name string
- s string
- scope string
- params string
- wantOk bool
- }{
- {
- name: "empty",
- s: "",
- wantOk: true,
- },
- {
- name: "scope-only",
- s: "device",
- scope: "device",
- wantOk: true,
- },
- {
- name: "scope-with-params",
- s: "user(1234)",
- scope: "user",
- params: "1234",
- wantOk: true,
- },
- {
- name: "params-empty-scope",
- s: "(1234)",
- scope: "",
- params: "1234",
- wantOk: true,
- },
- {
- name: "params-with-brackets",
- s: "test()())))())",
- scope: "test",
- params: ")())))()",
- wantOk: true,
- },
- {
- name: "no-closing-bracket",
- s: "user(1234",
- scope: "",
- params: "",
- wantOk: false,
- },
- {
- name: "open-before-close",
- s: ")user(1234",
- scope: "",
- params: "",
- wantOk: false,
- },
- {
- name: "brackets-only",
- s: ")(",
- scope: "",
- params: "",
- wantOk: false,
- },
- {
- name: "closing-bracket",
- s: ")",
- scope: "",
- params: "",
- wantOk: false,
- },
- {
- name: "opening-bracket",
- s: ")",
- scope: "",
- params: "",
- wantOk: false,
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- scope, params, ok := extractScopeAndParams(tt.s)
- if ok != tt.wantOk {
- t.Logf("OK: got %v; want %v", ok, tt.wantOk)
- }
- if scope != tt.scope {
- t.Logf("Scope: got %q; want %q", scope, tt.scope)
- }
- if params != tt.params {
- t.Logf("Params: got %v; want %v", params, tt.params)
- }
- })
- }
- }
|