OLD | NEW |
| (Empty) |
1 // Copyright 2006-2009 Google Inc. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 // ======================================================================== | |
15 // | |
16 // Unit test for the file_store. | |
17 | |
18 | |
19 | |
20 #include <shlobj.h> | |
21 #include "base/basictypes.h" | |
22 #include "omaha/base/file_store.h" | |
23 #include "omaha/base/scope_guard.h" | |
24 #include "omaha/base/utils.h" | |
25 #include "omaha/testing/unit_test.h" | |
26 | |
27 namespace omaha { | |
28 | |
29 const TCHAR kFilePathPrefix[] = _T("unittest"); | |
30 const TCHAR kFileName1[] = _T("fname1"); | |
31 const TCHAR kFileName2[] = _T("fname2"); | |
32 char kFileContent[] = "1234567890abcdefg"; | |
33 | |
34 TEST(FileStoreTest, FileStore) { | |
35 // Create a temp dir | |
36 TCHAR temp_path[MAX_PATH]; | |
37 *temp_path = 0; | |
38 ASSERT_LT(0u, ::GetTempPath(MAX_PATH, temp_path)); | |
39 | |
40 CString temp_dir; | |
41 temp_dir.Format(_T("%s%s%x"), temp_path, kFilePathPrefix, ::GetTickCount()); | |
42 ASSERT_EQ(::SHCreateDirectoryEx(0, temp_dir, 0), ERROR_SUCCESS); | |
43 ON_SCOPE_EXIT(DeleteDirectory, temp_dir); | |
44 | |
45 // Test the file store | |
46 FileStore file_store; | |
47 ASSERT_TRUE(file_store.Open(temp_dir)); | |
48 | |
49 // Make sure the folder is empty | |
50 uint32 value_count; | |
51 ASSERT_TRUE(file_store.GetValueCount(&value_count)); | |
52 ASSERT_EQ(value_count, 0); | |
53 CString value_name; | |
54 ASSERT_FALSE(file_store.GetValueNameAt(0, &value_name)); | |
55 | |
56 // Write 2 files | |
57 std::vector<byte> buffer; | |
58 ASSERT_TRUE(file_store.Write(kFileName1, | |
59 reinterpret_cast<byte*>(kFileContent), | |
60 arraysize(kFileContent))); | |
61 ASSERT_TRUE(file_store.Exists(kFileName1)); | |
62 ASSERT_TRUE(file_store.GetValueCount(&value_count)); | |
63 ASSERT_EQ(value_count, 1); | |
64 ASSERT_TRUE(file_store.GetValueNameAt(0, &value_name)); | |
65 ASSERT_TRUE(value_name == kFileName1); | |
66 ASSERT_TRUE(file_store.Read(kFileName1, &buffer)); | |
67 ASSERT_TRUE(memcmp(kFileContent, | |
68 &buffer.front(), | |
69 arraysize(kFileContent)) == 0); | |
70 | |
71 ASSERT_TRUE(file_store.Write(kFileName2, | |
72 reinterpret_cast<byte*>(kFileContent), | |
73 arraysize(kFileContent))); | |
74 ASSERT_TRUE(file_store.Exists(kFileName2)); | |
75 ASSERT_TRUE(file_store.GetValueCount(&value_count)); | |
76 ASSERT_EQ(value_count, 2); | |
77 ASSERT_TRUE(file_store.GetValueNameAt(1, &value_name)); | |
78 ASSERT_TRUE(value_name == kFileName2); | |
79 ASSERT_TRUE(file_store.Read(kFileName2, &buffer)); | |
80 ASSERT_TRUE(memcmp(kFileContent, | |
81 &buffer.front(), | |
82 arraysize(kFileContent)) == 0); | |
83 | |
84 // Remove files | |
85 ASSERT_TRUE(file_store.Remove(kFileName1)); | |
86 ASSERT_FALSE(file_store.Exists(kFileName1)); | |
87 ASSERT_TRUE(file_store.GetValueCount(&value_count)); | |
88 ASSERT_EQ(value_count, 1); | |
89 ASSERT_TRUE(file_store.Remove(kFileName2)); | |
90 ASSERT_FALSE(file_store.Exists(kFileName2)); | |
91 ASSERT_TRUE(file_store.GetValueCount(&value_count)); | |
92 ASSERT_EQ(value_count, 0); | |
93 | |
94 ASSERT_TRUE(file_store.Close()); | |
95 } | |
96 | |
97 } // namespace omaha | |
98 | |
OLD | NEW |