ci: update rust test ci

This commit is contained in:
ZhuJHua
2025-04-17 11:15:48 +08:00
parent 3be9f90575
commit cc4cee941d
9 changed files with 338 additions and 222 deletions

2
.fvmrc
View File

@@ -1,3 +1,3 @@
{
"flutter": "3.29.2"
"flutter": "3.29.3"
}

View File

@@ -38,6 +38,13 @@ jobs:
with:
flutter-version: ${{ env.flutter-version }}
- name: Set Up Rust
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly
override: true
- name: Install Dependencies
run: flutter pub get

28
.github/workflows/cargo-ci.yml vendored Normal file
View File

@@ -0,0 +1,28 @@
name: Cargo CI
on:
workflow_dispatch:
pull_request:
branches:
- master
jobs:
test:
name: Run Tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly
override: true
- name: Run cargo test
working-directory: rust
run: cargo test
- name: Fail on Errors
if: failure()
run: exit 1

View File

@@ -16,3 +16,7 @@ analyzer:
- "**/*.gr.dart"
- "**/*.mocks.dart"
- "rust_builder/**"
- "ios/**"
- "android/**"
- "windows/**"
- "macos/**"

401
rust/Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -8,12 +8,12 @@ crate-type = ["cdylib", "staticlib"]
[dependencies]
flutter_rust_bridge = "=2.9.0"
image = "0.25.5"
fast_image_resize = { version = "5.1.2", features = ["image"] }
anyhow = "1.0.97"
image = "0.25.6"
fast_image_resize = { version = "5.1.3", features = ["image"] }
anyhow = "1.0.98"
ttf-parser = { git = "https://github.com/ZhuJHua/ttf-parser", branch = "fvar" }
ring = "0.17.14"
zip = "2.4.2"
zip = "2.6.1"
walkdir = "2.5.0"
[lints.rust]

35
rust/src/test/aes_test.rs Normal file
View File

@@ -0,0 +1,35 @@
#[cfg(test)]
mod aes_test {
use crate::api::aes::AesEncryption;
#[test]
fn test_derive_key_consistency() {
let salt = "salt123".to_string();
let user_key = "password456".to_string();
let key1 = AesEncryption::derive_key(salt.clone(), user_key.clone()).unwrap();
let key2 = AesEncryption::derive_key(salt, user_key).unwrap();
assert_eq!(key1, key2);
}
#[test]
fn test_encrypt_decrypt() {
let salt = "testsalt".to_string();
let user_key = "testpassword".to_string();
let key = AesEncryption::derive_key(salt, user_key).unwrap();
let original_data = b"Hello Flutter Rust Bridge!".to_vec();
let encrypted = AesEncryption::encrypt(key.clone(), original_data.clone()).unwrap();
let decrypted = AesEncryption::decrypt(key, encrypted).unwrap();
assert_eq!(original_data, decrypted);
}
#[test]
fn test_decrypt_invalid_data() {
let key = vec![0u8; 32]; // dummy key
let bad_data = vec![1, 2, 3]; // too short
let result = AesEncryption::decrypt(key, bad_data);
assert!(result.is_err());
}
}

73
rust/src/test/kmp_test.rs Normal file
View File

@@ -0,0 +1,73 @@
#[cfg(test)]
mod kmp_test {
use crate::api::kmp::{kmp_search, Kmp};
use std::collections::HashMap;
#[test]
fn test_kmp_basic_match() {
let text = "ababcabcababc";
let pattern = "abc";
let matches = kmp_search(text, pattern);
assert_eq!(matches, vec![2, 5, 10]);
}
#[test]
fn test_replace_with_kmp_single() {
let text = "hello world, hello rust";
let mut replacements = HashMap::new();
replacements.insert("hello".to_string(), "hi".to_string());
let result = Kmp::replace_with_kmp(text.to_string(), replacements);
assert_eq!(result, "hi world, hi rust");
}
#[test]
fn test_replace_with_kmp_multiple_overlap() {
let text = "abcde";
let mut replacements = HashMap::new();
replacements.insert("abc".to_string(), "123".to_string());
replacements.insert("bcd".to_string(), "234".to_string());
let result = Kmp::replace_with_kmp(text.to_string(), replacements);
// 应优先替换更长的匹配从左到右“abc”会匹配成功然后跳过“bcd”
assert_eq!(result, "123de");
}
#[test]
fn test_replace_with_kmp_unicode() {
let text = "你好世界,世界你好";
let mut replacements = HashMap::new();
replacements.insert("世界".to_string(), "🌍".to_string());
let result = Kmp::replace_with_kmp(text.to_string(), replacements);
assert_eq!(result, "你好🌍,🌍你好");
}
#[test]
fn test_find_matches() {
let text = "flutter and rust are cool";
let patterns = vec![
"flutter".to_string(),
"rust".to_string(),
"dart".to_string(),
];
let result = Kmp::find_matches(text, patterns);
assert_eq!(result, vec!["flutter".to_string(), "rust".to_string()]);
}
#[test]
fn test_find_matches_empty() {
let text = "no match here";
let patterns = vec!["something".to_string(), "nothing".to_string()];
let result = Kmp::find_matches(text, patterns);
assert!(result.is_empty());
}
#[test]
fn test_empty_replacements() {
let text = "keep this";
let replacements = HashMap::new();
let result = Kmp::replace_with_kmp(text.to_string(), replacements);
assert_eq!(result, "keep this");
}
}

2
rust/src/test/mod.rs Normal file
View File

@@ -0,0 +1,2 @@
mod aes_test;
mod kmp_test;