1856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley/*
2856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley * Copyright (C) 2015, The Android Open Source Project
3856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley *
4856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley * Licensed under the Apache License, Version 2.0 (the "License");
5856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley * you may not use this file except in compliance with the License.
6856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley * You may obtain a copy of the License at
7856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley *
8856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley *     http://www.apache.org/licenses/LICENSE-2.0
9856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley *
10856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley * Unless required by applicable law or agreed to in writing, software
11856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley * distributed under the License is distributed on an "AS IS" BASIS,
12856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley * See the License for the specific language governing permissions and
14856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley * limitations under the License.
15856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley */
16856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley
17856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley#include <memory>
18856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley#include <string>
19856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley#include <vector>
20856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley
210a62067f35e957493bc37c4b42dfdcfc16353831Elliott Hughes#include <android-base/logging.h>
22856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley#include <gtest/gtest.h>
23856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley
24856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley#include "aidl.h"
25856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley#include "options.h"
26ea6e382143e4a8df894d47db3a6cb79a38ad4530Christopher Wiley#include "tests/fake_io_delegate.h"
27856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley#include "tests/test_data.h"
28ea6e382143e4a8df894d47db3a6cb79a38ad4530Christopher Wiley#include "tests/test_util.h"
29856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley
30ea6e382143e4a8df894d47db3a6cb79a38ad4530Christopher Wileyusing android::aidl::test::CanonicalNameToPath;
31ea6e382143e4a8df894d47db3a6cb79a38ad4530Christopher Wileyusing android::aidl::test::FakeIoDelegate;
32856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wileyusing std::string;
33856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wileyusing std::unique_ptr;
34856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wileyusing std::vector;
35856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley
364427d8654985209c1ebf2fb4705bb3e1a4262be2Christopher Wileynamespace android {
374427d8654985209c1ebf2fb4705bb3e1a4262be2Christopher Wileynamespace aidl {
38856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley
39856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wileyclass EndToEndTest : public ::testing::Test {
40856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley protected:
41856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley  virtual void SetUp() {
42856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley  }
43856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley
44e9351ccad61f2f7fc4a0721480119112c97f049eChristopher Wiley  void AddStubAidls(const char** parcelables, const char** interfaces,
45e9351ccad61f2f7fc4a0721480119112c97f049eChristopher Wiley                    const char* cpp_header=nullptr) {
46ea6e382143e4a8df894d47db3a6cb79a38ad4530Christopher Wiley    for ( ; *parcelables; ++parcelables) {
47e9351ccad61f2f7fc4a0721480119112c97f049eChristopher Wiley      io_delegate_.AddStubParcelable(
48e9351ccad61f2f7fc4a0721480119112c97f049eChristopher Wiley          *parcelables, (cpp_header) ? cpp_header : "");
498f3e0f5d67690ce8ea22a967b0cefcc8b1b01cfaChristopher Wiley    }
50ea6e382143e4a8df894d47db3a6cb79a38ad4530Christopher Wiley    for ( ; *interfaces; ++interfaces) {
51a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley      io_delegate_.AddStubInterface(*interfaces);
52856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley    }
53856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley  }
54856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley
55a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley  void CheckFileContents(const string& rel_path,
56856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley                         const string& expected_content) {
57a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley    string actual_content;
58a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley    ASSERT_TRUE(io_delegate_.GetWrittenContents(rel_path, &actual_content))
59a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley        << "Expected aidl to write to " << rel_path << " but it did not.";
600edf34239909bbc16e884ea2517e5f3398185d0fCasey Dahlin
61a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley    if (actual_content == expected_content) {
62a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley      return;  // success!
63856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley    }
64a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley
6580ada3d405cbf76797e007d5fc265d17315aa05cCasey Dahlin    test::PrintDiff(expected_content, actual_content);
66a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley    FAIL() << "Actual contents of " << rel_path
67a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley           << " did not match expected content";
68856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley  }
69856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley
70a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley  FakeIoDelegate io_delegate_;
71856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley};
72856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley
73856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher WileyTEST_F(EndToEndTest, IExampleInterface) {
74a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley  using namespace ::android::aidl::test_data::example_interface;
75a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley
7689eaab56f3ff913c247397b858fac3c8d3d4a206Christopher Wiley  JavaOptions options;
778f8cc9b91a8d0b4942a978e9d2edc5337503d583Christopher Wiley  options.fail_on_parcelable_ = true;
78ea6e382143e4a8df894d47db3a6cb79a38ad4530Christopher Wiley  options.import_paths_.push_back("");
79c5442de40df22a763804648f5ee1d16ec28a7cd2Christopher Wiley  options.input_file_name_ = CanonicalNameToPath(kCanonicalName, ".aidl");
80a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley  options.output_file_name_ = kJavaOutputPath;
81a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley  options.dep_file_name_ = "an/arbitrary/path/to/deps.P";
82ea6e382143e4a8df894d47db3a6cb79a38ad4530Christopher Wiley
83ea6e382143e4a8df894d47db3a6cb79a38ad4530Christopher Wiley  // Load up our fake file system with data.
84a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley  io_delegate_.SetFileContents(options.input_file_name_, kInterfaceDefinition);
85a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley  io_delegate_.AddCompoundParcelable("android.test.CompoundParcelable",
86a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley                                     {"Subclass1", "Subclass2"});
87a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley  AddStubAidls(kImportedParcelables, kImportedInterfaces);
88ea6e382143e4a8df894d47db3a6cb79a38ad4530Christopher Wiley
89ea6e382143e4a8df894d47db3a6cb79a38ad4530Christopher Wiley  // Check that we parse correctly.
90a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley  EXPECT_EQ(android::aidl::compile_aidl_to_java(options, io_delegate_), 0);
91a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley  CheckFileContents(kJavaOutputPath, kExpectedJavaOutput);
9219059cb70c5330787ba88f3860444e6da3ef3ca0Christopher Wiley  CheckFileContents(options.DependencyFilePath(), kExpectedJavaDepsOutput);
93856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley}
944427d8654985209c1ebf2fb4705bb3e1a4262be2Christopher Wiley
95af2d510018c546961e502b61682eaf8a019cc974Christopher WileyTEST_F(EndToEndTest, IPingResponderCpp) {
96af2d510018c546961e502b61682eaf8a019cc974Christopher Wiley  using namespace ::android::aidl::test_data::ping_responder;
97af2d510018c546961e502b61682eaf8a019cc974Christopher Wiley
98c5442de40df22a763804648f5ee1d16ec28a7cd2Christopher Wiley  const string input_path = CanonicalNameToPath(kCanonicalName, ".aidl");
9919059cb70c5330787ba88f3860444e6da3ef3ca0Christopher Wiley  const string output_file = kCppOutputPath;
10019059cb70c5330787ba88f3860444e6da3ef3ca0Christopher Wiley  const size_t argc = 6;
101af2d510018c546961e502b61682eaf8a019cc974Christopher Wiley  const char* cmdline[argc + 1] = {
10219059cb70c5330787ba88f3860444e6da3ef3ca0Christopher Wiley      "aidl-cpp", "-ddeps.P", "-I.", input_path.c_str(), kGenHeaderDir,
103af2d510018c546961e502b61682eaf8a019cc974Christopher Wiley      output_file.c_str(), nullptr
104af2d510018c546961e502b61682eaf8a019cc974Christopher Wiley  };
105af2d510018c546961e502b61682eaf8a019cc974Christopher Wiley  auto options = CppOptions::Parse(argc, cmdline);
106af2d510018c546961e502b61682eaf8a019cc974Christopher Wiley
107af2d510018c546961e502b61682eaf8a019cc974Christopher Wiley  // Set up input paths.
108af2d510018c546961e502b61682eaf8a019cc974Christopher Wiley  io_delegate_.SetFileContents(input_path, kInterfaceDefinition);
109e9351ccad61f2f7fc4a0721480119112c97f049eChristopher Wiley  AddStubAidls(kImportedParcelables, kImportedInterfaces, kCppParcelableHeader);
110af2d510018c546961e502b61682eaf8a019cc974Christopher Wiley
111af2d510018c546961e502b61682eaf8a019cc974Christopher Wiley  // Check that we parse and generate code correctly.
112af2d510018c546961e502b61682eaf8a019cc974Christopher Wiley  EXPECT_EQ(android::aidl::compile_aidl_to_cpp(*options, io_delegate_), 0);
113af2d510018c546961e502b61682eaf8a019cc974Christopher Wiley  CheckFileContents(output_file, kExpectedCppOutput);
114af2d510018c546961e502b61682eaf8a019cc974Christopher Wiley  CheckFileContents(kGenInterfaceHeaderPath, kExpectedIHeaderOutput);
115af2d510018c546961e502b61682eaf8a019cc974Christopher Wiley  CheckFileContents(kGenClientHeaderPath, kExpectedBpHeaderOutput);
116af2d510018c546961e502b61682eaf8a019cc974Christopher Wiley  CheckFileContents(kGenServerHeaderPath, kExpectedBnHeaderOutput);
11719059cb70c5330787ba88f3860444e6da3ef3ca0Christopher Wiley  CheckFileContents(options->DependencyFilePath(), kExpectedCppDepsOutput);
118af2d510018c546961e502b61682eaf8a019cc974Christopher Wiley}
119af2d510018c546961e502b61682eaf8a019cc974Christopher Wiley
120d6bdd8d7cfb0a96ccc826cbb005128f0f7ca3446Christopher WileyTEST_F(EndToEndTest, StringConstantsInCpp) {
121d6bdd8d7cfb0a96ccc826cbb005128f0f7ca3446Christopher Wiley  using namespace ::android::aidl::test_data::string_constants;
122d6bdd8d7cfb0a96ccc826cbb005128f0f7ca3446Christopher Wiley
123d6bdd8d7cfb0a96ccc826cbb005128f0f7ca3446Christopher Wiley  const string input_path = CanonicalNameToPath(kCanonicalName, ".aidl");
124d6bdd8d7cfb0a96ccc826cbb005128f0f7ca3446Christopher Wiley  const string output_file = kCppOutputPath;
125d6bdd8d7cfb0a96ccc826cbb005128f0f7ca3446Christopher Wiley  const size_t argc = 4;
126d6bdd8d7cfb0a96ccc826cbb005128f0f7ca3446Christopher Wiley  const char* cmdline[argc + 1] = {
127d6bdd8d7cfb0a96ccc826cbb005128f0f7ca3446Christopher Wiley      "aidl-cpp", input_path.c_str(), kGenHeaderDir,
128d6bdd8d7cfb0a96ccc826cbb005128f0f7ca3446Christopher Wiley      output_file.c_str(), nullptr
129d6bdd8d7cfb0a96ccc826cbb005128f0f7ca3446Christopher Wiley  };
130d6bdd8d7cfb0a96ccc826cbb005128f0f7ca3446Christopher Wiley  auto options = CppOptions::Parse(argc, cmdline);
131d6bdd8d7cfb0a96ccc826cbb005128f0f7ca3446Christopher Wiley
132d6bdd8d7cfb0a96ccc826cbb005128f0f7ca3446Christopher Wiley  // Set up input paths.
133d6bdd8d7cfb0a96ccc826cbb005128f0f7ca3446Christopher Wiley  io_delegate_.SetFileContents(input_path, kInterfaceDefinition);
134d6bdd8d7cfb0a96ccc826cbb005128f0f7ca3446Christopher Wiley
135d6bdd8d7cfb0a96ccc826cbb005128f0f7ca3446Christopher Wiley  // Check that we parse and generate code correctly.
136d6bdd8d7cfb0a96ccc826cbb005128f0f7ca3446Christopher Wiley  EXPECT_EQ(android::aidl::compile_aidl_to_cpp(*options, io_delegate_), 0);
137d6bdd8d7cfb0a96ccc826cbb005128f0f7ca3446Christopher Wiley  CheckFileContents(output_file, kExpectedCppOutput);
138d6bdd8d7cfb0a96ccc826cbb005128f0f7ca3446Christopher Wiley  CheckFileContents(kGenInterfaceHeaderPath, kExpectedIHeaderOutput);
139d6bdd8d7cfb0a96ccc826cbb005128f0f7ca3446Christopher Wiley}
140d6bdd8d7cfb0a96ccc826cbb005128f0f7ca3446Christopher Wiley
141d6bdd8d7cfb0a96ccc826cbb005128f0f7ca3446Christopher WileyTEST_F(EndToEndTest, StringConstantsInJava) {
142d6bdd8d7cfb0a96ccc826cbb005128f0f7ca3446Christopher Wiley  using namespace ::android::aidl::test_data::string_constants;
143d6bdd8d7cfb0a96ccc826cbb005128f0f7ca3446Christopher Wiley
144d6bdd8d7cfb0a96ccc826cbb005128f0f7ca3446Christopher Wiley  const string input_path = CanonicalNameToPath(kCanonicalName, ".aidl");
145d6bdd8d7cfb0a96ccc826cbb005128f0f7ca3446Christopher Wiley  const string output_file = kJavaOutputPath;
146d6bdd8d7cfb0a96ccc826cbb005128f0f7ca3446Christopher Wiley  const size_t argc = 4;
147d6bdd8d7cfb0a96ccc826cbb005128f0f7ca3446Christopher Wiley  const char* cmdline[argc + 1] = {
148d6bdd8d7cfb0a96ccc826cbb005128f0f7ca3446Christopher Wiley    "aidl",
149d6bdd8d7cfb0a96ccc826cbb005128f0f7ca3446Christopher Wiley    "-b",
150d6bdd8d7cfb0a96ccc826cbb005128f0f7ca3446Christopher Wiley    input_path.c_str(),
151d6bdd8d7cfb0a96ccc826cbb005128f0f7ca3446Christopher Wiley    output_file.c_str(),
152d6bdd8d7cfb0a96ccc826cbb005128f0f7ca3446Christopher Wiley    nullptr,
153d6bdd8d7cfb0a96ccc826cbb005128f0f7ca3446Christopher Wiley};
154d6bdd8d7cfb0a96ccc826cbb005128f0f7ca3446Christopher Wiley  auto options = JavaOptions::Parse(argc, cmdline);
155d6bdd8d7cfb0a96ccc826cbb005128f0f7ca3446Christopher Wiley
156d6bdd8d7cfb0a96ccc826cbb005128f0f7ca3446Christopher Wiley  // Load up our fake file system with data.
157d6bdd8d7cfb0a96ccc826cbb005128f0f7ca3446Christopher Wiley  io_delegate_.SetFileContents(input_path, kInterfaceDefinition);
158d6bdd8d7cfb0a96ccc826cbb005128f0f7ca3446Christopher Wiley
159d6bdd8d7cfb0a96ccc826cbb005128f0f7ca3446Christopher Wiley  // Check that we parse correctly.
160d6bdd8d7cfb0a96ccc826cbb005128f0f7ca3446Christopher Wiley  EXPECT_EQ(android::aidl::compile_aidl_to_java(*options, io_delegate_), 0);
161d6bdd8d7cfb0a96ccc826cbb005128f0f7ca3446Christopher Wiley  CheckFileContents(kJavaOutputPath, kExpectedJavaOutput);
162d6bdd8d7cfb0a96ccc826cbb005128f0f7ca3446Christopher Wiley}
163d6bdd8d7cfb0a96ccc826cbb005128f0f7ca3446Christopher Wiley
1644427d8654985209c1ebf2fb4705bb3e1a4262be2Christopher Wiley}  // namespace android
1654427d8654985209c1ebf2fb4705bb3e1a4262be2Christopher Wiley}  // namespace aidl
166