1dfe32fa1Soh_ci/*
2dfe32fa1Soh_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3dfe32fa1Soh_ci * Licensed under the Apache License, Version 2.0 (the "License");
4dfe32fa1Soh_ci * you may not use this file except in compliance with the License.
5dfe32fa1Soh_ci * You may obtain a copy of the License at
6dfe32fa1Soh_ci *
7dfe32fa1Soh_ci *    http://www.apache.org/licenses/LICENSE-2.0
8dfe32fa1Soh_ci *
9dfe32fa1Soh_ci * Unless required by applicable law or agreed to in writing, software
10dfe32fa1Soh_ci * distributed under the License is distributed on an "AS IS" BASIS,
11dfe32fa1Soh_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12dfe32fa1Soh_ci * See the License for the specific language governing permissions and
13dfe32fa1Soh_ci * limitations under the License.
14dfe32fa1Soh_ci */
15dfe32fa1Soh_ci
16dfe32fa1Soh_ci#include "asset_test_common.h"
17dfe32fa1Soh_ci
18dfe32fa1Soh_ci#include <string>
19dfe32fa1Soh_ci
20dfe32fa1Soh_ci#include "asset_api.h"
21dfe32fa1Soh_ci#include "asset_system_api.h"
22dfe32fa1Soh_ci
23dfe32fa1Soh_ciint32_t RemoveByAliasNdk(const char *alias)
24dfe32fa1Soh_ci{
25dfe32fa1Soh_ci    Asset_Attr attr[] = {
26dfe32fa1Soh_ci        {
27dfe32fa1Soh_ci            .tag = ASSET_TAG_ALIAS,
28dfe32fa1Soh_ci            .value.blob = {
29dfe32fa1Soh_ci                .size = static_cast<uint32_t>(strlen(alias)),
30dfe32fa1Soh_ci                .data = reinterpret_cast<uint8_t*>(const_cast<char*>(alias))
31dfe32fa1Soh_ci            }
32dfe32fa1Soh_ci        }
33dfe32fa1Soh_ci    };
34dfe32fa1Soh_ci    return OH_Asset_Remove(attr, ARRAY_SIZE(attr));
35dfe32fa1Soh_ci}
36dfe32fa1Soh_ci
37dfe32fa1Soh_ciint32_t RemoveByAliasSdk(const char *alias)
38dfe32fa1Soh_ci{
39dfe32fa1Soh_ci    AssetAttr attr[] = {
40dfe32fa1Soh_ci        { .tag = SEC_ASSET_TAG_ALIAS,
41dfe32fa1Soh_ci          .value.blob = { .size = static_cast<uint32_t>(strlen(alias)),
42dfe32fa1Soh_ci              .data = reinterpret_cast<uint8_t*>(const_cast<char*>(alias)) } },
43dfe32fa1Soh_ci        { .tag = SEC_ASSET_TAG_USER_ID, .value.u32 = SPECIFIC_USER_ID }
44dfe32fa1Soh_ci    };
45dfe32fa1Soh_ci    return AssetRemove(attr, ARRAY_SIZE(attr));
46dfe32fa1Soh_ci}
47dfe32fa1Soh_ci
48dfe32fa1Soh_ciint32_t QueryByAliasNdk(const char *alias, Asset_ResultSet *resultSet)
49dfe32fa1Soh_ci{
50dfe32fa1Soh_ci    Asset_Attr attr[] = {
51dfe32fa1Soh_ci        {
52dfe32fa1Soh_ci            .tag = ASSET_TAG_ALIAS,
53dfe32fa1Soh_ci            .value.blob = {
54dfe32fa1Soh_ci                .size = static_cast<uint32_t>(strlen(alias)),
55dfe32fa1Soh_ci                .data = reinterpret_cast<uint8_t*>(const_cast<char*>(alias))
56dfe32fa1Soh_ci            }
57dfe32fa1Soh_ci        }, {
58dfe32fa1Soh_ci            .tag = ASSET_TAG_RETURN_TYPE,
59dfe32fa1Soh_ci            .value.u32 = ASSET_RETURN_ALL
60dfe32fa1Soh_ci        }
61dfe32fa1Soh_ci    };
62dfe32fa1Soh_ci    return OH_Asset_Query(attr, ARRAY_SIZE(attr), resultSet);
63dfe32fa1Soh_ci}
64dfe32fa1Soh_ci
65dfe32fa1Soh_ciint32_t QueryByAliasSdk(const char *alias, AssetResultSet *resultSet)
66dfe32fa1Soh_ci{
67dfe32fa1Soh_ci    AssetAttr attr[] = {
68dfe32fa1Soh_ci        { .tag = SEC_ASSET_TAG_ALIAS,
69dfe32fa1Soh_ci          .value.blob = { .size = static_cast<uint32_t>(strlen(alias)),
70dfe32fa1Soh_ci              .data = reinterpret_cast<uint8_t*>(const_cast<char*>(alias)) } },
71dfe32fa1Soh_ci        { .tag = SEC_ASSET_TAG_RETURN_TYPE, .value.u32 = SEC_ASSET_RETURN_ALL },
72dfe32fa1Soh_ci        { .tag = SEC_ASSET_TAG_USER_ID, .value.u32 = SPECIFIC_USER_ID }
73dfe32fa1Soh_ci    };
74dfe32fa1Soh_ci    return AssetQuery(attr, ARRAY_SIZE(attr), resultSet);
75dfe32fa1Soh_ci}
76dfe32fa1Soh_ci
77dfe32fa1Soh_cibool CompareBlobNdk(const Asset_Blob *blob1, const Asset_Blob *blob2)
78dfe32fa1Soh_ci{
79dfe32fa1Soh_ci    return CompareBlobSdk(reinterpret_cast<const AssetBlob *>(blob1), reinterpret_cast<const AssetBlob *>(blob2));
80dfe32fa1Soh_ci}
81dfe32fa1Soh_ci
82dfe32fa1Soh_cibool CompareBlobSdk(const AssetBlob *blob1, const AssetBlob *blob2)
83dfe32fa1Soh_ci{
84dfe32fa1Soh_ci    if (blob1->size != blob2->size) {
85dfe32fa1Soh_ci        return false;
86dfe32fa1Soh_ci    }
87dfe32fa1Soh_ci    return memcmp(blob1->data, blob2->data, blob1->size) == 0;
88dfe32fa1Soh_ci}
89dfe32fa1Soh_ci
90dfe32fa1Soh_cibool CheckMatchAttrResultNdk(const Asset_Attr *attrs, uint32_t attrCnt, const Asset_Result *result)
91dfe32fa1Soh_ci{
92dfe32fa1Soh_ci    return CheckMatchAttrResultSdk(reinterpret_cast<const AssetAttr *>(attrs), attrCnt,
93dfe32fa1Soh_ci        reinterpret_cast<const AssetResult *>(result));
94dfe32fa1Soh_ci}
95dfe32fa1Soh_ci
96dfe32fa1Soh_cibool CheckMatchAttrResultSdk(const AssetAttr *attrs, uint32_t attrCnt, const AssetResult *result)
97dfe32fa1Soh_ci{
98dfe32fa1Soh_ci    for (uint32_t i = 0; i < attrCnt; i++) {
99dfe32fa1Soh_ci        if (attrs[i].tag == SEC_ASSET_TAG_CONFLICT_RESOLUTION || attrs[i].tag == SEC_ASSET_TAG_USER_ID) {
100dfe32fa1Soh_ci            continue;
101dfe32fa1Soh_ci        }
102dfe32fa1Soh_ci        AssetAttr *res = AssetParseAttr(result, static_cast<AssetTag>(attrs[i].tag));
103dfe32fa1Soh_ci        if (res == nullptr) {
104dfe32fa1Soh_ci            return false;
105dfe32fa1Soh_ci        }
106dfe32fa1Soh_ci        switch (attrs[i].tag & SEC_ASSET_TAG_TYPE_MASK) {
107dfe32fa1Soh_ci            case SEC_ASSET_TYPE_BOOL:
108dfe32fa1Soh_ci                if (attrs[i].value.boolean != res->value.boolean) {
109dfe32fa1Soh_ci                    printf("tag is %x, %u vs %u", attrs[i].tag, attrs[i].value.boolean, res->value.boolean);
110dfe32fa1Soh_ci                    return false;
111dfe32fa1Soh_ci                }
112dfe32fa1Soh_ci                break;
113dfe32fa1Soh_ci            case SEC_ASSET_TYPE_NUMBER:
114dfe32fa1Soh_ci                if (attrs[i].value.u32 != res->value.u32) {
115dfe32fa1Soh_ci                    printf("tag is %x, %u vs %u", attrs[i].tag, attrs[i].value.u32, res->value.u32);
116dfe32fa1Soh_ci                    return false;
117dfe32fa1Soh_ci                }
118dfe32fa1Soh_ci                break;
119dfe32fa1Soh_ci            case SEC_ASSET_TYPE_BYTES:
120dfe32fa1Soh_ci                if (!CompareBlobSdk(&attrs[i].value.blob, &res->value.blob)) {
121dfe32fa1Soh_ci                    printf("tag is %x, len %u vs len %u", attrs[i].tag, attrs[i].value.blob.size, res->value.blob.size);
122dfe32fa1Soh_ci                    return false;
123dfe32fa1Soh_ci                }
124dfe32fa1Soh_ci                break;
125dfe32fa1Soh_ci            default:
126dfe32fa1Soh_ci                return false;
127dfe32fa1Soh_ci        };
128dfe32fa1Soh_ci    }
129dfe32fa1Soh_ci    return true;
130dfe32fa1Soh_ci}