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_ciuse std::collections::HashMap; 17dfe32fa1Soh_ci 18dfe32fa1Soh_ciuse crate::common::*; 19dfe32fa1Soh_ciuse asset_sdk::*; 20dfe32fa1Soh_ci 21dfe32fa1Soh_ci#[test] 22dfe32fa1Soh_cifn query_non_exist_with_alias() { 23dfe32fa1Soh_ci let alias = function!().as_bytes(); 24dfe32fa1Soh_ci expect_error_eq(ErrCode::NotFound, query_attr_by_alias(alias).unwrap_err()); 25dfe32fa1Soh_ci expect_error_eq(ErrCode::NotFound, query_all_by_alias(alias).unwrap_err()); 26dfe32fa1Soh_ci} 27dfe32fa1Soh_ci 28dfe32fa1Soh_ci#[test] 29dfe32fa1Soh_cifn query_with_wrong_alias() { 30dfe32fa1Soh_ci let function_name = function!().as_bytes(); 31dfe32fa1Soh_ci add_default_asset(function_name, function_name).unwrap(); 32dfe32fa1Soh_ci 33dfe32fa1Soh_ci let alias_new = "query_with_wrong_alias_wrong_alias".as_bytes(); 34dfe32fa1Soh_ci expect_error_eq(ErrCode::NotFound, query_attr_by_alias(alias_new).unwrap_err()); 35dfe32fa1Soh_ci expect_error_eq(ErrCode::NotFound, query_all_by_alias(alias_new).unwrap_err()); 36dfe32fa1Soh_ci remove_by_alias(function_name).unwrap(); 37dfe32fa1Soh_ci} 38dfe32fa1Soh_ci 39dfe32fa1Soh_ci#[test] 40dfe32fa1Soh_cifn query_non_exist_without_alias() { 41dfe32fa1Soh_ci let mut query = AssetMap::new(); 42dfe32fa1Soh_ci query.insert_attr(Tag::RequirePasswordSet, true); 43dfe32fa1Soh_ci expect_error_eq(ErrCode::NotFound, asset_sdk::Manager::build().unwrap().query(&query).unwrap_err()); 44dfe32fa1Soh_ci} 45dfe32fa1Soh_ci 46dfe32fa1Soh_ci#[test] 47dfe32fa1Soh_cifn query_without_alias_with_wrong_condition() { 48dfe32fa1Soh_ci let function_name = function!().as_bytes(); 49dfe32fa1Soh_ci let mut add = AssetMap::new(); 50dfe32fa1Soh_ci add.insert_attr(Tag::RequirePasswordSet, false); 51dfe32fa1Soh_ci add.insert_attr(Tag::Alias, function_name.to_owned()); 52dfe32fa1Soh_ci add.insert_attr(Tag::Secret, function_name.to_owned()); 53dfe32fa1Soh_ci add.insert_attr(Tag::Accessibility, Accessibility::DevicePowerOn); 54dfe32fa1Soh_ci asset_sdk::Manager::build().unwrap().add(&add).unwrap(); 55dfe32fa1Soh_ci 56dfe32fa1Soh_ci let mut query = AssetMap::new(); 57dfe32fa1Soh_ci query.insert_attr(Tag::RequirePasswordSet, true); 58dfe32fa1Soh_ci expect_error_eq(ErrCode::NotFound, asset_sdk::Manager::build().unwrap().query(&query).unwrap_err()); 59dfe32fa1Soh_ci 60dfe32fa1Soh_ci remove_by_alias(function_name).unwrap(); 61dfe32fa1Soh_ci} 62dfe32fa1Soh_ci 63dfe32fa1Soh_ci#[test] 64dfe32fa1Soh_cifn query_without_limit() { 65dfe32fa1Soh_ci let _ = remove_all(); 66dfe32fa1Soh_ci let function_name = function!().as_bytes(); 67dfe32fa1Soh_ci let asset_num = 10; 68dfe32fa1Soh_ci for i in 0..asset_num { 69dfe32fa1Soh_ci let new_name = format!("{:?}{}", function_name, i); 70dfe32fa1Soh_ci add_default_asset(new_name.as_bytes(), new_name.as_bytes()).unwrap(); 71dfe32fa1Soh_ci } 72dfe32fa1Soh_ci 73dfe32fa1Soh_ci let query = AssetMap::new(); 74dfe32fa1Soh_ci 75dfe32fa1Soh_ci assert_eq!(asset_num, asset_sdk::Manager::build().unwrap().query(&query).unwrap().len() as u32); 76dfe32fa1Soh_ci 77dfe32fa1Soh_ci for i in 0..asset_num { 78dfe32fa1Soh_ci let new_name = format!("{:?}{}", function_name, i); 79dfe32fa1Soh_ci remove_by_alias(new_name.as_bytes()).unwrap(); 80dfe32fa1Soh_ci } 81dfe32fa1Soh_ci} 82dfe32fa1Soh_ci 83dfe32fa1Soh_ci#[test] 84dfe32fa1Soh_cifn query_with_offset_without_limit() { 85dfe32fa1Soh_ci let _ = remove_all(); 86dfe32fa1Soh_ci let function_name = function!().as_bytes(); 87dfe32fa1Soh_ci let asset_num = 10; 88dfe32fa1Soh_ci for i in 0..asset_num { 89dfe32fa1Soh_ci let new_name = format!("{:?}{}", function_name, i); 90dfe32fa1Soh_ci add_default_asset(new_name.as_bytes(), new_name.as_bytes()).unwrap(); 91dfe32fa1Soh_ci } 92dfe32fa1Soh_ci 93dfe32fa1Soh_ci let offset = 15; 94dfe32fa1Soh_ci let query = AssetMap::from([(Tag::ReturnOffset, Value::Number(offset))]); 95dfe32fa1Soh_ci expect_error_eq(ErrCode::NotFound, asset_sdk::Manager::build().unwrap().query(&query).unwrap_err()); 96dfe32fa1Soh_ci 97dfe32fa1Soh_ci let offset = 3; 98dfe32fa1Soh_ci let query = AssetMap::from([(Tag::ReturnOffset, Value::Number(offset))]); 99dfe32fa1Soh_ci assert_eq!(asset_num - offset, asset_sdk::Manager::build().unwrap().query(&query).unwrap().len() as u32); 100dfe32fa1Soh_ci 101dfe32fa1Soh_ci for i in 0..(asset_num - offset) { 102dfe32fa1Soh_ci let new_name = format!("{:?}{}", function_name, i + offset); 103dfe32fa1Soh_ci remove_by_alias(new_name.as_bytes()).unwrap(); 104dfe32fa1Soh_ci } 105dfe32fa1Soh_ci} 106dfe32fa1Soh_ci 107dfe32fa1Soh_ci#[test] 108dfe32fa1Soh_cifn query_with_limit_with_without_offset() { 109dfe32fa1Soh_ci let _ = remove_all(); 110dfe32fa1Soh_ci let function_name = function!().as_bytes(); 111dfe32fa1Soh_ci let asset_num = 10; 112dfe32fa1Soh_ci for i in 0..asset_num { 113dfe32fa1Soh_ci let new_name = format!("{:?}{}", function_name, i); 114dfe32fa1Soh_ci add_default_asset(new_name.as_bytes(), new_name.as_bytes()).unwrap(); 115dfe32fa1Soh_ci } 116dfe32fa1Soh_ci 117dfe32fa1Soh_ci let mut query = AssetMap::new(); 118dfe32fa1Soh_ci let limit = 15u32; 119dfe32fa1Soh_ci query.insert_attr(Tag::ReturnLimit, limit); 120dfe32fa1Soh_ci let assets = asset_sdk::Manager::build().unwrap().query(&query).unwrap(); 121dfe32fa1Soh_ci assert_eq!(asset_num, assets.len() as u32); 122dfe32fa1Soh_ci 123dfe32fa1Soh_ci let limit = 3u32; 124dfe32fa1Soh_ci query.insert_attr(Tag::ReturnLimit, limit); 125dfe32fa1Soh_ci let assets = asset_sdk::Manager::build().unwrap().query(&query).unwrap(); 126dfe32fa1Soh_ci assert_eq!(limit, assets.len() as u32); 127dfe32fa1Soh_ci for (i, asset) in assets.iter().enumerate() { 128dfe32fa1Soh_ci assert!(asset.get_bytes_attr(&Tag::Alias).unwrap().eq(format!("{:?}{}", function_name, i).as_bytes())); 129dfe32fa1Soh_ci } 130dfe32fa1Soh_ci 131dfe32fa1Soh_ci let offset = 2u32; 132dfe32fa1Soh_ci query.insert_attr(Tag::ReturnOffset, offset); 133dfe32fa1Soh_ci let assets = asset_sdk::Manager::build().unwrap().query(&query).unwrap(); 134dfe32fa1Soh_ci assert_eq!(limit, assets.len() as u32); 135dfe32fa1Soh_ci for (i, asset) in assets.iter().enumerate() { 136dfe32fa1Soh_ci assert!(asset 137dfe32fa1Soh_ci .get_bytes_attr(&Tag::Alias) 138dfe32fa1Soh_ci .unwrap() 139dfe32fa1Soh_ci .eq(format!("{:?}{}", function_name, i + offset as usize).as_bytes())); 140dfe32fa1Soh_ci } 141dfe32fa1Soh_ci 142dfe32fa1Soh_ci for i in 0..asset_num { 143dfe32fa1Soh_ci let new_name = format!("{:?}{}", function_name, i); 144dfe32fa1Soh_ci remove_by_alias(new_name.as_bytes()).unwrap(); 145dfe32fa1Soh_ci } 146dfe32fa1Soh_ci} 147dfe32fa1Soh_ci 148dfe32fa1Soh_ci#[test] 149dfe32fa1Soh_cifn query_with_without_return_type() { 150dfe32fa1Soh_ci let function_name = function!().as_bytes(); 151dfe32fa1Soh_ci add_default_asset(function_name, function_name).unwrap(); 152dfe32fa1Soh_ci 153dfe32fa1Soh_ci assert!(!query_attr_by_alias(function_name).unwrap()[0].contains_key(&Tag::Secret)); 154dfe32fa1Soh_ci assert!(query_all_by_alias(function_name).unwrap()[0].contains_key(&Tag::Secret)); 155dfe32fa1Soh_ci 156dfe32fa1Soh_ci remove_by_alias(function_name).unwrap(); 157dfe32fa1Soh_ci} 158dfe32fa1Soh_ci 159dfe32fa1Soh_ci#[test] 160dfe32fa1Soh_cifn query_with_secret() { 161dfe32fa1Soh_ci let function_name = function!().as_bytes(); 162dfe32fa1Soh_ci add_default_asset(function_name, function_name).unwrap(); 163dfe32fa1Soh_ci 164dfe32fa1Soh_ci let query = AssetMap::from([(Tag::Secret, Value::Bytes(function_name.to_vec()))]); 165dfe32fa1Soh_ci expect_error_eq(ErrCode::InvalidArgument, asset_sdk::Manager::build().unwrap().query(&query).unwrap_err()); 166dfe32fa1Soh_ci 167dfe32fa1Soh_ci remove_by_alias(function_name).unwrap(); 168dfe32fa1Soh_ci} 169dfe32fa1Soh_ci 170dfe32fa1Soh_ci#[test] 171dfe32fa1Soh_cifn query_with_return_all_without_alias() { 172dfe32fa1Soh_ci let function_name = function!().as_bytes(); 173dfe32fa1Soh_ci add_default_asset(function_name, function_name).unwrap(); 174dfe32fa1Soh_ci 175dfe32fa1Soh_ci let query = AssetMap::from([(Tag::ReturnType, Value::Number(ReturnType::All as u32))]); 176dfe32fa1Soh_ci expect_error_eq(ErrCode::Unsupported, asset_sdk::Manager::build().unwrap().query(&query).unwrap_err()); 177dfe32fa1Soh_ci 178dfe32fa1Soh_ci remove_by_alias(function_name).unwrap(); 179dfe32fa1Soh_ci} 180dfe32fa1Soh_ci 181dfe32fa1Soh_cifn query_with_order(order: &[u8], suffix: &[&[u8]]) { 182dfe32fa1Soh_ci let _ = remove_all(); 183dfe32fa1Soh_ci let function_name = function!(); 184dfe32fa1Soh_ci let asset_num = 4; 185dfe32fa1Soh_ci let mut add = AssetMap::new(); 186dfe32fa1Soh_ci add.insert_attr(Tag::Secret, function_name.as_bytes().to_vec()); 187dfe32fa1Soh_ci for item in suffix.iter().take(asset_num) { 188dfe32fa1Soh_ci let mut alias_new: Vec<u8> = Vec::new(); 189dfe32fa1Soh_ci alias_new.extend_from_slice(function_name.as_bytes()); 190dfe32fa1Soh_ci alias_new.extend_from_slice(item); 191dfe32fa1Soh_ci let mut order_new: Vec<u8> = Vec::new(); 192dfe32fa1Soh_ci order_new.extend_from_slice(order); 193dfe32fa1Soh_ci order_new.extend_from_slice(item); 194dfe32fa1Soh_ci add_default_asset(&alias_new, function_name.as_bytes()).unwrap(); 195dfe32fa1Soh_ci } 196dfe32fa1Soh_ci 197dfe32fa1Soh_ci let mut query = AssetMap::new(); 198dfe32fa1Soh_ci let limit = 3u32; 199dfe32fa1Soh_ci query.insert_attr(Tag::ReturnLimit, limit); 200dfe32fa1Soh_ci query.insert_attr(Tag::ReturnOrderedBy, Tag::DataLabelNormal1 as u32); 201dfe32fa1Soh_ci let assets = asset_sdk::Manager::build().unwrap().query(&query).unwrap(); 202dfe32fa1Soh_ci assert_eq!(limit, assets.len() as u32); 203dfe32fa1Soh_ci 204dfe32fa1Soh_ci for (i, asset) in assets.iter().enumerate() { 205dfe32fa1Soh_ci let get_alias = asset.get_bytes_attr(&Tag::Alias).unwrap(); 206dfe32fa1Soh_ci 207dfe32fa1Soh_ci let mut alias_new: Vec<u8> = Vec::new(); 208dfe32fa1Soh_ci alias_new.extend_from_slice(function_name.as_bytes()); 209dfe32fa1Soh_ci alias_new.extend_from_slice(suffix[i]); 210dfe32fa1Soh_ci assert_eq!(&alias_new, get_alias); 211dfe32fa1Soh_ci } 212dfe32fa1Soh_ci 213dfe32fa1Soh_ci for item in suffix.iter().take(asset_num) { 214dfe32fa1Soh_ci let mut alias_new: Vec<u8> = Vec::new(); 215dfe32fa1Soh_ci alias_new.extend_from_slice(function_name.as_bytes()); 216dfe32fa1Soh_ci alias_new.extend_from_slice(item); 217dfe32fa1Soh_ci 218dfe32fa1Soh_ci remove_by_alias(&alias_new).unwrap(); 219dfe32fa1Soh_ci } 220dfe32fa1Soh_ci} 221dfe32fa1Soh_ci 222dfe32fa1Soh_ci#[test] 223dfe32fa1Soh_cifn query_with_order_with_english() { 224dfe32fa1Soh_ci let order = "query_with_order_with_english".as_bytes(); 225dfe32fa1Soh_ci let mut suffix: Vec<&[u8]> = ["one", "two", "three", "four"].iter().map(|s| s.as_bytes()).collect(); 226dfe32fa1Soh_ci suffix.sort_by(|a, b| b.cmp(a)); 227dfe32fa1Soh_ci query_with_order(order, &suffix); 228dfe32fa1Soh_ci} 229dfe32fa1Soh_ci 230dfe32fa1Soh_ci#[test] 231dfe32fa1Soh_cifn query_with_order_with_chinese() { 232dfe32fa1Soh_ci let order = "排序".as_bytes(); 233dfe32fa1Soh_ci let mut suffix: Vec<&[u8]> = ["一", "二", "三", "四"].iter().map(|s| s.as_bytes()).collect(); 234dfe32fa1Soh_ci suffix.sort_by(|a, b| b.cmp(a)); 235dfe32fa1Soh_ci query_with_order(order, &suffix); 236dfe32fa1Soh_ci} 237dfe32fa1Soh_ci 238dfe32fa1Soh_ci#[test] 239dfe32fa1Soh_cifn query_with_order_with_number() { 240dfe32fa1Soh_ci let order = "123".as_bytes(); 241dfe32fa1Soh_ci let mut suffix: Vec<&[u8]> = ["11", "22", "33", "44"].iter().map(|s| s.as_bytes()).collect(); 242dfe32fa1Soh_ci suffix.sort_by(|a, b| b.cmp(a)); 243dfe32fa1Soh_ci query_with_order(order, &suffix); 244dfe32fa1Soh_ci} 245dfe32fa1Soh_ci 246dfe32fa1Soh_ci#[test] 247dfe32fa1Soh_cifn query_with_order_with_unreadible() { 248dfe32fa1Soh_ci let order = [11u8, 22u8, 33u8]; 249dfe32fa1Soh_ci let suffix: Vec<[u8; 4]> = [11u32, 22u32, 33u32, 44u32].iter().map(|s| s.to_le_bytes()).collect(); 250dfe32fa1Soh_ci 251dfe32fa1Soh_ci let mut suffix: Vec<&[u8]> = suffix.iter().map(|s| s as &[u8]).collect(); 252dfe32fa1Soh_ci suffix.sort_by(|a, b| b.cmp(a)); 253dfe32fa1Soh_ci query_with_order(&order, &suffix); 254dfe32fa1Soh_ci} 255dfe32fa1Soh_ci 256dfe32fa1Soh_ci#[test] 257dfe32fa1Soh_cifn query_with_wrong_auth_token() { 258dfe32fa1Soh_ci let function_name = function!().as_bytes(); 259dfe32fa1Soh_ci add_default_auth_asset(function_name, function_name).unwrap(); 260dfe32fa1Soh_ci 261dfe32fa1Soh_ci let mut query = AssetMap::new(); 262dfe32fa1Soh_ci let challenge = asset_sdk::Manager::build().unwrap().pre_query(&query).unwrap(); 263dfe32fa1Soh_ci query.insert_attr(Tag::Alias, function_name.to_owned()); 264dfe32fa1Soh_ci query.insert_attr(Tag::AuthType, AuthType::Any); 265dfe32fa1Soh_ci query.insert_attr(Tag::ReturnType, ReturnType::All); 266dfe32fa1Soh_ci query.insert_attr(Tag::AuthToken, vec![0; AUTH_TOKEN_SIZE]); 267dfe32fa1Soh_ci query.insert_attr(Tag::AuthChallenge, challenge.clone()); 268dfe32fa1Soh_ci expect_error_eq(ErrCode::AccessDenied, asset_sdk::Manager::build().unwrap().query(&query).unwrap_err()); 269dfe32fa1Soh_ci 270dfe32fa1Soh_ci let mut query = AssetMap::new(); 271dfe32fa1Soh_ci query.insert_attr(Tag::AuthChallenge, challenge); 272dfe32fa1Soh_ci asset_sdk::Manager::build().unwrap().post_query(&query).unwrap(); 273dfe32fa1Soh_ci remove_by_alias(function_name).unwrap(); 274dfe32fa1Soh_ci} 275dfe32fa1Soh_ci 276dfe32fa1Soh_ci#[test] 277dfe32fa1Soh_cifn query_with_bytes_tag() { 278dfe32fa1Soh_ci let function_name = function!().as_bytes(); 279dfe32fa1Soh_ci add_all_tags_asset(function_name).unwrap(); 280dfe32fa1Soh_ci let alias = "default_alias".as_bytes(); 281dfe32fa1Soh_ci add_default_asset(alias, alias).unwrap(); 282dfe32fa1Soh_ci 283dfe32fa1Soh_ci let mut bytes_tags: HashMap<Tag, &[u8]> = HashMap::new(); 284dfe32fa1Soh_ci bytes_tags.insert(Tag::Alias, function_name); 285dfe32fa1Soh_ci bytes_tags.insert(Tag::DataLabelCritical1, CRITICAL_LABEL1); 286dfe32fa1Soh_ci bytes_tags.insert(Tag::DataLabelCritical2, CRITICAL_LABEL2); 287dfe32fa1Soh_ci bytes_tags.insert(Tag::DataLabelCritical3, CRITICAL_LABEL3); 288dfe32fa1Soh_ci bytes_tags.insert(Tag::DataLabelCritical4, CRITICAL_LABEL4); 289dfe32fa1Soh_ci bytes_tags.insert(Tag::DataLabelNormal1, NORMAL_LABEL1); 290dfe32fa1Soh_ci bytes_tags.insert(Tag::DataLabelNormal2, NORMAL_LABEL2); 291dfe32fa1Soh_ci bytes_tags.insert(Tag::DataLabelNormal3, NORMAL_LABEL3); 292dfe32fa1Soh_ci bytes_tags.insert(Tag::DataLabelNormal4, NORMAL_LABEL4); 293dfe32fa1Soh_ci bytes_tags.insert(Tag::DataLabelNormalLocal1, NORMAL_LOCAL_LABEL1); 294dfe32fa1Soh_ci bytes_tags.insert(Tag::DataLabelNormalLocal2, NORMAL_LOCAL_LABEL2); 295dfe32fa1Soh_ci bytes_tags.insert(Tag::DataLabelNormalLocal3, NORMAL_LOCAL_LABEL3); 296dfe32fa1Soh_ci bytes_tags.insert(Tag::DataLabelNormalLocal4, NORMAL_LOCAL_LABEL4); 297dfe32fa1Soh_ci 298dfe32fa1Soh_ci for (tag, val) in bytes_tags.into_iter() { 299dfe32fa1Soh_ci let mut query = AssetMap::new(); 300dfe32fa1Soh_ci query.insert_attr(tag, val.to_vec()); 301dfe32fa1Soh_ci let assets = asset_sdk::Manager::build().unwrap().query(&query).unwrap(); 302dfe32fa1Soh_ci assert_eq!(1, assets.len() as u32); 303dfe32fa1Soh_ci } 304dfe32fa1Soh_ci remove_by_alias(function_name).unwrap(); 305dfe32fa1Soh_ci remove_by_alias(alias).unwrap(); 306dfe32fa1Soh_ci} 307dfe32fa1Soh_ci 308dfe32fa1Soh_ci#[test] 309dfe32fa1Soh_cifn query_with_accessibility() { 310dfe32fa1Soh_ci let function_name = function!().as_bytes(); 311dfe32fa1Soh_ci add_all_tags_asset(function_name).unwrap(); 312dfe32fa1Soh_ci 313dfe32fa1Soh_ci let mut query = AssetMap::new(); 314dfe32fa1Soh_ci query.insert_attr(Tag::Accessibility, Accessibility::DevicePowerOn); 315dfe32fa1Soh_ci let assets = asset_sdk::Manager::build().unwrap().query(&query).unwrap(); 316dfe32fa1Soh_ci assert_eq!(1, assets.len() as u32); 317dfe32fa1Soh_ci 318dfe32fa1Soh_ci query.insert_attr(Tag::Accessibility, Accessibility::DeviceUnlocked); 319dfe32fa1Soh_ci expect_error_eq(ErrCode::NotFound, asset_sdk::Manager::build().unwrap().query(&query).unwrap_err()); 320dfe32fa1Soh_ci 321dfe32fa1Soh_ci query.insert_attr(Tag::Accessibility, Accessibility::DeviceFirstUnlocked); 322dfe32fa1Soh_ci expect_error_eq(ErrCode::NotFound, asset_sdk::Manager::build().unwrap().query(&query).unwrap_err()); 323dfe32fa1Soh_ci remove_by_alias(function_name).unwrap(); 324dfe32fa1Soh_ci} 325dfe32fa1Soh_ci 326dfe32fa1Soh_ci#[test] 327dfe32fa1Soh_cifn query_with_return_order_by() { 328dfe32fa1Soh_ci let alias = "return_order_by_alias".as_bytes(); 329dfe32fa1Soh_ci let mut attrs = AssetMap::new(); 330dfe32fa1Soh_ci let normal_label = "return_order_by_label1".as_bytes(); 331dfe32fa1Soh_ci attrs.insert_attr(Tag::Alias, alias.to_vec()); 332dfe32fa1Soh_ci attrs.insert_attr(Tag::Secret, alias.to_vec()); 333dfe32fa1Soh_ci attrs.insert_attr(Tag::DataLabelNormal1, normal_label.to_vec()); 334dfe32fa1Soh_ci attrs.insert_attr(Tag::Accessibility, Accessibility::DevicePowerOn as u32); 335dfe32fa1Soh_ci asset_sdk::Manager::build().unwrap().add(&attrs).unwrap(); 336dfe32fa1Soh_ci 337dfe32fa1Soh_ci let function_name = function!().as_bytes(); 338dfe32fa1Soh_ci add_all_tags_asset(function_name).unwrap(); 339dfe32fa1Soh_ci 340dfe32fa1Soh_ci let mut query = AssetMap::new(); 341dfe32fa1Soh_ci query.insert_attr(Tag::ReturnOrderedBy, Tag::DataLabelNormal1 as u32); 342dfe32fa1Soh_ci let mut assets = asset_sdk::Manager::build().unwrap().query(&query).unwrap(); 343dfe32fa1Soh_ci let expect_query_len = 2; 344dfe32fa1Soh_ci assert_eq!(expect_query_len, assets.len() as u32); 345dfe32fa1Soh_ci assert!(assets[0].get_bytes_attr(&Tag::DataLabelNormal1).unwrap().eq(NORMAL_LABEL1)); 346dfe32fa1Soh_ci 347dfe32fa1Soh_ci query.remove(&Tag::ReturnOrderedBy); 348dfe32fa1Soh_ci assets = asset_sdk::Manager::build().unwrap().query(&query).unwrap(); 349dfe32fa1Soh_ci assert_eq!(expect_query_len, assets.len() as u32); 350dfe32fa1Soh_ci assert!(!assets[0].get_bytes_attr(&Tag::DataLabelNormal1).unwrap().eq(NORMAL_LABEL1)); 351dfe32fa1Soh_ci 352dfe32fa1Soh_ci remove_by_alias(function_name).unwrap(); 353dfe32fa1Soh_ci remove_by_alias(alias).unwrap(); 354dfe32fa1Soh_ci} 355dfe32fa1Soh_ci 356dfe32fa1Soh_ci#[test] 357dfe32fa1Soh_cifn query_with_return_limit() { 358dfe32fa1Soh_ci let function_name = function!().as_bytes(); 359dfe32fa1Soh_ci add_all_tags_asset(function_name).unwrap(); 360dfe32fa1Soh_ci let alias = "default_return_limit".as_bytes(); 361dfe32fa1Soh_ci add_default_asset(alias, alias).unwrap(); 362dfe32fa1Soh_ci 363dfe32fa1Soh_ci let mut query = AssetMap::new(); 364dfe32fa1Soh_ci query.insert_attr(Tag::ReturnLimit, 1); 365dfe32fa1Soh_ci let mut assets = asset_sdk::Manager::build().unwrap().query(&query).unwrap(); 366dfe32fa1Soh_ci assert_eq!(1, assets.len() as u32); 367dfe32fa1Soh_ci 368dfe32fa1Soh_ci query.insert_attr(Tag::ReturnLimit, 0); 369dfe32fa1Soh_ci expect_error_eq(ErrCode::InvalidArgument, asset_sdk::Manager::build().unwrap().query(&query).unwrap_err()); 370dfe32fa1Soh_ci 371dfe32fa1Soh_ci query.remove(&Tag::ReturnLimit); 372dfe32fa1Soh_ci assets = asset_sdk::Manager::build().unwrap().query(&query).unwrap(); 373dfe32fa1Soh_ci let expect_query_len = 2; 374dfe32fa1Soh_ci assert_eq!(expect_query_len, assets.len() as u32); 375dfe32fa1Soh_ci remove_by_alias(function_name).unwrap(); 376dfe32fa1Soh_ci remove_by_alias(alias).unwrap(); 377dfe32fa1Soh_ci} 378dfe32fa1Soh_ci 379dfe32fa1Soh_ci#[test] 380dfe32fa1Soh_cifn query_with_return_offset() { 381dfe32fa1Soh_ci let function_name = function!().as_bytes(); 382dfe32fa1Soh_ci add_all_tags_asset(function_name).unwrap(); 383dfe32fa1Soh_ci let alias = "default_return_offset".as_bytes(); 384dfe32fa1Soh_ci add_default_asset(alias, alias).unwrap(); 385dfe32fa1Soh_ci 386dfe32fa1Soh_ci let mut query = AssetMap::new(); 387dfe32fa1Soh_ci query.insert_attr(Tag::ReturnOffset, 1); 388dfe32fa1Soh_ci let mut assets = asset_sdk::Manager::build().unwrap().query(&query).unwrap(); 389dfe32fa1Soh_ci assert_eq!(1, assets.len() as u32); 390dfe32fa1Soh_ci 391dfe32fa1Soh_ci query.remove(&Tag::ReturnOffset); 392dfe32fa1Soh_ci assets = asset_sdk::Manager::build().unwrap().query(&query).unwrap(); 393dfe32fa1Soh_ci let expect_query_len = 2; 394dfe32fa1Soh_ci assert_eq!(expect_query_len, assets.len() as u32); 395dfe32fa1Soh_ci 396dfe32fa1Soh_ci let offset_start = 2; 397dfe32fa1Soh_ci query.insert_attr(Tag::ReturnOffset, offset_start); 398dfe32fa1Soh_ci expect_error_eq(ErrCode::NotFound, asset_sdk::Manager::build().unwrap().query(&query).unwrap_err()); 399dfe32fa1Soh_ci remove_by_alias(function_name).unwrap(); 400dfe32fa1Soh_ci remove_by_alias(alias).unwrap(); 401dfe32fa1Soh_ci} 402dfe32fa1Soh_ci 403dfe32fa1Soh_ci#[test] 404dfe32fa1Soh_cifn query_with_return_type() { 405dfe32fa1Soh_ci let function_name = function!().as_bytes(); 406dfe32fa1Soh_ci add_all_tags_asset(function_name).unwrap(); 407dfe32fa1Soh_ci 408dfe32fa1Soh_ci let mut query = AssetMap::new(); 409dfe32fa1Soh_ci query.insert_attr(Tag::ReturnType, ReturnType::Attributes as u32); 410dfe32fa1Soh_ci let assets = asset_sdk::Manager::build().unwrap().query(&query).unwrap(); 411dfe32fa1Soh_ci assert_eq!(1, assets.len() as u32); 412dfe32fa1Soh_ci 413dfe32fa1Soh_ci query.insert_attr(Tag::ReturnType, ReturnType::All as u32); 414dfe32fa1Soh_ci expect_error_eq(ErrCode::Unsupported, asset_sdk::Manager::build().unwrap().query(&query).unwrap_err()); 415dfe32fa1Soh_ci remove_by_alias(function_name).unwrap(); 416dfe32fa1Soh_ci} 417dfe32fa1Soh_ci 418dfe32fa1Soh_ci#[test] 419dfe32fa1Soh_cifn query_with_auth_type() { 420dfe32fa1Soh_ci let function_name = function!().as_bytes(); 421dfe32fa1Soh_ci add_all_tags_asset(function_name).unwrap(); 422dfe32fa1Soh_ci let alias = "default_number_alias".as_bytes(); 423dfe32fa1Soh_ci add_default_asset(alias, alias).unwrap(); 424dfe32fa1Soh_ci 425dfe32fa1Soh_ci for auth_type in vec![AuthType::Any as u32, AuthType::None as u32].into_iter() { 426dfe32fa1Soh_ci let mut query = AssetMap::new(); 427dfe32fa1Soh_ci query.insert_attr(Tag::AuthType, auth_type); 428dfe32fa1Soh_ci let assets = asset_sdk::Manager::build().unwrap().query(&query).unwrap(); 429dfe32fa1Soh_ci assert_eq!(1, assets.len() as u32); 430dfe32fa1Soh_ci } 431dfe32fa1Soh_ci remove_by_alias(function_name).unwrap(); 432dfe32fa1Soh_ci remove_by_alias(alias).unwrap(); 433dfe32fa1Soh_ci} 434dfe32fa1Soh_ci 435dfe32fa1Soh_ci#[test] 436dfe32fa1Soh_cifn query_with_sync_type() { 437dfe32fa1Soh_ci let function_name = function!().as_bytes(); 438dfe32fa1Soh_ci add_all_tags_asset(function_name).unwrap(); 439dfe32fa1Soh_ci 440dfe32fa1Soh_ci let mut query = AssetMap::new(); 441dfe32fa1Soh_ci let mut sync_type = SyncType::ThisDevice as u32 | SyncType::TrustedDevice as u32 | SyncType::TrustedAccount as u32; 442dfe32fa1Soh_ci query.insert_attr(Tag::SyncType, sync_type); 443dfe32fa1Soh_ci expect_error_eq(ErrCode::NotFound, asset_sdk::Manager::build().unwrap().query(&query).unwrap_err()); 444dfe32fa1Soh_ci 445dfe32fa1Soh_ci sync_type = SyncType::ThisDevice as u32; 446dfe32fa1Soh_ci query.insert_attr(Tag::SyncType, sync_type); 447dfe32fa1Soh_ci let assets = asset_sdk::Manager::build().unwrap().query(&query).unwrap(); 448dfe32fa1Soh_ci assert_eq!(1, assets.len() as u32); 449dfe32fa1Soh_ci 450dfe32fa1Soh_ci remove_by_alias(function_name).unwrap(); 451dfe32fa1Soh_ci} 452