summaryrefslogtreecommitdiffstats
path: root/chrome/test/mini_installer_test/mini_installer_test_util.cc
blob: 904f556863239b879746d19c55ea7edf08992b45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <algorithm>

#include "chrome/test/mini_installer_test/mini_installer_test_util.h"

#include "base/command_line.h"
#include "base/file_path.h"
#include "base/file_util.h"
#include "base/path_service.h"
#include "base/process_util.h"
#include "base/stringprintf.h"
#include "base/string_split.h"
#include "base/string_util.h"
#include "base/test/test_timeouts.h"
#include "base/threading/platform_thread.h"
#include "base/time.h"
#include "base/utf_string_conversions.h"
#include "chrome/installer/util/logging_installer.h"
#include "chrome/test/mini_installer_test/mini_installer_test_constants.h"
#include "testing/gtest/include/gtest/gtest.h"

// Change current directory so that chrome.dll from current folder
// will not be used as fall back.
bool MiniInstallerTestUtil::ChangeCurrentDirectory(FilePath* current_path) {
  FilePath backup_path;
  if (!file_util::GetCurrentDirectory(&backup_path))
    return false;

  if (!file_util::SetCurrentDirectory(backup_path.DirName()))
    return false;
  *current_path = backup_path;
  return true;
}

// Checks for all requested running processes and kills them.
void MiniInstallerTestUtil::CloseProcesses(
    const std::wstring& executable_name) {
  int timer = 0;
  while ((base::GetProcessCount(executable_name, NULL) > 0) &&
         (timer < 20000)) {
    base::KillProcesses(executable_name, 1, NULL);
    base::PlatformThread::Sleep(200);
    timer = timer + 200;
  }
  ASSERT_EQ(0, base::GetProcessCount(executable_name, NULL));
}

bool MiniInstallerTestUtil::CloseWindow(const wchar_t* window_name,
                                        UINT message) {
  int timer = 0;
  bool return_val = false;
  HWND hndl = FindWindow(NULL, window_name);
  while (hndl == NULL && (timer < 60000)) {
    hndl = FindWindow(NULL, window_name);
    base::PlatformThread::Sleep(200);
    timer = timer + 200;
  }
  if (hndl != NULL) {
    LRESULT _result = SendMessage(hndl, message, 1, 0);
    return_val = true;
  }
  return return_val;
}

bool IsNewer(const FileInfo& file_rbegin, const FileInfo& file_rend) {
  return (file_rbegin.creation_time_ > file_rend.creation_time_);
}

bool MiniInstallerTestUtil::GetCommandForTagging(
    std::wstring *return_command) {
  std::wstring tagged_installer_path = MiniInstallerTestUtil::GetFilePath(
      mini_installer_constants::kStandaloneInstaller);
  FilePath standalone_installer_path;
  if (!MiniInstallerTestUtil::GetStandaloneInstallerPath(
          &standalone_installer_path)) {
    return false;
  }

  *return_command = base::StringPrintf(L"%ls %ls %ls %ls",
      mini_installer_constants::kChromeApplyTagExe,
      standalone_installer_path.value().c_str(),
      tagged_installer_path.c_str(),
      mini_installer_constants::kChromeApplyTagParameters);
  VLOG(1) << "Command to run Apply tag: " << *return_command;
  return true;
}

std::wstring MiniInstallerTestUtil::GetFilePath(const wchar_t* exe_name) {
  FilePath installer_path;
  PathService::Get(base::DIR_EXE, &installer_path);
  installer_path = installer_path.Append(exe_name);
  return installer_path.value();
}

