SecurityDataHuksGenDelTest.cpp 29.2 KB
Newer Older
M
mamingshuai 已提交
1
/* Copyright (c) 2020-2021 Huawei Device Co., Ltd.
W
wenjun 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
*/

#include "SecurityDataHuks.h"
#include "hks_client.h"
#include "hks_types.h"
#include <securec.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <hks_file_api.h>
#include "gtest/gtest.h"
#include <stdarg.h>
#include <sys/stat.h>
#include <unistd.h>
#include "hks_errno.h"
#include <sys/time.h>
#include <hks_hardware_api.h>
using namespace std;
using namespace testing::ext;

M
mamingshuai 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
errno_t FopenS(FILE **fp, const char *filename, const char *modes)
{
    *fp = fopen(filename, modes);
    return 0;
}

int32_t FileSize(const char *filename)
{
    FILE *fp = nullptr;
    errno_t err;
    int32_t size;

    if (filename == nullptr) {
        return -1;
    }

    err = FopenS(&fp, filename, "rb");
    if (err != 0 || fp == nullptr) {
        return -1;
    }

    if (fseek(fp, 0L, SEEK_END) != 0) {
        fclose(fp);
        return -1;
    }

    size = ftell(fp);
    fclose(fp);

    return size;
}
int32_t FileWrite(const char *filename, uint32_t offset, const uint8_t *buf, uint32_t len)
{
    FILE *fp = nullptr;
    errno_t err;
    size_t size;

    if (filename == NULL || buf == NULL) {
        return -1;
    }

    err = FopenS(&fp, filename, "wb+");
    if (err != 0 || fp == nullptr) {
        return -1;
    }

    size = fwrite(buf, 1, len, fp);
    fclose(fp);

    if (size != len) {
        return -1;
    }

    return 0;
}
int32_t FileRead(const char *filename, uint32_t offset, uint8_t *buf, uint32_t len)
{
    FILE* fp = nullptr;
    errno_t err;
    size_t size;

    if (filename == NULL || buf == NULL) {
        return -1;
    }

    if (access(filename, 0) == -1) {
        return 0;
    }

    err = FopenS(&fp, filename, "rb");
    if (err == NUM2) {
        return 0;
    }
    if (err != 0 || fp == nullptr) {
        return -1;
    }

    size = fread(buf, 1, len, fp);
    fclose(fp);

    if (size == 0) {
        return -1;
    }

    return size;
}

uint64_t GetTimeMs()
{
    struct timeval timeVal;
    gettimeofday(&timeVal, nullptr);

    return (uint64_t)NUM1000000 * timeVal.tv_sec + timeVal.tv_usec;
}

void AddLog(const char* logType, const char *tag, const char *func, const char *format, const va_list* ap)
{
    char* buf = (char*)malloc(NUM2048);
    if (buf == nullptr) {
        return;
    }
    int offset = sprintf_s(buf, (NUM2048), "[%s][%llu]%s %s: ", logType, (unsigned long long)GetTimeMs(), tag, func);
    if (offset >= 0) {
        offset += vsprintf_s(buf + offset, (NUM2048) - offset, format, *ap);
    }
	
    free(buf);
    buf = nullptr;
}

void Logi(const char *tag, const char *func, const char *format, ...)
{
    va_list args;
    va_start(args, format);
    AddLog("Info", tag, func, format, &args);
    va_end(args);
}

void Logw(const char *tag, const char *func, const char *format, ...)
{
    va_list args;
    va_start(args, format);
    AddLog("Warn", tag, func, format, &args);
    va_end(args);
}

void Loge(const char *tag, const char *func, const char *format, ...)
{
    va_list args;
    va_start(args, format);
    AddLog("Error", tag, func, format, &args);
    va_end(args);
}

void Logd(const char *tag, const char *func, const char *format, ...)
{
    va_list args;
    va_start(args, format);
    AddLog("Debug", tag, func, format, &args);
    va_end(args);
}

uint8_t g_hksHardwareUdidId[32] = {
    0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
    0x09, 0x0A, 0x0C, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
    0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
    0x19, 0x1A, 0x1C, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
};

int32_t HksTestGetHardwareUdid(uint8_t* udid, uint32_t udidLen)
{
    int err;
    if ((udid == NULL) || (udidLen != NUM32)) {
        return -1;
    }

    int32_t rc = 1234567;
    if (rc <= 0) {
        err = memcpy_s(udid, NUM32, g_hksHardwareUdidId, NUM32);
        if (err !=  EOK) {
            return -1;
        }
    }

    char buf[128] = {0};
    uint32_t offset = 0;
    for (uint32_t i = 0; i < udidLen; ++i) {
        offset += sprintf_s(buf + offset, NUM128 - offset, "%02x ", udid[i]);
    }

    Logd("[hks_tester]", __func__, buf);
    return 0;
}

