• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 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 #ifndef MOJO_EDK_TEST_MULTIPROCESS_TEST_HELPER_H_
6 #define MOJO_EDK_TEST_MULTIPROCESS_TEST_HELPER_H_
7 
8 #include <string>
9 
10 #include "base/callback.h"
11 #include "base/macros.h"
12 #include "base/process/process.h"
13 #include "base/test/multiprocess_test.h"
14 #include "base/test/test_timeouts.h"
15 #include "mojo/edk/embedder/embedder.h"
16 #include "mojo/public/cpp/system/message_pipe.h"
17 #include "testing/multiprocess_func_list.h"
18 
19 namespace mojo {
20 
21 namespace edk {
22 
23 namespace test {
24 
25 class MultiprocessTestHelper {
26  public:
27   using HandlerCallback = base::Callback<void(ScopedMessagePipeHandle)>;
28 
29   enum class LaunchType {
30     // Launch the child process as a child in the mojo system.
31     CHILD,
32 
33     // Launch the child process as an unrelated peer process in the mojo system.
34     PEER,
35 
36     // Launch the child process as a child in the mojo system, using a named
37     // pipe.
38     NAMED_CHILD,
39 
40     // Launch the child process as an unrelated peer process in the mojo
41     // system, using a named pipe.
42     NAMED_PEER,
43   };
44 
45   MultiprocessTestHelper();
46   ~MultiprocessTestHelper();
47 
48   // Start a child process and run the "main" function "named" |test_child_name|
49   // declared using |MOJO_MULTIPROCESS_TEST_CHILD_MAIN()| or
50   // |MOJO_MULTIPROCESS_TEST_CHILD_TEST()| (below).
51   ScopedMessagePipeHandle StartChild(
52       const std::string& test_child_name,
53       LaunchType launch_type = LaunchType::CHILD);
54 
55   // Like |StartChild()|, but appends an extra switch (with ASCII value) to the
56   // command line. (The switch must not already be present in the default
57   // command line.)
58   ScopedMessagePipeHandle StartChildWithExtraSwitch(
59       const std::string& test_child_name,
60       const std::string& switch_string,
61       const std::string& switch_value,
62       LaunchType launch_type);
63 
set_process_error_callback(const ProcessErrorCallback & callback)64   void set_process_error_callback(const ProcessErrorCallback& callback) {
65     process_error_callback_ = callback;
66   }
67 
68   void ClosePeerConnection();
69 
70   // Wait for the child process to terminate.
71   // Returns the exit code of the child process. Note that, though it's declared
72   // to be an |int|, the exit code is subject to mangling by the OS. E.g., we
73   // usually return -1 on error in the child (e.g., if |test_child_name| was not
74   // found), but this is mangled to 255 on Linux. You should only rely on codes
75   // 0-127 being preserved, and -1 being outside the range 0-127.
76   int WaitForChildShutdown();
77 
78   // Like |WaitForChildShutdown()|, but returns true on success (exit code of 0)
79   // and false otherwise. You probably want to do something like
80   // |EXPECT_TRUE(WaitForChildTestShutdown());|.
81   bool WaitForChildTestShutdown();
82 
test_child()83   const base::Process& test_child() const { return test_child_; }
84 
85   // Used by macros in mojo/edk/test/mojo_test_base.h to support multiprocess
86   // test client initialization.
87   static void ChildSetup();
88   static int RunClientMain(const base::Callback<int(MojoHandle)>& main);
89   static int RunClientTestMain(const base::Callback<void(MojoHandle)>& main);
90 
91   // For use (and only valid) in the child process:
92   static mojo::ScopedMessagePipeHandle primordial_pipe;
93 
94  private:
95   // Valid after |StartChild()| and before |WaitForChildShutdown()|.
96   base::Process test_child_;
97 
98   ProcessErrorCallback process_error_callback_;
99 
100   std::string peer_token_;
101 
102   DISALLOW_COPY_AND_ASSIGN(MultiprocessTestHelper);
103 };
104 
105 }  // namespace test
106 }  // namespace edk
107 }  // namespace mojo
108 
109 #endif  // MOJO_EDK_TEST_MULTIPROCESS_TEST_HELPER_H_
110