Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Hinton committed May 14, 2024
1 parent e960910 commit 258d53a
Showing 1 changed file with 62 additions and 1 deletion.
63 changes: 62 additions & 1 deletion crates/bitwarden-crypto/src/sensitive/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,14 +201,16 @@ impl SensitiveString {

#[cfg(test)]
mod tests {
use schemars::schema_for;

use super::*;

#[test]
fn test_senitive_string() {
let mut s = SensitiveString::new("hello".to_string());
s.push_str(" world");

assert_eq!(s.inner.as_str(), "hello world");
assert_eq!(s, "hello world");
}

#[test]
Expand Down Expand Up @@ -240,4 +242,63 @@ mod tests {
let s = SensitiveString::new("Hello, world!".to_owned());
assert_eq!(&s[7..], "world!");
}

#[test]
fn test_schemars() {
#[derive(JsonSchema)]
struct TestStruct {
#[allow(dead_code)]
v: SensitiveString,
}

let schema = schema_for!(TestStruct);
let json = serde_json::to_string_pretty(&schema).unwrap();
let expected = r##"{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "TestStruct",
"type": "object",
"required": ["v"],
"properties": {
"v": {
"$ref": "#/definitions/String"
}
},
"definitions": {
"String": {
"type": "string"
}
}
}"##;

assert_eq!(
json.parse::<serde_json::Value>().unwrap(),
expected.parse::<serde_json::Value>().unwrap()
);
}

#[test]
fn test_eq_sensitive_string() {
let s1 = SensitiveString::new("Hello, world!".to_owned());
let s2 = SensitiveString::new("Hello, world!".to_owned());
assert_eq!(s1, s2);
}

#[test]
fn test_neq_sensitive_string() {
let s1 = SensitiveString::new("Hello, world!".to_owned());
let s2 = SensitiveString::new("Goodbye, world!".to_owned());
assert_ne!(s1, s2);
}

#[test]
fn test_eq_str() {
let s = SensitiveString::new("Hello, world!".to_owned());
assert_eq!(s, "Hello, world!");
}

#[test]
fn test_neq_str() {
let s = SensitiveString::new("Hello, world!".to_owned());
assert_ne!(s, "Goodbye, world!");
}
}

0 comments on commit 258d53a

Please sign in to comment.