// Copyright (C) 2024 Huawei Device Co., Ltd.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#[cfg(test)]
mod ut_wrapper {
use super::*;
use hex::encode;
use sha2::Digest;
use sha2::Sha256;
use std::ffi::CString;
use std::ptr;
// @tc.name: ut_wrapper_log_type_values
// @tc.desc: Verify LogType enum has correct values
// @tc.precon: NA
// @tc.step: 1. Check the values of LogType variants
// @tc.expect: All LogType variants have expected integer values
// @tc.type: FUNC
// @tc.require: issueNumber
// @tc.level: Level 0
#[test]
fn ut_wrapper_log_type_values() {
assert_eq!(ffi::LogType::LOG_TYPE_MIN as i32, 0);
assert_eq!(ffi::LogType::LOG_APP as i32, 0);
assert_eq!(ffi::LogType::LOG_INIT as i32, 1);
assert_eq!(ffi::LogType::LOG_CORE as i32, 3);
assert_eq!(ffi::LogType::LOG_KMSG as i32, 4);
assert_eq!(ffi::LogType::LOG_ONLY_PRERELEASE as i32, 5);
assert_eq!(ffi::LogType::LOG_TYPE_MAX as i32, 6);
}
// @tc.name: ut_wrapper_log_level_values
// @tc.desc: Verify LogLevel enum has correct values
// @tc.precon: NA
// @tc.step: 1. Check the values of LogLevel variants
// @tc.expect: All LogLevel variants have expected integer values
// @tc.type: FUNC
// @tc.require: issueNumber
// @tc.level: Level 0
#[test]
fn ut_wrapper_log_level_values() {
assert_eq!(ffi::LogLevel::LOG_LEVEL_MIN as i32, 0);
assert_eq!(ffi::LogLevel::LOG_DEBUG as i32, 3);
assert_eq!(ffi::LogLevel::LOG_INFO as i32, 4);
assert_eq!(ffi::LogLevel::LOG_WARN as i32, 5);
assert_eq!(ffi::LogLevel::LOG_ERROR as i32, 6);
assert_eq!(ffi::LogLevel::LOG_FATAL as i32, 7);
assert_eq!(ffi::LogLevel::LOG_LEVEL_MAX as i32, 8);
}
// @tc.name: ut_wrapper_sha256_valid_input
// @tc.desc: Test SHA256 with known input and expected output
// @tc.precon: NA
// @tc.step: 1. Call SHA256 with "hello world"
// 2. Compare result with expected hash
// @tc.expect: SHA256 returns correct hash for input
// @tc.type: FUNC
// @tc.require: issueNumber
// @tc.level: Level 1
#[test]
fn ut_wrapper_sha256_valid_input() {
let input = "hello world";
let result = SHA256(input);
// Note: This test assumes the C++ SHA256 implementation matches Rust's standard
// For actual testing, replace with the expected hash from the C++ implementation
let expected = rust_crypto::sha256(input);
assert_eq!(result, expected);
}
// @tc.name: ut_wrapper_sha256_empty_input
// @tc.desc: Test SHA256 with empty input string
// @tc.precon: NA
// @tc.step: 1. Call SHA256 with empty string
// 2. Verify result is non-empty
// @tc.expect: SHA256 returns non-empty hash for empty input
// @tc.type: FUNC
// @tc.require: issueNumber
// @tc.level: Level 2
#[test]
fn ut_wrapper_sha256_empty_input() {
let result = SHA256("");
assert!(!result.is_empty());
}
// @tc.name: ut_wrapper_sha256_long_input
// @tc.desc: Test SHA256 with very long input
// @tc.precon: NA
// @tc.step: 1. Create 10,000 character input string
// 2. Call SHA256 with this input
// 3. Verify result is non-empty
// @tc.expect: SHA256 returns non-empty hash for long input
// @tc.type: FUNC
// @tc.require: issueNumber
// @tc.level: Level 3
#[test]
fn ut_wrapper_sha256_long_input() {
let long_input = "a".repeat(10000);
let result = SHA256(&long_input);
assert!(!result.is_empty());
assert_eq!(result.len(), 64); // SHA256 should always return 64-character hex string
}
// @tc.name: ut_wrapper_hilog_print_basic
// @tc.desc: Test basic functionality of hilog_print
// @tc.precon: NA
// @tc.step: 1. Call hilog_print with sample parameters
// @tc.expect: Function executes without panicking
// @tc.type: FUNC
// @tc.require: issueNumber
// @tc.level: Level 1
#[test]
fn ut_wrapper_hilog_print_basic() {
hilog_print(
ffi::LogLevel::LOG_INFO,
0x1234,
"test_tag",
"Test log message".to_string(),
);
// Note: Verifying actual log output would require capturing system logs
}
// @tc.name: ut_wrapper_get_cache_dir_non_empty
// @tc.desc: Test GetCacheDir returns non-empty string
// @tc.precon: NA
// @tc.step: 1. Call GetCacheDir function
// 2. Verify the result is non-empty
// @tc.expect: GetCacheDir returns non-empty string
// @tc.type: FUNC
// @tc.require: issueNumber
// @tc.level: Level 1
#[test]
fn ut_wrapper_get_cache_dir_non_empty() {
let cache_dir = GetCacheDir();
assert!(!cache_dir.is_empty());
}
// Mock implementation of rust_crypto::sha256 for testing
#[cfg(test)]
mod rust_crypto {
use hex::encode;
use sha2::{Digest, Sha256};
pub fn sha256(input: &str) -> String {
let mut hasher = Sha256::new();
hasher.update(input);
let result = hasher.finalize();
encode(result)
}
}
}