|
6 | 6 | import com.uid2.shared.cloud.TaggableCloudStorage; |
7 | 7 | import com.uid2.shared.model.CloudEncryptionKey; |
8 | 8 | import com.uid2.shared.model.SaltEntry; |
| 9 | +import com.uid2.shared.model.SaltEntry.KeyMaterial; |
9 | 10 | import com.uid2.shared.store.CloudPath; |
10 | 11 | import com.uid2.shared.store.salt.RotatingSaltProvider; |
11 | 12 | import com.uid2.shared.store.reader.RotatingCloudEncryptionKeyProvider; |
|
29 | 30 | import java.time.Instant; |
30 | 31 | import java.util.*; |
31 | 32 |
|
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.*; |
34 | 35 | import static org.mockito.Mockito.*; |
35 | 36 | import static com.uid2.shared.util.CloudEncryptionHelpers.decryptInputStream; |
36 | 37 |
|
@@ -81,31 +82,19 @@ public void setup() throws Exception { |
81 | 82 | when(rotatingCloudEncryptionKeyProvider.getEncryptionKeyForSite(SITE_ID)).thenReturn(encryptionKey); |
82 | 83 | } |
83 | 84 |
|
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 | | - |
105 | 85 | @Test |
106 | 86 | 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 | + |
109 | 98 | JsonObject metadata = new JsonObject() |
110 | 99 | .put("version", 1742770328863L) |
111 | 100 | .put("generated", 1742770328) |
@@ -135,7 +124,7 @@ public void testUploadNew() throws Exception { |
135 | 124 | assertEquals(capturedMetadata.getString("id_prefix"), metadata.getValue("id_prefix")); |
136 | 125 | verify(taggableCloudStorage,times(2)).upload(pathCaptor.capture(), cloudPathCaptor.capture(), any()); |
137 | 126 |
|
138 | | - verifyFile(pathCaptor.getValue(), snapshot); |
| 127 | + assertWrittenFileEquals(pathCaptor.getValue(), snapshot2); |
139 | 128 | } |
140 | 129 |
|
141 | 130 | @Test |
@@ -191,4 +180,48 @@ public void testUnencryptedAndEncryptedBehavesTheSame() throws Exception { |
191 | 180 | assertEquals(10, salt.getInteger("size"), "Size should match second entries"); |
192 | 181 | verify(taggableCloudStorage,atLeastOnce()).upload(pathCaptor.capture(), cloudPathCaptor.capture(), any()); |
193 | 182 | } |
| 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 | + } |
194 | 227 | } |
0 commit comments