1 /*
2 * Copyright (C) 2015, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <string>
18
19 #include <android-base/stringprintf.h>
20 #include <gtest/gtest.h>
21
22 #include "aidl.h"
23 #include "aidl_language.h"
24 #include "code_writer.h"
25 #include "generate_cpp.h"
26 #include "os.h"
27 #include "tests/fake_io_delegate.h"
28 #include "tests/test_util.h"
29
30 using ::android::aidl::test::FakeIoDelegate;
31 using ::android::base::StringPrintf;
32 using std::string;
33 using std::unique_ptr;
34
35 namespace android {
36 namespace aidl {
37 namespace cpp {
38
39 class ASTTest : public ::testing::Test {
40 protected:
ASTTest(const string & cmdline,const string & file_contents)41 ASTTest(const string& cmdline, const string& file_contents)
42 : options_(Options::From(cmdline)), file_contents_(file_contents) {
43 }
44
ParseSingleInterface()45 AidlInterface* ParseSingleInterface() {
46 io_delegate_.SetFileContents(options_.InputFiles().at(0), file_contents_);
47
48 vector<string> imported_files;
49 ImportResolver import_resolver{io_delegate_, options_.InputFiles().at(0), {"."}};
50 AidlError err = ::android::aidl::internals::load_and_validate_aidl(
51 options_.InputFiles().front(), options_, io_delegate_, &typenames_, &imported_files);
52
53 if (err != AidlError::OK) {
54 return nullptr;
55 }
56
57 const auto& defined_types = typenames_.MainDocument().DefinedTypes();
58 EXPECT_EQ(1ul, defined_types.size());
59 EXPECT_NE(nullptr, defined_types.front().get()->AsInterface());
60
61 return defined_types.front().get()->AsInterface();
62 }
63
64 const Options options_;
65 const string file_contents_;
66 FakeIoDelegate io_delegate_;
67 AidlTypenames typenames_;
68 };
69
70 namespace test_io_handling {
71
72 const char kInputPath[] = "a/IFoo.aidl";
73 const char kOutputPath[] = "output.cpp";
74 const char kHeaderDir[] = "headers";
75 const char kInterfaceHeaderRelPath[] = "a/IFoo.h";
76
77 const string kCmdline =
78 string("aidl-cpp -I . ") + kInputPath + " " + kHeaderDir + " " + kOutputPath;
79
80 } // namespace test_io_handling
81
82 class IoErrorHandlingTest : public ASTTest {
83 public:
IoErrorHandlingTest()84 IoErrorHandlingTest() : ASTTest(test_io_handling::kCmdline, "package a; interface IFoo {}") {}
85 };
86
TEST_F(IoErrorHandlingTest,GenerateCorrectlyAbsentErrors)87 TEST_F(IoErrorHandlingTest, GenerateCorrectlyAbsentErrors) {
88 // Confirm that this is working correctly without I/O problems.
89 AidlInterface* interface = ParseSingleInterface();
90 ASSERT_NE(interface, nullptr);
91 ASSERT_TRUE(GenerateCpp(options_.OutputFile(), options_, typenames_, *interface, io_delegate_));
92 }
93
TEST_F(IoErrorHandlingTest,HandlesBadHeaderWrite)94 TEST_F(IoErrorHandlingTest, HandlesBadHeaderWrite) {
95 using namespace test_io_handling;
96 AidlInterface* interface = ParseSingleInterface();
97 ASSERT_NE(interface, nullptr);
98
99 // Simulate issues closing the interface header.
100 const string header_path =
101 StringPrintf("%s%c%s", kHeaderDir, OS_PATH_SEPARATOR,
102 kInterfaceHeaderRelPath);
103 io_delegate_.AddBrokenFilePath(header_path);
104 ASSERT_DEATH(GenerateCpp(options_.OutputFile(), options_, typenames_, *interface, io_delegate_),
105 "I/O Error!");
106 // We should never attempt to write the C++ file if we fail writing headers.
107 ASSERT_FALSE(io_delegate_.GetWrittenContents(kOutputPath, nullptr));
108 }
109
TEST_F(IoErrorHandlingTest,HandlesBadCppWrite)110 TEST_F(IoErrorHandlingTest, HandlesBadCppWrite) {
111 using test_io_handling::kOutputPath;
112 AidlInterface* interface = ParseSingleInterface();
113 ASSERT_NE(interface, nullptr);
114
115 // Simulate issues closing the cpp file.
116 io_delegate_.AddBrokenFilePath(kOutputPath);
117 ASSERT_DEATH(GenerateCpp(options_.OutputFile(), options_, typenames_, *interface, io_delegate_),
118 "I/O Error!");
119 }
120
121 } // namespace cpp
122 } // namespace aidl
123 } // namespace android
124