1/* 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16#include "se_impl.h" 17#include <hdf_base.h> 18#include <hdf_log.h> 19#include <vector> 20#include <gtest/gtest.h> 21#include <hdf_device_desc.h> 22#include <hdf_sbuf_ipc.h> 23#include "v1_0/infc_interface.h" 24#include "v1_0/nfc_types.h" 25#include "v1_0/isecure_element_interface.h" 26#include "v1_0/secure_element_types.h" 27#include "v1_0/isecure_element_callback.h" 28#include "nfc_chip_type_parser.h" 29#include "common_utils.h" 30 31using namespace OHOS::HDI::SecureElement::V1_0; 32using namespace testing::ext; 33using namespace std; 34using namespace OHOS::HDI::Nfc; 35using ISeHdiV1_0 = OHOS::HDI::SecureElement::V1_0::ISecureElementInterface; 36using OHOS::HDI::SecureElement::V1_0::SecureElementStatus; 37using OHOS::HDI::SecureElement::V1_0::ISecureElementCallback; 38using namespace OHOS::NFC; 39using namespace OHOS::SE; 40std::vector<uint8_t> vecCommand = {128, 202, 159, 127, 0}; 41std::recursive_mutex mLock_{}; 42 43class SeClientCallback : public ISecureElementCallback { 44public: 45 explicit SeClientCallback() { 46 } 47 48 int32_t OnSeStateChanged(bool connected) override 49 { 50 return HDF_SUCCESS; 51 } 52}; 53 54OHOS::sptr<ISeHdiV1_0> mSeHdi_ = nullptr; 55OHOS::sptr<ISecureElementCallback> mSeHdiCallback = nullptr; 56 57class HdfNfcHdiTest : public testing::Test { 58public: 59 static void SetUpTestCase(); 60 static void TearDownTestCase(); 61 void SetUp(); 62 void TearDown(); 63}; 64void HdfNfcHdiTest::SetUpTestCase() 65{ 66 mSeHdi_ = ISeHdiV1_0::Get(); 67 mSeHdiCallback = new SeClientCallback(); 68} 69void HdfNfcHdiTest::TearDownTestCase() 70{ 71} 72void HdfNfcHdiTest::SetUp() 73{ 74} 75void HdfNfcHdiTest::TearDown() 76{ 77} 78 79/** 80* @tc.name: SUB_DriverSystem_Hdinfcopen_0100 81* @tc.desc: Enables the nfc controller and initialize the nfc core. 82* @tc.type: FUNC 83*/ 84HWTEST_F(HdfNfcHdiTest, SUB_DriverSystem_secureElement_0100, TestSize.Level2) 85{ 86 if (!NfcChipTypeParser::IsSupportedChipType()) { 87 EXPECT_EQ(HDF_SUCCESS, 0); 88 } 89 else { 90 SecureElementStatus status = SecureElementStatus::SE_GENERAL_ERROR; 91 int32_t ret = mSeHdi_->init(mSeHdiCallback, status); 92 if (ret != HDF_SUCCESS) { 93 printf("SeTerminal Error Initialize secure element hdf ret = %d", ret); 94 } 95 if (status != SecureElementStatus::SE_SUCCESS) { 96 EXPECT_EQ(HDF_SUCCESS, 0); 97 } 98 } 99} 100 101/** 102* @tc.name: SUB_DriverSystem_HdinfcCoreInitialized_0200 103* @tc.desc: Configures the nfc chip after initializing the nfc core. 104* @tc.type: FUNC 105*/ 106HWTEST_F(HdfNfcHdiTest, SUB_DriverSystem_secureElement_0200, TestSize.Level2) 107{ 108 if (!NfcChipTypeParser::IsSupportedChipType()) { 109 printf("SeTerminal SUB_DriverSystem_secureElement_0200 1 "); 110 EXPECT_EQ(HDF_SUCCESS, 0); 111 } 112 else { 113 if (mSeHdi_ == nullptr) { 114 printf("SeTerminal SUB_DriverSystem_secureElement_0200 2"); 115 ASSERT_NE(nullptr, mSeHdi_); 116 return; 117 } 118 std::vector<uint8_t> vecResponse(MAX_APDU_RESP_BYTES, 0); 119 int32_t ret = mSeHdi_->getAtr(vecResponse); 120 printf("SeTerminal SUB_DriverSystem_secureElement_0200 = %d", ret); 121 EXPECT_EQ(0, ret); 122 } 123} 124 125/** 126* @tc.name: SUB_DriverSystem_HdinfcPrediscover_0300 127* @tc.desc: Specifically configures the nfc chip before starting RF discovering. 128* @tc.type: FUNC 129*/ 130HWTEST_F(HdfNfcHdiTest, SUB_DriverSystem_secureElement_0300, TestSize.Level2) 131{ 132 if (!NfcChipTypeParser::IsSupportedChipType()) { 133 printf("SeTerminal SUB_DriverSystem_secureElement_0300 1 "); 134 EXPECT_EQ(HDF_SUCCESS, 0); 135 } 136 else { 137 if (mSeHdi_ == nullptr) { 138 printf("SeTerminal SUB_DriverSystem_secureElement_0300 2 "); 139 ASSERT_NE(nullptr, mSeHdi_); 140 return; 141 } 142 bool present = false; 143 int32_t ret = mSeHdi_->isSecureElementPresent(present); 144 printf("SeTerminal SUB_DriverSystem_secureElement_0300 = %d", ret); 145 EXPECT_EQ(0, ret); 146 } 147} 148 149/** 150* @tc.name: SUB_DriverSystem_HdinfcWrite_0400 151* @tc.desc: Writes NCI data to the nfc core. 152* @tc.type: FUNC 153*/ 154HWTEST_F(HdfNfcHdiTest, SUB_DriverSystem_secureElement_0400, TestSize.Level2) 155{ 156 if (!NfcChipTypeParser::IsSupportedChipType()) { 157 EXPECT_EQ(HDF_SUCCESS, 0); 158 } 159 else { 160 if (mSeHdi_ == nullptr) { 161 ASSERT_NE(nullptr, mSeHdi_); 162 return; 163 } 164 std::lock_guard<std::recursive_mutex> lock(mLock_); 165 SecureElementStatus status = SecureElementStatus::SE_GENERAL_ERROR; 166 uint8_t channelNumber = 0; 167 std::vector<uint8_t> vecAid; 168 int p2 = 0x00; 169 static const std::string aid = "A000000151000000"; 170 std::vector<uint8_t> vecResponse(MAX_APDU_RESP_BYTES, 0); 171 CommonUtils::HexStringToBytesArray(aid, vecAid); 172 int32_t ret = mSeHdi_->openLogicalChannel(vecAid, p2, vecResponse, channelNumber, status); 173 EXPECT_EQ(0, ret); 174 } 175} 176 177/** 178* @tc.name: SUB_DriverSystem_HdinfcControlGranted_0500 179* @tc.desc: Sets the HDF to allow to send NCI data. 180* @tc.type: FUNC 181*/ 182HWTEST_F(HdfNfcHdiTest, SUB_DriverSystem_secureElement_0500, TestSize.Level2) 183{ 184 if (!NfcChipTypeParser::IsSupportedChipType()) { 185 EXPECT_EQ(HDF_SUCCESS, 0); 186 } 187 else { 188 if (mSeHdi_ == nullptr) { 189 ASSERT_NE(nullptr, mSeHdi_); 190 return; 191 } 192 uint8_t channelNumber = 0; 193 SecureElementStatus status = SecureElementStatus::SE_GENERAL_ERROR; 194 int32_t ret = mSeHdi_->closeChannel(channelNumber, status); 195 EXPECT_EQ(0, ret); 196 } 197} 198 199/** 200* @tc.name: SUB_DriverSystem_HdinfcPowerCycle_0600 201* @tc.desc: Restarts the nfc controller according to each power cycle. 202* @tc.type: FUNC 203*/ 204HWTEST_F(HdfNfcHdiTest, SUB_DriverSystem_secureElement_0600, TestSize.Level2) 205{ 206 if (!NfcChipTypeParser::IsSupportedChipType()) { 207 EXPECT_EQ(HDF_SUCCESS, 0); 208 } 209 else { 210 if (mSeHdi_ == nullptr) { 211 ASSERT_NE(nullptr, mSeHdi_); 212 return; 213 } 214 std::lock_guard<std::recursive_mutex> lock(mLock_); 215 int p2 = 0x00; 216 static const std::string aid = "A000000151000000"; 217 std::vector<uint8_t> vecAid; 218 SecureElementStatus status = SecureElementStatus::SE_GENERAL_ERROR; 219 std::vector<uint8_t> vecResponse(MAX_APDU_RESP_BYTES, 0); 220 CommonUtils::HexStringToBytesArray(aid, vecAid); 221 int32_t ret = mSeHdi_->openBasicChannel(vecAid, p2, vecResponse, status); 222 EXPECT_EQ(0, ret); 223 } 224} 225 226/** 227* @tc.name: SUB_DriverSystem_HdinfcIoctl_0700 228* @tc.desc: Sends I/O control commands and data from the nfc stack to HDI. 229* @tc.type: FUNC 230*/ 231HWTEST_F(HdfNfcHdiTest, SUB_DriverSystem_secureElement_0700, TestSize.Level2) 232{ 233 if (!NfcChipTypeParser::IsSupportedChipType()) { 234 EXPECT_EQ(HDF_SUCCESS, 0); 235 } 236 else { 237 if (mSeHdi_ == nullptr) { 238 ASSERT_NE(nullptr, mSeHdi_); 239 return; 240 } 241 static const std::string aid = "A000000151000000"; 242 std::vector<uint8_t> vecCommand; 243 std::vector<uint8_t> vecResponse(MAX_APDU_RESP_BYTES, 0); 244 CommonUtils::HexStringToBytesArray(aid, vecCommand); 245 SecureElementStatus status = SecureElementStatus::SE_GENERAL_ERROR; 246 int32_t ret = mSeHdi_->transmit(vecCommand, vecResponse, status); 247 EXPECT_EQ(0, ret); 248 } 249} 250 251/** 252* @tc.name: SUB_DriverSystem_HdinfcClose_0800 253* @tc.desc: Disables the nfc controller and releases the resource. 254* @tc.type: FUNC 255*/ 256HWTEST_F(HdfNfcHdiTest, SUB_DriverSystem_secureElement_0800, TestSize.Level2) 257{ 258 if (!NfcChipTypeParser::IsSupportedChipType()) { 259 EXPECT_EQ(HDF_SUCCESS, 0); 260 } 261 else { 262 if (mSeHdi_ == nullptr) { 263 ASSERT_NE(nullptr, mSeHdi_); 264 return; 265 } 266 SecureElementStatus status = SecureElementStatus::SE_GENERAL_ERROR; 267 int32_t ret = mSeHdi_->reset(status); 268 EXPECT_EQ(0, ret); 269 } 270} 271 272/** 273* @tc.name: SUB_DriverSystem_secureElement_0900 274* @tc.desc: HdinfcType test 275* @tc.type: FUNC 276*/ 277HWTEST_F(HdfNfcHdiTest, SUB_DriverSystem_secureElement_0900, TestSize.Level2) 278{ 279 SecureElementStatus success = SecureElementStatus::SE_SUCCESS; 280 std::cout << "SecureElementStatus SE_SUCCESS = " << success << std::endl; 281 EXPECT_EQ(success, 0); 282 283 SecureElementStatus null_pointer_error = SecureElementStatus::SE_NULL_POINTER_ERROR; 284 std::cout << "SecureElementStatus SE_NULL_POINTER_ERROR = " << null_pointer_error << std::endl; 285 286 SecureElementStatus illegal_parameter_error = SecureElementStatus::SE_ILLEGAL_PARAMETER_ERROR; 287 std::cout << "SecureElementStatus SE_ILLEGAL_PARAMETER_ERROR = " << illegal_parameter_error << std::endl; 288 289 SecureElementStatus illegal_state_error = SecureElementStatus::SE_ILLEGAL_STATE_ERROR; 290 std::cout << "SecureElementStatus SE_ILLEGAL_STATE_ERROR = " << illegal_state_error << std::endl; 291 292 SecureElementStatus security_error = SecureElementStatus::SE_SECURITY_ERROR; 293 std::cout << "SecureElementStatus SE_SECURITY_ERROR = " << security_error << std::endl; 294 295 SecureElementStatus channel_not_available_error = SecureElementStatus::SE_CHANNEL_NOT_AVAILABLE_ERROR; 296 std::cout << "SecureElementStatus SE_CHANNEL_NOT_AVAILABLE_ERROR = " << channel_not_available_error << std::endl; 297 298 SecureElementStatus no_such_element_error = SecureElementStatus::SE_NO_SUCH_ELEMENT_ERROR; 299 std::cout << "SecureElementStatus SE_NO_SUCH_ELEMENT_ERROR = " << no_such_element_error << std::endl; 300 301 SecureElementStatus illegal_reference_error = SecureElementStatus::SE_ILLEGAL_REFERENCE_ERROR; 302 std::cout << "SecureElementStatus SE_ILLEGAL_REFERENCE_ERROR = " << illegal_reference_error << std::endl; 303 304 SecureElementStatus operation_not_supported_error = SecureElementStatus::SE_OPERATION_NOT_SUPPORTED_ERROR; 305 std::cout << "SecureElementStatus SE_OPERATION_NOT_SUPPORTED_ERROR = " << operation_not_supported_error << std::endl; 306 307 SecureElementStatus io_error = SecureElementStatus::SE_IO_ERROR; 308 std::cout << "SecureElementStatus SE_IO_ERROR = " << io_error << std::endl; 309 310 SecureElementStatus general_error = SecureElementStatus::SE_GENERAL_ERROR; 311 std::cout << "SecureElementStatus SE_GENERAL_ERROR = " << general_error << std::endl; 312}