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