Home | History | Annotate | Download | only in java_test
      1 /*
      2  * Copyright (C) 2017 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 //#define LOG_NDEBUG 0
     18 #define LOG_TAG "hidl_test_java_native"
     19 
     20 #include <android-base/logging.h>
     21 
     22 #include <android/hardware/tests/baz/1.0/IBaz.h>
     23 
     24 #include <hidl/LegacySupport.h>
     25 
     26 #include <gtest/gtest.h>
     27 
     28 #include <hidl/HidlTransportSupport.h>
     29 #include <hidl/Status.h>
     30 using ::android::sp;
     31 using ::android::hardware::tests::baz::V1_0::IBase;
     32 using ::android::hardware::tests::baz::V1_0::IBaz;
     33 using ::android::hardware::tests::baz::V1_0::IBazCallback;
     34 
     35 using ::android::hardware::hidl_array;
     36 using ::android::hardware::hidl_vec;
     37 using ::android::hardware::hidl_string;
     38 using ::android::hardware::configureRpcThreadpool;
     39 using ::android::hardware::defaultPassthroughServiceImplementation;
     40 using ::android::hardware::joinRpcThreadpool;
     41 using ::android::hardware::Return;
     42 using ::android::hardware::Void;
     43 
     44 struct BazCallback : public IBazCallback {
     45     Return<void> heyItsMe(const sp<IBazCallback> &cb) override;
     46     Return<void> hey() override;
     47 };
     48 
     49 Return<void> BazCallback::heyItsMe(
     50         const sp<IBazCallback> &cb) {
     51     LOG(INFO) << "SERVER: heyItsMe cb = " << cb.get();
     52 
     53     return Void();
     54 }
     55 
     56 Return<void> BazCallback::hey() {
     57     LOG(INFO) << "SERVER: hey";
     58 
     59     return Void();
     60 }
     61 
     62 using std::to_string;
     63 
     64 static void usage(const char *me) {
     65     fprintf(stderr, "%s [-c]lient | [-s]erver\n", me);
     66 }
     67 
     68 struct HidlEnvironment : public ::testing::Environment {
     69     void SetUp() override {
     70     }
     71 
     72     void TearDown() override {
     73     }
     74 };
     75 
     76 struct HidlTest : public ::testing::Test {
     77     sp<IBaz> baz;
     78 
     79     void SetUp() override {
     80         using namespace ::android::hardware;
     81 
     82         baz = IBaz::getService("baz");
     83 
     84         CHECK(baz != NULL);
     85     }
     86 
     87     void TearDown() override {
     88     }
     89 };
     90 
     91 template <typename T>
     92 static void EXPECT_OK(const ::android::hardware::Return<T> &ret) {
     93     EXPECT_TRUE(ret.isOk());
     94 }
     95 
     96 TEST_F(HidlTest, GetDescriptorTest) {
     97     EXPECT_OK(baz->interfaceDescriptor([&] (const auto &desc) {
     98         EXPECT_EQ(desc, IBaz::descriptor);
     99     }));
    100 }
    101 
    102 TEST_F(HidlTest, BazSomeBaseMethodTest) {
    103     EXPECT_OK(baz->someBaseMethod());
    104 }
    105 
    106 TEST_F(HidlTest, BazSomeOtherBaseMethodTest) {
    107     IBase::Foo foo;
    108     foo.x = 1;
    109     foo.y.z = 2.5;
    110     // A valid UTF-8 string
    111     foo.y.s = "Hello, world, \x46\x6F\x6F\x20\xC2\xA9\x20\x62\x61\x72\x20\xF0\x9D\x8C\x86\x20\x54\x72\x65\x62\x6C\x65\x20\xE2\x98\x83\x20\x72\x6F\x63\x6B\x73";
    112 
    113     foo.aaa.resize(5);
    114     for (size_t i = 0; i < foo.aaa.size(); ++i) {
    115         foo.aaa[i].z = 1.0f + (float)i * 0.01f;
    116         foo.aaa[i].s = ("Hello, world " + std::to_string(i)).c_str();
    117     }
    118 
    119     EXPECT_OK(
    120             baz->someOtherBaseMethod(
    121                 foo,
    122                 [&](const auto &result) {
    123                     // Strings should have the same size as they did before
    124                     // marshaling. b/35038064
    125                     EXPECT_EQ(result.y.s.size(), foo.y.s.size());
    126                     EXPECT_EQ(foo, result);
    127                }));
    128 }
    129 
    130 TEST_F(HidlTest, BazSomeMethodWithFooArraysTest) {
    131     hidl_array<IBase::Foo, 2> foo;
    132 
    133     foo[0].x = 1;
    134     foo[0].y.z = 2.5;
    135     foo[0].y.s = "Hello, world";
    136 
    137     foo[0].aaa.resize(5);
    138     for (size_t i = 0; i < foo[0].aaa.size(); ++i) {
    139         foo[0].aaa[i].z = 1.0f + (float)i * 0.01f;
    140         foo[0].aaa[i].s = ("Hello, world " + std::to_string(i)).c_str();
    141     }
    142 
    143     foo[1].x = 2;
    144     foo[1].y.z = -2.5;
    145     foo[1].y.s = "Morituri te salutant";
    146 
    147     foo[1].aaa.resize(3);
    148     for (size_t i = 0; i < foo[1].aaa.size(); ++i) {
    149         foo[1].aaa[i].z = 2.0f - (float)i * 0.01f;
    150         foo[1].aaa[i].s = ("Alea iacta est: " + std::to_string(i)).c_str();
    151     }
    152 
    153     hidl_array<IBaz::Foo, 2> fooExpectedOutput;
    154     fooExpectedOutput[0] = foo[1];
    155     fooExpectedOutput[1] = foo[0];
    156 
    157     EXPECT_OK(
    158             baz->someMethodWithFooArrays(
    159                 foo,
    160                 [&](const auto &result) {
    161                     EXPECT_EQ(result, fooExpectedOutput);
    162                }));
    163 }
    164 
    165 TEST_F(HidlTest, BazSomeMethodWithFooVectorsTest) {
    166     hidl_vec<IBase::Foo> foo;
    167     foo.resize(2);
    168 
    169     foo[0].x = 1;
    170     foo[0].y.z = 2.5;
    171     foo[0].y.s = "Hello, world";
    172 
    173     foo[0].aaa.resize(5);
    174     for (size_t i = 0; i < foo[0].aaa.size(); ++i) {
    175         foo[0].aaa[i].z = 1.0f + (float)i * 0.01f;
    176         foo[0].aaa[i].s = ("Hello, world " + std::to_string(i)).c_str();
    177     }
    178 
    179     foo[1].x = 2;
    180     foo[1].y.z = -2.5;
    181     foo[1].y.s = "Morituri te salutant";
    182 
    183     foo[1].aaa.resize(3);
    184     for (size_t i = 0; i < foo[1].aaa.size(); ++i) {
    185         foo[1].aaa[i].z = 2.0f - (float)i * 0.01f;
    186         foo[1].aaa[i].s = ("Alea iacta est: " + std::to_string(i)).c_str();
    187     }
    188 
    189     hidl_vec<IBaz::Foo> fooExpectedOutput;
    190     fooExpectedOutput.resize(2);
    191     fooExpectedOutput[0] = foo[1];
    192     fooExpectedOutput[1] = foo[0];
    193 
    194     EXPECT_OK(
    195             baz->someMethodWithFooVectors(
    196                 foo,
    197                 [&](const auto &result) {
    198                     EXPECT_EQ(result, fooExpectedOutput);
    199                 }));
    200 }
    201 
    202 TEST_F(HidlTest, BazSomeMethodWithVectorOfArray) {
    203     IBase::VectorOfArray in, expectedOut;
    204     in.addresses.resize(3);
    205     expectedOut.addresses.resize(3);
    206 
    207     size_t k = 0;
    208     const size_t n = in.addresses.size();
    209 
    210     for (size_t i = 0; i < n; ++i) {
    211         for (size_t j = 0; j < 6; ++j, ++k) {
    212             in.addresses[i][j] = k;
    213             expectedOut.addresses[n - 1 - i][j] = k;
    214         }
    215     }
    216 
    217     EXPECT_OK(
    218             baz->someMethodWithVectorOfArray(
    219                 in,
    220                 [&](const auto &out) {
    221                     EXPECT_EQ(expectedOut, out);
    222                 }));
    223 }
    224 
    225 TEST_F(HidlTest, BazSomeMethodTakingAVectorOfArray) {
    226     hidl_vec<hidl_array<uint8_t, 6> > in, expectedOut;
    227     in.resize(3);
    228     expectedOut.resize(3);
    229 
    230     size_t k = 0;
    231     const size_t n = in.size();
    232     for (size_t i = 0; i < n; ++i) {
    233         for (size_t j = 0; j < 6; ++j, ++k) {
    234             in[i][j] = k;
    235             expectedOut[n - 1 - i][j] = k;
    236         }
    237     }
    238 
    239     EXPECT_OK(
    240             baz->someMethodTakingAVectorOfArray(
    241                 in,
    242                 [&](const auto &out) {
    243                     EXPECT_EQ(expectedOut, out);
    244                 }));
    245 }
    246 
    247 static std::string numberToEnglish(int x) {
    248     static const char *const kDigits[] = {
    249         "zero",
    250         "one",
    251         "two",
    252         "three",
    253         "four",
    254         "five",
    255         "six",
    256         "seven",
    257         "eight",
    258         "nine",
    259     };
    260 
    261     if (x < 0) {
    262         return "negative " + numberToEnglish(-x);
    263     }
    264 
    265     if (x < 10) {
    266         return kDigits[x];
    267     }
    268 
    269     if (x <= 15) {
    270         static const char *const kSpecialTens[] = {
    271             "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
    272         };
    273 
    274         return kSpecialTens[x - 10];
    275     }
    276 
    277     if (x < 20) {
    278         return std::string(kDigits[x % 10]) + "teen";
    279     }
    280 
    281     if (x < 100) {
    282         static const char *const kDecades[] = {
    283             "twenty", "thirty", "forty", "fifty", "sixty", "seventy",
    284             "eighty", "ninety",
    285         };
    286 
    287         return std::string(kDecades[x / 10 - 2]) + kDigits[x % 10];
    288     }
    289 
    290     return "positively huge!";
    291 }
    292 
    293 TEST_F(HidlTest, BazTransposeTest) {
    294     IBase::StringMatrix5x3 in;
    295     IBase::StringMatrix3x5 expectedOut;
    296 
    297     for (int i = 0; i < 5; ++i) {
    298         for (int j = 0; j < 3; ++j) {
    299             in.s[i][j] = expectedOut.s[j][i] = numberToEnglish(3 * i + j + 1).c_str();
    300         }
    301     }
    302 
    303     EXPECT_OK(baz->transpose(
    304                 in,
    305                 [&](const auto &out) {
    306                     EXPECT_EQ(expectedOut, out);
    307                 }));
    308 }
    309 
    310 TEST_F(HidlTest, BazTranspose2Test) {
    311     hidl_array<hidl_string, 5, 3> in;
    312     hidl_array<hidl_string, 3, 5> expectedOut;
    313 
    314     for (int i = 0; i < 5; ++i) {
    315         for (int j = 0; j < 3; ++j) {
    316             in[i][j] = expectedOut[j][i] = numberToEnglish(3 * i + j + 1).c_str();
    317         }
    318     }
    319 
    320     EXPECT_OK(baz->transpose2(
    321                 in,
    322                 [&](const auto &out) {
    323                     EXPECT_EQ(expectedOut, out);
    324                 }));
    325 }
    326 
    327 TEST_F(HidlTest, BazSomeBoolMethodTest) {
    328     auto result = baz->someBoolMethod(true);
    329     EXPECT_OK(result);
    330     EXPECT_EQ(result, false);
    331 }
    332 
    333 TEST_F(HidlTest, BazSomeBoolArrayMethodTest) {
    334     hidl_array<bool, 3> someBoolArray;
    335     someBoolArray[0] = true;
    336     someBoolArray[1] = false;
    337     someBoolArray[2] = true;
    338 
    339     hidl_array<bool, 4> expectedOut;
    340     expectedOut[0] = false;
    341     expectedOut[1] = true;
    342     expectedOut[2] = false;
    343     expectedOut[3] = true;
    344 
    345     EXPECT_OK(
    346             baz->someBoolArrayMethod(
    347                 someBoolArray,
    348                 [&](const auto &result) {
    349                     EXPECT_EQ(expectedOut, result);
    350                 }));
    351 }
    352 
    353 TEST_F(HidlTest, BazSomeBoolVectorMethodTest) {
    354     hidl_vec<bool> someBoolVector, expected;
    355     someBoolVector.resize(4);
    356     expected.resize(4);
    357 
    358     for (size_t i = 0; i < someBoolVector.size(); ++i) {
    359         someBoolVector[i] = ((i & 1) == 0);
    360         expected[i] = !someBoolVector[i];
    361     }
    362 
    363     EXPECT_OK(
    364             baz->someBoolVectorMethod(
    365                 someBoolVector,
    366                 [&](const auto &result) {
    367                     EXPECT_EQ(expected, result);
    368                 }));
    369 }
    370 
    371 TEST_F(HidlTest, BazDoThisMethodTest) {
    372     EXPECT_OK(baz->doThis(1.0f));
    373 }
    374 
    375 TEST_F(HidlTest, BazDoThatAndReturnSomethingMethodTest) {
    376     auto result = baz->doThatAndReturnSomething(1);
    377     EXPECT_OK(result);
    378     EXPECT_EQ(result, 666);
    379 }
    380 
    381 TEST_F(HidlTest, BazDoQuiteABitMethodTest) {
    382     auto result = baz->doQuiteABit(1, 2ll, 3.0f, 4.0);
    383 
    384     EXPECT_OK(result);
    385     EXPECT_EQ(result, 666.5);
    386 }
    387 
    388 TEST_F(HidlTest, BazDoSomethingElseMethodTest) {
    389     hidl_array<int32_t, 15> param;
    390     hidl_array<int32_t, 32> expected;
    391 
    392     for (size_t i = 0; i < 15; ++i) {
    393         param[i] = expected[15 + i] = i;
    394         expected[i] = 2 * i;
    395     }
    396 
    397     expected[30] = 1;
    398     expected[31] = 2;
    399 
    400     EXPECT_OK(
    401             baz->doSomethingElse(
    402                 param,
    403                 [&](const auto &result) {
    404                     EXPECT_EQ(expected, result);
    405                 }));
    406 }
    407 
    408 TEST_F(HidlTest, BazDoStuffAndReturnAStringMethodTest) {
    409     std::string expected = "Hello, world!";
    410     EXPECT_OK(
    411             baz->doStuffAndReturnAString(
    412                 [&](const auto &result) {
    413                     EXPECT_EQ(expected, result);
    414                 }));
    415 }
    416 
    417 TEST_F(HidlTest, BazMapThisVectorMethodTest) {
    418     hidl_vec<int32_t> vec_param, expected;
    419     vec_param.resize(15);
    420     expected.resize(15);
    421 
    422     for (size_t i = 0; i < 15; ++i) {
    423         vec_param[i] = i;
    424         expected[i] = 2 * i;
    425     }
    426 
    427     EXPECT_OK(
    428             baz->mapThisVector(
    429                 vec_param,
    430                 [&](const auto &result) {
    431                     EXPECT_EQ(expected, result);
    432                 }));
    433 }
    434 
    435 TEST_F(HidlTest, BazCallMeMethodTest) {
    436     EXPECT_OK(baz->callMe(new BazCallback()));
    437 }
    438 
    439 TEST_F(HidlTest, BazCallMeLaterMethodTest) {
    440     EXPECT_OK(baz->callMeLater(new BazCallback()));
    441     EXPECT_OK(baz->iAmFreeNow());
    442 }
    443 
    444 TEST_F(HidlTest, BazUseAnEnumMethodTest) {
    445     auto result = baz->useAnEnum(IBaz::SomeEnum::bar);
    446 
    447     EXPECT_OK(result);
    448     EXPECT_TRUE(result == IBaz::SomeEnum::quux);
    449 }
    450 
    451 TEST_F(HidlTest, BazHaveSomeStringsMethodTest) {
    452     hidl_array<hidl_string, 3> string_params;
    453     string_params[0] = "one";
    454     string_params[1] = "two";
    455     string_params[2] = "three";
    456 
    457     hidl_array<hidl_string, 2> expected;
    458     expected[0] = "Hello";
    459     expected[1] = "World";
    460 
    461     EXPECT_OK(
    462             baz->haveSomeStrings(
    463                 string_params,
    464                 [&](const auto &result) {
    465                     EXPECT_EQ(expected, result);
    466                 }));
    467 }
    468 
    469 TEST_F(HidlTest, BazHaveAStringVecMethodTest) {
    470     hidl_vec<hidl_string> string_vec{ "Uno", "Dos", "Tres", "Cuatro" };
    471     hidl_vec<hidl_string> expected{"Hello", "World"};
    472 
    473     EXPECT_OK(
    474             baz->haveAStringVec(
    475                 string_vec,
    476                 [&](const auto &result) {
    477                     EXPECT_EQ(expected, result);
    478                 }));
    479 }
    480 
    481 TEST_F(HidlTest, BazReturnABunchOfStringsMethodTest) {
    482     std::string expectedA = "Eins";
    483     std::string expectedB = "Zwei";
    484     std::string expectedC = "Drei";
    485     EXPECT_OK(
    486             baz->returnABunchOfStrings(
    487                 [&](const auto &a, const auto &b, const auto &c) {
    488                     EXPECT_EQ(a, expectedA);
    489                     EXPECT_EQ(b, expectedB);
    490                     EXPECT_EQ(c, expectedC);
    491                 }));
    492 }
    493 
    494 int main(int argc, char **argv) {
    495     setenv("TREBLE_TESTING_OVERRIDE", "true", true);
    496 
    497     using namespace android::hardware;
    498 
    499     const char *me = argv[0];
    500 
    501     bool wantClient = false;
    502     bool wantServer = false;
    503 
    504     int res;
    505     while ((res = getopt(argc, argv, "chs")) >= 0) {
    506         switch (res) {
    507             case 'c':
    508             {
    509                 wantClient = true;
    510                 break;
    511             }
    512 
    513             case 's':
    514             {
    515                 wantServer = true;
    516                 break;
    517             }
    518 
    519             case '?':
    520             case 'h':
    521             default:
    522             {
    523                 usage(me);
    524                 exit(1);
    525                 break;
    526             }
    527         }
    528     }
    529 
    530     if ((!wantClient && !wantServer) || (wantClient && wantServer)) {
    531         usage(me);
    532         exit(1);
    533     }
    534 
    535     if (wantClient) {
    536         ::testing::AddGlobalTestEnvironment(new HidlEnvironment);
    537         ::testing::InitGoogleTest(&argc, argv);
    538         int status = RUN_ALL_TESTS();
    539         return status;
    540     }
    541 
    542     return defaultPassthroughServiceImplementation<IBaz>("baz");
    543 
    544 }
    545