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 #[cfg(test)] 16 mod parser_tests { 17 use crate::{ 18 host_app::HostAppTask, 19 parser::{self, Parsed, ParsedCommand}, 20 }; 21 use hdc::config::{self, HdcCommand}; 22 23 #[test] if_parse_cmd_param_worksnull24 fn if_parse_cmd_param_works() { 25 let input = "file recv file1 /data/local/tmp/file" 26 .split(" ") 27 .map(|s| s.to_string()) 28 .collect(); 29 let expected = Parsed { 30 options: vec![], 31 command: Some(HdcCommand::FileInit), 32 parameters: "file recv file1 /data/local/tmp/file" 33 .split(" ") 34 .map(|s| s.to_string()) 35 .collect(), 36 }; 37 let actual = parser::split_opt_and_cmd(input); 38 assert_eq!(actual.options, expected.options); 39 assert_eq!(actual.command, expected.command); 40 assert_eq!(actual.parameters, expected.parameters); 41 } 42 43 #[test] if_parse_opt_cmd_worksnull44 fn if_parse_opt_cmd_works() { 45 let input = "-l5 checkserver" 46 .split(" ") 47 .map(|s| s.to_string()) 48 .collect(); 49 let expected = Parsed { 50 options: vec!["-l5".to_string()], 51 command: Some(HdcCommand::KernelCheckServer), 52 parameters: vec![], 53 }; 54 let actual = parser::split_opt_and_cmd(input); 55 assert_eq!(actual.options, expected.options); 56 assert_eq!(actual.command, expected.command); 57 } 58 59 #[test] if_parse_opt_cmd_param_worksnull60 fn if_parse_opt_cmd_param_works() { 61 let input = "-l5 file recv file1 /data/local/tmp/file" 62 .split(" ") 63 .map(|s| s.to_string()) 64 .collect(); 65 let expected = Parsed { 66 options: vec!["-l5".to_string()], 67 command: Some(HdcCommand::FileInit), 68 parameters: "file recv file1 /data/local/tmp/file" 69 .split(" ") 70 .map(|s| s.to_string()) 71 .collect(), 72 }; 73 let actual = parser::split_opt_and_cmd(input); 74 assert_eq!(actual.options, expected.options); 75 assert_eq!(actual.command, expected.command); 76 assert_eq!(actual.parameters, expected.parameters); 77 } 78 79 #[test] if_extract_opt_lt_worksnull80 fn if_extract_opt_lt_works() { 81 let opts = "-l5 -t 123456".split(" ").map(|s| s.to_string()).collect(); 82 let expected = ParsedCommand { 83 run_in_server: false, 84 launch_server: true, 85 connect_key: "123456".to_string(), 86 log_level: 5, 87 server_addr: format!("0.0.0.0:{}", config::SERVER_DEFAULT_PORT), 88 ..Default::default() 89 }; 90 let actual = parser::extract_global_params(opts).unwrap(); 91 assert_eq!(actual, expected); 92 } 93 94 #[test] if_extract_opt_sm_worksnull95 fn if_extract_opt_sm_works() { 96 let opts = "-s 127.0.0.1:23333 -m" 97 .split(" ") 98 .map(|s| s.to_string()) 99 .collect(); 100 let expected = ParsedCommand { 101 run_in_server: true, 102 launch_server: true, 103 connect_key: "".to_string(), 104 log_level: 3, 105 server_addr: "127.0.0.1:23333".to_string(), 106 ..Default::default() 107 }; 108 let actual = parser::extract_global_params(opts).unwrap(); 109 assert_eq!(actual, expected); 110 } 111 112 #[test] if_extract_opt_port_worksnull113 fn if_extract_opt_port_works() { 114 let opts = "-s 23333".split(" ").map(|s| s.to_string()).collect(); 115 let expected = ParsedCommand { 116 run_in_server: false, 117 launch_server: true, 118 connect_key: "".to_string(), 119 log_level: 3, 120 server_addr: "127.0.0.1:23333".to_string(), 121 ..Default::default() 122 }; 123 let actual = parser::extract_global_params(opts).unwrap(); 124 assert_eq!(actual, expected); 125 } 126 127 #[test] if_extract_opt_ipv6_worksnull128 fn if_extract_opt_ipv6_works() { 129 let opts = "-s FC00:0:130F:0:0:9C0:876A:130B:23333 -p" 130 .split(" ") 131 .map(|s| s.to_string()) 132 .collect(); 133 let expected = ParsedCommand { 134 run_in_server: false, 135 launch_server: false, 136 connect_key: "".to_string(), 137 log_level: 3, 138 server_addr: "FC00:0:130F:0:0:9C0:876A:130B:23333".to_string(), 139 ..Default::default() 140 }; 141 let actual = parser::extract_global_params(opts).unwrap(); 142 assert_eq!(actual, expected); 143 } 144 145 #[test] if_extract_opt_invalid_ipv6_worksnull146 fn if_extract_opt_invalid_ipv6_works() { 147 let opts = "-s FC00:0:130F:0:0:9C0:876A:23333" 148 .split(" ") 149 .map(|s| s.to_string()) 150 .collect(); 151 let actual = parser::extract_global_params(opts); 152 assert!(actual.is_err()); 153 } 154 155 #[test] if_extract_opt_invalid_port_worksnull156 fn if_extract_opt_invalid_port_works() { 157 let opts = "-s 233333".split(" ").map(|s| s.to_string()).collect(); 158 let actual = parser::extract_global_params(opts); 159 assert!(actual.is_err()); 160 } 161 162 #[test] if_init_install_worksnull163 fn if_init_install_works() { 164 let mut task = HostAppTask::new(0, 0); 165 task.init_install(&String::from("-cwd \"/home/\" 1234.hap")); 166 167 assert_eq!(task.transfer.local_path, "/home/1234.hap"); 168 let ret = task 169 .transfer 170 .transfer_config 171 .optional_name 172 .ends_with(".hap"); 173 assert_eq!(ret, true); 174 } 175 } 176