summaryrefslogtreecommitdiffstats
path: root/chrome/browser/utility_process_host_unittest.cc
blob: ffb99a7acdd0bb5065b7de13d04a61dc9276dbb2 (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
// Copyright (c) 2009 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 "chrome/browser/utility_process_host.h"

#include "base/file_path.h"
#include "base/file_util.h"
#include "base/logging.h"
#include "base/message_loop.h"
#include "base/path_service.h"
#include "base/string_util.h"
#include "build/build_config.h"
#include "chrome/browser/renderer_host/resource_dispatcher_host.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/notification_registrar.h"
#include "chrome/common/notification_service.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

class UtilityProcessHostTest : public testing::Test {
 public:
  UtilityProcessHostTest() {
  }

 protected:
  MessageLoopForIO message_loop_;
};

class TestUtilityProcessHostClient : public UtilityProcessHost::Client {
 public:
  explicit TestUtilityProcessHostClient(MessageLoop* message_loop)
      : message_loop_(message_loop), success_(false) {
  }

  // UtilityProcessHost::Client methods.
  virtual void OnProcessCrashed() {
    NOTREACHED();
  }

  virtual void OnUnpackExtensionSucceeded(const DictionaryValue& manifest) {
    success_ = true;
  }

  virtual void OnUnpackExtensionFailed(const std::string& error_message) {
    NOTREACHED();
  }

  virtual void OnUnpackWebResourceSucceeded(
      const ListValue& json_data) {
    NOTREACHED();
  }

  virtual void OnUnpackWebResourceFailed(
      const std::string& error_message) {
    NOTREACHED();
  }

  bool success() {
    return success_;
  }

 private:
  MessageLoop* message_loop_;
  bool success_;
};

class TestUtilityProcessHost : public UtilityProcessHost {
 public:
  TestUtilityProcessHost(TestUtilityProcessHostClient* client,
                         MessageLoop* loop_io,
                         ResourceDispatcherHost* rdh)
      : UtilityProcessHost(rdh, client, loop_io) {
  }

 protected:
  virtual FilePath GetUtilityProcessCmd() {
    FilePath exe_path;
    PathService::Get(base::DIR_EXE, &exe_path);
    exe_path = exe_path.AppendASCII(WideToASCII(
        chrome::kHelperProcessExecutablePath));
    return exe_path;
  }

  virtual bool UseSandbox() {
    return false;
  }

 private:
};

class ProcessClosedObserver : public NotificationObserver {
 public:
  explicit ProcessClosedObserver(MessageLoop* message_loop)
      : message_loop_(message_loop) {
    registrar_.Add(this, NotificationType::CHILD_PROCESS_HOST_DISCONNECTED,
                   NotificationService::AllSources());
  }

  void RunUntilClose(int child_id) {
    child_id_ = child_id;
    observed_ = false;
    message_loop_->Run();
    DCHECK(observed_);
  }

 private:
  virtual void Observe(NotificationType type,
                       const NotificationSource& source,
                       const NotificationDetails& details) {
    DCHECK(type == NotificationType::CHILD_PROCESS_HOST_DISCONNECTED);
    ChildProcessInfo* info = Details<ChildProcessInfo>(details).ptr();
    if (info->id() == child_id_) {
      observed_ = true;
      message_loop_->Quit();
    }
  }

  MessageLoop* message_loop_;
  NotificationRegistrar registrar_;
  int child_id_;
  bool observed_;
};

#if !defined(OS_POSIX)
// We should not run this on linux (crbug.com/22703) or MacOS (crbug.com/8102)
// until problems related to autoupdate are fixed.
TEST_F(UtilityProcessHostTest, ExtensionUnpacker) {
  // Copy the test extension into a temp dir and install from the temp dir.
  FilePath extension_file;
  ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extension_file));
  extension_file = extension_file.AppendASCII("extensions")
                                 .AppendASCII("theme.crx");
  FilePath temp_extension_dir;
  ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &temp_extension_dir));
  temp_extension_dir = temp_extension_dir.AppendASCII("extension_test");
  ASSERT_TRUE(file_util::CreateDirectory(temp_extension_dir));
  ASSERT_TRUE(file_util::CopyFile(extension_file,
                                  temp_extension_dir.AppendASCII("theme.crx")));

  scoped_refptr<TestUtilityProcessHostClient> client(
      new TestUtilityProcessHostClient(&message_loop_));
  ResourceDispatcherHost rdh(NULL);
  TestUtilityProcessHost* process_host =
      new TestUtilityProcessHost(client.get(), &message_loop_, &rdh);
  // process_host will delete itself when it's done.
  ProcessClosedObserver observer(&message_loop_);
  process_host->StartExtensionUnpacker(
      temp_extension_dir.AppendASCII("theme.crx"));
  observer.RunUntilClose(process_host->id());
  EXPECT_TRUE(client->success());

  // Clean up the temp dir.
  file_util::Delete(temp_extension_dir, true);
}
#endif

}  // namespace