1 // Copyright 2017 The Chromium OS Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include <gtest/gtest.h> 6 7 #include <base/files/file_util.h> 8 #include <base/files/scoped_temp_dir.h> 9 10 #include "policy/policy_util.h" 11 12 namespace em = enterprise_management; 13 14 namespace policy { 15 16 // Test LoadPolicyFromPath returns correct values and has policy data when 17 // successful. 18 TEST(DevicePolicyUtilTest, LoadPolicyFromPath) { 19 // Create the temporary directory. 20 base::ScopedTempDir temp_dir; 21 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 22 23 base::FilePath invalid_policy_data_path(temp_dir.path().Append("policy")); 24 base::FilePath inexistent_file(temp_dir.path().Append("policy.1")); 25 base::FilePath good_policy_data_path(temp_dir.path().Append("policy.2")); 26 27 // Create the file with invalid data. 28 std::string data = "invalid data"; 29 ASSERT_TRUE( 30 base::WriteFile(invalid_policy_data_path, data.data(), data.size())); 31 32 // Create the file with good policy data. 33 em::PolicyData policy_data; 34 policy_data.set_username("user@example.com"); 35 policy_data.set_management_mode(em::PolicyData::LOCAL_OWNER); 36 policy_data.set_request_token("codepath-must-ignore-dmtoken"); 37 std::string policy_blob; 38 policy_data.SerializeToString(&policy_blob); 39 ASSERT_TRUE(base::WriteFile(good_policy_data_path, policy_blob.data(), 40 policy_blob.size())); 41 42 std::string policy_data_str; 43 enterprise_management::PolicyFetchResponse policy; 44 EXPECT_EQ( 45 LoadPolicyResult::kInvalidPolicyData, 46 LoadPolicyFromPath(invalid_policy_data_path, &policy_data_str, &policy)); 47 EXPECT_EQ(LoadPolicyResult::kFileNotFound, 48 LoadPolicyFromPath(inexistent_file, &policy_data_str, &policy)); 49 EXPECT_EQ( 50 LoadPolicyResult::kSuccess, 51 LoadPolicyFromPath(good_policy_data_path, &policy_data_str, &policy)); 52 ASSERT_TRUE(policy.has_policy_data()); 53 } 54 55 } // namespace policy 56