void HksStBlobInit1(struct hks_blob *blob, size_t nmemb, size_t size, uint8_t type)
{
    if (blob == nullptr || nmemb == 0 || size == 0) {
        EXPECT_EQ(0, 1);
        return;
    }
    blob->data = (uint8_t *)calloc(nmemb, size);
    if (blob->data == NULL) {
        EXPECT_EQ(0, 1);
        return;
    }
    if (memset_s(blob->data, size, 0, size) != EOK) {
        EXPECT_EQ(0, 1);
        return;
    }
    blob->size = size;
    blob->type = type;
}

void HksBlobDestroyT1(struct hks_blob *blob)
{
    if (blob == nullptr) {
        EXPECT_EQ(0, 1);
        return;
    }
    if (blob && blob->data) {
        if (memset_s(blob->data, blob->size, 0, blob->size) != EOK) {
        EXPECT_EQ(0, 1);
        return;
        }
        HKS_FREE_PTR1(blob->data);
    }
    blob->data = NULL;
    blob->size = 0;
    blob->type = HKS_BLOB_TYPE_RAW;
}

void HexStringToByte(const char *str, int nLen, unsigned char *pHex)
{
    unsigned int number = 4;
    if (nLen % NUM2) {
        EXPECT_EQ(0, 1);
        return;
    }
    int nHexLen = nLen / NUM2;
    unsigned char nibble[2];

    if (nHexLen >= MAX_INT) {
        return;
    }
    for (int i = 0; i < nHexLen; i++) {
        nibble[0] = str[i * NUM2];
        nibble[1] = str[i * NUM2 + NUM1];
        for (int j = 0; j < NUM2; j++) {
            if (nibble[j] <= 'F' && nibble[j] >= 'A') {
                nibble[j] = nibble[j] - 'A' + NUM10;
            } else if (nibble[j] <= 'f' && nibble[j] >= 'a') {
                nibble[j] = nibble[j] - 'a' + NUM10;
            } else if (nibble[j] >= '0' && nibble[j] <= '9') {
                nibble[j] = nibble[j] - '0';
            } else {
                EXPECT_EQ(0, 1);
                return;
            }
        }
        pHex[i] = nibble[0] << number;
        pHex[i] |= nibble[1];
    }
}

void BuildBlobData(struct hks_blob *param, const char *str, uint8_t type, uint32_t size, uint8_t isDataNull)
{
    if (param == nullptr) {
        EXPECT_EQ(0, 1);
        return;
    }
    param->type = type;
    param->size = size;
    if (isDataNull == 1)
        param->data = NULL;
    else {
        if (size + NUM2 == 0) {
            EXPECT_EQ(0, 1);
            return;
        }
        unsigned char *buff = (unsigned char *)malloc(size + NUM2);
        if (buff == nullptr) {
            EXPECT_EQ(0, 1);
            return;
        }
        if (memset_s(buff, size + NUM2, 0, size + NUM2) != EOK) {
            EXPECT_EQ(0, 1);
            free(buff);
            buff = nullptr;
            return;
        }
        HexStringToByte(str, size * NUM2, buff);
        param->data = (uint8_t *)buff;
    }
}

W
wenjun 已提交
309 310 311 312 313 314 315
class SecurityDataHuksGenDelTestSuite : public testing::Test {
protected:
    // SetUpTestCase: Testsuit setup, run before 1st testcase
    static void SetUpTestCase(void) {}
    // TearDownTestCase: Testsuit teardown, run after last testcase
    static void TearDownTestCase(void) {}
    // Testcase setup
M
mamingshuai 已提交
316
    virtual void SetUp()
W
wenjun 已提交
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
    {
        int32_t status;
        struct hks_file_callbacks fileCallbacks;

        fileCallbacks.read = FileRead;
        fileCallbacks.write = FileWrite;
        fileCallbacks.file_size = FileSize;

        status = hks_register_file_callbacks(&fileCallbacks);
        EXPECT_EQ(0, status);

        struct hks_log_f_group logFunc;
        logFunc.log_info = Logi;
        logFunc.log_warn = Logw;
        logFunc.log_error = Loge;
        logFunc.log_debug = Logd;

        status = hks_register_log_interface(&logFunc);
        EXPECT_EQ(0, status);

        status = hks_register_get_hardware_udid_callback(HksTestGetHardwareUdid);

        EXPECT_EQ(0, status);

        status = hks_init();
        if (status != 0) {
            status = hks_refresh_key_info();
        }
        EXPECT_EQ(0, status);
    }
    // Testcase teardown
    virtual void TearDown() {}
};

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++generate key
M
mamingshuai 已提交
352
// begin+++++++++++++++++++++++++++++++++++++++++++++++++++0000-0120
W
wenjun 已提交
353 354 355

