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 = string("aidl-cpp ") + kInputPath + " " + kHeaderDir + " " + kOutputPath;
78
79 } // namespace test_io_handling
80
81 class IoErrorHandlingTest : public ASTTest {
82 public:
IoErrorHandlingTest()83 IoErrorHandlingTest() : ASTTest(test_io_handling::kCmdline, "package a; interface IFoo {}") {}
84 };
85
TEST_F(IoErrorHandlingTest,GenerateCorrectlyAbsentErrors)86 TEST_F(IoErrorHandlingTest, GenerateCorrectlyAbsentErrors) {
87 // Confirm that this is working correctly without I/O problems.
88 AidlInterface* interface = ParseSingleInterface();
89 ASSERT_NE(interface, nullptr);
90 ASSERT_TRUE(GenerateCpp(options_.OutputFile(), options_, typenames_, *interface, io_delegate_));
91 }
92
TEST_F(IoErrorHandlingTest,HandlesBadHeaderWrite)93 TEST_F(IoErrorHandlingTest, HandlesBadHeaderWrite) {
94 using namespace test_io_handling;
95 AidlInterface* interface = ParseSingleInterface();
96 ASSERT_NE(interface, nullptr);
97
98 // Simulate issues closing the interface header.
99 const string header_path =
100 StringPrintf("%s%c%s", kHeaderDir, OS_PATH_SEPARATOR,
101 kInterfaceHeaderRelPath);
102 io_delegate_.AddBrokenFilePath(header_path);
103 ASSERT_DEATH(GenerateCpp(options_.OutputFile(), options_, typenames_, *interface, io_delegate_),
104 "I/O Error!");
105 // We should never attempt to write the C++ file if we fail writing headers.
106 ASSERT_FALSE(io_delegate_.GetWrittenContents(kOutputPath, nullptr));
107 }
108
TEST_F(IoErrorHandlingTest,HandlesBadCppWrite)109 TEST_F(IoErrorHandlingTest, HandlesBadCppWrite) {
110 using test_io_handling::kOutputPath;
111 AidlInterface* interface = ParseSingleInterface();
112 ASSERT_NE(interface, nullptr);
113
114 // Simulate issues closing the cpp file.
115 io_delegate_.AddBrokenFilePath(kOutputPath);
116 ASSERT_DEATH(GenerateCpp(options_.OutputFile(), options_, typenames_, *interface, io_delegate_),
117 "I/O Error!");
118 }
119
120 } // namespace cpp
121 } // namespace aidl
122 } // namespace android
123