Skip to content

Commit ffae6d2

Browse files
committed
Adapted existing tests to new fields
1 parent 6ee04a9 commit ffae6d2

File tree

1 file changed

+59
-26
lines changed

1 file changed

+59
-26
lines changed

src/test/java/com/uid2/admin/store/writer/EncryptedSaltStoreWriterTest.java

Lines changed: 59 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.uid2.shared.cloud.TaggableCloudStorage;
77
import com.uid2.shared.model.CloudEncryptionKey;
88
import com.uid2.shared.model.SaltEntry;
9+
import com.uid2.shared.model.SaltEntry.KeyMaterial;
910
import com.uid2.shared.store.CloudPath;
1011
import com.uid2.shared.store.salt.RotatingSaltProvider;
1112
import com.uid2.shared.store.reader.RotatingCloudEncryptionKeyProvider;
@@ -29,8 +30,8 @@
2930
import java.time.Instant;
3031
import java.util.*;
3132

32-
import static org.junit.jupiter.api.Assertions.assertEquals;
33-
import static org.junit.jupiter.api.Assertions.assertNull;
33+
import static java.lang.Long.parseLong;
34+
import static org.junit.jupiter.api.Assertions.*;
3435
import static org.mockito.Mockito.*;
3536
import static com.uid2.shared.util.CloudEncryptionHelpers.decryptInputStream;
3637

@@ -81,31 +82,19 @@ public void setup() throws Exception {
8182
when(rotatingCloudEncryptionKeyProvider.getEncryptionKeyForSite(SITE_ID)).thenReturn(encryptionKey);
8283
}
8384

84-
private RotatingSaltProvider.SaltSnapshot makeSnapshot(Instant effective, Instant expires, int nsalts) {
85-
SaltEntry[] entries = new SaltEntry[nsalts];
86-
for (int i = 0; i < entries.length; ++i) {
87-
entries[i] = new SaltEntry(i, "hashed_id", effective.toEpochMilli(), "salt", null, null, null, null);
88-
}
89-
return new RotatingSaltProvider.SaltSnapshot(effective, expires, entries, "test_first_level_salt");
90-
}
91-
92-
private void verifyFile(String filelocation, RotatingSaltProvider.SaltSnapshot snapshot) throws IOException {
93-
InputStream encoded = Files.newInputStream(Paths.get(filelocation));
94-
String contents = decryptInputStream(encoded, rotatingCloudEncryptionKeyProvider, "salts");
95-
SaltEntry[] entries = snapshot.getAllRotatingSalts();
96-
int idx = 0;
97-
for (String line : contents.split("\n")) {
98-
String[] entrySplit = line.split(",");
99-
assertEquals(entries[idx].id(), Long.parseLong(entrySplit[0]));
100-
assertEquals(entries[idx].currentSalt(), entrySplit[2]);
101-
idx++;
102-
}
103-
}
104-
10585
@Test
10686
public void testUploadNew() throws Exception {
107-
RotatingSaltProvider.SaltSnapshot snapshot = makeSnapshot(Instant.ofEpochMilli(1740607938167L), Instant.ofEpochMilli(Instant.now().toEpochMilli() + 90002), 100);
108-
RotatingSaltProvider.SaltSnapshot snapshot2 = makeSnapshot(Instant.ofEpochMilli(1740694476392L), Instant.ofEpochMilli(Instant.now().toEpochMilli() + 130000), 10);
87+
RotatingSaltProvider.SaltSnapshot snapshot = makeSnapshot(
88+
Instant.ofEpochMilli(1740607938167L),
89+
Instant.ofEpochMilli(Instant.now().toEpochMilli() + 90002),
90+
100
91+
); // Older snapshot
92+
RotatingSaltProvider.SaltSnapshot snapshot2 = makeSnapshot(
93+
Instant.ofEpochMilli(1740694476392L),
94+
Instant.ofEpochMilli(Instant.now().toEpochMilli() + 130000),
95+
10
96+
); // Newer active snapshot
97+
10998
JsonObject metadata = new JsonObject()
11099
.put("version", 1742770328863L)
111100
.put("generated", 1742770328)
@@ -135,7 +124,7 @@ public void testUploadNew() throws Exception {
135124
assertEquals(capturedMetadata.getString("id_prefix"), metadata.getValue("id_prefix"));
136125
verify(taggableCloudStorage,times(2)).upload(pathCaptor.capture(), cloudPathCaptor.capture(), any());
137126

138-
verifyFile(pathCaptor.getValue(), snapshot);
127+
assertWrittenFileEquals(pathCaptor.getValue(), snapshot2);
139128
}
140129

141130
@Test
@@ -191,4 +180,48 @@ public void testUnencryptedAndEncryptedBehavesTheSame() throws Exception {
191180
assertEquals(10, salt.getInteger("size"), "Size should match second entries");
192181
verify(taggableCloudStorage,atLeastOnce()).upload(pathCaptor.capture(), cloudPathCaptor.capture(), any());
193182
}
183+
184+
private RotatingSaltProvider.SaltSnapshot makeSnapshot(Instant effective, Instant expires, int nsalts) {
185+
SaltEntry[] entries = new SaltEntry[nsalts];
186+
187+
for (int i = 0; i < entries.length; ++i) {
188+
entries[i] = new SaltEntry(
189+
i,
190+
"hashed_id",
191+
effective.toEpochMilli(),
192+
"salt",
193+
1000L,
194+
"previous salt",
195+
new KeyMaterial(1, "key 1", "key salt 1"),
196+
new KeyMaterial(2, "key 2", "key salt 2")
197+
);
198+
}
199+
return new RotatingSaltProvider.SaltSnapshot(effective, expires, entries, "test_first_level_salt");
200+
}
201+
202+
private void assertWrittenFileEquals(String fileLocation, RotatingSaltProvider.SaltSnapshot snapshot) throws IOException {
203+
InputStream encoded = Files.newInputStream(Paths.get(fileLocation));
204+
String contents = decryptInputStream(encoded, rotatingCloudEncryptionKeyProvider, "salts");
205+
SaltEntry[] entries = snapshot.getAllRotatingSalts();
206+
var lines = contents.split("\n");
207+
for (int i = 0; i < lines.length; i++) {
208+
var line = lines[i];
209+
var entry = entries[i];
210+
String[] fields = line.split(",");
211+
212+
assertAll(
213+
() -> assertEquals(entry.id(), parseLong(fields[0])),
214+
() -> assertEquals(entry.lastUpdated(), parseLong(fields[1])),
215+
() -> assertEquals(entry.currentSalt(), fields[2]),
216+
() -> assertEquals(entry.refreshFrom(), parseLong(fields[3])),
217+
() -> assertEquals(entry.previousSalt(), fields[4]),
218+
() -> assertEquals(entry.currentKey().id(), parseLong(fields[5])),
219+
() -> assertEquals(entry.currentKey().key(), fields[6]),
220+
() -> assertEquals(entry.currentKey().salt(), fields[7]),
221+
() -> assertEquals(entry.previousKey().id(), parseLong(fields[8])),
222+
() -> assertEquals(entry.previousKey().key(), fields[9]),
223+
() -> assertEquals(entry.previousKey().salt(), fields[10])
224+
);
225+
}
226+
}
194227
}

0 commit comments

Comments
 (0)