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,IPingResponderCpp)95 TEST_F(EndToEndTest, IPingResponderCpp) {
96 using namespace ::android::aidl::test_data::ping_responder;
97
98 const string input_path = CanonicalNameToPath(kCanonicalName, ".aidl");
99 const string output_file = kCppOutputPath;
100 const size_t argc = 6;
101 const char* cmdline[argc + 1] = {
102 "aidl-cpp", "-ddeps.P", "-I.", input_path.c_str(), kGenHeaderDir,
103 output_file.c_str(), nullptr
104 };
105 auto options = CppOptions::Parse(argc, cmdline);
106
107 // Set up input paths.
108 io_delegate_.SetFileContents(input_path, kInterfaceDefinition);
109 AddStubAidls(kImportedParcelables, kImportedInterfaces, kCppParcelableHeader);
110
111 // Check that we parse and generate code correctly.
112 EXPECT_EQ(android::aidl::compile_aidl_to_cpp(*options, io_delegate_), 0);
113 CheckFileContents(output_file, kExpectedCppOutput);
114 CheckFileContents(kGenInterfaceHeaderPath, kExpectedIHeaderOutput);
115 CheckFileContents(kGenClientHeaderPath, kExpectedBpHeaderOutput);
116 CheckFileContents(kGenServerHeaderPath, kExpectedBnHeaderOutput);
117 CheckFileContents(options->DependencyFilePath(), kExpectedCppDepsOutput);
118 }
119
120 } // namespace android
121 } // namespace aidl
122