Skip to content

Commit 37bd5b6

Browse files
committed
All tests finally pass
1 parent f233b72 commit 37bd5b6

7 files changed

Lines changed: 45 additions & 62 deletions

File tree

src/crypto/attrs/flattened_encrypted_attributes.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ impl FlattenedEncryptedAttributes {
1919
self.0.is_empty()
2020
}
2121

22+
// TODO: REmove this
23+
pub(crate) fn len(&self) -> usize {
24+
self.0.len()
25+
}
26+
2227
// TODO: Test this
2328
/// Decrypt self, returning a [FlattenedProtectedAttributes].
2429
pub(crate) async fn decrypt_all(

src/crypto/attrs/flattened_protected_attributes.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,20 @@ impl FlattenedProtectedAttributes {
2626
self.0.into_iter()
2727
}
2828

29+
// TODO: Do some more testing with the chunking
2930
/// Encrypt all attributes in the set and return a list of [FlattenedEncryptedAttributes] objects.
30-
/// Each [FlattenedEncryptedAttributes] object contains `chunk_size` encrypted attributes.
31+
/// The output is a vec of `chunk_into` [FlattenedEncryptedAttributes] objects.
3132
pub(crate) async fn encrypt_all(
3233
self,
3334
cipher: &Encryption<impl Credentials<Token = ServiceToken>>,
34-
chunk_size: usize,
35+
chunk_into: usize,
3536
) -> Result<Vec<FlattenedEncryptedAttributes>, SealError> {
36-
println!("Encrypting all attributes, chunk size: {}", chunk_size);
37+
let chunk_size = self.0.len() / chunk_into;
38+
println!("Encrypting all attributes, chunk into: {}, chunk_size = {}", chunk_into, chunk_size);
3739

38-
let x = cipher
40+
cipher
3941
.encrypt(self.0.into_iter())
40-
.await?;
41-
42-
dbg!(&x);
43-
44-
x
42+
.await?
4543
.into_iter()
4644
.chunks(chunk_size)
4745
.into_iter()

src/crypto/sealed.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,15 @@ impl SealedTableEntry {
105105
})
106106
.collect()
107107
} else {
108+
let chunk_size = protected_items.len() / unprotected_items.len();
109+
108110
let r1 = protected_items
109111
.decrypt_all(cipher)
110112
.await?
111113
.into_iter()
112-
.chunks(protected_attributes.len())
114+
// FIXME: chunk_size is not the same as protected_attributes.len() when dealing with maps
115+
// TODO: Can we make decrypt_all return a Vec of FlattenedProtectedAttributes? (like the mirror of encrypt_all)
116+
.chunks(chunk_size)
113117
.into_iter()
114118
.map(|fpa| fpa.into_iter().collect::<NormalizedProtectedAttributes>())
115119
.collect_vec();

src/crypto/sealer.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,13 @@ impl<'a> RecordsWithTerms<'a> {
9393
.collect()
9494

9595
} else {
96-
let encrypted = protected.encrypt_all(cipher, self.num_protected_attributes).await?;
96+
println!("Protecteds before encryption: {:?}", protected);
97+
let encrypted = protected.encrypt_all(cipher, num_records).await?;
98+
99+
println!("Encrypted len = {}", encrypted.len());
100+
println!("Unprotected len = {}", unprotecteds.len());
101+
println!("Encrypted: {:?}", encrypted);
102+
println!("Unprotected: {:?}", unprotecteds);
97103

98104
encrypted
99105
.into_iter()
@@ -150,9 +156,6 @@ impl Sealer {
150156
let protected_attributes = protected_attributes.as_ref();
151157
let num_protected_attributes = protected_attributes.len();
152158

153-
dbg!(&protected_attributes);
154-
dbg!(num_protected_attributes);
155-
156159
records
157160
.into_iter()
158161
.map(|sealer| {

src/encrypted_table/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,6 @@ impl<D> EncryptedTable<D> {
366366
) -> Result<DynamoRecordPatch, PutError> {
367367
let mut seen_sk = HashSet::new();
368368

369-
dbg!(&record);
370-
371369
let PreparedRecord {
372370
protected_attributes,
373371
protected_indexes,

src/encrypted_table/table_attributes.rs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,6 @@ use std::{borrow::Cow, collections::HashMap};
55
#[derive(Debug, Clone)]
66
/// Represents a collection of attributes for a table entry.
77
/// Attributes are stored as a map of `String` to `TableAttribute`.
8-
///
9-
/// ## Namespacing
10-
///
11-
/// Attributes can be namespaced by using a dot (`.`) in the attribute name.
12-
/// For example:
13-
///
14-
/// ```rust
15-
/// use cipherstash_dynamodb::TableAttributes;
16-
/// use std::collections::HashMap;
17-
///
18-
/// // TODO: Add a test for this
19-
/// ```
20-
///
21-
/// TODO: Explain how namespacing works with normalizing and denormalizing
228
pub struct TableAttributes(HashMap<String, TableAttribute>);
239

2410
impl TableAttributes {

tests/nested_tests.rs

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ mod common;
22

33
// TODO: Use the derive macros for this test
44
use std::{borrow::Cow, collections::BTreeMap};
5-
use tracing_test::traced_test;
6-
5+
use miette::IntoDiagnostic;
76
use cipherstash_client::encryption::TypeParseError;
87
use cipherstash_dynamodb::{
98
crypto::Unsealed,
@@ -55,10 +54,9 @@ impl Identifiable for Test {
5554
}
5655
}
5756

58-
// TODO: Make this function consume and return the Unsealed
5957
fn put_attrs(unsealed: &mut Unsealed, attrs: BTreeMap<String, String>) {
6058
attrs.into_iter().for_each(|(k, v)| {
61-
unsealed.add_protected(format!("attrs.{k}"), Plaintext::from(v));
59+
unsealed.add_protected_map_field("attrs", k, Plaintext::from(v));
6260
})
6361
}
6462

@@ -76,9 +74,8 @@ impl Encryptable for Test {
7674
}
7775

7876
fn into_unsealed(self) -> Unsealed {
79-
// FIXME: This should be a "consuming" method
8077
let mut unsealed = Unsealed::new_with_descriptor(<Self as Identifiable>::type_name());
81-
unsealed.add_protected("pk", Plaintext::from(self.pk));
78+
unsealed.add_unprotected("pk", TableAttribute::from(self.pk));
8279
unsealed.add_unprotected("sk", TableAttribute::from(self.sk));
8380
unsealed.add_protected("name", Plaintext::from(self.name));
8481
unsealed.add_protected("age", Plaintext::from(self.age));
@@ -94,7 +91,6 @@ where
9491
{
9592
unsealed
9693
.take_protected_map("attrs")
97-
// FIXME: This method should have a better error
9894
.ok_or(TypeParseError("attrs".to_string()))?
9995
.into_iter()
10096
.map(|(k, v)| TryFromPlaintext::try_from_plaintext(v).map(|v| (k, v)))
@@ -103,26 +99,23 @@ where
10399

104100
impl Decryptable for Test {
105101
fn from_unsealed(mut unsealed: Unsealed) -> Result<Self, SealError> {
106-
println!("IN FROM UNSEALED");
107102
Ok(Self {
108-
/*pk: TryFromTableAttr::try_from_table_attr(
103+
pk: TryFromTableAttr::try_from_table_attr(
109104
unsealed.get_plaintext("pk"),
110105
)?,
111106
sk: TryFromTableAttr::try_from_table_attr(
112107
unsealed.get_plaintext("sk"),
113-
)?,*/
114-
pk: String::from("pk-hack"),
115-
sk: String::from("sk-hack"),
116-
name: dbg!(TryFromPlaintext::try_from_optional_plaintext(
117-
dbg!(unsealed.take_protected("name")),
118-
))?,
119-
age: dbg!(TryFromPlaintext::try_from_optional_plaintext(
108+
)?,
109+
name: TryFromPlaintext::try_from_optional_plaintext(
110+
unsealed.take_protected("name"),
111+
)?,
112+
age: TryFromPlaintext::try_from_optional_plaintext(
120113
unsealed.take_protected("age"),
121-
))?,
122-
tag: dbg!(TryFromTableAttr::try_from_table_attr(
114+
)?,
115+
tag: TryFromTableAttr::try_from_table_attr(
123116
unsealed.get_plaintext("tag"),
124-
))?,
125-
attrs: dbg!(get_attrs(&mut unsealed))?,
117+
)?,
118+
attrs: get_attrs(&mut unsealed)?,
126119
})
127120
}
128121

@@ -140,8 +133,7 @@ impl Decryptable for Test {
140133
}
141134

142135
#[tokio::test]
143-
#[traced_test]
144-
async fn test_round_trip() {
136+
async fn test_round_trip() -> Result<(), Box<dyn std::error::Error>> {
145137
let config = aws_config::from_env()
146138
.endpoint_url("http://localhost:8000")
147139
.load()
@@ -154,7 +146,7 @@ async fn test_round_trip() {
154146

155147
let table = EncryptedTable::init(client, table_name)
156148
.await
157-
.expect("Failed to init table");
149+
.into_diagnostic()?;
158150

159151
let record = Test {
160152
pk: "pk".to_string(),
@@ -168,17 +160,14 @@ async fn test_round_trip() {
168160
table
169161
.put(record.clone())
170162
.await
171-
.expect("Failed to insert record");
163+
.into_diagnostic()?;
172164

173-
/*let check = table
165+
let check = table
174166
.get::<Test>(("pk", "sk"))
175-
.await;
176-
177-
if let Err(e) = check {
178-
panic!("Failed to get record: {:?}", e);
179-
}*/
167+
.await
168+
.into_diagnostic()?;
180169

181-
assert!(false);
170+
assert_eq!(check, Some(record));
182171

183-
//assert_eq!(check, record);
172+
Ok(())
184173
}

0 commit comments

Comments
 (0)