aidl_test_client_primitives.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 "aidl_test_client_primitives.h"
  17. #include <iostream>
  18. #include <vector>
  19. #include <utils/String16.h>
  20. #include <utils/String8.h>
  21. #include <binder/Value.h>
  22. #include <binder/Map.h>
  23. #include "android/aidl/tests/INamedCallback.h"
  24. #include "test_helpers.h"
  25. // libutils:
  26. using android::sp;
  27. using android::String16;
  28. using android::String8;
  29. // libbinder:
  30. using android::binder::Status;
  31. using android::binder::Value;
  32. using android::binder::Map;
  33. // generated
  34. using android::aidl::tests::ITestService;
  35. using android::aidl::tests::INamedCallback;
  36. using std::cerr;
  37. using std::cout;
  38. using std::endl;
  39. using std::vector;
  40. namespace android {
  41. namespace aidl {
  42. namespace tests {
  43. namespace client {
  44. bool ConfirmPrimitiveRepeat(const sp<ITestService>& s) {
  45. cout << "Confirming passing and returning primitives works." << endl;
  46. Map test_map;
  47. test_map["first_val"] = int8_t{-128};
  48. test_map["second_val"] = int32_t{1 << 30};
  49. test_map["third_val"] = String16("OHAI");
  50. if (!RepeatPrimitive(s, &ITestService::RepeatBoolean, true) ||
  51. !RepeatPrimitive(s, &ITestService::RepeatByte, int8_t{-128}) ||
  52. !RepeatPrimitive(s, &ITestService::RepeatChar, char16_t{'A'}) ||
  53. !RepeatPrimitive(s, &ITestService::RepeatInt, int32_t{1 << 30}) ||
  54. !RepeatPrimitive(s, &ITestService::RepeatLong, int64_t{1LL << 60}) ||
  55. !RepeatPrimitive(s, &ITestService::RepeatFloat, float{1.0f/3.0f}) ||
  56. !RepeatPrimitive(s, &ITestService::RepeatDouble, double{1.0/3.0}) ||
  57. !RepeatPrimitive(s, &ITestService::RepeatMap, test_map) ||
  58. !RepeatPrimitive(
  59. s, &ITestService::RepeatInt, ITestService::TEST_CONSTANT) ||
  60. !RepeatPrimitive(
  61. s, &ITestService::RepeatInt, ITestService::TEST_CONSTANT2) ||
  62. !RepeatPrimitive(
  63. s, &ITestService::RepeatInt, ITestService::TEST_CONSTANT3) ||
  64. !RepeatPrimitive(
  65. s, &ITestService::RepeatInt, ITestService::TEST_CONSTANT4) ||
  66. !RepeatPrimitive(
  67. s, &ITestService::RepeatInt, ITestService::TEST_CONSTANT5) ||
  68. !RepeatPrimitive(
  69. s, &ITestService::RepeatInt, ITestService::TEST_CONSTANT6) ||
  70. !RepeatPrimitive(
  71. s, &ITestService::RepeatInt, ITestService::TEST_CONSTANT7) ||
  72. !RepeatPrimitive(
  73. s, &ITestService::RepeatInt, ITestService::TEST_CONSTANT8) ||
  74. !RepeatPrimitive(
  75. s, &ITestService::RepeatInt, ITestService::TEST_CONSTANT9) ||
  76. !RepeatPrimitive(
  77. s, &ITestService::RepeatInt, ITestService::TEST_CONSTANT10) ||
  78. !RepeatPrimitive(
  79. s, &ITestService::RepeatInt, ITestService::TEST_CONSTANT11) ||
  80. !RepeatPrimitive(
  81. s, &ITestService::RepeatInt, ITestService::TEST_CONSTANT12)
  82. ) {
  83. return false;
  84. }
  85. vector<String16> inputs = {
  86. String16("Deliver us from evil."),
  87. String16(),
  88. String16("\0\0", 2),
  89. // This is actually two unicode code points:
  90. // U+10437: The 'small letter yee' character in the deseret alphabet
  91. // U+20AC: A euro sign
  92. String16("\xD8\x01\xDC\x37\x20\xAC"),
  93. ITestService::STRING_TEST_CONSTANT(),
  94. ITestService::STRING_TEST_CONSTANT2(),
  95. };
  96. for (const auto& input : inputs) {
  97. String16 reply;
  98. Status status = s->RepeatString(input, &reply);
  99. if (!status.isOk() || input != reply) {
  100. cerr << "Failed while requesting service to repeat String16=\""
  101. << String8(input).string()
  102. << "\". Got status=" << status.toString8() << endl;
  103. return false;
  104. }
  105. }
  106. return true;
  107. }
  108. bool ConfirmReverseArrays(const sp<ITestService>& s) {
  109. cout << "Confirming passing and returning arrays works." << endl;
  110. if (!ReverseArray(s, &ITestService::ReverseBoolean,
  111. {true, false, false}) ||
  112. !ReverseArray(s, &ITestService::ReverseByte,
  113. {uint8_t{255}, uint8_t{0}, uint8_t{127}}) ||
  114. !ReverseArray(s, &ITestService::ReverseChar,
  115. {char16_t{'A'}, char16_t{'B'}, char16_t{'C'}}) ||
  116. !ReverseArray(s, &ITestService::ReverseInt,
  117. {1, 2, 3}) ||
  118. !ReverseArray(s, &ITestService::ReverseLong,
  119. {-1LL, 0LL, int64_t{1LL << 60}}) ||
  120. !ReverseArray(s, &ITestService::ReverseFloat,
  121. {-0.3f, -0.7f, 8.0f}) ||
  122. !ReverseArray(s, &ITestService::ReverseDouble,
  123. {1.0/3.0, 1.0/7.0, 42.0}) ||
  124. !ReverseArray(s, &ITestService::ReverseString,
  125. {String16{"f"}, String16{"a"}, String16{"b"}})) {
  126. return false;
  127. }
  128. return true;
  129. }
  130. bool ConfirmReverseLists(const sp<ITestService>& s) {
  131. cout << "Confirming passing and returning List<T> works." << endl;
  132. if (!ReverseArray(s, &ITestService::ReverseStringList,
  133. {String16{"f"}, String16{"a"}, String16{"b"}})) {
  134. return false;
  135. }
  136. return true;
  137. }
  138. bool ConfirmReverseBinderLists(const sp<ITestService>& s) {
  139. Status status;
  140. cout << "Confirming passing and returning List<T> works with binders." << endl;
  141. vector<String16> names = {
  142. String16{"Larry"},
  143. String16{"Curly"},
  144. String16{"Moe"}
  145. };
  146. vector<sp<IBinder>> input;
  147. for (int i = 0; i < 3; i++) {
  148. sp<INamedCallback> got;
  149. status = s->GetOtherTestService(names[i], &got);
  150. if (!status.isOk()) {
  151. cerr << "Could not retrieve service for test." << endl;
  152. return false;
  153. }
  154. input.push_back(INamedCallback::asBinder(got));
  155. }
  156. vector<sp<IBinder>> output;
  157. vector<sp<IBinder>> reversed;
  158. status = s->ReverseNamedCallbackList(input, &output, &reversed);
  159. if (!status.isOk()) {
  160. cerr << "Failed to reverse named callback list." << endl;
  161. }
  162. if (output.size() != 3) {
  163. cerr << "ReverseNamedCallbackList gave repetition with wrong length." << endl;
  164. return false;
  165. }
  166. if (reversed.size() != 3) {
  167. cerr << "ReverseNamedCallbackList gave reversal with wrong length." << endl;
  168. return false;
  169. }
  170. for (int i = 0; i < 3; i++) {
  171. String16 ret;
  172. sp<INamedCallback> named_callback =
  173. android::interface_cast<INamedCallback>(output[i]);
  174. status = named_callback->GetName(&ret);
  175. if (!status.isOk()) {
  176. cerr << "Could not query INamedCallback from output" << endl;
  177. return false;
  178. }
  179. if (ret != names[i]) {
  180. cerr << "Output had wrong INamedCallback" << endl;
  181. return false;
  182. }
  183. }
  184. for (int i = 0; i < 3; i++) {
  185. String16 ret;
  186. sp<INamedCallback> named_callback =
  187. android::interface_cast<INamedCallback>(reversed[i]);
  188. status = named_callback->GetName(&ret);
  189. if (!status.isOk()) {
  190. cerr << "Could not query INamedCallback from reversed output" << endl;
  191. return false;
  192. }
  193. if (ret != names[2 - i]) {
  194. cerr << "Reversed output had wrong INamedCallback" << endl;
  195. return false;
  196. }
  197. }
  198. return true;
  199. }
  200. } // namespace client
  201. } // namespace tests
  202. } // namespace aidl
  203. } // namespace android