end_to_end_tests.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. #include <memory>
  17. #include <string>
  18. #include <vector>
  19. #include <android-base/logging.h>
  20. #include <gtest/gtest.h>
  21. #include "aidl.h"
  22. #include "options.h"
  23. #include "tests/fake_io_delegate.h"
  24. #include "tests/test_data.h"
  25. #include "tests/test_util.h"
  26. using android::aidl::test::CanonicalNameToPath;
  27. using android::aidl::test::FakeIoDelegate;
  28. using std::string;
  29. using std::unique_ptr;
  30. using std::vector;
  31. namespace android {
  32. namespace aidl {
  33. class EndToEndTest : public ::testing::Test {
  34. protected:
  35. void SetUp() override {
  36. }
  37. void AddStubAidls(const char** parcelables, const char** interfaces,
  38. const char* cpp_header=nullptr) {
  39. for ( ; *parcelables; ++parcelables) {
  40. io_delegate_.AddStubParcelable(
  41. *parcelables, (cpp_header) ? cpp_header : "");
  42. }
  43. for ( ; *interfaces; ++interfaces) {
  44. io_delegate_.AddStubInterface(*interfaces);
  45. }
  46. }
  47. void CheckFileContents(const string& rel_path,
  48. const string& expected_content) {
  49. string actual_content;
  50. ASSERT_TRUE(io_delegate_.GetWrittenContents(rel_path, &actual_content))
  51. << "Expected aidl to write to " << rel_path << " but it did not.";
  52. if (actual_content == expected_content) {
  53. return; // success!
  54. }
  55. test::PrintDiff(expected_content, actual_content);
  56. FAIL() << "Actual contents of " << rel_path
  57. << " did not match expected content";
  58. }
  59. FakeIoDelegate io_delegate_;
  60. };
  61. TEST_F(EndToEndTest, IExampleInterface) {
  62. using namespace ::android::aidl::test_data::example_interface;
  63. vector<string> args = {
  64. "aidl",
  65. "-b",
  66. "-I .",
  67. "-d an/arbitrary/path/to/dep.P",
  68. CanonicalNameToPath(kCanonicalName, ".aidl"),
  69. kJavaOutputPath};
  70. Options options = Options::From(args);
  71. // Load up our fake file system with data.
  72. io_delegate_.SetFileContents(options.InputFiles().front(), kInterfaceDefinition);
  73. io_delegate_.AddCompoundParcelable("android.test.CompoundParcelable",
  74. {"Subclass1", "Subclass2"});
  75. AddStubAidls(kImportedParcelables, kImportedInterfaces);
  76. // Check that we parse correctly.
  77. EXPECT_EQ(android::aidl::compile_aidl(options, io_delegate_), 0);
  78. CheckFileContents(kJavaOutputPath, kExpectedJavaOutput);
  79. CheckFileContents(options.DependencyFile(), kExpectedJavaDepsOutput);
  80. }
  81. TEST_F(EndToEndTest, IExampleInterface_WithTrace) {
  82. using namespace ::android::aidl::test_data::example_interface;
  83. vector<string> args = {
  84. "aidl",
  85. "-b",
  86. "-I .",
  87. "-t", //trace
  88. "-d an/arbitrary/path/to/dep.P",
  89. CanonicalNameToPath(kCanonicalName, ".aidl"),
  90. kJavaOutputPath};
  91. Options options = Options::From(args);
  92. // Load up our fake file system with data.
  93. io_delegate_.SetFileContents(options.InputFiles().front(), kInterfaceDefinition);
  94. io_delegate_.AddCompoundParcelable("android.test.CompoundParcelable",
  95. {"Subclass1", "Subclass2"});
  96. AddStubAidls(kImportedParcelables, kImportedInterfaces);
  97. // Check that we parse correctly.
  98. EXPECT_EQ(android::aidl::compile_aidl(options, io_delegate_), 0);
  99. CheckFileContents(kJavaOutputPath, kExpectedJavaOutputWithTrace);
  100. CheckFileContents(options.DependencyFile(), kExpectedJavaDepsOutput);
  101. }
  102. TEST_F(EndToEndTest, IExampleInterface_WithTransactionNames) {
  103. using namespace ::android::aidl::test_data::example_interface;
  104. vector<string> args = {
  105. "aidl",
  106. "-b",
  107. "-I .",
  108. "--transaction_name", //trace
  109. "-d an/arbitrary/path/to/dep.P",
  110. CanonicalNameToPath(kCanonicalName, ".aidl"),
  111. kJavaOutputPath};
  112. Options options = Options::From(args);
  113. // Load up our fake file system with data.
  114. io_delegate_.SetFileContents(options.InputFiles().front(), kInterfaceDefinition);
  115. io_delegate_.AddCompoundParcelable("android.test.CompoundParcelable",
  116. {"Subclass1", "Subclass2"});
  117. AddStubAidls(kImportedParcelables, kImportedInterfaces);
  118. // Check that we parse correctly.
  119. EXPECT_EQ(android::aidl::compile_aidl(options, io_delegate_), 0);
  120. CheckFileContents(kJavaOutputPath, kExpectedJavaOutputWithTransactionNames);
  121. CheckFileContents(options.DependencyFile(), kExpectedJavaDepsOutput);
  122. }
  123. TEST_F(EndToEndTest, IExampleInterface_Outlining) {
  124. using namespace ::android::aidl::test_data::example_interface;
  125. vector<string> args = {
  126. "aidl",
  127. "-b",
  128. "-I .",
  129. "-d an/arbitrary/path/to/dep.P",
  130. CanonicalNameToPath(kCanonicalName, ".aidl"),
  131. kJavaOutputPath};
  132. Options options = Options::From(args);
  133. options.onTransact_outline_threshold_ = 4;
  134. options.onTransact_non_outline_count_ = 3;
  135. // Load up our fake file system with data.
  136. io_delegate_.SetFileContents(options.InputFiles().front(), kInterfaceDefinitionOutlining);
  137. io_delegate_.AddCompoundParcelable("android.test.CompoundParcelable",
  138. {"Subclass1", "Subclass2"});
  139. AddStubAidls(kImportedParcelables, kImportedInterfaces);
  140. // Check that we parse correctly.
  141. EXPECT_EQ(android::aidl::compile_aidl(options, io_delegate_), 0);
  142. CheckFileContents(kJavaOutputPath, kExpectedJavaOutputOutlining);
  143. CheckFileContents(options.DependencyFile(), kExpectedJavaDepsOutput);
  144. }
  145. TEST_F(EndToEndTest, IExampleInterface_WithVersion) {
  146. using namespace ::android::aidl::test_data::example_interface;
  147. vector<string> args = {
  148. "aidl",
  149. "-b",
  150. "-I .",
  151. "-d an/arbitrary/path/to/dep.P",
  152. "--version=10",
  153. CanonicalNameToPath(kCanonicalName, ".aidl"),
  154. kJavaOutputPath};
  155. Options options = Options::From(args);
  156. options.onTransact_outline_threshold_ = 4;
  157. options.onTransact_non_outline_count_ = 3;
  158. // Load up our fake file system with data.
  159. io_delegate_.SetFileContents(options.InputFiles().front(), kInterfaceDefinitionOutlining);
  160. io_delegate_.AddCompoundParcelable("android.test.CompoundParcelable",
  161. {"Subclass1", "Subclass2"});
  162. AddStubAidls(kImportedParcelables, kImportedInterfaces);
  163. // Check that we parse correctly.
  164. EXPECT_EQ(android::aidl::compile_aidl(options, io_delegate_), 0);
  165. CheckFileContents(kJavaOutputPath, kExpectedJavaOutputWithVersion);
  166. CheckFileContents(options.DependencyFile(), kExpectedJavaDepsOutput);
  167. }
  168. TEST_F(EndToEndTest, IPingResponderCpp) {
  169. using namespace ::android::aidl::test_data::ping_responder;
  170. vector<string> args = {
  171. "aidl-cpp",
  172. "-d deps.P",
  173. "-I .",
  174. CanonicalNameToPath(kCanonicalName, ".aidl"),
  175. kGenHeaderDir,
  176. kCppOutputPath};
  177. Options options = Options::From(args);
  178. // Set up input paths.
  179. io_delegate_.SetFileContents(CanonicalNameToPath(kCanonicalName, ".aidl"), kInterfaceDefinition);
  180. AddStubAidls(kImportedParcelables, kImportedInterfaces, kCppParcelableHeader);
  181. // Check that we parse and generate code correctly.
  182. EXPECT_EQ(android::aidl::compile_aidl(options, io_delegate_), 0);
  183. CheckFileContents(kCppOutputPath, kExpectedCppOutput);
  184. CheckFileContents(kGenInterfaceHeaderPath, kExpectedIHeaderOutput);
  185. CheckFileContents(kGenClientHeaderPath, kExpectedBpHeaderOutput);
  186. CheckFileContents(kGenServerHeaderPath, kExpectedBnHeaderOutput);
  187. CheckFileContents(options.DependencyFile(), kExpectedCppDepsOutput);
  188. }
  189. TEST_F(EndToEndTest, IPingResponderCpp_WithVersion) {
  190. using namespace ::android::aidl::test_data::ping_responder;
  191. vector<string> args = {
  192. "aidl-cpp",
  193. "-d deps.P",
  194. "-I .",
  195. "--version=10",
  196. CanonicalNameToPath(kCanonicalName, ".aidl"),
  197. kGenHeaderDir,
  198. kCppOutputPath};
  199. Options options = Options::From(args);
  200. // Set up input paths.
  201. io_delegate_.SetFileContents(CanonicalNameToPath(kCanonicalName, ".aidl"), kInterfaceDefinition);
  202. AddStubAidls(kImportedParcelables, kImportedInterfaces, kCppParcelableHeader);
  203. // Check that we parse and generate code correctly.
  204. EXPECT_EQ(android::aidl::compile_aidl(options, io_delegate_), 0);
  205. CheckFileContents(kCppOutputPath, kExpectedCppOutputWithVersion);
  206. CheckFileContents(kGenInterfaceHeaderPath, kExpectedIHeaderOutputWithVersion);
  207. CheckFileContents(kGenClientHeaderPath, kExpectedBpHeaderOutputWithVersion);
  208. CheckFileContents(kGenServerHeaderPath, kExpectedBnHeaderOutputWithVersion);
  209. CheckFileContents(options.DependencyFile(), kExpectedCppDepsOutput);
  210. }
  211. TEST_F(EndToEndTest, StringConstantsInCpp) {
  212. using namespace ::android::aidl::test_data::string_constants;
  213. vector<string> args = {
  214. "aidl-cpp",
  215. CanonicalNameToPath(kCanonicalName, ".aidl"),
  216. kGenHeaderDir,
  217. kCppOutputPath};
  218. Options options = Options::From(args);
  219. // Set up input paths.
  220. io_delegate_.SetFileContents(CanonicalNameToPath(kCanonicalName, ".aidl"), kInterfaceDefinition);
  221. // Check that we parse and generate code correctly.
  222. EXPECT_EQ(android::aidl::compile_aidl(options, io_delegate_), 0);
  223. CheckFileContents(kCppOutputPath, kExpectedCppOutput);
  224. CheckFileContents(kGenInterfaceHeaderPath, kExpectedIHeaderOutput);
  225. }
  226. TEST_F(EndToEndTest, StringConstantsInJava) {
  227. using namespace ::android::aidl::test_data::string_constants;
  228. vector<string> args = {
  229. "aidl",
  230. "-b",
  231. CanonicalNameToPath(kCanonicalName, ".aidl"),
  232. kJavaOutputPath};
  233. Options options = Options::From(args);
  234. // Load up our fake file system with data.
  235. io_delegate_.SetFileContents(CanonicalNameToPath(kCanonicalName, ".aidl"), kInterfaceDefinition);
  236. // Check that we parse correctly.
  237. EXPECT_EQ(android::aidl::compile_aidl(options, io_delegate_), 0);
  238. CheckFileContents(kJavaOutputPath, kExpectedJavaOutput);
  239. }
  240. TEST_F(EndToEndTest, StringConstantsInCpp_WithVersion) {
  241. using namespace ::android::aidl::test_data::string_constants;
  242. vector<string> args = {
  243. "aidl-cpp",
  244. "--version=10",
  245. CanonicalNameToPath(kCanonicalName, ".aidl"),
  246. kGenHeaderDir,
  247. kCppOutputPath};
  248. Options options = Options::From(args);
  249. // Set up input paths.
  250. io_delegate_.SetFileContents(CanonicalNameToPath(kCanonicalName, ".aidl"), kInterfaceDefinition);
  251. // Check that we parse and generate code correctly.
  252. EXPECT_EQ(android::aidl::compile_aidl(options, io_delegate_), 0);
  253. CheckFileContents(kCppOutputPath, kExpectedCppOutputWithVersion);
  254. CheckFileContents(kGenInterfaceHeaderPath, kExpectedIHeaderOutputWithVersion);
  255. }
  256. TEST_F(EndToEndTest, StringConstantsInJava_WithVersion) {
  257. using namespace ::android::aidl::test_data::string_constants;
  258. vector<string> args = {
  259. "aidl",
  260. "-b",
  261. "--version=10",
  262. CanonicalNameToPath(kCanonicalName, ".aidl"),
  263. kJavaOutputPath};
  264. Options options = Options::From(args);
  265. // Load up our fake file system with data.
  266. io_delegate_.SetFileContents(CanonicalNameToPath(kCanonicalName, ".aidl"), kInterfaceDefinition);
  267. // Check that we parse correctly.
  268. EXPECT_EQ(android::aidl::compile_aidl(options, io_delegate_), 0);
  269. CheckFileContents(kJavaOutputPath, kExpectedJavaOutputWithVersion);
  270. }
  271. } // namespace android
  272. } // namespace aidl