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 <map>
8 #include <string>
9
10 #include <base/files/file_path.h>
11 #include <base/files/file_util.h>
12 #include <base/files/scoped_temp_dir.h>
13
14 #include "policy/resilient_policy_util.h"
15
16 namespace {
17
18 const char kDefaultResilientPolicyFilePath[] = "policy";
19
CreateFile(const base::FilePath & file_path)20 void CreateFile(const base::FilePath& file_path) {
21 base::File file(file_path, base::File::FLAG_CREATE | base::File::FLAG_WRITE);
22 }
23
24 } // namespace
25
26 namespace policy {
27
28 // Test that the policy files from the folder are identified correctly.
TEST(DevicePolicyUtilTest,GetSortedResilientPolicyFilePaths)29 TEST(DevicePolicyUtilTest, GetSortedResilientPolicyFilePaths) {
30 // Create the temporary directory.
31 base::ScopedTempDir temp_dir;
32 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
33
34 base::FilePath file0(temp_dir.GetPath().Append("policy"));
35 base::FilePath file1(temp_dir.GetPath().Append("policy.12"));
36 base::FilePath file2(temp_dir.GetPath().Append("policy.2"));
37 base::FilePath file3(temp_dir.GetPath().Append("policy.30"));
38 base::FilePath invalid(temp_dir.GetPath().Append("policy_4"));
39
40 CreateFile(file0);
41 CreateFile(file1);
42 CreateFile(file2);
43 CreateFile(file3);
44
45 const base::FilePath test_file_path(
46 temp_dir.GetPath().Append(kDefaultResilientPolicyFilePath));
47 std::map<int, base::FilePath> sorted_file_paths =
48 GetSortedResilientPolicyFilePaths(test_file_path);
49
50 EXPECT_EQ(4, sorted_file_paths.size());
51 EXPECT_EQ(file0.value(), sorted_file_paths[0].value());
52 EXPECT_EQ(file1.value(), sorted_file_paths[12].value());
53 EXPECT_EQ(file2.value(), sorted_file_paths[2].value());
54 EXPECT_EQ(file3.value(), sorted_file_paths[30].value());
55 EXPECT_EQ("", sorted_file_paths[4].value());
56 }
57
58 } // namespace policy
59