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#![allow(dead_code)]
17dfe32fa1Soh_ci
18dfe32fa1Soh_ciuse asset_sdk::*;
19dfe32fa1Soh_ci
20dfe32fa1Soh_ci#[macro_export]
21dfe32fa1Soh_cimacro_rules! function {
22dfe32fa1Soh_ci    () => {{
23dfe32fa1Soh_ci        fn f() {}
24dfe32fa1Soh_ci        fn type_name_of<T>(_: T) -> &'static str {
25dfe32fa1Soh_ci            std::any::type_name::<T>()
26dfe32fa1Soh_ci        }
27dfe32fa1Soh_ci        type_name_of(f).rsplit("::").find(|&part| part != "f" && part != "{{closure}}").expect("Short function name")
28dfe32fa1Soh_ci    }};
29dfe32fa1Soh_ci}
30dfe32fa1Soh_ci
31dfe32fa1Soh_cipub(crate) const MIN_NUMBER_VALUE: u32 = 0;
32dfe32fa1Soh_cipub(crate) const MAX_RETURN_LIMIT: u32 = 0x10000; // 65536
33dfe32fa1Soh_cipub(crate) const MAX_AUTH_VALID_PERIOD: u32 = 600; // 10min
34dfe32fa1Soh_cipub(crate) const CRYPTO_CAPACITY: u32 = 16;
35dfe32fa1Soh_ci
36dfe32fa1Soh_cipub(crate) const MIN_ARRAY_SIZE: usize = 0;
37dfe32fa1Soh_cipub(crate) const MAX_SECRET_SIZE: usize = 1024;
38dfe32fa1Soh_ci
39dfe32fa1Soh_cipub(crate) const MAX_ALIAS_SIZE: usize = 256;
40dfe32fa1Soh_cipub(crate) const MAX_LABEL_SIZE: usize = 2048;
41dfe32fa1Soh_ci
42dfe32fa1Soh_cipub(crate) const AUTH_TOKEN_SIZE: usize = 280;
43dfe32fa1Soh_cipub(crate) const CHALLENGE_SIZE: usize = 32;
44dfe32fa1Soh_cipub(crate) const SYNC_TYPE_MIN_BITS: u32 = 0;
45dfe32fa1Soh_cipub(crate) const SYNC_TYPE_MAX_BITS: u32 = 3;
46dfe32fa1Soh_ci
47dfe32fa1Soh_cipub(crate) const CRITICAL_LABEL_ATTRS: [Tag; 4] =
48dfe32fa1Soh_ci    [Tag::DataLabelCritical1, Tag::DataLabelCritical2, Tag::DataLabelCritical3, Tag::DataLabelCritical4];
49dfe32fa1Soh_ci
50dfe32fa1Soh_cipub(crate) const NORMAL_LABEL_ATTRS: [Tag; 4] =
51dfe32fa1Soh_ci    [Tag::DataLabelNormal1, Tag::DataLabelNormal2, Tag::DataLabelNormal3, Tag::DataLabelNormal4];
52dfe32fa1Soh_ci
53dfe32fa1Soh_cipub(crate) fn remove_by_alias(alias: &[u8]) -> Result<()> {
54dfe32fa1Soh_ci    asset_sdk::Manager::build()?.remove(&AssetMap::from([(Tag::Alias, Value::Bytes(alias.to_vec()))]))
55dfe32fa1Soh_ci}
56dfe32fa1Soh_ci
57dfe32fa1Soh_cipub(crate) const SECRET: &[u8] = "all_tags_secret".as_bytes();
58dfe32fa1Soh_cipub(crate) const NORMAL_LABEL1: &[u8] = "all_tags_normal_label1".as_bytes();
59dfe32fa1Soh_cipub(crate) const NORMAL_LABEL2: &[u8] = "all_tags_normal_label2".as_bytes();
60dfe32fa1Soh_cipub(crate) const NORMAL_LABEL3: &[u8] = "all_tags_normal_label3".as_bytes();
61dfe32fa1Soh_cipub(crate) const NORMAL_LABEL4: &[u8] = "all_tags_normal_label4".as_bytes();
62dfe32fa1Soh_cipub(crate) const NORMAL_LOCAL_LABEL1: &[u8] = "all_tags_normal_local_label1".as_bytes();
63dfe32fa1Soh_cipub(crate) const NORMAL_LOCAL_LABEL2: &[u8] = "all_tags_normal_local_label2".as_bytes();
64dfe32fa1Soh_cipub(crate) const NORMAL_LOCAL_LABEL3: &[u8] = "all_tags_normal_local_label3".as_bytes();
65dfe32fa1Soh_cipub(crate) const NORMAL_LOCAL_LABEL4: &[u8] = "all_tags_normal_local_label4".as_bytes();
66dfe32fa1Soh_cipub(crate) const CRITICAL_LABEL1: &[u8] = "all_tags_critical_label1".as_bytes();
67dfe32fa1Soh_cipub(crate) const CRITICAL_LABEL2: &[u8] = "all_tags_critical_label2".as_bytes();
68dfe32fa1Soh_cipub(crate) const CRITICAL_LABEL3: &[u8] = "all_tags_critical_label3".as_bytes();
69dfe32fa1Soh_cipub(crate) const CRITICAL_LABEL4: &[u8] = "all_tags_critical_label4".as_bytes();
70dfe32fa1Soh_ci
71dfe32fa1Soh_cipub(crate) fn remove_all() -> Result<()> {
72dfe32fa1Soh_ci    asset_sdk::Manager::build()?.remove(&AssetMap::new())
73dfe32fa1Soh_ci}
74dfe32fa1Soh_ci
75dfe32fa1Soh_cipub(crate) fn query_all_by_alias(alias: &[u8]) -> Result<Vec<AssetMap>> {
76dfe32fa1Soh_ci    asset_sdk::Manager::build()?.query(&AssetMap::from([
77dfe32fa1Soh_ci        (Tag::Alias, Value::Bytes(alias.to_vec())),
78dfe32fa1Soh_ci        (Tag::ReturnType, Value::Number(ReturnType::All as u32)),
79dfe32fa1Soh_ci    ]))
80dfe32fa1Soh_ci}
81dfe32fa1Soh_ci
82dfe32fa1Soh_cipub(crate) fn query_attr_by_alias(alias: &[u8]) -> Result<Vec<AssetMap>> {
83dfe32fa1Soh_ci    asset_sdk::Manager::build()?.query(&AssetMap::from([
84dfe32fa1Soh_ci        (Tag::Alias, Value::Bytes(alias.to_vec())),
85dfe32fa1Soh_ci        (Tag::ReturnType, Value::Number(ReturnType::Attributes as u32)),
86dfe32fa1Soh_ci    ]))
87dfe32fa1Soh_ci}
88dfe32fa1Soh_ci
89dfe32fa1Soh_cipub(crate) fn add_default_asset(alias: &[u8], secret: &[u8]) -> Result<()> {
90dfe32fa1Soh_ci    asset_sdk::Manager::build()?.add(&AssetMap::from([
91dfe32fa1Soh_ci        (Tag::Alias, Value::Bytes(alias.to_vec())),
92dfe32fa1Soh_ci        (Tag::Secret, Value::Bytes(secret.to_vec())),
93dfe32fa1Soh_ci        (Tag::Accessibility, Value::Number(Accessibility::DevicePowerOn as u32)),
94dfe32fa1Soh_ci    ]))
95dfe32fa1Soh_ci}
96dfe32fa1Soh_ci
97dfe32fa1Soh_cipub(crate) fn add_default_auth_asset(alias: &[u8], secret: &[u8]) -> Result<()> {
98dfe32fa1Soh_ci    asset_sdk::Manager::build()?.add(&AssetMap::from([
99dfe32fa1Soh_ci        (Tag::Alias, Value::Bytes(alias.to_vec())),
100dfe32fa1Soh_ci        (Tag::Secret, Value::Bytes(secret.to_vec())),
101dfe32fa1Soh_ci        (Tag::Accessibility, Value::Number(Accessibility::DevicePowerOn as u32)),
102dfe32fa1Soh_ci        (Tag::AuthType, Value::Number(AuthType::Any as u32)),
103dfe32fa1Soh_ci    ]))
104dfe32fa1Soh_ci}
105dfe32fa1Soh_ci
106dfe32fa1Soh_cipub(crate) fn add_all_tags_asset(alias: &[u8]) -> Result<()> {
107dfe32fa1Soh_ci    let mut attrs = AssetMap::new();
108dfe32fa1Soh_ci    attrs.insert_attr(Tag::Alias, alias.to_vec());
109dfe32fa1Soh_ci    attrs.insert_attr(Tag::Secret, SECRET.to_vec());
110dfe32fa1Soh_ci    attrs.insert_attr(Tag::DataLabelNormal1, NORMAL_LABEL1.to_owned());
111dfe32fa1Soh_ci    attrs.insert_attr(Tag::DataLabelNormal2, NORMAL_LABEL2.to_owned());
112dfe32fa1Soh_ci    attrs.insert_attr(Tag::DataLabelNormal3, NORMAL_LABEL3.to_owned());
113dfe32fa1Soh_ci    attrs.insert_attr(Tag::DataLabelNormal4, NORMAL_LABEL4.to_owned());
114dfe32fa1Soh_ci    attrs.insert_attr(Tag::DataLabelNormalLocal1, NORMAL_LOCAL_LABEL1.to_owned());
115dfe32fa1Soh_ci    attrs.insert_attr(Tag::DataLabelNormalLocal2, NORMAL_LOCAL_LABEL2.to_owned());
116dfe32fa1Soh_ci    attrs.insert_attr(Tag::DataLabelNormalLocal3, NORMAL_LOCAL_LABEL3.to_owned());
117dfe32fa1Soh_ci    attrs.insert_attr(Tag::DataLabelNormalLocal4, NORMAL_LOCAL_LABEL4.to_owned());
118dfe32fa1Soh_ci    attrs.insert_attr(Tag::DataLabelCritical1, CRITICAL_LABEL1.to_owned());
119dfe32fa1Soh_ci    attrs.insert_attr(Tag::DataLabelCritical2, CRITICAL_LABEL2.to_owned());
120dfe32fa1Soh_ci    attrs.insert_attr(Tag::DataLabelCritical3, CRITICAL_LABEL3.to_owned());
121dfe32fa1Soh_ci    attrs.insert_attr(Tag::DataLabelCritical4, CRITICAL_LABEL4.to_owned());
122dfe32fa1Soh_ci    attrs.insert_attr(Tag::Accessibility, Accessibility::DevicePowerOn);
123dfe32fa1Soh_ci    attrs.insert_attr(Tag::AuthType, AuthType::Any);
124dfe32fa1Soh_ci    attrs.insert_attr(Tag::SyncType, SyncType::ThisDevice);
125dfe32fa1Soh_ci    attrs.insert_attr(Tag::RequirePasswordSet, false);
126dfe32fa1Soh_ci    attrs.insert_attr(Tag::ConflictResolution, ConflictResolution::Overwrite);
127dfe32fa1Soh_ci    asset_sdk::Manager::build().unwrap().add(&attrs)
128dfe32fa1Soh_ci}
129dfe32fa1Soh_ci
130dfe32fa1Soh_cipub(crate) fn expect_error_eq(expect_err: ErrCode, real_err: AssetError) {
131dfe32fa1Soh_ci    assert_eq!(expect_err, real_err.code)
132dfe32fa1Soh_ci}
133