1c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Copyright (c) 2013 The Chromium Authors. All rights reserved.
2c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// found in the LICENSE file.
4c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
5c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include <vector>
6c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
7c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "base/command_line.h"
8c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "base/files/file_path.h"
9c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "base/path_service.h"
10c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/browser/extensions/startup_helper.h"
11c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/common/chrome_paths.h"
12c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/common/chrome_switches.h"
13c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/test/base/in_process_browser_test.h"
14c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
15c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class StartupHelperBrowserTest : public InProcessBrowserTest {
16c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) public:
17c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  StartupHelperBrowserTest() {}
18c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  virtual ~StartupHelperBrowserTest() {}
19c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
20c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
21c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    command_line->AppendSwitch(switches::kNoStartupWindow);
22c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_);
23c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    test_data_dir_ = test_data_dir_.AppendASCII("extensions");
24c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
25c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
26c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) protected:
27c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  base::FilePath test_data_dir_;
28c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)};
29c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
30c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)IN_PROC_BROWSER_TEST_F(StartupHelperBrowserTest, ValidateCrx) {
31c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // A list of crx file paths along with an expected result of valid (true) or
32c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // invalid (false).
33c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  std::vector<std::pair<base::FilePath, bool> > expectations;
34c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  expectations.push_back(
35c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      std::make_pair(test_data_dir_.AppendASCII("good.crx"), true));
36c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  expectations.push_back(
37c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      std::make_pair(test_data_dir_.AppendASCII("good2.crx"), true));
38c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  expectations.push_back(
393551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      std::make_pair(test_data_dir_.AppendASCII("bad_underscore.crx"), true));
40c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  expectations.push_back(
413551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      std::make_pair(test_data_dir_.AppendASCII("bad_magic.crx"), false));
42c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
43c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  for (std::vector<std::pair<base::FilePath, bool> >::iterator i =
44c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)           expectations.begin();
45c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)       i != expectations.end(); ++i) {
46c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    CommandLine command_line(CommandLine::NO_PROGRAM);
47c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const base::FilePath& path = i->first;
48c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    command_line.AppendSwitchPath(switches::kValidateCrx, path);
49c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
50c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    std::string error;
51c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    extensions::StartupHelper helper;
52c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    bool result = helper.ValidateCrx(command_line, &error);
53c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (i->second) {
54c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_TRUE(result) << path.LossyDisplayName()
55c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                          << " expected to be valid but wasn't";
56c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    } else {
57c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_FALSE(result) << path.LossyDisplayName()
58c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                           << " expected to be invalid but wasn't";
59c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_FALSE(error.empty()) << "Error message wasn't set for "
60c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                  << path.LossyDisplayName();
61c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
62c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
63c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
64