// This method will first call GetLatestFile to get the list of all
// builds, sorted on creation time. Then goes through each build folder
// until it finds the installer file that matches the pattern argument.
bool MiniInstallerTestUtil::GetInstaller(const wchar_t* pattern,
    std::wstring *path, const wchar_t* channel_type, bool chrome_frame) {
  FileInfoList builds_list;
  FileInfoList exe_list;
  std::wstring chrome_diff_installer(
      mini_installer_constants::kChromeDiffInstallerLocation);

  chrome_diff_installer.append(L"*");
  if (!GetLatestFile(chrome_diff_installer.c_str(),
                     channel_type, &builds_list))
    return false;

  FileInfoList::const_reverse_iterator builds_list_size = builds_list.rbegin();
  while (builds_list_size != builds_list.rend()) {
    path->assign(mini_installer_constants::kChromeDiffInstallerLocation);
    file_util::AppendToPath(path, builds_list_size->name_);
    file_util::AppendToPath(path, mini_installer_constants::kWinFolder);
    std::wstring installer_path(path->c_str());
    file_util::AppendToPath(&installer_path, L"*.exe");
    if (!GetLatestFile(installer_path.c_str(), pattern, &exe_list)) {
      ++builds_list_size;
    } else {
      file_util::AppendToPath(path, exe_list.at(0).name_.c_str());
      if (!file_util::PathExists(FilePath::FromWStringHack(*path))) {
        ++builds_list_size;
      } else {
        break;
      }
    }
  }
  return file_util::PathExists(FilePath::FromWStringHack(*path));
}

// This method will get the latest installer filename from the directory.
bool MiniInstallerTestUtil::GetLatestFile(const wchar_t* file_name,
    const wchar_t* pattern, FileInfoList *file_details) {

  WIN32_FIND_DATA find_file_data;
  HANDLE file_handle = FindFirstFile(file_name, &find_file_data);
  if (file_handle == INVALID_HANDLE_VALUE) {
    VLOG(1) << "Handle is invalid.";
    return false;
  }
  BOOL ret = TRUE;
  bool return_val = false;
  while (ret) {
    std::wstring search_path = find_file_data.cFileName;
    size_t position_found = search_path.find(pattern);
    if (position_found != -1) {
      std::wstring extension = FilePath(file_name).Extension();
      if ((base::strcasecmp(WideToUTF8(extension).c_str(), ".exe")) == 0) {
        file_details->push_back(FileInfo(find_file_data.cFileName, 0));
        return_val = true;
        break;
      } else {
        FILETIME file_time = find_file_data.ftCreationTime;
        base::Time creation_time = base::Time::FromFileTime(file_time);
        file_details->push_back(FileInfo(find_file_data.cFileName,
                                static_cast<int>(creation_time.ToDoubleT())));
        return_val = true;
      }
    }
    ret = FindNextFile(file_handle, &find_file_data);
  }
  std::sort(file_details->rbegin(), file_details->rend(), &IsNewer);
  FindClose(file_handle);
  return return_val;
}

// This method retrieves the previous build version for the given diff
// installer path.
bool MiniInstallerTestUtil::GetPreviousBuildNumber(const std::wstring& path,
                                                   std::wstring *build_number) {

  std::wstring diff_name = FilePath(path).BaseName().value();
  // We want to remove 'from_', so add its length to found index (which is 5)
  std::wstring::size_type start_position = diff_name.find(L"from_") + 5;
  std::wstring::size_type end_position = diff_name.find(L"_c");
  std::wstring::size_type size = end_position - start_position;

  std::wstring build_no = diff_name.substr(start_position, size);

  // Search for a build folder with this build suffix.
  std::wstring pattern = L"*" + build_no;

  file_util::FileEnumerator files(FilePath(
      mini_installer_constants::kChromeDiffInstallerLocation),
      false, file_util::FileEnumerator::DIRECTORIES, pattern);
  FilePath folder = files.Next();
  if (folder.empty())
    return false;

  build_number->assign(folder.BaseName().value());
  return true;
}


