1// Copyright (c) 2011 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#ifndef CHROME_BROWSER_IMPORTER_FIREFOX_IMPORTER_UNITTEST_UTILS_H_
6#define CHROME_BROWSER_IMPORTER_FIREFOX_IMPORTER_UNITTEST_UTILS_H_
7#pragma once
8
9#include "base/basictypes.h"
10#include "base/memory/scoped_ptr.h"
11#include "base/process_util.h"
12#include "chrome/browser/importer/nss_decryptor.h"
13
14class FFDecryptorServerChannelListener;
15namespace IPC {
16  class Channel;
17}  // namespace IPC
18class MessageLoopForIO;
19
20// On OS X NSSDecryptor needs to run in a separate process. To allow us to use
21// the same unit test on all platforms we use a proxy class which spawns a
22// child process to do decryption on OS X, and calls through directly
23// to NSSDecryptor on other platforms.
24// On OS X:
25// 2 IPC messages are sent for every method of NSSDecryptor, one containing the
26// input arguments sent from Server->Child and one coming back containing
27// the return argument e.g.
28//
29// -> Msg_Decryptor_Init(dll_path, db_path)
30// <- Msg_Decryptor_InitReturnCode(bool)
31class FFUnitTestDecryptorProxy {
32 public:
33  FFUnitTestDecryptorProxy();
34  virtual ~FFUnitTestDecryptorProxy();
35
36  // Initialize a decryptor, returns true if the object was
37  // constructed successfully.
38  bool Setup(const FilePath& nss_path);
39
40  // This match the parallel functions in NSSDecryptor.
41  bool DecryptorInit(const FilePath& dll_path, const FilePath& db_path);
42  string16 Decrypt(const std::string& crypt);
43
44 private:
45#if defined(OS_MACOSX)
46  // Blocks until either a timeout is reached, or until the client process
47  // responds to an IPC message.
48  // Returns true if a reply was received successfully and false if the
49  // the operation timed out.
50  bool WaitForClientResponse();
51
52  base::ProcessHandle child_process_;
53  scoped_ptr<IPC::Channel> channel_;
54  scoped_ptr<FFDecryptorServerChannelListener> listener_;
55  scoped_ptr<MessageLoopForIO> message_loop_;
56#else
57  NSSDecryptor decryptor_;
58#endif  // !OS_MACOSX
59  DISALLOW_COPY_AND_ASSIGN(FFUnitTestDecryptorProxy);
60};
61
62// On Non-OSX platforms FFUnitTestDecryptorProxy simply calls through to
63// NSSDecryptor.
64#if !defined(OS_MACOSX)
65FFUnitTestDecryptorProxy::FFUnitTestDecryptorProxy() {
66}
67
68FFUnitTestDecryptorProxy::~FFUnitTestDecryptorProxy() {
69}
70
71bool FFUnitTestDecryptorProxy::Setup(const FilePath& nss_path) {
72  return true;
73}
74
75bool FFUnitTestDecryptorProxy::DecryptorInit(const FilePath& dll_path,
76                                             const FilePath& db_path) {
77  return decryptor_.Init(dll_path, db_path);
78}
79
80string16 FFUnitTestDecryptorProxy::Decrypt(const std::string& crypt) {
81  return decryptor_.Decrypt(crypt);
82}
83#endif  // !OS_MACOSX
84
85#endif  // CHROME_BROWSER_IMPORTER_FIREFOX_IMPORTER_UNITTEST_UTILS_H_
86