|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// Copyright 2025 Keylime Authors |
| 3 | + |
| 4 | +use serde::{Deserialize, Serialize}; |
| 5 | + |
| 6 | +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] |
| 7 | +pub struct EncryptedData { |
| 8 | + bytes: Vec<u8>, |
| 9 | +} |
| 10 | + |
| 11 | +impl AsRef<[u8]> for EncryptedData { |
| 12 | + fn as_ref(&self) -> &[u8] { |
| 13 | + self.bytes.as_slice() |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +impl From<&[u8]> for EncryptedData { |
| 18 | + fn from(v: &[u8]) -> Self { |
| 19 | + EncryptedData { bytes: v.to_vec() } |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +impl From<Vec<u8>> for EncryptedData { |
| 24 | + fn from(v: Vec<u8>) -> Self { |
| 25 | + EncryptedData { bytes: v } |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +#[cfg(test)] |
| 30 | +mod test { |
| 31 | + use super::*; |
| 32 | + |
| 33 | + #[test] |
| 34 | + fn test_encrypted_data_as_ref() { |
| 35 | + let a = EncryptedData { |
| 36 | + bytes: vec![0x0A, 16], |
| 37 | + }; |
| 38 | + |
| 39 | + let r = a.as_ref(); |
| 40 | + assert_eq!(r, vec![0x0A, 16]); |
| 41 | + } |
| 42 | + |
| 43 | + #[test] |
| 44 | + fn test_encrypted_data_from_slice() { |
| 45 | + let a: [u8; 16] = [0x0B; 16]; |
| 46 | + let expected = EncryptedData { |
| 47 | + bytes: vec![0x0B; 16], |
| 48 | + }; |
| 49 | + |
| 50 | + let r = EncryptedData::from(a.as_ref()); |
| 51 | + |
| 52 | + assert_eq!(r, expected); |
| 53 | + } |
| 54 | + |
| 55 | + #[test] |
| 56 | + fn test_encrypted_data_from_vec() { |
| 57 | + let a: Vec<u8> = vec![0x0C; 16]; |
| 58 | + let expected = EncryptedData { |
| 59 | + bytes: vec![0x0C; 16], |
| 60 | + }; |
| 61 | + |
| 62 | + let r = EncryptedData::from(a); |
| 63 | + |
| 64 | + assert_eq!(r, expected); |
| 65 | + } |
| 66 | +} |
0 commit comments