1/*
2 * Copyright (c) 2023 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 "asset_test_common.h"
17
18#include <string>
19
20#include "asset_api.h"
21#include "asset_system_api.h"
22
23int32_t RemoveByAliasNdk(const char *alias)
24{
25    Asset_Attr attr[] = {
26        {
27            .tag = ASSET_TAG_ALIAS,
28            .value.blob = {
29                .size = static_cast<uint32_t>(strlen(alias)),
30                .data = reinterpret_cast<uint8_t*>(const_cast<char*>(alias))
31            }
32        }
33    };
34    return OH_Asset_Remove(attr, ARRAY_SIZE(attr));
35}
36
37int32_t RemoveByAliasSdk(const char *alias)
38{
39    AssetAttr attr[] = {
40        { .tag = SEC_ASSET_TAG_ALIAS,
41          .value.blob = { .size = static_cast<uint32_t>(strlen(alias)),
42              .data = reinterpret_cast<uint8_t*>(const_cast<char*>(alias)) } },
43        { .tag = SEC_ASSET_TAG_USER_ID, .value.u32 = SPECIFIC_USER_ID }
44    };
45    return AssetRemove(attr, ARRAY_SIZE(attr));
46}
47
48int32_t QueryByAliasNdk(const char *alias, Asset_ResultSet *resultSet)
49{
50    Asset_Attr attr[] = {
51        {
52            .tag = ASSET_TAG_ALIAS,
53            .value.blob = {
54                .size = static_cast<uint32_t>(strlen(alias)),
55                .data = reinterpret_cast<uint8_t*>(const_cast<char*>(alias))
56            }
57        }, {
58            .tag = ASSET_TAG_RETURN_TYPE,
59            .value.u32 = ASSET_RETURN_ALL
60        }
61    };
62    return OH_Asset_Query(attr, ARRAY_SIZE(attr), resultSet);
63}
64
65int32_t QueryByAliasSdk(const char *alias, AssetResultSet *resultSet)
66{
67    AssetAttr attr[] = {
68        { .tag = SEC_ASSET_TAG_ALIAS,
69          .value.blob = { .size = static_cast<uint32_t>(strlen(alias)),
70              .data = reinterpret_cast<uint8_t*>(const_cast<char*>(alias)) } },
71        { .tag = SEC_ASSET_TAG_RETURN_TYPE, .value.u32 = SEC_ASSET_RETURN_ALL },
72        { .tag = SEC_ASSET_TAG_USER_ID, .value.u32 = SPECIFIC_USER_ID }
73    };
74    return AssetQuery(attr, ARRAY_SIZE(attr), resultSet);
75}
76
77bool CompareBlobNdk(const Asset_Blob *blob1, const Asset_Blob *blob2)
78{
79    return CompareBlobSdk(reinterpret_cast<const AssetBlob *>(blob1), reinterpret_cast<const AssetBlob *>(blob2));
80}
81
82bool CompareBlobSdk(const AssetBlob *blob1, const AssetBlob *blob2)
83{
84    if (blob1->size != blob2->size) {
85        return false;
86    }
87    return memcmp(blob1->data, blob2->data, blob1->size) == 0;
88}
89
90bool CheckMatchAttrResultNdk(const Asset_Attr *attrs, uint32_t attrCnt, const Asset_Result *result)
91{
92    return CheckMatchAttrResultSdk(reinterpret_cast<const AssetAttr *>(attrs), attrCnt,
93        reinterpret_cast<const AssetResult *>(result));
94}
95
96bool CheckMatchAttrResultSdk(const AssetAttr *attrs, uint32_t attrCnt, const AssetResult *result)
97{
98    for (uint32_t i = 0; i < attrCnt; i++) {
99        if (attrs[i].tag == SEC_ASSET_TAG_CONFLICT_RESOLUTION || attrs[i].tag == SEC_ASSET_TAG_USER_ID) {
100            continue;
101        }
102        AssetAttr *res = AssetParseAttr(result, static_cast<AssetTag>(attrs[i].tag));
103        if (res == nullptr) {
104            return false;
105        }
106        switch (attrs[i].tag & SEC_ASSET_TAG_TYPE_MASK) {
107            case SEC_ASSET_TYPE_BOOL:
108                if (attrs[i].value.boolean != res->value.boolean) {
109                    printf("tag is %x, %u vs %u", attrs[i].tag, attrs[i].value.boolean, res->value.boolean);
110                    return false;
111                }
112                break;
113            case SEC_ASSET_TYPE_NUMBER:
114                if (attrs[i].value.u32 != res->value.u32) {
115                    printf("tag is %x, %u vs %u", attrs[i].tag, attrs[i].value.u32, res->value.u32);
116                    return false;
117                }
118                break;
119            case SEC_ASSET_TYPE_BYTES:
120                if (!CompareBlobSdk(&attrs[i].value.blob, &res->value.blob)) {
121                    printf("tag is %x, len %u vs len %u", attrs[i].tag, attrs[i].value.blob.size, res->value.blob.size);
122                    return false;
123                }
124                break;
125            default:
126                return false;
127        };
128    }
129    return true;
130}