Skip to content

Commit 6967843

Browse files
secret: Add various tests
1 parent f439719 commit 6967843

1 file changed

Lines changed: 90 additions & 0 deletions

File tree

client/src/secret.rs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ impl AsRef<[u8]> for Secret {
181181

182182
#[cfg(test)]
183183
mod tests {
184+
use zvariant::{serialized::Context, to_bytes, Endian};
185+
184186
use super::*;
185187

186188
#[test]
@@ -191,4 +193,92 @@ mod tests {
191193
assert_eq!(format!("{:?}", text_secret), "Secret::Text([REDACTED])");
192194
assert_eq!(format!("{:?}", blob_secret), "Secret::Blob([REDACTED])");
193195
}
196+
197+
#[test]
198+
fn content_type_serialization() {
199+
let ctxt = Context::new_dbus(Endian::Little, 0);
200+
201+
// Test Text serialization
202+
let encoded = to_bytes(ctxt, &ContentType::Text).unwrap();
203+
let value: String = encoded.deserialize().unwrap().0;
204+
assert_eq!(value, "text/plain");
205+
206+
// Test Blob serialization
207+
let encoded = to_bytes(ctxt, &ContentType::Blob).unwrap();
208+
let value: String = encoded.deserialize().unwrap().0;
209+
assert_eq!(value, "application/octet-stream");
210+
211+
// Test Text deserialization
212+
let encoded = to_bytes(ctxt, &"text/plain").unwrap();
213+
let content_type: ContentType = encoded.deserialize().unwrap().0;
214+
assert_eq!(content_type, ContentType::Text);
215+
216+
// Test Blob deserialization
217+
let encoded = to_bytes(ctxt, &"application/octet-stream").unwrap();
218+
let content_type: ContentType = encoded.deserialize().unwrap().0;
219+
assert_eq!(content_type, ContentType::Blob);
220+
221+
// Test invalid content type deserialization
222+
let encoded = to_bytes(ctxt, &"invalid/type").unwrap();
223+
let result: Result<(ContentType, _), _> = encoded.deserialize();
224+
assert!(result.is_err());
225+
assert!(result
226+
.unwrap_err()
227+
.to_string()
228+
.contains("Invalid content type"));
229+
}
230+
231+
#[test]
232+
fn content_type_from_str() {
233+
assert_eq!(ContentType::from_str("text/plain").unwrap(), ContentType::Text);
234+
assert_eq!(
235+
ContentType::from_str("application/octet-stream").unwrap(),
236+
ContentType::Blob
237+
);
238+
239+
// Test error case
240+
let result = ContentType::from_str("invalid");
241+
assert!(result.is_err());
242+
assert!(result.unwrap_err().contains("Invalid content type"));
243+
}
244+
245+
#[test]
246+
fn invalid_utf8() {
247+
// Test with invalid UTF-8 bytes
248+
let invalid_utf8 = vec![0xFF, 0xFE, 0xFD];
249+
250+
// Should fall back to blob when UTF-8 decoding fails
251+
let secret = Secret::with_content_type(ContentType::Text, &invalid_utf8);
252+
assert_eq!(secret.content_type(), ContentType::Blob);
253+
assert_eq!(&*secret, &[0xFF, 0xFE, 0xFD]);
254+
255+
// Test with valid UTF-8
256+
let valid_utf8 = "Hello, World!";
257+
let secret = Secret::with_content_type(ContentType::Text, valid_utf8.as_bytes());
258+
assert_eq!(secret.content_type(), ContentType::Text);
259+
assert_eq!(&*secret, valid_utf8.as_bytes());
260+
261+
// Test with blob content type
262+
let data = vec![1, 2, 3, 4];
263+
let secret = Secret::with_content_type(ContentType::Blob, &data);
264+
assert_eq!(secret.content_type(), ContentType::Blob);
265+
assert_eq!(&*secret, &[1, 2, 3, 4]);
266+
}
267+
268+
#[test]
269+
fn random() {
270+
let secret1 = Secret::random().unwrap();
271+
let secret2 = Secret::random().unwrap();
272+
273+
// Random secrets should be blobs
274+
assert_eq!(secret1.content_type(), ContentType::Blob);
275+
assert_eq!(secret2.content_type(), ContentType::Blob);
276+
277+
// Should be 64 bytes
278+
assert_eq!(secret1.as_bytes().len(), 64);
279+
assert_eq!(secret2.as_bytes().len(), 64);
280+
281+
// Should be different
282+
assert_ne!(secret1.as_bytes(), secret2.as_bytes());
283+
}
194284
}

0 commit comments

Comments
 (0)