• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2024 Huawei Device Co., Ltd.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 //     http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 #[cfg(test)]
15 mod ut_error {
16     use super::*;
17     use std::io;
18 
19     // Mock struct implementing CommonError for testing
20     struct MockCommonError {
21         code: u16,
22         msg: String,
23     }
24 
25     impl CommonError for MockCommonError {
code(&self) -> u1626         fn code(&self) -> u16 {
27             self.code
28         }
29 
msg(&self) -> &str30         fn msg(&self) -> &str {
31             &self.msg
32         }
33     }
34 
35     // @tc.name: ut_cache_download_error_new
36     // @tc.desc: Test CacheDownloadError creation with proper initialization
37     // @tc.precon: NA
38     // @tc.step: 1. Create CacheDownloadError with sample values
39     // 2. Verify all fields are initialized correctly
40     // @tc.expect: CacheDownloadError instance created with correct code, message and kind
41     // @tc.type: FUNC
42     // @tc.require: issueNumber
43     // @tc.level: Level 0
44     #[test]
ut_cache_download_error_new()45     fn ut_cache_download_error_new() {
46         let error = CacheDownloadError {
47             code: Some(404),
48             message: "Not Found".to_string(),
49             kind: ErrorKind::Http,
50         };
51 
52         assert_eq!(error.code(), 404);
53         assert_eq!(error.message(), "Not Found");
54         assert_eq!(error.ffi_kind(), ErrorKind::Http as i32);
55     }
56 
57     // @tc.name: ut_cache_download_error_code
58     // @tc.desc: Test code() method returns correct value
59     // @tc.precon: NA
60     // @tc.step: 1. Create CacheDownloadError with Some code
61     // 2. Call code() method
62     // 3. Verify returned value matches expected code
63     // @tc.expect: code() returns the stored error code
64     // @tc.type: FUNC
65     // @tc.require: issueNumber
66     // @tc.level: Level 1
67     #[test]
ut_cache_download_error_code()68     fn ut_cache_download_error_code() {
69         let error = CacheDownloadError {
70             code: Some(500),
71             message: "Internal Server Error".to_string(),
72             kind: ErrorKind::Http,
73         };
74 
75         assert_eq!(error.code(), 500);
76     }
77 
78     // @tc.name: ut_cache_download_error_code_default
79     // @tc.desc: Test code() method returns 0 when code is None
80     // @tc.precon: NA
81     // @tc.step: 1. Create CacheDownloadError with None code
82     // 2. Call code() method
83     // 3. Verify returned value is 0
84     // @tc.expect: code() returns 0 for None code
85     // @tc.type: FUNC
86     // @tc.require: issueNumber
87     // @tc.level: Level 2
88     #[test]
ut_cache_download_error_code_default()89     fn ut_cache_download_error_code_default() {
90         let error = CacheDownloadError {
91             code: None,
92             message: "Unknown Error".to_string(),
93             kind: ErrorKind::Http,
94         };
95 
96         assert_eq!(error.code(), 0);
97     }
98 
99     // @tc.name: ut_cache_download_error_message
100     // @tc.desc: Test message() method returns correct string
101     // @tc.precon: NA
102     // @tc.step: 1. Create CacheDownloadError with sample message
103     // 2. Call message() method
104     // 3. Verify returned string matches expected message
105     // @tc.expect: message() returns the stored error message
106     // @tc.type: FUNC
107     // @tc.require: issueNumber
108     // @tc.level: Level 1
109     #[test]
ut_cache_download_error_message()110     fn ut_cache_download_error_message() {
111         let error = CacheDownloadError {
112             code: Some(400),
113             message: "Bad Request".to_string(),
114             kind: ErrorKind::Http,
115         };
116 
117         assert_eq!(error.message(), "Bad Request");
118     }
119 
120     // @tc.name: ut_cache_download_error_ffi_kind
121     // @tc.desc: Test ffi_kind() method returns correct i32 value
122     // @tc.precon: NA
123     // @tc.step: 1. Create CacheDownloadError with Http kind
124     // 2. Call ffi_kind() method
125     // 3. Create CacheDownloadError with Io kind
126     // 4. Call ffi_kind() method
127     // 5. Verify returned values match expected i32 representations
128     // @tc.expect: ffi_kind() returns 0 for Http and 1 for Io
129     // @tc.type: FUNC
130     // @tc.require: issueNumber
131     // @tc.level: Level 1
132     #[test]
ut_cache_download_error_ffi_kind()133     fn ut_cache_download_error_ffi_kind() {
134         let http_error = CacheDownloadError {
135             code: Some(404),
136             message: "Not Found".to_string(),
137             kind: ErrorKind::Http,
138         };
139 
140         let io_error = CacheDownloadError {
141             code: Some(1),
142             message: "IO Error".to_string(),
143             kind: ErrorKind::Io,
144         };
145 
146         assert_eq!(http_error.ffi_kind(), 0);
147         assert_eq!(io_error.ffi_kind(), 1);
148     }
149 
150     // @tc.name: ut_cache_download_error_from_io_error
151     // @tc.desc: Test From<io::Error> conversion
152     // @tc.precon: NA
153     // @tc.step: 1. Create io::Error with known code and message
154     // 2. Convert to CacheDownloadError using From trait
155     // 3. Verify all fields are correctly converted
156     // @tc.expect: CacheDownloadError created with Io kind, matching code and message
157     // @tc.type: FUNC
158     // @tc.require: issueNumber
159     // @tc.level: Level 1
160     #[test]
ut_cache_download_error_from_io_error()161     fn ut_cache_download_error_from_io_error() {
162         let io_err = io::Error::new(io::ErrorKind::NotFound, "File not found");
163         let cache_err: CacheDownloadError = io_err.into();
164 
165         assert_eq!(cache_err.kind, ErrorKind::Io);
166         assert_eq!(cache_err.message(), "File not found");
167         assert_eq!(
168             cache_err.code(),
169             io::Error::new(io::ErrorKind::NotFound, "")
170                 .raw_os_error()
171                 .unwrap_or(0)
172         );
173     }
174 
175     // @tc.name: ut_cache_download_error_from_common_error
176     // @tc.desc: Test From<&E> conversion where E: CommonError
177     // @tc.precon: NA
178     // @tc.step: 1. Create MockCommonError with sample code and message
179     // 2. Convert to CacheDownloadError using From trait
180     // 3. Verify all fields are correctly converted
181     // @tc.expect: CacheDownloadError created with Http kind, matching code and message
182     // @tc.type: FUNC
183     // @tc.require: issueNumber
184     // @tc.level: Level 1
185     #[test]
ut_cache_download_error_from_common_error()186     fn ut_cache_download_error_from_common_error() {
187         let common_err = MockCommonError {
188             code: 403,
189             msg: "Forbidden".to_string(),
190         };
191 
192         let cache_err = CacheDownloadError::from(&common_err);
193 
194         assert_eq!(cache_err.kind, ErrorKind::Http);
195         assert_eq!(cache_err.code(), 403);
196         assert_eq!(cache_err.message(), "Forbidden");
197     }
198 }
199