1 : // Copyright 2014 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/zap_timestamp/zap_timestamp_app.h"
16 :
17 : #include "gtest/gtest.h"
18 : #include "syzygy/pe/unittest_util.h"
19 :
20 : namespace zap_timestamp {
21 :
22 : namespace {
23 :
24 : class TestZapTimestampApp : public ZapTimestampApp {
25 : public:
26 : using ZapTimestampApp::zap_;
27 : };
28 :
29 : typedef application::Application<TestZapTimestampApp> TestApp;
30 :
31 : class ZapTimestampAppTest : public testing::PELibUnitTest {
32 : public:
33 : typedef testing::PELibUnitTest Super;
34 :
35 : ZapTimestampAppTest()
36 : : cmd_line_(base::FilePath(L"zap_timestamp.exe")),
37 E : test_impl_(test_app_.implementation()) {
38 E : }
39 :
40 E : void SetUp() {
41 E : Super::SetUp();
42 :
43 : // Several of the tests generate progress and (deliberate) error messages
44 : // that would otherwise clutter the unittest output.
45 E : logging::SetMinLogLevel(logging::LOG_FATAL);
46 :
47 : // Setup the IO streams.
48 E : CreateTemporaryDir(&temp_dir_);
49 E : stdin_path_ = temp_dir_.Append(L"NUL");
50 E : stdout_path_ = temp_dir_.Append(L"stdout.txt");
51 E : stderr_path_ = temp_dir_.Append(L"stderr.txt");
52 E : InitStreams(stdin_path_, stdout_path_, stderr_path_);
53 :
54 : // Point the application at the test's command-line and IO streams.
55 E : test_app_.set_command_line(&cmd_line_);
56 E : test_app_.set_in(in());
57 E : test_app_.set_out(out());
58 E : test_app_.set_err(err());
59 E : }
60 :
61 : // @name The application under test.
62 : // @{
63 : TestApp test_app_;
64 : TestApp::Implementation& test_impl_;
65 : base::FilePath temp_dir_;
66 : base::FilePath stdin_path_;
67 : base::FilePath stdout_path_;
68 : base::FilePath stderr_path_;
69 : // @}
70 :
71 : // @name Command-line and parameters.
72 : // @{
73 : base::CommandLine cmd_line_;
74 : };
75 :
76 : } // namespace
77 :
78 E : TEST_F(ZapTimestampAppTest, ParseEmptyCommandLine) {
79 E : EXPECT_FALSE(test_impl_.ParseCommandLine(&cmd_line_));
80 E : }
81 :
82 E : TEST_F(ZapTimestampAppTest, ParseHelp) {
83 E : cmd_line_.AppendSwitch("help");
84 E : EXPECT_FALSE(test_impl_.ParseCommandLine(&cmd_line_));
85 E : }
86 :
87 E : TEST_F(ZapTimestampAppTest, ParseMinimalCommandLine) {
88 E : base::FilePath input_image(L"foo.dll");
89 E : cmd_line_.AppendSwitchPath("input-image", input_image);
90 E : EXPECT_TRUE(test_impl_.ParseCommandLine(&cmd_line_));
91 :
92 E : EXPECT_EQ(input_image, test_impl_.zap_.input_image());
93 E : EXPECT_TRUE(test_impl_.zap_.input_pdb().empty());
94 E : EXPECT_TRUE(test_impl_.zap_.output_image().empty());
95 E : EXPECT_TRUE(test_impl_.zap_.output_pdb().empty());
96 E : EXPECT_TRUE(test_impl_.zap_.write_image());
97 E : EXPECT_TRUE(test_impl_.zap_.write_pdb());
98 E : EXPECT_FALSE(test_impl_.zap_.overwrite());
99 E : }
100 :
101 E : TEST_F(ZapTimestampAppTest, ParseMaximalCommandLine) {
102 E : base::FilePath input_image(L"foo.dll");
103 E : base::FilePath input_pdb(L"foo.dll");
104 E : base::FilePath output_image(L"foo.dll");
105 E : base::FilePath output_pdb(L"foo.dll");
106 :
107 E : cmd_line_.AppendSwitchPath("input-image", input_image);
108 E : cmd_line_.AppendSwitchPath("input-pdb", input_pdb);
109 E : cmd_line_.AppendSwitchPath("output-image", output_image);
110 E : cmd_line_.AppendSwitchPath("output-pdb", output_pdb);
111 E : cmd_line_.AppendSwitch("no-write-image");
112 E : cmd_line_.AppendSwitch("no-write-pdb");
113 E : cmd_line_.AppendSwitch("overwrite");
114 E : cmd_line_.AppendSwitchASCII("timestamp-value", "42");
115 E : EXPECT_TRUE(test_impl_.ParseCommandLine(&cmd_line_));
116 :
117 E : EXPECT_EQ(input_image, test_impl_.zap_.input_image());
118 E : EXPECT_EQ(input_pdb, test_impl_.zap_.input_pdb());
119 E : EXPECT_EQ(output_image, test_impl_.zap_.output_image());
120 E : EXPECT_EQ(output_pdb, test_impl_.zap_.output_pdb());
121 E : EXPECT_FALSE(test_impl_.zap_.write_image());
122 E : EXPECT_FALSE(test_impl_.zap_.write_pdb());
123 E : EXPECT_TRUE(test_impl_.zap_.overwrite());
124 E : EXPECT_EQ(42, test_impl_.zap_.timestamp_value());
125 E : }
126 :
127 : } // namespace zap_timestamp
|