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