Home | History | Annotate | Download | only in functional
      1 /*
      2  * Copyright (C) 2019 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 <string>
     18 
     19 #define LOG_TAG "secure_element_hidl_hal_test"
     20 #include <android-base/logging.h>
     21 
     22 #include <android/hardware/secure_element/1.0/types.h>
     23 #include <android/hardware/secure_element/1.1/ISecureElement.h>
     24 #include <android/hardware/secure_element/1.1/ISecureElementHalCallback.h>
     25 
     26 #include <VtsHalHidlTargetCallbackBase.h>
     27 #include <VtsHalHidlTargetTestBase.h>
     28 #include <VtsHalHidlTargetTestEnvBase.h>
     29 
     30 using ::android::sp;
     31 using ::android::hardware::hidl_string;
     32 using ::android::hardware::Return;
     33 using ::android::hardware::Void;
     34 using ::android::hardware::secure_element::V1_1::ISecureElement;
     35 using ::android::hardware::secure_element::V1_1::ISecureElementHalCallback;
     36 using ::testing::VtsHalHidlTargetTestEnvBase;
     37 
     38 constexpr char kCallbackNameOnStateChange[] = "onStateChange";
     39 
     40 class SecureElementCallbackArgs {
     41    public:
     42     bool state_;
     43     hidl_string reason_;
     44 };
     45 
     46 class SecureElementHalCallback
     47     : public ::testing::VtsHalHidlTargetCallbackBase<SecureElementCallbackArgs>,
     48       public ISecureElementHalCallback {
     49    public:
     50     virtual ~SecureElementHalCallback() = default;
     51 
     52     Return<void> onStateChange_1_1(bool state, const hidl_string& reason) override {
     53         SecureElementCallbackArgs args;
     54         args.state_ = state;
     55         args.reason_ = reason;
     56         NotifyFromCallback(kCallbackNameOnStateChange, args);
     57         return Void();
     58     };
     59 
     60     Return<void> onStateChange(__attribute__((unused)) bool state) override { return Void(); }
     61 };
     62 
     63 class SecureElementHidlEnvironment : public VtsHalHidlTargetTestEnvBase {
     64    public:
     65     // get the test environment singleton
     66     static SecureElementHidlEnvironment* Instance() {
     67         static SecureElementHidlEnvironment* instance = new SecureElementHidlEnvironment;
     68         return instance;
     69     }
     70 
     71     virtual void registerTestServices() override { registerTestService<ISecureElement>(); }
     72 
     73    private:
     74     SecureElementHidlEnvironment() {}
     75 
     76     GTEST_DISALLOW_COPY_AND_ASSIGN_(SecureElementHidlEnvironment);
     77 };
     78 
     79 class SecureElementHidlTest : public ::testing::VtsHalHidlTargetTestBase {
     80    public:
     81     virtual void SetUp() override {
     82         std::string serviceName =
     83                 SecureElementHidlEnvironment::Instance()->getServiceName<ISecureElement>("eSE1");
     84         LOG(INFO) << "get service with name:" << serviceName;
     85         ASSERT_FALSE(serviceName.empty());
     86         se_ = ::testing::VtsHalHidlTargetTestBase::getService<ISecureElement>(serviceName);
     87         ASSERT_NE(se_, nullptr);
     88 
     89         se_cb_ = new SecureElementHalCallback();
     90         ASSERT_NE(se_cb_, nullptr);
     91         se_->init_1_1(se_cb_);
     92         auto res = se_cb_->WaitForCallback(kCallbackNameOnStateChange);
     93         EXPECT_TRUE(res.no_timeout);
     94         EXPECT_TRUE(res.args->state_);
     95         EXPECT_NE(res.args->reason_, "");
     96     }
     97 
     98     sp<ISecureElement> se_;
     99     sp<SecureElementHalCallback> se_cb_;
    100 };
    101 
    102 /*
    103  * isCardPresent:
    104  * Expects the card to be present
    105  */
    106 TEST_F(SecureElementHidlTest, isCardPresent) {
    107     EXPECT_TRUE(se_->isCardPresent());
    108 }
    109 
    110 int main(int argc, char** argv) {
    111     ::testing::AddGlobalTestEnvironment(SecureElementHidlEnvironment::Instance());
    112     ::testing::InitGoogleTest(&argc, argv);
    113     SecureElementHidlEnvironment::Instance()->init(&argc, argv);
    114     int status = RUN_ALL_TESTS();
    115     return status;
    116 }
    117