| 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 extractor and the ApplyTag class. | |
| 17 // | |
| 18 // TODO(omaha): eliminate the dependency on the hardcoded "GoogleUpdate.exe" | |
| 19 // program name. | |
| 20 | |
| 21 #include <shlobj.h> | |
| 22 #include "base/scoped_ptr.h" | |
| 23 #include "omaha/base/apply_tag.h" | |
| 24 #include "omaha/base/app_util.h" | |
| 25 #include "omaha/base/extractor.h" | |
| 26 #include "omaha/base/scope_guard.h" | |
| 27 #include "omaha/base/utils.h" | |
| 28 #include "omaha/testing/unit_test.h" | |
| 29 | |
| 30 namespace omaha { | |
| 31 | |
| 32 const TCHAR kFilePath[] = _T("."); | |
| 33 const TCHAR kFileName[] = _T("GoogleUpdate.exe"); | |
| 34 const char kTagString[] = "1234567890abcdefg"; | |
| 35 const char kAppendTagString[] = "..AppendedStr"; | |
| 36 | |
| 37 TEST(ExtractorTest, EmbedExtract) { | |
| 38 // Test the extractor. | |
| 39 TagExtractor extractor; | |
| 40 ASSERT_FALSE(extractor.IsFileOpen()); | |
| 41 | |
| 42 CString signed_exe_file; | |
| 43 signed_exe_file.Format(_T("%s\\%s\\%s"), | |
| 44 app_util::GetCurrentModuleDirectory(), | |
| 45 kFilePath, kFileName); | |
| 46 ASSERT_TRUE(extractor.OpenFile(signed_exe_file)); | |
| 47 | |
| 48 // No tag string in the original exe file. | |
| 49 int tag_buffer_size = 0; | |
| 50 ASSERT_FALSE(extractor.ExtractTag(NULL, &tag_buffer_size)); | |
| 51 ASSERT_EQ(tag_buffer_size, 0); | |
| 52 extractor.CloseFile(); | |
| 53 | |
| 54 // Create a temp dir. | |
| 55 TCHAR temp_path[MAX_PATH] = {0}; | |
| 56 *temp_path = 0; | |
| 57 ASSERT_NE(::GetTempPath(MAX_PATH, temp_path), 0); | |
| 58 | |
| 59 // Embed the tag string. | |
| 60 CString tagged_file; | |
| 61 tagged_file.Format(_T("%s%s"), temp_path, kFileName); | |
| 62 omaha::ApplyTag tag; | |
| 63 ASSERT_HRESULT_SUCCEEDED(tag.Init(signed_exe_file, | |
| 64 kTagString, | |
| 65 strlen(kTagString), | |
| 66 tagged_file, | |
| 67 false)); | |
| 68 ASSERT_SUCCEEDED(tag.EmbedTagString()); | |
| 69 ON_SCOPE_EXIT(::DeleteFile, tagged_file); | |
| 70 | |
| 71 // Extract the tag string. | |
| 72 tag_buffer_size = 0; | |
| 73 ASSERT_TRUE(extractor.OpenFile(tagged_file)); | |
| 74 ASSERT_TRUE(extractor.ExtractTag(NULL, &tag_buffer_size)); | |
| 75 ASSERT_EQ(tag_buffer_size, arraysize(kTagString)); | |
| 76 | |
| 77 char tag_buffer[arraysize(kTagString)] = {0}; | |
| 78 ASSERT_TRUE(extractor.ExtractTag(tag_buffer, &tag_buffer_size)); | |
| 79 ASSERT_EQ(tag_buffer_size, arraysize(kTagString)); | |
| 80 ASSERT_EQ(memcmp(tag_buffer, kTagString, arraysize(kTagString)), 0); | |
| 81 extractor.CloseFile(); | |
| 82 } | |
| 83 | |
| 84 TEST(ExtractorTest, EmbedAppendExtract) { | |
| 85 // Test the extractor. | |
| 86 TagExtractor extractor; | |
| 87 ASSERT_FALSE(extractor.IsFileOpen()); | |
| 88 | |
| 89 CString signed_exe_file; | |
| 90 signed_exe_file.Format(_T("%s\\%s\\%s"), | |
| 91 app_util::GetCurrentModuleDirectory(), | |
| 92 kFilePath, kFileName); | |
| 93 ASSERT_TRUE(extractor.OpenFile(signed_exe_file)); | |
| 94 | |
| 95 // No tag string in the original exe file. | |
| 96 int tag_buffer_size = 0; | |
| 97 ASSERT_FALSE(extractor.ExtractTag(NULL, &tag_buffer_size)); | |
| 98 ASSERT_GT(extractor.cert_length(), 0); | |
| 99 ASSERT_EQ(tag_buffer_size, 0); | |
| 100 extractor.CloseFile(); | |
| 101 | |
| 102 // Create a temp dir. | |
| 103 TCHAR temp_path[MAX_PATH] = {0}; | |
| 104 *temp_path = 0; | |
| 105 ASSERT_NE(::GetTempPath(MAX_PATH, temp_path), 0); | |
| 106 | |
| 107 // Embed the tag string. | |
| 108 CString tagged_file; | |
| 109 tagged_file.Format(_T("%s%d%s"), temp_path, 1, kFileName); | |
| 110 omaha::ApplyTag tag; | |
| 111 ASSERT_HRESULT_SUCCEEDED(tag.Init(signed_exe_file, | |
| 112 kTagString, | |
| 113 strlen(kTagString), | |
| 114 tagged_file, | |
| 115 false)); | |
| 116 ASSERT_SUCCEEDED(tag.EmbedTagString()); | |
| 117 ON_SCOPE_EXIT(::DeleteFile, tagged_file); | |
| 118 | |
| 119 // Append another tag string. | |
| 120 CString tagged_appended_file; | |
| 121 tagged_appended_file.Format(_T("%s%d%s"), temp_path, 2, kFileName); | |
| 122 omaha::ApplyTag tag1; | |
| 123 | |
| 124 ASSERT_HRESULT_SUCCEEDED(tag1.Init(tagged_file, | |
| 125 kAppendTagString, | |
| 126 strlen(kAppendTagString), | |
| 127 tagged_appended_file, | |
| 128 true)); | |
| 129 ASSERT_SUCCEEDED(tag1.EmbedTagString()); | |
| 130 ON_SCOPE_EXIT(::DeleteFile, tagged_appended_file); | |
| 131 | |
| 132 // Append another tag string. | |
| 133 CString tagged_appended_file2; | |
| 134 tagged_appended_file2.Format(_T("%s%d%s"), temp_path, 3, kFileName); | |
| 135 omaha::ApplyTag tag2; | |
| 136 ASSERT_HRESULT_SUCCEEDED(tag2.Init(tagged_appended_file, | |
| 137 kAppendTagString, | |
| 138 strlen(kAppendTagString), | |
| 139 tagged_appended_file2, | |
| 140 true)); | |
| 141 ASSERT_SUCCEEDED(tag2.EmbedTagString()); | |
| 142 ON_SCOPE_EXIT(::DeleteFile, tagged_appended_file2); | |
| 143 | |
| 144 // Extract the tag string. | |
| 145 tag_buffer_size = 0; | |
| 146 CStringA expected_tag_string(kTagString); | |
| 147 expected_tag_string += kAppendTagString; | |
| 148 expected_tag_string += kAppendTagString; | |
| 149 int expected_tag_string_len = expected_tag_string.GetLength() + 1; | |
| 150 ASSERT_TRUE(extractor.OpenFile(tagged_appended_file2)); | |
| 151 ASSERT_TRUE(extractor.ExtractTag(NULL, &tag_buffer_size)); | |
| 152 ASSERT_EQ(tag_buffer_size, expected_tag_string_len); | |
| 153 | |
| 154 scoped_array<char> tag_buffer(new char[expected_tag_string_len]); | |
| 155 ASSERT_TRUE(extractor.ExtractTag(tag_buffer.get(), &tag_buffer_size)); | |
| 156 ASSERT_EQ(tag_buffer_size, expected_tag_string_len); | |
| 157 ASSERT_EQ(memcmp(tag_buffer.get(), | |
| 158 expected_tag_string, | |
| 159 expected_tag_string_len), | |
| 160 0); | |
| 161 extractor.CloseFile(); | |
| 162 } | |
| 163 | |
| 164 TEST(ExtractorTest, AlreadyTaggedError) { | |
| 165 // Test the extractor. | |
| 166 TagExtractor extractor; | |
| 167 ASSERT_FALSE(extractor.IsFileOpen()); | |
| 168 | |
| 169 CString signed_exe_file; | |
| 170 signed_exe_file.Format(_T("%s\\%s\\%s"), | |
| 171 app_util::GetCurrentModuleDirectory(), | |
| 172 kFilePath, kFileName); | |
| 173 ASSERT_TRUE(extractor.OpenFile(signed_exe_file)); | |
| 174 | |
| 175 // No tag string in the original exe file. | |
| 176 int tag_buffer_size = 0; | |
| 177 ASSERT_FALSE(extractor.ExtractTag(NULL, &tag_buffer_size)); | |
| 178 ASSERT_GT(extractor.cert_length(), 0); | |
| 179 ASSERT_EQ(tag_buffer_size, 0); | |
| 180 extractor.CloseFile(); | |
| 181 | |
| 182 // Create a temp dir. | |
| 183 TCHAR temp_path[MAX_PATH] = {0}; | |
| 184 *temp_path = 0; | |
| 185 ASSERT_NE(::GetTempPath(MAX_PATH, temp_path), 0); | |
| 186 | |
| 187 // Embed the tag string. | |
| 188 CString tagged_file; | |
| 189 tagged_file.Format(_T("%s%d%s"), temp_path, 1, kFileName); | |
| 190 omaha::ApplyTag tag1; | |
| 191 ASSERT_HRESULT_SUCCEEDED(tag1.Init(signed_exe_file, | |
| 192 kTagString, | |
| 193 strlen(kTagString), | |
| 194 tagged_file, | |
| 195 false)); | |
| 196 ASSERT_SUCCEEDED(tag1.EmbedTagString()); | |
| 197 ON_SCOPE_EXIT(::DeleteFile, tagged_file); | |
| 198 | |
| 199 CString tagged_appended_file; | |
| 200 tagged_appended_file.Format(_T("%s%d%s"), temp_path, 2, kFileName); | |
| 201 omaha::ApplyTag tag2; | |
| 202 ASSERT_HRESULT_SUCCEEDED(tag2.Init(tagged_file, | |
| 203 kAppendTagString, | |
| 204 strlen(kAppendTagString), | |
| 205 tagged_appended_file, | |
| 206 false)); | |
| 207 ASSERT_EQ(tag2.EmbedTagString(), APPLYTAG_E_ALREADY_TAGGED); | |
| 208 ON_SCOPE_EXIT(::DeleteFile, tagged_appended_file); | |
| 209 extractor.CloseFile(); | |
| 210 } | |
| 211 | |
| 212 TEST(ApplyTagTest, InvalidCharsTest) { | |
| 213 // Accepted Regex = [-%{}/\a&=._]* | |
| 214 CString signed_exe_file; | |
| 215 signed_exe_file.Format(_T("%s\\%s\\%s"), | |
| 216 app_util::GetCurrentModuleDirectory(), | |
| 217 kFilePath, kFileName); | |
| 218 CString tagged_file(_T("out.txt")); | |
| 219 | |
| 220 const char* const input_str = "abcd"; | |
| 221 omaha::ApplyTag tag1; | |
| 222 ASSERT_HRESULT_SUCCEEDED(tag1.Init(signed_exe_file, | |
| 223 input_str, | |
| 224 strlen(input_str), | |
| 225 tagged_file, | |
| 226 false)); | |
| 227 | |
| 228 const char* const input_str2 = "abcd$%#"; | |
| 229 omaha::ApplyTag tag2; | |
| 230 ASSERT_HRESULT_FAILED(tag2.Init(signed_exe_file, | |
| 231 input_str2, | |
| 232 strlen(input_str2), | |
| 233 tagged_file, | |
| 234 false)); | |
| 235 | |
| 236 const char* const input_str3 = "abcd asdf"; | |
| 237 omaha::ApplyTag tag3; | |
| 238 ASSERT_HRESULT_FAILED(tag3.Init(signed_exe_file, | |
| 239 input_str3, | |
| 240 strlen(input_str3), | |
| 241 tagged_file, | |
| 242 false)); | |
| 243 } | |
| 244 | |
| 245 } // namespace omaha | |
| 246 | |
| OLD | NEW |