| // Copyright 2021 Google LLC |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| |
| package devicepolicy |
| |
| import ( |
| pmpb "policy-manager/protos" |
| |
| "crypto/rand" |
| "crypto/rsa" |
| "os" |
| "testing" |
| |
| "github.com/golang/protobuf/proto" |
| ) |
| |
| // TestSetFromInstanceConfig tests that if DevicePolicy can be |
| // correctly generated from an InstanceConfig. In this test, a list of |
| // InstanceConfig will set to a DevicePolicy object sequentially, and asserts |
| // that the DevicePolicy is in the correct state after each setting. |
| func TestSetFromInstanceConfig(t *testing.T) { |
| tests := []struct { |
| name string |
| instanceConfig *pmpb.InstanceConfig |
| expectChanged bool |
| expectedInstanceConfig *pmpb.InstanceConfig |
| }{ |
| { |
| "InitConfig", |
| instanceConfig, |
| true, |
| &pmpb.InstanceConfig{ |
| UpdateStrategy: instanceConfig.UpdateStrategy, |
| MetricsEnabled: instanceConfig.MetricsEnabled, |
| HealthMonitorConfig: &pmpb.HealthMonitorConfig{ |
| Enforced: proto.Bool(true), |
| LoggingEnabled: proto.Bool(true), |
| MonitoringEnabled: proto.Bool(false), |
| }, |
| }, |
| }, |
| { |
| "NoChange", |
| instanceConfig, |
| false, |
| &pmpb.InstanceConfig{ |
| UpdateStrategy: instanceConfig.UpdateStrategy, |
| MetricsEnabled: instanceConfig.MetricsEnabled, |
| HealthMonitorConfig: &pmpb.HealthMonitorConfig{ |
| Enforced: proto.Bool(true), |
| LoggingEnabled: proto.Bool(true), |
| MonitoringEnabled: proto.Bool(false), |
| }, |
| }, |
| }, |
| { |
| "NewInstanceConfig", |
| &pmpb.InstanceConfig{ |
| MetricsEnabled: proto.Bool(false), |
| UpdateStrategy: proto.String(""), |
| HealthMonitorConfig: &pmpb.HealthMonitorConfig{ |
| Enforced: proto.Bool(true), |
| LoggingEnabled: proto.Bool(false), |
| MonitoringEnabled: proto.Bool(false), |
| }, |
| }, |
| true, |
| &pmpb.InstanceConfig{ |
| MetricsEnabled: proto.Bool(false), |
| UpdateStrategy: proto.String(""), |
| HealthMonitorConfig: &pmpb.HealthMonitorConfig{ |
| Enforced: proto.Bool(true), |
| LoggingEnabled: proto.Bool(false), |
| MonitoringEnabled: proto.Bool(false), |
| }, |
| }, |
| }, |
| { |
| "EnabledMetrics", |
| &pmpb.InstanceConfig{ |
| MetricsEnabled: proto.Bool(true), |
| UpdateStrategy: proto.String(""), |
| HealthMonitorConfig: &pmpb.HealthMonitorConfig{ |
| Enforced: proto.Bool(true), |
| LoggingEnabled: proto.Bool(true), |
| MonitoringEnabled: proto.Bool(false), |
| }, |
| }, |
| true, |
| &pmpb.InstanceConfig{ |
| MetricsEnabled: proto.Bool(true), |
| UpdateStrategy: proto.String(""), |
| HealthMonitorConfig: &pmpb.HealthMonitorConfig{ |
| Enforced: proto.Bool(true), |
| LoggingEnabled: proto.Bool(true), |
| MonitoringEnabled: proto.Bool(false), |
| }, |
| }, |
| }, |
| { |
| "DisableUpdate", |
| &pmpb.InstanceConfig{ |
| MetricsEnabled: proto.Bool(false), |
| UpdateStrategy: proto.String("update_disabled"), |
| HealthMonitorConfig: &pmpb.HealthMonitorConfig{ |
| Enforced: proto.Bool(true), |
| LoggingEnabled: proto.Bool(true), |
| MonitoringEnabled: proto.Bool(false), |
| }, |
| }, |
| true, |
| &pmpb.InstanceConfig{ |
| MetricsEnabled: proto.Bool(false), |
| UpdateStrategy: proto.String("update_disabled"), |
| HealthMonitorConfig: &pmpb.HealthMonitorConfig{ |
| Enforced: proto.Bool(true), |
| LoggingEnabled: proto.Bool(true), |
| MonitoringEnabled: proto.Bool(false), |
| }, |
| }, |
| }, |
| { |
| "EnableLogging", |
| &pmpb.InstanceConfig{ |
| MetricsEnabled: proto.Bool(false), |
| UpdateStrategy: proto.String(""), |
| HealthMonitorConfig: &pmpb.HealthMonitorConfig{ |
| Enforced: proto.Bool(true), |
| LoggingEnabled: proto.Bool(true), |
| MonitoringEnabled: proto.Bool(false), |
| }, |
| }, |
| true, |
| &pmpb.InstanceConfig{ |
| MetricsEnabled: proto.Bool(false), |
| UpdateStrategy: proto.String(""), |
| HealthMonitorConfig: &pmpb.HealthMonitorConfig{ |
| Enforced: proto.Bool(true), |
| LoggingEnabled: proto.Bool(true), |
| MonitoringEnabled: proto.Bool(false), |
| }, |
| }, |
| }, |
| { |
| "EnableMonitoring", |
| &pmpb.InstanceConfig{ |
| MetricsEnabled: proto.Bool(false), |
| UpdateStrategy: proto.String(""), |
| HealthMonitorConfig: &pmpb.HealthMonitorConfig{ |
| Enforced: proto.Bool(true), |
| LoggingEnabled: proto.Bool(false), |
| MonitoringEnabled: proto.Bool(true), |
| }, |
| }, |
| true, |
| &pmpb.InstanceConfig{ |
| MetricsEnabled: proto.Bool(false), |
| UpdateStrategy: proto.String(""), |
| HealthMonitorConfig: &pmpb.HealthMonitorConfig{ |
| Enforced: proto.Bool(true), |
| LoggingEnabled: proto.Bool(false), |
| MonitoringEnabled: proto.Bool(true), |
| }, |
| }, |
| }, |
| } |
| fakePolicy := new(DevicePolicy) |
| for _, test := range tests { |
| t.Run(test.name, func(t *testing.T) { |
| policyChanged := fakePolicy.SetFromInstanceConfig(test.instanceConfig) |
| if policyChanged != test.expectChanged { |
| t.Errorf("got %t, expect %t", policyChanged, test.expectChanged) |
| } |
| if !proto.Equal(fakePolicy.instanceConfig, test.expectedInstanceConfig) { |
| t.Errorf("got %s, expect %s", |
| proto.MarshalTextString(fakePolicy.instanceConfig), |
| proto.MarshalTextString(test.expectedInstanceConfig)) |
| } |
| }) |
| } |
| } |
| |
| // TestToInstanceConfig tests that update settings are added to DevicePolicy |
| // correctly. |
| func TestToInstanceConfigWithInstanceConfig(t *testing.T) { |
| fakePolicy := &DevicePolicy{ |
| instanceConfig: &pmpb.InstanceConfig{ |
| UpdateStrategy: proto.String(""), |
| MetricsEnabled: proto.Bool(true), |
| HealthMonitorConfig: &pmpb.HealthMonitorConfig{ |
| Enforced: proto.Bool(true), |
| LoggingEnabled: proto.Bool(true), |
| MonitoringEnabled: proto.Bool(false), |
| }, |
| }, |
| } |
| expected_instance_config := &pmpb.InstanceConfig{ |
| UpdateStrategy: proto.String(""), |
| MetricsEnabled: proto.Bool(true), |
| HealthMonitorConfig: &pmpb.HealthMonitorConfig{ |
| Enforced: proto.Bool(true), |
| LoggingEnabled: proto.Bool(true), |
| MonitoringEnabled: proto.Bool(false), |
| }, |
| } |
| |
| config := new(pmpb.InstanceConfig) |
| fakePolicy.ToInstanceConfig(config) |
| if !proto.Equal(config, expected_instance_config) { |
| t.Errorf("got: %s, expected: %s", |
| proto.MarshalTextString(config), |
| proto.MarshalTextString(expected_instance_config)) |
| } |
| } |
| |
| // TestWriteFile tests whether the WriteFile function writes a |
| // policy file. |
| func TestWriteFile(t *testing.T) { |
| config := &pmpb.InstanceConfig{ |
| MetricsEnabled: proto.Bool(true), |
| UpdateStrategy: proto.String(""), |
| HealthMonitorConfig: &pmpb.HealthMonitorConfig{ |
| Enforced: proto.Bool(true), |
| LoggingEnabled: proto.Bool(true), |
| MonitoringEnabled: proto.Bool(true), |
| }, |
| } |
| |
| // Create fake policy. |
| fakePolicy := new(DevicePolicy) |
| fakePolicy.SetFromInstanceConfig(config) |
| key, _ := rsa.GenerateKey(rand.Reader, rsaKeyBits) |
| |
| // Generate tmp policy file and key names |
| fileMgr := generateTmpFiles(t) |
| defer os.RemoveAll(fileMgr.TmpDir) |
| |
| if err := fakePolicy.SignAndWrite(key, fileMgr.TmpDevicePolicyFile, fileMgr.TmpInstanceConfigFile); err != nil { |
| t.Error(err) |
| } |
| } |
| |
| // TestReadPolicyFromDisk tests that the policy is correctly read from disk. |
| func TestReadPolicyFromDisk(t *testing.T) { |
| |
| // Create fake policy. |
| fakePolicy := new(DevicePolicy) |
| fakePolicy.SetFromInstanceConfig(instanceConfig) |
| key, _ := rsa.GenerateKey(rand.Reader, rsaKeyBits) |
| |
| // Generate tmp policy file and key names |
| fileMgr := generateTmpFiles(t) |
| defer os.RemoveAll(fileMgr.TmpDir) |
| |
| if err := fakePolicy.SignAndWrite(key, fileMgr.TmpDevicePolicyFile, fileMgr.TmpInstanceConfigFile); err != nil { |
| t.Error(err) |
| } |
| |
| if err := fakePolicy.ReadPolicyFromDisk(fileMgr.TmpInstanceConfigFile); err != nil { |
| t.Error(err) |
| } |
| |
| // Expected expectInstanceConfig proto. |
| expectInstanceConfig := &pmpb.InstanceConfig{ |
| UpdateStrategy: instanceConfig.UpdateStrategy, |
| MetricsEnabled: instanceConfig.MetricsEnabled, |
| HealthMonitorConfig: &pmpb.HealthMonitorConfig{ |
| Enforced: proto.Bool(true), |
| LoggingEnabled: proto.Bool(true), |
| MonitoringEnabled: proto.Bool(false), |
| }, |
| } |
| if !proto.Equal(fakePolicy.instanceConfig, expectInstanceConfig) { |
| t.Errorf("got %v, expect %v", |
| proto.MarshalTextString(fakePolicy.instanceConfig), |
| proto.MarshalTextString(expectInstanceConfig)) |
| } |
| } |