OLD | NEW |
---|---|
(Empty) | |
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/runlaa/runlaa_app.h" | |
16 | |
17 #include "gmock/gmock.h" | |
18 #include "gtest/gtest.h" | |
19 #include "syzygy/pe/unittest_util.h" | |
20 | |
21 namespace runlaa { | |
22 | |
23 namespace { | |
24 | |
25 class TestRunLaaApp : public RunLaaApp { | |
26 public: | |
27 using RunLaaApp::expect_mode_; | |
28 using RunLaaApp::is_laa_; | |
29 using RunLaaApp::image_; | |
30 using RunLaaApp::in_place_; | |
31 using RunLaaApp::keep_temp_dir_; | |
32 using RunLaaApp::child_argv_; | |
33 }; | |
34 | |
35 typedef application::Application<TestRunLaaApp> TestApp; | |
36 | |
37 class RunLaaAppTest : public testing::PELibUnitTest { | |
38 public: | |
39 typedef testing::PELibUnitTest Super; | |
40 | |
41 RunLaaAppTest() | |
42 : cmd_line_(base::FilePath(L"runlaa.exe")), | |
43 test_impl_(test_app_.implementation()) {} | |
44 | |
45 void SetUp() { | |
46 Super::SetUp(); | |
47 | |
48 logging::SetMinLogLevel(logging::LOG_ERROR); | |
49 | |
50 // Setup the IO streams. | |
51 CreateTemporaryDir(&temp_dir_); | |
Sébastien Marchand
2015/07/30 18:23:28
The stream redirection could probably be extracted
chrisha
2015/07/30 20:06:12
I've filed a bug to clean this up.
| |
52 stdin_path_ = temp_dir_.Append(L"NUL"); | |
53 stdout_path_ = temp_dir_.Append(L"stdout.txt"); | |
54 stderr_path_ = temp_dir_.Append(L"stderr.txt"); | |
55 InitStreams(stdin_path_, stdout_path_, stderr_path_); | |
56 | |
57 exe_path_ = testing::GetExeRelativePath(L"runlaa.exe"); | |
Sébastien Marchand
2015/07/30 18:23:28
This is such a boring name :)
| |
58 ASSERT_NO_FATAL_FAILURE(ConfigureTestApp(&test_app_)); | |
59 } | |
60 | |
61 // Points the application at the fixture's command-line and IO streams. | |
62 template <typename TestAppType> | |
63 void ConfigureTestApp(TestAppType* test_app) { | |
64 test_app->set_command_line(&cmd_line_); | |
65 test_app->set_in(in()); | |
66 test_app->set_out(out()); | |
67 test_app->set_err(err()); | |
68 } | |
69 | |
70 // Stashes the current log-level before each test instance and restores it | |
71 // after each test completes. | |
72 testing::ScopedLogLevelSaver log_level_saver; | |
73 | |
74 // @name The application under test. | |
75 // @{ | |
76 TestApp test_app_; | |
77 TestApp::Implementation& test_impl_; | |
78 base::FilePath temp_dir_; | |
79 base::FilePath stdin_path_; | |
80 base::FilePath stdout_path_; | |
81 base::FilePath stderr_path_; | |
82 // @} | |
83 | |
84 base::FilePath exe_path_; | |
85 base::CommandLine cmd_line_; | |
86 }; | |
87 | |
88 } // namespace | |
89 | |
90 TEST_F(RunLaaAppTest, GetHelp) { | |
91 cmd_line_.AppendSwitch("help"); | |
92 ASSERT_FALSE(test_impl_.ParseCommandLine(&cmd_line_)); | |
93 } | |
94 | |
95 TEST_F(RunLaaAppTest, ParseEmptyCommandLineFails) { | |
96 ASSERT_FALSE(test_impl_.ParseCommandLine(&cmd_line_)); | |
97 } | |
98 | |
99 TEST_F(RunLaaAppTest, ParseMinimalCommandLineSucceeds) { | |
100 cmd_line_.AppendSwitchPath("image", exe_path_); | |
101 cmd_line_.AppendSwitchASCII("mode", "laa"); | |
102 ASSERT_TRUE(test_impl_.ParseCommandLine(&cmd_line_)); | |
103 EXPECT_TRUE(test_impl_.expect_mode_.empty()); | |
104 EXPECT_EQ(exe_path_, test_impl_.image_); | |
105 EXPECT_TRUE(test_impl_.is_laa_); | |
106 EXPECT_FALSE(test_impl_.in_place_); | |
107 EXPECT_FALSE(test_impl_.keep_temp_dir_); | |
108 EXPECT_TRUE(test_impl_.child_argv_.empty()); | |
109 } | |
110 | |
111 TEST_F(RunLaaAppTest, ParseMaximalCommandLineSucceeds) { | |
112 cmd_line_.AppendSwitchPath("image", exe_path_); | |
113 cmd_line_.AppendSwitch("in-place"); | |
114 cmd_line_.AppendSwitch("keep-temp-dir"); | |
115 cmd_line_.AppendSwitchASCII("mode", "nolaa"); | |
116 cmd_line_.AppendArg("--"); | |
117 cmd_line_.AppendArg("--foo"); | |
118 ASSERT_TRUE(test_impl_.ParseCommandLine(&cmd_line_)); | |
119 EXPECT_TRUE(test_impl_.expect_mode_.empty()); | |
120 EXPECT_EQ(exe_path_, test_impl_.image_); | |
121 EXPECT_FALSE(test_impl_.is_laa_); | |
122 EXPECT_TRUE(test_impl_.in_place_); | |
123 EXPECT_TRUE(test_impl_.keep_temp_dir_); | |
124 EXPECT_THAT(test_impl_.child_argv_, testing::Contains(L"--foo")); | |
125 } | |
126 | |
127 TEST_F(RunLaaAppTest, ParseCommandLineInvalidModeFails) { | |
128 cmd_line_.AppendSwitchPath("image", exe_path_); | |
129 cmd_line_.AppendSwitchASCII("mode", "foo"); | |
130 ASSERT_FALSE(test_impl_.ParseCommandLine(&cmd_line_)); | |
131 } | |
132 | |
133 TEST_F(RunLaaAppTest, ParseTestingCommandLineSucceeds) { | |
134 cmd_line_.AppendSwitchASCII("expect-mode", "nolaa"); | |
135 ASSERT_TRUE(test_impl_.ParseCommandLine(&cmd_line_)); | |
136 EXPECT_STREQ("nolaa", test_impl_.expect_mode_.c_str()); | |
137 } | |
138 | |
139 TEST_F(RunLaaAppTest, LaaModeSucceeds) { | |
140 cmd_line_.AppendSwitchPath("image", exe_path_); | |
141 cmd_line_.AppendSwitchASCII("mode", "laa"); | |
142 cmd_line_.AppendArg("--"); | |
143 cmd_line_.AppendArg("--expect-mode=laa"); | |
144 ASSERT_TRUE(test_impl_.ParseCommandLine(&cmd_line_)); | |
145 EXPECT_EQ(0, test_impl_.Run()); | |
146 } | |
147 | |
148 TEST_F(RunLaaAppTest, NoLaaModeSucceeds) { | |
149 cmd_line_.AppendSwitchPath("image", exe_path_); | |
150 cmd_line_.AppendSwitchASCII("mode", "nolaa"); | |
151 cmd_line_.AppendArg("--"); | |
152 cmd_line_.AppendArg("--expect-mode=nolaa"); | |
153 ASSERT_TRUE(test_impl_.ParseCommandLine(&cmd_line_)); | |
154 EXPECT_EQ(0, test_impl_.Run()); | |
155 } | |
156 | |
157 TEST_F(RunLaaAppTest, LaaModeUnexpected) { | |
158 cmd_line_.AppendSwitchPath("image", exe_path_); | |
159 cmd_line_.AppendSwitchASCII("mode", "laa"); | |
160 cmd_line_.AppendArg("--"); | |
161 cmd_line_.AppendArg("--expect-mode=nolaa"); | |
162 ASSERT_TRUE(test_impl_.ParseCommandLine(&cmd_line_)); | |
163 EXPECT_EQ(1, test_impl_.Run()); | |
164 } | |
165 | |
166 TEST_F(RunLaaAppTest, NoLaaModeUnexpected) { | |
167 cmd_line_.AppendSwitchPath("image", exe_path_); | |
168 cmd_line_.AppendSwitchASCII("mode", "nolaa"); | |
169 cmd_line_.AppendArg("--"); | |
170 cmd_line_.AppendArg("--expect-mode=laa"); | |
171 ASSERT_TRUE(test_impl_.ParseCommandLine(&cmd_line_)); | |
172 EXPECT_EQ(1, test_impl_.Run()); | |
173 } | |
174 | |
175 } // namespace runlaa | |
OLD | NEW |