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 <memory>
18 #include <string>
19 #include <vector>
20
21 #include <android-base/logging.h>
22 #include <gtest/gtest.h>
23
24 #include "aidl.h"
25 #include "options.h"
26 #include "tests/fake_io_delegate.h"
27 #include "tests/test_data.h"
28 #include "tests/test_util.h"
29
30 using android::aidl::test::CanonicalNameToPath;
31 using android::aidl::test::FakeIoDelegate;
32 using std::string;
33 using std::unique_ptr;
34 using std::vector;
35
36 namespace android {
37 namespace aidl {
38
39 class EndToEndTest : public ::testing::Test {
40 protected:
SetUp()41 virtual void SetUp() {
42 }
43
AddStubAidls(const char ** parcelables,const char ** interfaces,const char * cpp_header=nullptr)44 void AddStubAidls(const char** parcelables, const char** interfaces,
45 const char* cpp_header=nullptr) {
46 for ( ; *parcelables; ++parcelables) {
47 io_delegate_.AddStubParcelable(
48 *parcelables, (cpp_header) ? cpp_header : "");
49 }
50 for ( ; *interfaces; ++interfaces) {
51 io_delegate_.AddStubInterface(*interfaces);
52 }
53 }
54
CheckFileContents(const string & rel_path,const string & expected_content)55 void CheckFileContents(const string& rel_path,
56 const string& expected_content) {
57 string actual_content;
58 ASSERT_TRUE(io_delegate_.GetWrittenContents(rel_path, &actual_content))
59 << "Expected aidl to write to " << rel_path << " but it did not.";
60
61 if (actual_content == expected_content) {
62 return; // success!
63 }
64
65 test::PrintDiff(expected_content, actual_content);
66 FAIL() << "Actual contents of " << rel_path
67 << " did not match expected content";
68 }
69
70 FakeIoDelegate io_delegate_;
71 };
72
TEST_F(EndToEndTest,IExampleInterface)73 TEST_F(EndToEndTest, IExampleInterface) {
74 using namespace ::android::aidl::test_data::example_interface;
75
76 JavaOptions options;
77 options.fail_on_parcelable_ = true;
78 options.import_paths_.push_back("");
79 options.input_file_name_ = CanonicalNameToPath(kCanonicalName, ".aidl");
80 options.output_file_name_ = kJavaOutputPath;
81 options.dep_file_name_ = "an/arbitrary/path/to/deps.P";
82
83 // Load up our fake file system with data.
84 io_delegate_.SetFileContents(options.input_file_name_, kInterfaceDefinition);
85 io_delegate_.AddCompoundParcelable("android.test.CompoundParcelable",
86 {"Subclass1", "Subclass2"});
87 AddStubAidls(kImportedParcelables, kImportedInterfaces);
88
89 // Check that we parse correctly.
90 EXPECT_EQ(android::aidl::compile_aidl_to_java(options, io_delegate_), 0);
91 CheckFileContents(kJavaOutputPath, kExpectedJavaOutput);
92 CheckFileContents(options.DependencyFilePath(), kExpectedJavaDepsOutput);
93 }
94
TEST_F(EndToEndTest,IExampleInterface_WithTrace)95 TEST_F(EndToEndTest, IExampleInterface_WithTrace) {
96 using namespace ::android::aidl::test_data::example_interface;
97
98 JavaOptions options;
99 options.fail_on_parcelable_ = true;
100 options.import_paths_.push_back("");
101 options.input_file_name_ = CanonicalNameToPath(kCanonicalName, ".aidl");
102 options.output_file_name_ = kJavaOutputPath;
103 options.dep_file_name_ = "an/arbitrary/path/to/deps.P";
104 options.gen_traces_ = true;
105
106 // Load up our fake file system with data.
107 io_delegate_.SetFileContents(options.input_file_name_, kInterfaceDefinition);
108 io_delegate_.AddCompoundParcelable("android.test.CompoundParcelable",
109 {"Subclass1", "Subclass2"});
110 AddStubAidls(kImportedParcelables, kImportedInterfaces);
111
112 // Check that we parse correctly.
113 EXPECT_EQ(android::aidl::compile_aidl_to_java(options, io_delegate_), 0);
114 CheckFileContents(kJavaOutputPath, kExpectedJavaOutputWithTrace);
115 CheckFileContents(options.DependencyFilePath(), kExpectedJavaDepsOutput);
116 }
117
TEST_F(EndToEndTest,IExampleInterface_Outlining)118 TEST_F(EndToEndTest, IExampleInterface_Outlining) {
119 using namespace ::android::aidl::test_data::example_interface;
120
121 JavaOptions options;
122 options.fail_on_parcelable_ = true;
123 options.import_paths_.push_back("");
124 options.input_file_name_ = CanonicalNameToPath(kCanonicalName, ".aidl");
125 options.output_file_name_ = kJavaOutputPath;
126 options.dep_file_name_ = "an/arbitrary/path/to/deps.P";
127 options.onTransact_outline_threshold_ = 4;
128 options.onTransact_non_outline_count_ = 3;
129
130 // Load up our fake file system with data.
131 io_delegate_.SetFileContents(options.input_file_name_, kInterfaceDefinitionOutlining);
132 io_delegate_.AddCompoundParcelable("android.test.CompoundParcelable",
133 {"Subclass1", "Subclass2"});
134 AddStubAidls(kImportedParcelables, kImportedInterfaces);
135
136 // Check that we parse correctly.
137 EXPECT_EQ(android::aidl::compile_aidl_to_java(options, io_delegate_), 0);
138 CheckFileContents(kJavaOutputPath, kExpectedJavaOutputOutlining);
139 CheckFileContents(options.DependencyFilePath(), kExpectedJavaDepsOutput);
140 }
141
TEST_F(EndToEndTest,IPingResponderCpp)142 TEST_F(EndToEndTest, IPingResponderCpp) {
143 using namespace ::android::aidl::test_data::ping_responder;
144
145 const string input_path = CanonicalNameToPath(kCanonicalName, ".aidl");
146 const string output_file = kCppOutputPath;
147 const size_t argc = 6;
148 const char* cmdline[argc + 1] = {
149 "aidl-cpp", "-ddeps.P", "-I.", input_path.c_str(), kGenHeaderDir,
150 output_file.c_str(), nullptr
151 };
152 auto options = CppOptions::Parse(argc, cmdline);
153
154 // Set up input paths.
155 io_delegate_.SetFileContents(input_path, kInterfaceDefinition);
156 AddStubAidls(kImportedParcelables, kImportedInterfaces, kCppParcelableHeader);
157
158 // Check that we parse and generate code correctly.
159 EXPECT_EQ(android::aidl::compile_aidl_to_cpp(*options, io_delegate_), 0);
160 CheckFileContents(output_file, kExpectedCppOutput);
161 CheckFileContents(kGenInterfaceHeaderPath, kExpectedIHeaderOutput);
162 CheckFileContents(kGenClientHeaderPath, kExpectedBpHeaderOutput);
163 CheckFileContents(kGenServerHeaderPath, kExpectedBnHeaderOutput);
164 CheckFileContents(options->DependencyFilePath(), kExpectedCppDepsOutput);
165 }
166
TEST_F(EndToEndTest,StringConstantsInCpp)167 TEST_F(EndToEndTest, StringConstantsInCpp) {
168 using namespace ::android::aidl::test_data::string_constants;
169
170 const string input_path = CanonicalNameToPath(kCanonicalName, ".aidl");
171 const string output_file = kCppOutputPath;
172 const size_t argc = 4;
173 const char* cmdline[argc + 1] = {
174 "aidl-cpp", input_path.c_str(), kGenHeaderDir,
175 output_file.c_str(), nullptr
176 };
177 auto options = CppOptions::Parse(argc, cmdline);
178
179 // Set up input paths.
180 io_delegate_.SetFileContents(input_path, kInterfaceDefinition);
181
182 // Check that we parse and generate code correctly.
183 EXPECT_EQ(android::aidl::compile_aidl_to_cpp(*options, io_delegate_), 0);
184 CheckFileContents(output_file, kExpectedCppOutput);
185 CheckFileContents(kGenInterfaceHeaderPath, kExpectedIHeaderOutput);
186 }
187
TEST_F(EndToEndTest,StringConstantsInJava)188 TEST_F(EndToEndTest, StringConstantsInJava) {
189 using namespace ::android::aidl::test_data::string_constants;
190
191 const string input_path = CanonicalNameToPath(kCanonicalName, ".aidl");
192 const string output_file = kJavaOutputPath;
193 const size_t argc = 4;
194 const char* cmdline[argc + 1] = {
195 "aidl",
196 "-b",
197 input_path.c_str(),
198 output_file.c_str(),
199 nullptr,
200 };
201 auto options = JavaOptions::Parse(argc, cmdline);
202
203 // Load up our fake file system with data.
204 io_delegate_.SetFileContents(input_path, kInterfaceDefinition);
205
206 // Check that we parse correctly.
207 EXPECT_EQ(android::aidl::compile_aidl_to_java(*options, io_delegate_), 0);
208 CheckFileContents(kJavaOutputPath, kExpectedJavaOutput);
209 }
210
211 } // namespace android
212 } // namespace aidl
213