feat: improve challenge test case (#8825)

This commit is contained in:
darkskygit 2024-11-14 10:22:38 +00:00
parent 129cceade9
commit 991e0b9b63
No known key found for this signature in database
GPG Key ID: 97B7D036B1566E9D
7 changed files with 165 additions and 27 deletions

28
.github/actions/setup-rust/action.yml vendored Normal file
View File

@ -0,0 +1,28 @@
name: 'Rust setup'
description: 'Rust setup, including cache configuration'
inputs:
components:
description: 'Cargo components'
required: false
targets:
description: 'Cargo target'
required: false
toolchain:
description: 'Rustup toolchain'
required: false
default: 'stable'
runs:
using: 'composite'
steps:
- name: Setup Rust
uses: dtolnay/rust-toolchain@stable
with:
toolchain: ${{ inputs.toolchain }}
targets: ${{ inputs.targets }}
components: ${{ inputs.components }}
- name: Add Targets
if: ${{ inputs.targets }}
run: rustup target add ${{ inputs.targets }}
shell: bash
- uses: Swatinem/rust-cache@v2

View File

@ -379,6 +379,23 @@ jobs:
name: affine
fail_ci_if_error: false
server-native-test:
name: Run server native tests
runs-on: ubuntu-latest
env:
RUSTFLAGS: -D warnings
CARGO_TERM_COLOR: always
steps:
- uses: actions/checkout@v4
- name: Setup Rust
uses: ./.github/actions/setup-rust
- name: Install latest nextest release
uses: taiki-e/install-action@nextest
- name: Run tests
run: cargo nextest run --release
copilot-api-test:
name: Server Copilot Api Test
runs-on: ubuntu-latest
@ -755,6 +772,8 @@ jobs:
- build-server-native
- build-electron-renderer
- server-test
- server-native-test
- copilot-api-test
- copilot-e2e-test
- server-e2e-test
- desktop-test

43
Cargo.lock generated
View File

@ -32,6 +32,7 @@ dependencies = [
"once_cell",
"parking_lot",
"rand",
"rayon",
"serde",
"serde_json",
"sha3",
@ -55,6 +56,7 @@ dependencies = [
"napi-build",
"napi-derive",
"rand",
"rayon",
"sha3",
"tiktoken-rs",
"tokio",
@ -331,6 +333,25 @@ version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "19d374276b40fb8bbdee95aef7c7fa6b5316ec764510eb64b8dd0e2ed0d7e7f5"
[[package]]
name = "crossbeam-deque"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "613f8cc01fe9cf1a3eb3d7f488fd2fa8388403e97039e2f73692932e291a770d"
dependencies = [
"crossbeam-epoch",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-epoch"
version = "0.9.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e"
dependencies = [
"crossbeam-utils",
]
[[package]]
name = "crossbeam-queue"
version = "0.3.11"
@ -1013,7 +1034,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4979f22fdb869068da03c9f7528f8297c6fd2606bc3a4affe42e6a823fdb8da4"
dependencies = [
"cfg-if",
"windows-targets 0.48.5",
"windows-targets 0.52.6",
]
[[package]]
@ -1514,6 +1535,26 @@ dependencies = [
"rand",
]
[[package]]
name = "rayon"
version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b418a60154510ca1a002a752ca9714984e21e4241e804d32555251faf8b78ffa"
dependencies = [
"either",
"rayon-core",
]
[[package]]
name = "rayon-core"
version = "1.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1465873a3dfdaa8ae7cb14b4383657caab0b3e8a0aa9ae8e04b044854c8dfce2"
dependencies = [
"crossbeam-deque",
"crossbeam-utils",
]
[[package]]
name = "redox_syscall"
version = "0.5.7"

View File

@ -15,6 +15,7 @@ notify = { version = "7", features = ["serde"] }
once_cell = "1"
parking_lot = "0.12"
rand = "0.8"
rayon = "1.10"
serde = "1"
serde_json = "1"
sha3 = "0.10"

View File

@ -24,7 +24,8 @@ mimalloc = { workspace = true }
mimalloc = { workspace = true, features = ["local_dynamic_tls"] }
[dev-dependencies]
tokio = "1"
rayon = { workspace = true }
tokio = { workspace = true }
[build-dependencies]
napi-build = { workspace = true }

View File

@ -23,6 +23,9 @@ sqlx = { workspace = true, default-features = false, features = ["chron
tokio = { workspace = true, features = ["full"] }
uuid = { workspace = true, features = ["fast-rng", "serde", "v4"] }
[dev-dependencies]
rayon = { workspace = true }
[build-dependencies]
affine_schema = { path = "./schema" }
dotenv = { workspace = true }

View File

@ -104,7 +104,12 @@ impl TryFrom<&str> for Stamp {
fn try_from(value: &str) -> Result<Self, Self::Error> {
let stamp_vec = value.split(':').collect::<Vec<&str>>();
if stamp_vec.len() != 7 {
if stamp_vec.len() != 7
|| stamp_vec
.iter()
.enumerate()
.any(|(i, s)| i != 4 && s.is_empty())
{
return Err(format!(
"Malformed stamp, expected 6 parts, got {}",
stamp_vec.len()
@ -191,35 +196,75 @@ pub fn mint_challenge_response(
#[cfg(test)]
mod tests {
use super::Stamp;
use rand::{distributions::Alphanumeric, Rng};
use rayon::prelude::*;
#[test]
fn test_mint() {
let response = Stamp::mint("test".into(), Some(22)).format();
assert!(Stamp::try_from(response.as_str())
.unwrap()
.check(22, "test"));
{
let response = Stamp::mint("test".into(), Some(20)).format();
assert!(
Stamp::try_from(response.as_str())
.unwrap()
.check(20, "test"),
"should pass"
);
}
{
let response = Stamp::mint("test".into(), Some(19)).format();
assert!(
!Stamp::try_from(response.as_str())
.unwrap()
.check(20, "test"),
"should fail with lower bits"
);
}
{
let response = Stamp::mint("test".into(), Some(20)).format();
assert!(
!Stamp::try_from(response.as_str())
.unwrap()
.check(20, "test2"),
"should fail with different resource"
);
}
}
#[test]
fn test_check() {
assert!(Stamp::try_from("1:20:20202116:test::Z4p8WaiO:31c14")
.unwrap()
.check(20, "test"));
assert!(!Stamp::try_from("1:20:20202116:test1::Z4p8WaiO:31c14")
.unwrap()
.check(20, "test"));
assert!(!Stamp::try_from("1:20:20202116:test::z4p8WaiO:31c14")
.unwrap()
.check(20, "test"));
assert!(!Stamp::try_from("1:20:20202116:test::Z4p8WaiO:31C14")
.unwrap()
.check(20, "test"));
assert!(Stamp::try_from("0:20:20202116:test::Z4p8WaiO:31c14").is_err());
assert!(!Stamp::try_from("1:19:20202116:test::Z4p8WaiO:31c14")
.unwrap()
.check(20, "test"));
assert!(!Stamp::try_from("1:20:20202115:test::Z4p8WaiO:31c14")
.unwrap()
.check(20, "test"));
fn test_check_expiration() {
let response = Stamp::mint("test".into(), Some(20));
assert!(response.check_expiration());
}
#[test]
fn test_format() {
let response = Stamp::mint("test".into(), Some(20));
assert_eq!(
response.format(),
format!(
"1:20:{}:test::{}:{}",
response.ts, response.rand, response.counter
)
);
}
#[test]
fn test_fuzz() {
(0..1000).into_par_iter().for_each(|_| {
let bit = rand::random::<u32>() % 20 + 1;
let resource = rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(7)
.map(char::from)
.collect::<String>();
let response = Stamp::mint(resource.clone(), Some(bit)).format();
assert!(
Stamp::try_from(response.as_str())
.unwrap()
.check(bit, resource),
"should pass"
);
});
}
}