• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "ast_cpp.h"
25 #include "code_writer.h"
26 #include "generate_cpp.h"
27 #include "os.h"
28 #include "tests/fake_io_delegate.h"
29 #include "tests/test_util.h"
30 
31 using ::android::aidl::test::FakeIoDelegate;
32 using ::android::base::StringPrintf;
33 using std::string;
34 using std::unique_ptr;
35 
36 namespace android {
37 namespace aidl {
38 namespace cpp {
39 
40 class ASTTest : public ::testing::Test {
41  protected:
ASTTest(const string & cmdline,const string & file_contents)42   ASTTest(const string& cmdline, const string& file_contents)
43       : options_(Options::From(cmdline)), file_contents_(file_contents) {
44   }
45 
ParseSingleInterface()46   AidlInterface* ParseSingleInterface() {
47     io_delegate_.SetFileContents(options_.InputFiles().at(0), file_contents_);
48 
49     vector<string> imported_files;
50     ImportResolver import_resolver{io_delegate_, options_.InputFiles().at(0), {"."}, {}};
51     AidlError err = ::android::aidl::internals::load_and_validate_aidl(
52         options_.InputFiles().front(), options_, io_delegate_, &typenames_, &imported_files);
53 
54     if (err != AidlError::OK) {
55       return nullptr;
56     }
57 
58     const auto& defined_types = typenames_.MainDocument().DefinedTypes();
59     EXPECT_EQ(1ul, defined_types.size());
60     EXPECT_NE(nullptr, defined_types.front().get()->AsInterface());
61 
62     return defined_types.front().get()->AsInterface();
63   }
64 
ParseSingleEnumDeclaration()65   AidlEnumDeclaration* ParseSingleEnumDeclaration() {
66     io_delegate_.SetFileContents(options_.InputFiles().at(0), file_contents_);
67 
68     vector<string> imported_files;
69     AidlError err = ::android::aidl::internals::load_and_validate_aidl(
70         options_.InputFiles().front(), options_, io_delegate_, &typenames_, &imported_files);
71 
72     if (err != AidlError::OK) {
73       return nullptr;
74     }
75 
76     const auto& defined_types = typenames_.MainDocument().DefinedTypes();
77     EXPECT_EQ(1ul, defined_types.size());
78     EXPECT_NE(nullptr, defined_types.front().get()->AsEnumDeclaration());
79 
80     return defined_types.front().get()->AsEnumDeclaration();
81   }
82 
Compare(Document * doc,const char * expected)83   void Compare(Document* doc, const char* expected) {
84     string output;
85     doc->Write(CodeWriter::ForString(&output).get());
86 
87     if (expected == output) {
88       return; // Success
89     }
90 
91     test::PrintDiff(expected, output);
92     FAIL() << "Document contents did not match expected contents";
93   }
94 
95   const Options options_;
96   const string file_contents_;
97   FakeIoDelegate io_delegate_;
98   AidlTypenames typenames_;
99 };
100 
101 namespace test_io_handling {
102 
103 const char kInputPath[] = "a/IFoo.aidl";
104 const char kOutputPath[] = "output.cpp";
105 const char kHeaderDir[] = "headers";
106 const char kInterfaceHeaderRelPath[] = "a/IFoo.h";
107 
108 const string kCmdline = string("aidl-cpp ") + kInputPath + " " + kHeaderDir + " " + kOutputPath;
109 
110 }  // namespace test_io_handling
111 
112 class IoErrorHandlingTest : public ASTTest {
113  public:
IoErrorHandlingTest()114   IoErrorHandlingTest() : ASTTest(test_io_handling::kCmdline, "package a; interface IFoo {}") {}
115 };
116 
TEST_F(IoErrorHandlingTest,GenerateCorrectlyAbsentErrors)117 TEST_F(IoErrorHandlingTest, GenerateCorrectlyAbsentErrors) {
118   // Confirm that this is working correctly without I/O problems.
119   AidlInterface* interface = ParseSingleInterface();
120   ASSERT_NE(interface, nullptr);
121   ASSERT_TRUE(GenerateCpp(options_.OutputFile(), options_, typenames_, *interface, io_delegate_));
122 }
123 
TEST_F(IoErrorHandlingTest,HandlesBadHeaderWrite)124 TEST_F(IoErrorHandlingTest, HandlesBadHeaderWrite) {
125   using namespace test_io_handling;
126   AidlInterface* interface = ParseSingleInterface();
127   ASSERT_NE(interface, nullptr);
128 
129   // Simulate issues closing the interface header.
130   const string header_path =
131       StringPrintf("%s%c%s", kHeaderDir, OS_PATH_SEPARATOR,
132                    kInterfaceHeaderRelPath);
133   io_delegate_.AddBrokenFilePath(header_path);
134   ASSERT_FALSE(GenerateCpp(options_.OutputFile(), options_, typenames_, *interface, io_delegate_));
135   // We should never attempt to write the C++ file if we fail writing headers.
136   ASSERT_FALSE(io_delegate_.GetWrittenContents(kOutputPath, nullptr));
137   // We should remove partial results.
138   ASSERT_TRUE(io_delegate_.PathWasRemoved(header_path));
139 }
140 
TEST_F(IoErrorHandlingTest,HandlesBadCppWrite)141 TEST_F(IoErrorHandlingTest, HandlesBadCppWrite) {
142   using test_io_handling::kOutputPath;
143   AidlInterface* interface = ParseSingleInterface();
144   ASSERT_NE(interface, nullptr);
145 
146   // Simulate issues closing the cpp file.
147   io_delegate_.AddBrokenFilePath(kOutputPath);
148   ASSERT_FALSE(GenerateCpp(options_.OutputFile(), options_, typenames_, *interface, io_delegate_));
149   // We should remove partial results.
150   ASSERT_TRUE(io_delegate_.PathWasRemoved(kOutputPath));
151 }
152 
153 }  // namespace cpp
154 }  // namespace aidl
155 }  // namespace android
156