// This method will get the previous full installer path
// from given diff installer path. It will first get the
// filename from the diff installer path, gets the previous
// build information from the filename, then computes the
// path for previous full installer.
bool MiniInstallerTestUtil::GetPreviousFullInstaller(
    const std::wstring& diff_path, std::wstring *previous, bool chrome_frame) {
  std::wstring build_no;

  if (!GetPreviousBuildNumber(diff_path, &build_no))
    return false;

  std::vector<std::wstring> build_version;
  base::SplitString(build_no, '.', &build_version);
  if (build_version.size() < 4)
    return false;

  std::wstring name = build_version[2] + L"." + build_version[3] +
      mini_installer_constants::kFullInstallerPattern + L".exe";

  // Create the full installer path.
  FilePath installer = FilePath(
      mini_installer_constants::kChromeDiffInstallerLocation);
  installer =
     installer.Append(build_no)
         .Append(mini_installer_constants::kWinFolder).Append(name);
  previous->assign(installer.value());

  return file_util::PathExists(installer);
}

bool MiniInstallerTestUtil::GetStandaloneInstallerPath(FilePath* path) {
  const CommandLine* cmd = CommandLine::ForCurrentProcess();
  std::wstring build =
      cmd->GetSwitchValueNative(switches::kInstallerTestBuild);
  std::wstring standalone_installer(
      mini_installer_constants::kChromeStandAloneInstallerLocation);
  standalone_installer.append(L"\\" + build + L"\\win\\");

  // Get the file name.
  std::vector<std::wstring> tokenizedBuildNumber;
  Tokenize(build, L".", &tokenizedBuildNumber);
  std::wstring standalone_installer_filename = base::StringPrintf(
      L"%ls%ls_%ls.exe", mini_installer_constants::kUntaggedInstallerPattern,
          tokenizedBuildNumber[2].c_str(), tokenizedBuildNumber[3].c_str());
  standalone_installer.append(standalone_installer_filename);

  FilePath filename(standalone_installer);
  if (!file_util::PathExists(filename))
    return false;
  *path = filename;
  return true;
}

bool MiniInstallerTestUtil::GetStandaloneVersion(
    std::wstring* version) {
  const CommandLine* cmd = CommandLine::ForCurrentProcess();
  std::wstring build =
      cmd->GetSwitchValueNative(switches::kInstallerTestBuild);
  if (build.empty())
    return false;
  *version = build;
  VLOG(1) << "Standalone installer version: " << *version;
  return true;
}

void MiniInstallerTestUtil::SendEnterKeyToWindow() {
  INPUT key;
  key.type = INPUT_KEYBOARD;
  key.ki.wVk = VK_RETURN;
  key.ki.dwFlags = 0;
  key.ki.time = 0;
  key.ki.wScan = 0;
  key.ki.dwExtraInfo = 0;
  SendInput(1, &key, sizeof(INPUT));
  key.ki.dwExtraInfo = KEYEVENTF_KEYUP;
  SendInput(1, &key, sizeof(INPUT));
}


void MiniInstallerTestUtil::VerifyProcessLaunch(
    const wchar_t* process_name, bool expected_status) {
  int timer = 0, wait_time = 60000;
  if (!expected_status)
    wait_time = 8000;

  while ((base::GetProcessCount(process_name, NULL) == 0) &&
         (timer < wait_time)) {
    base::PlatformThread::Sleep(200);
    timer = timer + 200;
  }

  if (expected_status)
    ASSERT_NE(0, base::GetProcessCount(process_name, NULL));
  else
    ASSERT_EQ(0, base::GetProcessCount(process_name, NULL));
}

bool MiniInstallerTestUtil::VerifyProcessClose(
    const wchar_t* process_name) {
  int timer = 0;
  if (base::GetProcessCount(process_name, NULL) > 0) {
    VLOG(1) << "Waiting for this process to end: " << process_name;
    while ((base::GetProcessCount(process_name, NULL) > 0) &&
           (timer < TestTimeouts::large_test_timeout_ms())) {
      base::PlatformThread::Sleep(200);
      timer = timer + 200;
    }
  } else {
    if (base::GetProcessCount(process_name, NULL) != 0)
      return false;
  }
  return true;
}

bool MiniInstallerTestUtil::VerifyProcessHandleClosed(
    base::ProcessHandle handle) {
  DWORD result = WaitForSingleObject(handle,
      TestTimeouts::large_test_timeout_ms());
  return result == WAIT_OBJECT_0;
}