/* *
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0000
M
mamingshuai 已提交
356
 * @tc.name      : Generate key, normal input parameters keyAlias and keyParam
W
wenjun 已提交
357 358
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
359
HWTEST_F(SecurityDataHuksGenDelTestSuite, securityDataGenerateKey0000, Function | MediumTest | Level1)
W
wenjun 已提交
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
{
    char testFileName[] = "keyalias1";
    char testFileName1[] = "key_auth_id1";
    int32_t statusGenerate;
    int32_t statusDelete;
    struct hks_key_param keyParam;
    keyParam.key_auth_id.data = (uint8_t *)testFileName1;
    keyParam.key_auth_id.size = sizeof(testFileName1);
    keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID;
    keyParam.key_type = HKS_KEY_TYPE_EDDSA_KEYPAIR_ED25519;
    keyParam.key_len = 0;
    keyParam.key_usage = 0;
    keyParam.key_pad = 0;
    struct hks_blob keyAliasGenerate;
    HksStBlobInit1(&keyAliasGenerate, sizeof(uint8_t), sizeof(testFileName) + NUM3, HKS_BLOB_TYPE_ALIAS);
    if (memcpy_s(keyAliasGenerate.data, sizeof(testFileName), testFileName, sizeof(testFileName)) != EOK) {
        HksBlobDestroyT1(&keyAliasGenerate);
        EXPECT_EQ(0, 1);
        return;
    }
    char tmpGenerate[NUM3] = { 0 };
M
mamingshuai 已提交
381 382 383
    if (strcat_s((char *)keyAliasGenerate.data,
        strlen((char *)keyAliasGenerate.data) + strlen(tmpGenerate) + 1,
        tmpGenerate) != EOK) {
W
wenjun 已提交
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
        HksBlobDestroyT1(&keyAliasGenerate);
        EXPECT_EQ(0, 1);
        return;
    }
    keyAliasGenerate.size = strlen((char *)keyAliasGenerate.data);
    statusGenerate = hks_generate_key(&keyAliasGenerate, &keyParam);
    EXPECT_EQ(0, statusGenerate);
    HksBlobDestroyT1(&keyAliasGenerate);
    struct hks_blob keyAliasDelete;
    HksStBlobInit1(&keyAliasDelete, sizeof(uint8_t), sizeof(testFileName) + NUM3, HKS_BLOB_TYPE_ALIAS);
    if (memcpy_s(keyAliasDelete.data, sizeof(testFileName), testFileName, sizeof(testFileName)) != EOK) {
        HksBlobDestroyT1(&keyAliasDelete);
        EXPECT_EQ(0, 1);
        return;
    }
    char tmpDelete[NUM3] = { 0 };
M
mamingshuai 已提交
400 401 402
    if (strcat_s((char *)keyAliasDelete.data,
        strlen((char *)keyAliasDelete.data) + strlen(tmpDelete) + 1,
        tmpDelete) != EOK) {
W
wenjun 已提交
403 404 405 406 407 408 409 410 411 412 413
        HksBlobDestroyT1(&keyAliasDelete);
        EXPECT_EQ(0, 1);
        return;
    }
    keyAliasDelete.size = strlen((char *)keyAliasDelete.data);
    statusDelete = hks_delete_key(&keyAliasDelete);
    EXPECT_EQ(0, statusDelete);
    HksBlobDestroyT1(&keyAliasDelete);
};

/* *
M
mamingshuai 已提交
414 415
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0010
 * @tc.name      : Generate key, abnormal input parameters keyAlias is null
W
wenjun 已提交
416 417
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
418
HWTEST_F(SecurityDataHuksGenDelTestSuite, securityDataGenerateKey0010, Function | MediumTest | Level2)
W
wenjun 已提交
419 420
{
    char testFileName1[] = "key_auth_id1";
M
mamingshuai 已提交
421
    struct hks_blob *keyAlias = nullptr;
W
wenjun 已提交
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
    int32_t status;

    struct hks_key_param keyParam;
    keyParam.key_auth_id.data = (uint8_t *)testFileName1;
    keyParam.key_auth_id.size = sizeof(testFileName1);
    keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID;
    keyParam.key_type = HKS_KEY_TYPE_EDDSA_KEYPAIR_ED25519;
    keyParam.key_len = 0;
    keyParam.key_usage = 0;
    keyParam.key_pad = 0;

    status = hks_generate_key(keyAlias, &keyParam);
    EXPECT_EQ(NUM1000, status);
}

/* *
M
mamingshuai 已提交
438 439
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0020
 * @tc.name      : Generate key, abnormal input parameters keyAlias.size is 0
W
wenjun 已提交
440 441
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
442
HWTEST_F(SecurityDataHuksGenDelTestSuite, securityDataGenerateKey0020, Function | MediumTest | Level2)
W
wenjun 已提交
443 444 445 446 447 448 449 450
{
    char testFileName[] = "keyalias1";
    char testFileName1[] = "key_auth_id1";
    struct hks_blob keyAlias;
    int32_t status;

    keyAlias.type = HKS_BLOB_TYPE_ALIAS;
    keyAlias.data = (uint8_t *)testFileName;
M
mamingshuai 已提交
451
    keyAlias.size = 0;
W
wenjun 已提交
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466

    struct hks_key_param keyParam;
    keyParam.key_auth_id.data = (uint8_t *)testFileName1;
    keyParam.key_auth_id.size = sizeof(testFileName1);
    keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID;
    keyParam.key_type = HKS_KEY_TYPE_EDDSA_KEYPAIR_ED25519;
    keyParam.key_len = 0;
    keyParam.key_usage = 0;
    keyParam.key_pad = 0;

    status = hks_generate_key(&keyAlias, &keyParam);
    EXPECT_EQ(NUM135, status);
}

/* *
M
mamingshuai 已提交
467 468
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0030
 * @tc.name      : Generate key, abnormal input parameters keyAlias.size is more than 64
W
wenjun 已提交
469 470
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
471
HWTEST_F(SecurityDataHuksGenDelTestSuite, securityDataGenerateKey0030, Function | MediumTest | Level2)
W
wenjun 已提交
472 473 474 475 476 477 478 479
{
    char testFileName[] = "keyalias1";
    char testFileName1[] = "key_auth_id1";
    struct hks_blob keyAlias;
    int32_t status;

    keyAlias.type = HKS_BLOB_TYPE_ALIAS;
    keyAlias.data = (uint8_t *)testFileName;
M
mamingshuai 已提交
480
    keyAlias.size = NUM65;
W
wenjun 已提交
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495

    struct hks_key_param keyParam;
    keyParam.key_auth_id.data = (uint8_t *)testFileName1;
    keyParam.key_auth_id.size = sizeof(testFileName1);
    keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID;
    keyParam.key_type = HKS_KEY_TYPE_EDDSA_KEYPAIR_ED25519;
    keyParam.key_len = 0;
    keyParam.key_usage = 0;
    keyParam.key_pad = 0;

    status = hks_generate_key(&keyAlias, &keyParam);
    EXPECT_EQ(NUM135, status);
}

/* *
M
mamingshuai 已提交
496 497
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0040
 * @tc.name      : Generate key, abnormal input parameters keyAlias.type is not equal to HKS_BLOB_TYPE_ALIAS
W
wenjun 已提交
498 499
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
500
HWTEST_F(SecurityDataHuksGenDelTestSuite, securityDataGenerateKey0040, Function | MediumTest | Level2)
W
wenjun 已提交
501 502 503 504 505 506
{
    char testFileName[] = "keyalias1";
    char testFileName1[] = "key_auth_id1";
    struct hks_blob keyAlias;
    int32_t status;

M
mamingshuai 已提交
507
    keyAlias.type = 0;
W
wenjun 已提交
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
    keyAlias.data = (uint8_t *)testFileName;
    keyAlias.size = sizeof(testFileName);

    struct hks_key_param keyParam;
    keyParam.key_auth_id.data = (uint8_t *)testFileName1;
    keyParam.key_auth_id.size = sizeof(testFileName1);
    keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID;
    keyParam.key_type = HKS_KEY_TYPE_EDDSA_KEYPAIR_ED25519;
    keyParam.key_len = 0;
    keyParam.key_usage = 0;
    keyParam.key_pad = 0;

    status = hks_generate_key(&keyAlias, &keyParam);
    EXPECT_EQ(NUM135, status);
}

/* *
M
mamingshuai 已提交
525 526
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0050
 * @tc.name      : Generate key, abnormal input parameters keyAlias.data is null
W
wenjun 已提交
527 528
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
529
HWTEST_F(SecurityDataHuksGenDelTestSuite, securityDataGenerateKey0050, Function | MediumTest | Level2)
W
wenjun 已提交
530 531 532 533 534 535
{
    char testFileName[] = "keyalias1";
    char testFileName1[] = "key_auth_id1";
    struct hks_blob keyAlias;

    keyAlias.type = HKS_BLOB_TYPE_ALIAS;
M
mamingshuai 已提交
536
    keyAlias.data = NULL;
W
wenjun 已提交
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
    keyAlias.size = sizeof(testFileName);

    struct hks_key_param keyParam;
    keyParam.key_auth_id.data = (uint8_t *)testFileName1;
    keyParam.key_auth_id.size = sizeof(testFileName1);
    keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID;
    keyParam.key_type = HKS_KEY_TYPE_EDDSA_KEYPAIR_ED25519;
    keyParam.key_len = 0;
    keyParam.key_usage = 0;
    keyParam.key_pad = 0;

    int32_t status = hks_generate_key(&keyAlias, &keyParam);
    EXPECT_EQ(NUM135, status);
}

/* *
M
mamingshuai 已提交
553 554
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0060
 * @tc.name      : Generate key, abnormal input parameters keyParam is null
W
wenjun 已提交
555 556
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
557
HWTEST_F(SecurityDataHuksGenDelTestSuite, securityDataGenerateKey0060, Function | MediumTest | Level2)
W
wenjun 已提交
558 559 560 561 562 563 564 565 566
{
    char testFileName[] = "keyalias1";
    struct hks_blob keyAlias;
    int32_t status;

    keyAlias.type = HKS_BLOB_TYPE_ALIAS;
    keyAlias.data = (uint8_t *)testFileName;
    keyAlias.size = sizeof(testFileName);

M
mamingshuai 已提交
567
    struct hks_key_param *keyParam = nullptr;
W
wenjun 已提交
568 569 570 571 572 573

    status = hks_generate_key(&keyAlias, keyParam);
    EXPECT_EQ(NUM1000, status);
}

/* *
M
mamingshuai 已提交
574 575 576
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0070
 * @tc.name      : Generate key, abnormal input parameters keyParam.key_type
                   is not equal to HKS_KEY_TYPE_EDDSA_KEYPAIR_ED25519
W
wenjun 已提交
577 578
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
579
HWTEST_F(SecurityDataHuksGenDelTestSuite, securityDataGenerateKey0070, Function | MediumTest | Level2)
W
wenjun 已提交
580 581 582 583 584 585 586 587 588 589 590 591 592 593
{
    char testFileName[] = "keyalias1";
    char testFileName1[] = "key_auth_id1";
    struct hks_blob keyAlias;
    int32_t status;

    keyAlias.type = HKS_BLOB_TYPE_ALIAS;
    keyAlias.data = (uint8_t *)testFileName;
    keyAlias.size = sizeof(testFileName);

    struct hks_key_param keyParam;
    keyParam.key_auth_id.data = (uint8_t *)testFileName1;
    keyParam.key_auth_id.size = sizeof(testFileName1);
    keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID;
M
mamingshuai 已提交
594
    keyParam.key_type = 1;
W
wenjun 已提交
595 596 597 598 599 600 601 602 603
    keyParam.key_len = 0;
    keyParam.key_usage = 0;
    keyParam.key_pad = 0;

    status = hks_generate_key(&keyAlias, &keyParam);
    EXPECT_EQ(NUM134, status);
}

/* *
M
mamingshuai 已提交
604 605
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0080
 * @tc.name      : Generate key, abnormal input parameters keyParam.key_auth_id.size is 0
W
wenjun 已提交
606 607
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
608
HWTEST_F(SecurityDataHuksGenDelTestSuite, securityDataGenerateKey0080, Function | MediumTest | Level2)
W
wenjun 已提交
609 610 611 612 613 614 615 616 617 618 619 620
{
    char testFileName[] = "keyalias1";
    char testFileName1[] = "key_auth_id1";
    struct hks_blob keyAlias;
    int32_t status;

    keyAlias.type = HKS_BLOB_TYPE_ALIAS;
    keyAlias.data = (uint8_t *)testFileName;
    keyAlias.size = sizeof(testFileName);

    struct hks_key_param keyParam;
    keyParam.key_auth_id.data = (uint8_t *)testFileName1;
M
mamingshuai 已提交
621
    keyParam.key_auth_id.size = 0;
W
wenjun 已提交
622 623 624 625 626 627 628 629 630 631 632
    keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID;
    keyParam.key_type = HKS_KEY_TYPE_EDDSA_KEYPAIR_ED25519;
    keyParam.key_len = 0;
    keyParam.key_usage = 0;
    keyParam.key_pad = 0;

    status = hks_generate_key(&keyAlias, &keyParam);
    EXPECT_EQ(NUM135, status);
}

/* *
M
mamingshuai 已提交
633 634
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0090
 * @tc.name      : Generate key, abnormal input parameters keyParam.key_auth_id.size is more than 64
W
wenjun 已提交
635 636
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
637
HWTEST_F(SecurityDataHuksGenDelTestSuite, securityDataGenerateKey0090, Function | MediumTest | Level2)
W
wenjun 已提交
638 639 640 641 642 643 644 645 646 647 648 649
{
    char testFileName[] = "keyalias1";
    char testFileName1[] = "key_auth_id1";
    struct hks_blob keyAlias;
    int32_t status;

    keyAlias.type = HKS_BLOB_TYPE_ALIAS;
    keyAlias.data = (uint8_t *)testFileName;
    keyAlias.size = sizeof(testFileName);

    struct hks_key_param keyParam;
    keyParam.key_auth_id.data = (uint8_t *)testFileName1;
M
mamingshuai 已提交
650
    keyParam.key_auth_id.size = NUM65;
W
wenjun 已提交
651 652 653 654 655 656 657 658 659 660 661
    keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID;
    keyParam.key_type = HKS_KEY_TYPE_EDDSA_KEYPAIR_ED25519;
    keyParam.key_len = 0;
    keyParam.key_usage = 0;
    keyParam.key_pad = 0;

    status = hks_generate_key(&keyAlias, &keyParam);
    EXPECT_EQ(NUM135, status);
}

/* *
M
mamingshuai 已提交
662 663 664
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0100
 * @tc.name      : Generate key, abnormal input parameters keyParam.key_auth_id.type
                   is not equal to HKS_BLOB_TYPE_AUTHID
W
wenjun 已提交
665 666
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
667
HWTEST_F(SecurityDataHuksGenDelTestSuite, securityDataGenerateKey0100, Function | MediumTest | Level2)
W
wenjun 已提交
668 669 670 671 672 673 674 675 676 677 678 679 680
{
    char testFileName[] = "keyalias1";
    char testFileName1[] = "key_auth_id1";
    struct hks_blob keyAlias;
    int32_t status;

    keyAlias.type = HKS_BLOB_TYPE_ALIAS;
    keyAlias.data = (uint8_t *)testFileName;
    keyAlias.size = sizeof(testFileName);

    struct hks_key_param keyParam;
    keyParam.key_auth_id.data = (uint8_t *)testFileName1;
    keyParam.key_auth_id.size = sizeof(testFileName1);
M
mamingshuai 已提交
681
    keyParam.key_auth_id.type = 0;
W
wenjun 已提交
682 683 684 685 686 687 688 689 690 691
    keyParam.key_type = HKS_KEY_TYPE_EDDSA_KEYPAIR_ED25519;
    keyParam.key_len = 0;
    keyParam.key_usage = 0;
    keyParam.key_pad = 0;

    status = hks_generate_key(&keyAlias, &keyParam);
    EXPECT_EQ(NUM135, status);
}

/* *
M
mamingshuai 已提交
692 693
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0110
 * @tc.name      : Generate key, abnormal input parameters keyParam.key_auth_id.data is null
W
wenjun 已提交
694 695
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
696
HWTEST_F(SecurityDataHuksGenDelTestSuite, securityDataGenerateKey0110, Function | MediumTest | Level2)
W
wenjun 已提交
697 698 699 700 701 702 703 704 705 706 707
{
    char testFileName[] = "keyalias1";
    char testFileName1[] = "key_auth_id1";
    struct hks_blob keyAlias;
    int32_t status;

    keyAlias.type = HKS_BLOB_TYPE_ALIAS;
    keyAlias.data = (uint8_t *)testFileName;
    keyAlias.size = sizeof(testFileName);

    struct hks_key_param keyParam;
M
mamingshuai 已提交
708
    keyParam.key_auth_id.data = NULL;
W
wenjun 已提交
709 710 711 712 713 714 715 716 717 718 719 720
    keyParam.key_auth_id.size = sizeof(testFileName1);
    keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID;
    keyParam.key_type = HKS_KEY_TYPE_EDDSA_KEYPAIR_ED25519;
    keyParam.key_len = 0;
    keyParam.key_usage = 0;
    keyParam.key_pad = 0;

    status = hks_generate_key(&keyAlias, &keyParam);
    EXPECT_EQ(NUM1000, status);
}

/* *
M
mamingshuai 已提交
721 722
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0120
 * @tc.name      : Generate key, the number of stored keys is more than 20
W
wenjun 已提交
723 724
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
725
HWTEST_F(SecurityDataHuksGenDelTestSuite, securityDataGenerateKey0120, Function | MediumTest | Level2)
W
wenjun 已提交
726 727 728
{
    char testFileName[] = "keyalias1", testFileName1[] = "key_auth_id1";
    struct hks_key_param keyParam;
M
mamingshuai 已提交
729 730 731
    keyParam.key_auth_id.data = (uint8_t*)testFileName1; keyParam.key_auth_id.size = sizeof(testFileName1);
    keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID; keyParam.key_type = HKS_KEY_TYPE_EDDSA_KEYPAIR_ED25519;
    keyParam.key_len = 0; keyParam.key_usage = 0; keyParam.key_pad = 0;
W
wenjun 已提交
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
    for (int j = 0; j < NUM21; j++) {
        struct hks_blob keyAlias;
        HksStBlobInit1(&keyAlias, sizeof(uint8_t), sizeof(testFileName) + NUM3, HKS_BLOB_TYPE_ALIAS);
        if (memcpy_s(keyAlias.data, sizeof(testFileName), testFileName, sizeof(testFileName)) != EOK) {
            HksBlobDestroyT1(&keyAlias);
            EXPECT_EQ(0, 1);
            return;
        }
        char tmp[NUM3] = { 0 };
        sprintf_s(tmp, sizeof(tmp), "%d", j);
        if (strcat_s((char*)keyAlias.data, strlen((char*)keyAlias.data) + strlen(tmp) + 1, tmp) != EOK) {
            HksBlobDestroyT1(&keyAlias);
            EXPECT_EQ(0, 1);
            return;
        }
        keyAlias.size = strlen((char*)keyAlias.data);
        int status = hks_generate_key(&keyAlias, &keyParam);
M
mamingshuai 已提交
749 750
        if (j < NUM20){EXPECT_EQ(0, status); }
        else {EXPECT_EQ(NUM142, status); }
W
wenjun 已提交
751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
        HksBlobDestroyT1(&keyAlias);
    }
    for (int j = 0; j < NUM20; j++) {
        struct hks_blob keyAlias;
        HksStBlobInit1(&keyAlias, sizeof(uint8_t), sizeof(testFileName) + NUM3, HKS_BLOB_TYPE_ALIAS);
        if (memcpy_s(keyAlias.data, sizeof(testFileName), testFileName, sizeof(testFileName)) != EOK) {
            HksBlobDestroyT1(&keyAlias);
            EXPECT_EQ(0, 1);
            return;
        }
        char tmp[NUM3] = { 0 };
        sprintf_s(tmp, sizeof(tmp), "%d", j);
        if (strcat_s((char*)keyAlias.data, strlen((char*)keyAlias.data) + strlen(tmp) + 1, tmp) != EOK) {
            HksBlobDestroyT1(&keyAlias);
            EXPECT_EQ(0, 1);
            return;
        }
        keyAlias.size = strlen((char*)keyAlias.data);
        int status = hks_delete_key(&keyAlias);
        EXPECT_EQ(0, status);
        HksBlobDestroyT1(&keyAlias);
    }
}


// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++generate key
M
mamingshuai 已提交
777
// end+++++++++++++++++++++++++++++++++++++++++++++++++++++0000-0120
W
wenjun 已提交
778 779

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++Delete key
M
mamingshuai 已提交
780
// begin+++++++++++++++++++++++++++++++++++++++++++++++++++++0130-0190
W
wenjun 已提交
781 782

/* *
M
mamingshuai 已提交
783 784
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0130
 * @tc.name      : Delete key, normal input parameters keyAlias and keyParam
W
wenjun 已提交
785 786
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
787
HWTEST_F(SecurityDataHuksGenDelTestSuite, securityDataDeleteKey0130, Function | MediumTest | Level1)
W
wenjun 已提交
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
{
    char testFileName[] = "keyalias1";
    char testFileName1[] = "key_auth_id1";
    int32_t statusGenerate;
    int32_t statusDelete;
    struct hks_key_param keyParam;
    keyParam.key_auth_id.data = (uint8_t *)testFileName1;
    keyParam.key_auth_id.size = sizeof(testFileName1);
    keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID;
    keyParam.key_type = HKS_KEY_TYPE_EDDSA_KEYPAIR_ED25519;
    keyParam.key_len = 0;
    keyParam.key_usage = 0;
    keyParam.key_pad = 0;
    struct hks_blob keyAliasGenerate;
    HksStBlobInit1(&keyAliasGenerate, sizeof(uint8_t), sizeof(testFileName) + NUM3, HKS_BLOB_TYPE_ALIAS);
    if (memcpy_s(keyAliasGenerate.data, sizeof(testFileName), testFileName, sizeof(testFileName)) != EOK) {
        HksBlobDestroyT1(&keyAliasGenerate);
        EXPECT_EQ(0, 1);
        return;
    }
    char tmpGenerate[NUM3] = { 0 };
M
mamingshuai 已提交
809 810 811
    if (strcat_s((char *)keyAliasGenerate.data,
        strlen((char *)keyAliasGenerate.data) + strlen(tmpGenerate) + 1,
        tmpGenerate) != EOK) {
W
wenjun 已提交
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
        HksBlobDestroyT1(&keyAliasGenerate);
        EXPECT_EQ(0, 1);
        return;
    }
    keyAliasGenerate.size = strlen((char *)keyAliasGenerate.data);
    statusGenerate = hks_generate_key(&keyAliasGenerate, &keyParam);
    EXPECT_EQ(0, statusGenerate);
    HksBlobDestroyT1(&keyAliasGenerate);
    struct hks_blob keyAliasDelete;
    HksStBlobInit1(&keyAliasDelete, sizeof(uint8_t), sizeof(testFileName) + NUM3, HKS_BLOB_TYPE_ALIAS);
    if (memcpy_s(keyAliasDelete.data, sizeof(testFileName), testFileName, sizeof(testFileName)) != EOK) {
        HksBlobDestroyT1(&keyAliasDelete);
        EXPECT_EQ(0, 1);
        return;
    }
    char tmpDelete[NUM3] = { 0 };
M
mamingshuai 已提交
828 829 830
    if (strcat_s((char *)keyAliasDelete.data,
        strlen((char *)keyAliasDelete.data) + strlen(tmpDelete) + 1,
        tmpDelete) != EOK) {
W
wenjun 已提交
831 832 833 834 835 836 837 838 839 840 841
        HksBlobDestroyT1(&keyAliasDelete);
        EXPECT_EQ(0, 1);
        return;
    }
    keyAliasDelete.size = strlen((char *)keyAliasDelete.data);
    statusDelete = hks_delete_key(&keyAliasDelete);
    EXPECT_EQ(0, statusDelete);
    HksBlobDestroyT1(&keyAliasDelete);
};

/* *
M
mamingshuai 已提交
842 843
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0140
 * @tc.name      : Delete key, abnormal input parameters keyAlias is null
W
wenjun 已提交
844 845
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
846
HWTEST_F(SecurityDataHuksGenDelTestSuite, securityDataDeleteKey0140, Function | MediumTest | Level2)
W
wenjun 已提交
847 848
{
    int32_t status;
M
mamingshuai 已提交
849
    struct hks_blob *keyAlias = nullptr;
W
wenjun 已提交
850 851 852 853 854 855

    status = hks_delete_key(keyAlias);
    EXPECT_EQ(NUM1000, status);
}

/* *
M
mamingshuai 已提交
856 857
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0150
 * @tc.name      : Delete key, abnormal input parameters keyAlias.data is null
W
wenjun 已提交
858 859
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
860
HWTEST_F(SecurityDataHuksGenDelTestSuite, securityDataDeleteKey0150, Function | MediumTest | Level2)
W
wenjun 已提交
861 862 863 864 865
{
    int32_t status;
    char testFileName[] = "Test_file_north_interfaces";
    struct hks_blob keyAlias = { 0 };

M
mamingshuai 已提交
866
    keyAlias.data = NULL;
W
wenjun 已提交
867 868 869 870 871 872 873 874
    keyAlias.size = sizeof(testFileName);
    keyAlias.type = HKS_BLOB_TYPE_ALIAS;

    status = hks_delete_key(&keyAlias);
    EXPECT_EQ(NUM135, status);
}

/* *
M
mamingshuai 已提交
875 876
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0160
 * @tc.name      : Delete key, abnormal input parameters keyAlias.type is not equal to HKS_BLOB_TYPE_ALIAS
W
wenjun 已提交
877 878
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
879
HWTEST_F(SecurityDataHuksGenDelTestSuite, securityDataDeleteKey0160, Function | MediumTest | Level2)
W
wenjun 已提交
880 881 882 883 884 885 886
{
    int32_t status;
    char testFileName[] = "Test_file_north_interfaces";
    struct hks_blob keyAlias = { 0 };

    keyAlias.data = (uint8_t *)testFileName;
    keyAlias.size = sizeof(testFileName);
M
mamingshuai 已提交
887
    keyAlias.type = 0;
W
wenjun 已提交
888 889 890 891 892 893

    status = hks_delete_key(&keyAlias);
    EXPECT_EQ(NUM135, status);
}

/* *
M
mamingshuai 已提交
894 895
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0170
 * @tc.name      : Delete key, abnormal input parameters keyAlias.size is 0
W
wenjun 已提交
896 897
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
898
HWTEST_F(SecurityDataHuksGenDelTestSuite, securityDataDeleteKey0170, Function | MediumTest | Level2)
W
wenjun 已提交
899 900 901 902 903 904 905 906 907 908 909 910 911 912
{
    int32_t status;
    char testFileName[] = "Test_file_north_interfaces";
    struct hks_blob keyAlias = { 0 };

    keyAlias.data = (uint8_t *)testFileName;
    keyAlias.size = 0;
    keyAlias.type = HKS_BLOB_TYPE_ALIAS;

    status = hks_delete_key(&keyAlias);
    EXPECT_EQ(NUM135, status);
}

/* *
M
mamingshuai 已提交
913 914
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0180
 * @tc.name      : Delete key, abnormal input parameters keyAlias.size is more than 64
W
wenjun 已提交
915 916
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
917
HWTEST_F(SecurityDataHuksGenDelTestSuite, securityDataDeleteKey0180, Function | MediumTest | Level2)
W
wenjun 已提交
918 919 920 921 922 923 924 925 926 927 928 929 930 931
{
    int32_t status;
    char testFileName[] = "Test_file_north_interfaces";
    struct hks_blob keyAlias = { 0 };

    keyAlias.data = (uint8_t *)testFileName;
    keyAlias.size = NUM65;
    keyAlias.type = HKS_BLOB_TYPE_ALIAS;

    status = hks_delete_key(&keyAlias);
    EXPECT_EQ(NUM135, status);
}

/* *
M
mamingshuai 已提交
932 933
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0190
 * @tc.name      : Delete key, the key does not exist
W
wenjun 已提交
934 935
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
936
HWTEST_F(SecurityDataHuksGenDelTestSuite, securityDataDeleteKey0190, Function | MediumTest | Level2)
W
wenjun 已提交
937 938 939 940 941 942 943 944 945 946 947 948 949
{
    int32_t status;
    char testFileName[] = "Test_file_north_interfaces";
    struct hks_blob keyAlias = { 0 };

    keyAlias.data = (uint8_t *)testFileName;
    keyAlias.size = sizeof(testFileName);
    keyAlias.type = HKS_BLOB_TYPE_ALIAS;

    status = hks_delete_key(&keyAlias);
    EXPECT_EQ(NUM1010, status);
}
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++Delete key
M
mamingshuai 已提交
950
// end+++++++++++++++++++++++++++++++++++++++++++++++++++++++0130-0190