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