1 : // Copyright 2015 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 :
15 : #include "syzygy/kasko/testing/launch_python_process.h"
16 :
17 : #include "base/bind.h"
18 : #include "base/callback_helpers.h"
19 : #include "base/command_line.h"
20 : #include "base/files/file_path.h"
21 : #include "base/process/kill.h"
22 : #include "base/win/scoped_handle.h"
23 : #include "gtest/gtest.h"
24 :
25 m : namespace kasko {
26 m : namespace testing {
27 :
28 m : TEST(LaunchPythonProcessTest, BasicTest) {
29 m : base::CommandLine args(base::CommandLine::NO_PROGRAM);
30 m : base::Process process;
31 m : process = LaunchPythonProcess(
32 m : base::FilePath(L"syzygy/kasko/testing/exit_with.py"), args);
33 m : ASSERT_TRUE(process.IsValid());
34 m : int exit_code = 0;
35 m : ASSERT_TRUE(process.WaitForExit(&exit_code));
36 m : ASSERT_EQ(0, exit_code);
37 :
38 : // Pass an argument.
39 m : args.AppendArg("2");
40 m : process = LaunchPythonProcess(
41 m : base::FilePath(L"syzygy/kasko/testing/exit_with.py"), args);
42 m : ASSERT_TRUE(process.IsValid());
43 m : ASSERT_TRUE(process.WaitForExit(&exit_code));
44 m : ASSERT_EQ(2, exit_code);
45 :
46 : // Switches are treated differently than arguments by CommandLine, and proved
47 : // to be tricky in the implementation. Hence this test case with both a switch
48 : // and an argument.
49 m : args.AppendSwitch("-p 3");
50 m : process = LaunchPythonProcess(
51 m : base::FilePath(L"syzygy/kasko/testing/exit_with.py"), args);
52 m : ASSERT_TRUE(process.IsValid());
53 m : ASSERT_TRUE(process.WaitForExit(&exit_code));
54 m : ASSERT_EQ(5, exit_code);
55 :
56 : // Set stdin to NULL, as the test launcher does in a parallel test mode.
57 m : base::ScopedClosureRunner reset_stdin(
58 m : base::Bind(base::IgnoreResult(&::SetStdHandle), STD_INPUT_HANDLE,
59 m : ::GetStdHandle(STD_INPUT_HANDLE)));
60 m : ::SetStdHandle(STD_INPUT_HANDLE, INVALID_HANDLE_VALUE);
61 m : process = LaunchPythonProcess(
62 m : base::FilePath(L"syzygy/kasko/testing/exit_with.py"), args);
63 m : ASSERT_TRUE(process.IsValid());
64 m : ASSERT_TRUE(process.WaitForExit(&exit_code));
65 m : ASSERT_EQ(5, exit_code);
66 m : }
67 :
68 m : } // namespace testing
69 m : } // namespace kasko
|