Home | History | Annotate | Download | only in functional
      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 #include <functional>
     18 #include <map>
     19 #include <string>
     20 
     21 #include <android/hidl/base/1.0/IBase.h>
     22 #include <android/hidl/manager/1.0/IServiceManager.h>
     23 #include <gtest/gtest.h>
     24 #include <hidl/HidlBinderSupport.h>
     25 #include <hidl/ServiceManagement.h>
     26 
     27 using android::hardware::hidl_array;
     28 using android::hardware::hidl_death_recipient;
     29 using android::hardware::hidl_handle;
     30 using android::hardware::hidl_string;
     31 using android::hardware::hidl_vec;
     32 using android::hardware::IBinder;
     33 using android::hardware::toBinder;
     34 using android::hidl::base::V1_0::IBase;
     35 using android::hidl::manager::V1_0::IServiceManager;
     36 using android::sp;
     37 using android::wp;
     38 
     39 template <typename T>
     40 static inline ::testing::AssertionResult isOk(const ::android::hardware::Return<T>& ret) {
     41     return ret.isOk() ? (::testing::AssertionSuccess() << ret.description())
     42                       : (::testing::AssertionFailure() << ret.description());
     43 }
     44 #define ASSERT_OK(__ret__) ASSERT_TRUE(isOk(__ret__))
     45 #define EXPECT_OK(__ret__) EXPECT_TRUE(isOk(__ret__))
     46 
     47 struct Hal {
     48     sp<IBase> service;
     49     std::string name;  // space separated list of android.hidl.foo (at) 1.0::IFoo/instance-name
     50 };
     51 
     52 class VtsHalBaseV1_0TargetTest : public ::testing::Test {
     53    public:
     54     virtual void SetUp() override {
     55         default_manager_ = ::android::hardware::defaultServiceManager();
     56 
     57         ASSERT_NE(default_manager_, nullptr)
     58             << "Failed to get default service manager." << std::endl;
     59 
     60         ASSERT_OK(default_manager_->list([&](const auto& list) {
     61             for (const auto& name : list) {
     62                 const std::string strName = name;
     63                 auto loc = strName.find_first_of('/');
     64                 if (loc == std::string::npos) {
     65                     ADD_FAILURE() << "Invalid FQName: " << strName;
     66                     continue;
     67                 }
     68                 const std::string fqName = strName.substr(0, loc);
     69                 const std::string instance = strName.substr(loc + 1);
     70 
     71                 sp<IBase> service = default_manager_->get(fqName, instance);
     72                 if (service == nullptr) {
     73                     ADD_FAILURE() << "Null service for " << name << " " << fqName << " "
     74                                   << instance;
     75                     continue;
     76                 }
     77 
     78                 sp<IBinder> binder = toBinder(service);
     79                 if (binder == nullptr) {
     80                     ADD_FAILURE() << "Null binder for " << name;
     81                     continue;
     82                 }
     83 
     84                 auto iter = all_hals_.find(binder);
     85                 if (iter != all_hals_.end()) {
     86                     // include all the names this is registered as for error messages
     87                     iter->second.name += " " + strName;
     88                 } else {
     89                     all_hals_.insert(iter, {binder, Hal{service, strName}});
     90                 }
     91             }
     92         }));
     93 
     94         ASSERT_FALSE(all_hals_.empty());  // sanity
     95     }
     96 
     97     void EachHal(const std::function<void(const Hal&)>& check) {
     98         for (auto iter = all_hals_.begin(); iter != all_hals_.end(); ++iter) {
     99             check(iter->second);
    100         }
    101     }
    102 
    103     // default service manager
    104     sp<IServiceManager> default_manager_;
    105 
    106     // map from underlying instance to actual instance
    107     //
    108     // this prevents calling the same service twice since the same service
    109     // will get registered multiple times for its entire inheritance
    110     // hierarchy (or perhaps as different instance names)
    111     std::map<sp<IBinder>, Hal> all_hals_;
    112 };
    113 
    114 TEST_F(VtsHalBaseV1_0TargetTest, CanPing) {
    115     EachHal(
    116         [&](const Hal& base) { EXPECT_OK(base.service->ping()) << "Cannot ping " << base.name; });
    117 }
    118 
    119 TEST_F(VtsHalBaseV1_0TargetTest, InterfaceChain) {
    120     EachHal([&](const Hal& base) {
    121         EXPECT_OK(base.service->interfaceChain([&](const auto& interfaceChain) {
    122             // must include IBase + subclasses
    123             EXPECT_GT(interfaceChain.size(), 1u) << "Invalid instance name " << base.name;
    124         })) << base.name;
    125     });
    126 }
    127 
    128 TEST_F(VtsHalBaseV1_0TargetTest, Descriptor) {
    129     EachHal([&](const Hal& base) {
    130         EXPECT_OK(base.service->interfaceDescriptor([&](const auto& descriptor) {
    131             // must include IBase + subclasses
    132             EXPECT_GT(descriptor.size(), 0u) << base.name;
    133             EXPECT_NE(IBase::descriptor, descriptor) << base.name;
    134         })) << base.name;
    135     });
    136 }
    137 
    138 TEST_F(VtsHalBaseV1_0TargetTest, Death) {
    139     struct HidlDeathRecipient : hidl_death_recipient {
    140         virtual void serviceDied(uint64_t /* cookie */, const wp<IBase>& /* who */){};
    141     };
    142     sp<hidl_death_recipient> recipient = new HidlDeathRecipient;
    143 
    144     EachHal([&](const Hal& base) {
    145         EXPECT_OK(base.service->linkToDeath(recipient, 0 /* cookie */))
    146             << "Register death recipient " << base.name;
    147         EXPECT_OK(base.service->unlinkToDeath(recipient)) << "Unlink death recipient " << base.name;
    148     });
    149 }
    150 
    151 TEST_F(VtsHalBaseV1_0TargetTest, Debug) {
    152     EachHal([&](const Hal& base) {
    153         // normally one is passed, but this is tested by dumpstate
    154         EXPECT_OK(base.service->debug(hidl_handle(), {}))
    155             << "Handle empty debug handle " << base.name;
    156     });
    157 }
    158 
    159 TEST_F(VtsHalBaseV1_0TargetTest, HashChain) {
    160     EachHal([&](const Hal& base) {
    161         EXPECT_OK(base.service->getHashChain([&](const auto& hashChain) {
    162             // must include IBase + subclasses
    163             EXPECT_NE(0u, hashChain.size()) << "Invalid hash chain " << base.name;
    164         })) << base.name;
    165     });
    166 }
    167 
    168 int main(int argc, char** argv) {
    169     ::testing::InitGoogleTest(&argc, argv);
    170     return RUN_ALL_TESTS();
    171 }
    172