OLD | NEW |
| (Empty) |
1 // Copyright 2007-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 #include "base/basictypes.h" | |
17 #include "omaha/base/logging.h" | |
18 #include "omaha/testing/unit_test.h" | |
19 | |
20 namespace omaha { | |
21 | |
22 TEST(LoggingTest, Logging) { | |
23 #ifdef _DEBUG | |
24 OPT_LOG(L1, (_T("[OPT_LOG from debug build.]"))); | |
25 #else | |
26 OPT_LOG(L1, (_T("[OPT_LOG from optimized build.]"))); | |
27 #endif | |
28 } | |
29 | |
30 class FileLogWriterTest : public testing::Test { | |
31 public: | |
32 | |
33 int FindFirstInMultiString(const TCHAR* multi_str, | |
34 size_t count, | |
35 const TCHAR* str) { | |
36 return FileLogWriter::FindFirstInMultiString(multi_str, count, str); | |
37 } | |
38 }; | |
39 | |
40 class HistoryTest : public testing::Test { | |
41 protected: | |
42 HistoryTest() { | |
43 logging_ = GetLogging(); | |
44 } | |
45 | |
46 void AppendToHistory(const wchar_t* msg) { | |
47 logging_->AppendToHistory(msg); | |
48 } | |
49 | |
50 CString GetHistory() { | |
51 return logging_->GetHistory(); | |
52 } | |
53 | |
54 private: | |
55 Logging* logging_; | |
56 }; | |
57 | |
58 #define EOS _T("") | |
59 TEST_F(FileLogWriterTest, FindInMultiString) { | |
60 // One string of one char. | |
61 const TCHAR s1[] = _T("a\0") EOS; | |
62 EXPECT_EQ(FindFirstInMultiString(s1, arraysize(s1), _T("a")), 0); | |
63 | |
64 // One string. | |
65 const TCHAR s2[] = _T("abc\0") EOS; | |
66 EXPECT_EQ(FindFirstInMultiString(s2, arraysize(s2), _T("abc")), 0); | |
67 | |
68 // Two strings of one char. | |
69 const TCHAR s3[] = _T("a\0b\0") EOS; | |
70 EXPECT_EQ(FindFirstInMultiString(s3, arraysize(s3), _T("b")), 2); | |
71 | |
72 // Two strings. | |
73 const TCHAR s4[] = _T("ab\0cde\0") EOS; | |
74 EXPECT_EQ(FindFirstInMultiString(s4, arraysize(s4), _T("cde")), 3); | |
75 | |
76 // Three strings one char. | |
77 const TCHAR s5[] = _T("a\0b\0c\0") EOS; | |
78 EXPECT_EQ(FindFirstInMultiString(s5, arraysize(s5), _T("c")), 4); | |
79 | |
80 // Many strings. | |
81 const TCHAR s6[] = _T("a\0bcd\0efgh\0") EOS; | |
82 EXPECT_EQ(FindFirstInMultiString(s6, arraysize(s6), _T("efgh")), 6); | |
83 | |
84 // Many strings including empty string. | |
85 const TCHAR s7[] = _T("a\0\0bc\0\0de\0fg") EOS; | |
86 EXPECT_EQ(FindFirstInMultiString(s7, arraysize(s7), _T("fg")), 10); | |
87 | |
88 // Many strings, empty string at the end, negative test. | |
89 const TCHAR s8[] = _T("a\0bcd\0efgh\0\0") EOS; | |
90 EXPECT_EQ(FindFirstInMultiString(s8, arraysize(s8), _T("foo")), -1); | |
91 | |
92 // Another negative test. | |
93 const TCHAR s9[] = _T("a\0bcd\0\0\0efgh\0") EOS; | |
94 EXPECT_EQ(FindFirstInMultiString(s9, arraysize(s9), _T("foo")), -1); | |
95 | |
96 // Empty string is always found. | |
97 const TCHAR s10[] = _T("\0") EOS; | |
98 EXPECT_EQ(FindFirstInMultiString(s10, arraysize(s10), _T("\0")), 0); | |
99 | |
100 const TCHAR s11[] = _T("\0") EOS; | |
101 EXPECT_EQ(FindFirstInMultiString(s11, arraysize(s11), _T("a")), -1); | |
102 } | |
103 | |
104 TEST_F(HistoryTest, GetHistory) { | |
105 EXPECT_TRUE(GetHistory().IsEmpty()); | |
106 | |
107 const TCHAR msg1[] = _T("Hello"); | |
108 AppendToHistory(msg1); | |
109 EXPECT_STREQ(msg1, GetHistory()); | |
110 EXPECT_TRUE(GetHistory().IsEmpty()); | |
111 } | |
112 | |
113 TEST_F(HistoryTest, AppendToHistoryTest) { | |
114 // Test one character. | |
115 const TCHAR msg1[] = _T("A"); | |
116 AppendToHistory(msg1); | |
117 EXPECT_STREQ(msg1, GetHistory()); | |
118 | |
119 // Test small string. | |
120 const TCHAR msg2[] = _T("ABCD"); | |
121 AppendToHistory(msg2); | |
122 EXPECT_STREQ(msg2, GetHistory()); | |
123 | |
124 // Test one string that fills the buffer. | |
125 TCHAR msg3[kMaxHistoryBufferSize + 1] = {0}; | |
126 for (int i = 0; i <= kMaxHistoryBufferSize; ++i) { | |
127 msg3[i] = _T('A'); | |
128 } | |
129 msg3[kMaxHistoryBufferSize] = _T('\0'); | |
130 AppendToHistory(msg3); | |
131 EXPECT_STREQ(msg3, GetHistory()); | |
132 | |
133 // Test set of strings that exactly fill buffer. | |
134 const int test_buffer_size = 64; | |
135 TCHAR msg4[test_buffer_size + 1] = {0}; | |
136 for (int i = 0; i <= test_buffer_size; ++i) { | |
137 msg4[i] = _T('A'); | |
138 } | |
139 msg4[test_buffer_size] = _T('\0'); | |
140 | |
141 int num_times_to_append = kMaxHistoryBufferSize / test_buffer_size; | |
142 EXPECT_EQ(kMaxHistoryBufferSize, num_times_to_append * test_buffer_size); | |
143 for (int i = 0; i < num_times_to_append; ++i) { | |
144 AppendToHistory(msg4); | |
145 } | |
146 EXPECT_STREQ(msg3, GetHistory()); | |
147 } | |
148 | |
149 TEST_F(HistoryTest, AppendToHistoryTest_WrapAround) { | |
150 // Test string that wraps around the buffer. | |
151 // First fill kMaxHistoryBufferSize - 1 with one string, then use | |
152 // another string of length 2("XX"), and another string to length of length 3. | |
153 // "XFFFGGGGG....GGGX" should be the result. The returned string should | |
154 // be in correct FIFO order i.e. "GGGGG......XXFFF". | |
155 const TCHAR msg6[] = _T("XX"); | |
156 const TCHAR msg7[] = _T("FFF"); | |
157 const int test_buffer_size = kMaxHistoryBufferSize - 1; | |
158 TCHAR msg5[test_buffer_size + 1] = {0}; | |
159 TCHAR expected_buffer[kMaxHistoryBufferSize + 1] = {0}; | |
160 for (int i = 0; i <= test_buffer_size; ++i) { | |
161 msg5[i] = _T('G'); | |
162 } | |
163 msg5[test_buffer_size] = _T('\0'); | |
164 | |
165 // Call test method. | |
166 AppendToHistory(msg5); | |
167 AppendToHistory(msg6); | |
168 AppendToHistory(msg7); | |
169 | |
170 // Create the expected string. | |
171 for (int i = 0; i <= kMaxHistoryBufferSize; ++i) { | |
172 expected_buffer[i] = _T('G'); | |
173 } | |
174 int msg6len = wcslen(msg6); | |
175 int msg7len = wcslen(msg7); | |
176 memcpy(expected_buffer + kMaxHistoryBufferSize - msg6len - msg7len, | |
177 msg6, | |
178 msg6len * sizeof(TCHAR)); | |
179 memcpy(expected_buffer + kMaxHistoryBufferSize - msg7len, | |
180 msg7, | |
181 msg7len * sizeof(TCHAR)); | |
182 expected_buffer[kMaxHistoryBufferSize] = _T('\0'); | |
183 EXPECT_STREQ(expected_buffer, GetHistory()); | |
184 } | |
185 | |
186 TEST_F(HistoryTest, AppendToHistoryTest_AnotherWrapAroundTest) { | |
187 // Test string that wraps around the buffer. | |
188 // First fill the kMaxHistoryBufferSize - 1 with one string, then fill it with | |
189 // another string of same length. | |
190 const int test_buffer_size = kMaxHistoryBufferSize - 1; | |
191 TCHAR msg2[test_buffer_size + 1] = {0}; | |
192 TCHAR msg1[test_buffer_size + 1] = {0}; | |
193 TCHAR expected_buffer[kMaxHistoryBufferSize + 1] = {0}; | |
194 for (int i = 0; i <= test_buffer_size; ++i) { | |
195 msg1[i] = _T('G'); | |
196 msg2[i] = _T('J'); | |
197 } | |
198 msg2[test_buffer_size] = _T('\0'); | |
199 msg1[test_buffer_size] = _T('\0'); | |
200 | |
201 // Call test method. | |
202 AppendToHistory(msg1); | |
203 AppendToHistory(msg2); | |
204 | |
205 // Create the expected string. | |
206 for (int i = 0; i <= kMaxHistoryBufferSize; ++i) { | |
207 expected_buffer[i] = _T('J'); | |
208 } | |
209 expected_buffer[0] = _T('G'); | |
210 expected_buffer[kMaxHistoryBufferSize] = _T('\0'); | |
211 EXPECT_STREQ(expected_buffer, GetHistory()); | |
212 } | |
213 | |
214 TEST_F(HistoryTest, AppendToHistoryTest_LotsOfLogs) { | |
215 // Run over a number of Append calls, with strings length | |
216 // (kMaxHistoryBufferSize / 2) + 1, causing wrap on every run. | |
217 TCHAR expected_buffer[kMaxHistoryBufferSize + 1] = {0}; | |
218 const int test_buffer_size = (kMaxHistoryBufferSize / 2) + 1; | |
219 TCHAR msg1[test_buffer_size + 1] = {0}; | |
220 for (int test_char = 'A'; test_char <= 'Z'; ++test_char) { | |
221 for (int i = 0; i <= test_buffer_size; ++i) { | |
222 msg1[i] = static_cast<TCHAR>(test_char); | |
223 } | |
224 msg1[test_buffer_size] = _T('\0'); | |
225 | |
226 // Call test method. | |
227 AppendToHistory(msg1); | |
228 } | |
229 | |
230 // Create the expected string. | |
231 int i = 0; | |
232 for (; i < test_buffer_size - 2; ++i) { | |
233 expected_buffer[i] = _T('Y'); | |
234 } | |
235 for (; i <= kMaxHistoryBufferSize; ++i) { | |
236 expected_buffer[i] = _T('Z'); | |
237 } | |
238 expected_buffer[kMaxHistoryBufferSize] = _T('\0'); | |
239 EXPECT_STREQ(expected_buffer, GetHistory()); | |
240 } | |
241 | |
242 TEST_F(HistoryTest, AppendToHistoryTest_LargeBuffer) { | |
243 // Test with a message that is larger than the buffer. | |
244 const int test_buffer_size = kMaxHistoryBufferSize + 10; | |
245 TCHAR msg4[test_buffer_size + 1] = {0}; | |
246 TCHAR expected_buffer[kMaxHistoryBufferSize + 1] = {0}; | |
247 for (int i = 0; i < test_buffer_size; ++i) { | |
248 msg4[i] = _T('A'); | |
249 } | |
250 msg4[test_buffer_size] = _T('\0'); | |
251 | |
252 for (int i = 0; i <= kMaxHistoryBufferSize; ++i) { | |
253 expected_buffer[i] = _T('A'); | |
254 } | |
255 expected_buffer[kMaxHistoryBufferSize] = _T('\0'); | |
256 | |
257 AppendToHistory(msg4); | |
258 EXPECT_STREQ(expected_buffer, GetHistory()); | |
259 } | |
260 | |
261 TEST_F(HistoryTest, AppendToHistoryTest_EmptyBuffer) { | |
262 CString test_string; | |
263 AppendToHistory(test_string); | |
264 EXPECT_TRUE(GetHistory().IsEmpty()); | |
265 } | |
266 | |
267 } // namespace omaha | |
268 | |
OLD | NEW |