blob: d79f22509633504bb1c027691071ad8f5ba1f5ce [file] [log] [blame]
Casey Dahlina834dd42015-09-23 11:52:15 -07001/*
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
Elliott Hughes0a620672015-12-04 13:53:18 -080019#include <android-base/stringprintf.h>
Casey Dahlina834dd42015-09-23 11:52:15 -070020#include <gtest/gtest.h>
21
Casey Dahlin2cc93162015-10-02 16:14:17 -070022#include "aidl.h"
Casey Dahlina834dd42015-09-23 11:52:15 -070023#include "aidl_language.h"
Casey Dahlina834dd42015-09-23 11:52:15 -070024#include "code_writer.h"
Christopher Wileyad339272015-10-05 19:11:58 -070025#include "generate_cpp.h"
Christopher Wiley9d6e0b22015-11-13 12:18:16 -080026#include "os.h"
Christopher Wiley4a2884b2015-10-07 11:27:45 -070027#include "tests/fake_io_delegate.h"
Casey Dahlin80ada3d2015-10-20 20:33:56 -070028#include "tests/test_util.h"
Casey Dahlina834dd42015-09-23 11:52:15 -070029
Casey Dahlinb8d9e882015-11-24 10:57:23 -080030using ::android::aidl::test::FakeIoDelegate;
31using ::android::base::StringPrintf;
Casey Dahlina834dd42015-09-23 11:52:15 -070032using std::string;
33using std::unique_ptr;
34
35namespace android {
36namespace aidl {
Christopher Wileyf944e792015-09-29 10:00:46 -070037namespace cpp {
Casey Dahlina834dd42015-09-23 11:52:15 -070038
Casey Dahlinb0966612015-10-19 16:35:26 -070039class ASTTest : public ::testing::Test {
Christopher Wiley0c732db2015-09-29 14:36:44 -070040 protected:
Jiyong Park6f77e0c2018-07-28 16:55:44 +090041 ASTTest(const string& cmdline, const string& file_contents)
42 : options_(Options::From(cmdline)), file_contents_(file_contents) {
Christopher Wiley56799522015-10-31 10:17:04 -070043 }
Casey Dahlinb0966612015-10-19 16:35:26 -070044
Jiyong Parkb034bf02018-07-30 17:44:33 +090045 AidlInterface* ParseSingleInterface() {
Jiyong Park6f77e0c2018-07-28 16:55:44 +090046 io_delegate_.SetFileContents(options_.InputFiles().at(0), file_contents_);
Casey Dahlina834dd42015-09-23 11:52:15 -070047
Jiyong Parkb034bf02018-07-30 17:44:33 +090048 vector<string> imported_files;
Jooyung Han6529b822021-06-11 22:05:26 +090049 ImportResolver import_resolver{io_delegate_, options_.InputFiles().at(0), {"."}};
Jiyong Park6f77e0c2018-07-28 16:55:44 +090050 AidlError err = ::android::aidl::internals::load_and_validate_aidl(
Jiyong Park8e79b7f2020-07-20 20:52:38 +090051 options_.InputFiles().front(), options_, io_delegate_, &typenames_, &imported_files);
Christopher Wiley4a2884b2015-10-07 11:27:45 -070052
Steven Moreland5557f1c2018-07-02 13:50:23 -070053 if (err != AidlError::OK) {
Casey Dahlin2cc93162015-10-02 16:14:17 -070054 return nullptr;
Steven Moreland5557f1c2018-07-02 13:50:23 -070055 }
Casey Dahlina834dd42015-09-23 11:52:15 -070056
Jiyong Park8e79b7f2020-07-20 20:52:38 +090057 const auto& defined_types = typenames_.MainDocument().DefinedTypes();
Jiyong Parkb034bf02018-07-30 17:44:33 +090058 EXPECT_EQ(1ul, defined_types.size());
Jiyong Park8e79b7f2020-07-20 20:52:38 +090059 EXPECT_NE(nullptr, defined_types.front().get()->AsInterface());
Steven Moreland5557f1c2018-07-02 13:50:23 -070060
Jiyong Park8e79b7f2020-07-20 20:52:38 +090061 return defined_types.front().get()->AsInterface();
Christopher Wiley90be4e32015-10-20 14:55:25 -070062 }
Casey Dahlina834dd42015-09-23 11:52:15 -070063
Jiyong Park6f77e0c2018-07-28 16:55:44 +090064 const Options options_;
Casey Dahlin389781f2015-10-22 13:13:21 -070065 const string file_contents_;
66 FakeIoDelegate io_delegate_;
Jeongik Cha047c5ee2019-08-07 23:16:49 +090067 AidlTypenames typenames_;
Christopher Wiley0c732db2015-09-29 14:36:44 -070068};
69
Christopher Wiley9d6e0b22015-11-13 12:18:16 -080070namespace test_io_handling {
71
72const char kInputPath[] = "a/IFoo.aidl";
73const char kOutputPath[] = "output.cpp";
74const char kHeaderDir[] = "headers";
75const char kInterfaceHeaderRelPath[] = "a/IFoo.h";
76
Jiyong Park6f77e0c2018-07-28 16:55:44 +090077const string kCmdline = string("aidl-cpp ") + kInputPath + " " + kHeaderDir + " " + kOutputPath;
78
Christopher Wiley9d6e0b22015-11-13 12:18:16 -080079} // namespace test_io_handling
80
81class IoErrorHandlingTest : public ASTTest {
82 public:
Jiyong Park6f77e0c2018-07-28 16:55:44 +090083 IoErrorHandlingTest() : ASTTest(test_io_handling::kCmdline, "package a; interface IFoo {}") {}
Christopher Wiley9d6e0b22015-11-13 12:18:16 -080084};
85
86TEST_F(IoErrorHandlingTest, GenerateCorrectlyAbsentErrors) {
87 // Confirm that this is working correctly without I/O problems.
Jiyong Parkb034bf02018-07-30 17:44:33 +090088 AidlInterface* interface = ParseSingleInterface();
Christopher Wiley9d6e0b22015-11-13 12:18:16 -080089 ASSERT_NE(interface, nullptr);
Jeongik Cha047c5ee2019-08-07 23:16:49 +090090 ASSERT_TRUE(GenerateCpp(options_.OutputFile(), options_, typenames_, *interface, io_delegate_));
Christopher Wiley9d6e0b22015-11-13 12:18:16 -080091}
92
93TEST_F(IoErrorHandlingTest, HandlesBadHeaderWrite) {
94 using namespace test_io_handling;
Jiyong Parkb034bf02018-07-30 17:44:33 +090095 AidlInterface* interface = ParseSingleInterface();
Christopher Wiley9d6e0b22015-11-13 12:18:16 -080096 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);
Steven Morelande422fce2021-09-30 15:55:56 -0700103 ASSERT_DEATH(GenerateCpp(options_.OutputFile(), options_, typenames_, *interface, io_delegate_),
104 "I/O Error!");
Christopher Wiley9d6e0b22015-11-13 12:18:16 -0800105 // We should never attempt to write the C++ file if we fail writing headers.
106 ASSERT_FALSE(io_delegate_.GetWrittenContents(kOutputPath, nullptr));
Christopher Wiley9d6e0b22015-11-13 12:18:16 -0800107}
108
109TEST_F(IoErrorHandlingTest, HandlesBadCppWrite) {
110 using test_io_handling::kOutputPath;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900111 AidlInterface* interface = ParseSingleInterface();
Christopher Wiley9d6e0b22015-11-13 12:18:16 -0800112 ASSERT_NE(interface, nullptr);
113
114 // Simulate issues closing the cpp file.
115 io_delegate_.AddBrokenFilePath(kOutputPath);
Steven Morelande422fce2021-09-30 15:55:56 -0700116 ASSERT_DEATH(GenerateCpp(options_.OutputFile(), options_, typenames_, *interface, io_delegate_),
117 "I/O Error!");
Christopher Wiley9d6e0b22015-11-13 12:18:16 -0800118}
119
Christopher Wileyf944e792015-09-29 10:00:46 -0700120} // namespace cpp
Casey Dahlina834dd42015-09-23 11:52:15 -0700121} // namespace aidl
122} // namespace android