|
10 | 10 | import com.uid2.shared.store.RotatingSaltProvider; |
11 | 11 | import com.uid2.shared.store.reader.RotatingCloudEncryptionKeyProvider; |
12 | 12 | import com.uid2.shared.store.scope.StoreScope; |
| 13 | +import io.vertx.core.json.JsonArray; |
13 | 14 | import io.vertx.core.json.JsonObject; |
14 | 15 | import org.junit.jupiter.api.BeforeEach; |
15 | 16 | import org.junit.jupiter.api.Test; |
|
20 | 21 |
|
21 | 22 | import java.io.IOException; |
22 | 23 | import java.io.InputStream; |
| 24 | +import java.nio.charset.StandardCharsets; |
23 | 25 | import java.nio.file.Files; |
24 | 26 | import java.nio.file.Paths; |
25 | 27 | import java.time.Instant; |
@@ -107,21 +109,77 @@ private void verifyFile(String filelocation, RotatingSaltProvider.SaltSnapshot s |
107 | 109 |
|
108 | 110 | @Test |
109 | 111 | public void testUploadNew() throws Exception { |
110 | | - RotatingSaltProvider.SaltSnapshot snapshot = makeSnapshot(Instant.now(), Instant.ofEpochMilli(Instant.now().toEpochMilli() + 10000), 1000000); |
111 | | - |
| 112 | + RotatingSaltProvider.SaltSnapshot snapshot = makeSnapshot(Instant.ofEpochMilli(1740607938167L), Instant.ofEpochMilli(Instant.now().toEpochMilli() + 90002), 100); |
| 113 | + RotatingSaltProvider.SaltSnapshot snapshot2 = makeSnapshot(Instant.ofEpochMilli(1740694476392L), Instant.ofEpochMilli(Instant.now().toEpochMilli() + 130000), 10); |
112 | 114 | when(rotatingSaltProvider.getMetadata()).thenThrow(new CloudStorageException("The specified key does not exist: AmazonS3Exception: test-core-bucket")); |
113 | 115 | when(rotatingSaltProvider.getSnapshots()).thenReturn(null); |
114 | 116 |
|
115 | 117 | when(taggableCloudStorage.list(anyString())).thenReturn(new ArrayList<>()); |
116 | 118 |
|
| 119 | + ArgumentCaptor<JsonObject> metadataCaptor = ArgumentCaptor.forClass(JsonObject.class); |
| 120 | + ArgumentCaptor<String> nameCaptor = ArgumentCaptor.forClass(String.class); |
| 121 | + ArgumentCaptor<CloudPath> locationCaptor = ArgumentCaptor.forClass(CloudPath.class); |
| 122 | + |
117 | 123 | EncryptedSaltStoreWriter encryptedSaltStoreWriter = new EncryptedSaltStoreWriter(config, rotatingSaltProvider, |
118 | 124 | fileManager, taggableCloudStorage, versionGenerator, storeScope, rotatingCloudEncryptionKeyProvider, siteId); |
119 | 125 |
|
120 | 126 | encryptedSaltStoreWriter.upload(snapshot); |
| 127 | + verify(fileManager).uploadMetadata(metadataCaptor.capture(), nameCaptor.capture(), locationCaptor.capture()); |
| 128 | + |
| 129 | + // Capture the metadata |
| 130 | + JsonObject capturedMetadata = metadataCaptor.getValue(); |
| 131 | + assertEquals(1, capturedMetadata.getJsonArray("salts").size(), "The 'salts' array should contain exactly 1 item"); |
| 132 | + encryptedSaltStoreWriter.upload(snapshot2); |
121 | 133 |
|
122 | | - verify(taggableCloudStorage).upload(pathCaptor.capture(), cloudPathCaptor.capture(), any()); |
123 | | - assertEquals(cloudPathCaptor.getValue(), "test/path"); |
| 134 | + verify(fileManager,times(2)).uploadMetadata(metadataCaptor.capture(), nameCaptor.capture(), locationCaptor.capture()); |
| 135 | + capturedMetadata = metadataCaptor.getValue(); |
| 136 | + assertEquals(2, capturedMetadata.getJsonArray("salts").size(), "The 'salts' array should contain 2 items"); |
| 137 | + |
| 138 | + verify(taggableCloudStorage,times(3)).upload(pathCaptor.capture(), cloudPathCaptor.capture(), any()); |
124 | 139 |
|
125 | 140 | verifyFile(pathCaptor.getValue(), snapshot); |
126 | 141 | } |
| 142 | + |
| 143 | + @Test |
| 144 | + public void testUnencryptedAndEncryptedBehavesTheSame() throws Exception { |
| 145 | + RotatingSaltProvider.SaltSnapshot snapshot = makeSnapshot(Instant.ofEpochMilli(1740607938167L), Instant.ofEpochMilli(Instant.now().toEpochMilli() + 90000), 100); |
| 146 | + RotatingSaltProvider.SaltSnapshot snapshot2 = makeSnapshot(Instant.ofEpochMilli(1740694476392L), Instant.ofEpochMilli(Instant.now().toEpochMilli() + 130000), 10); |
| 147 | + List<RotatingSaltProvider.SaltSnapshot> snapshots = List.of(snapshot, snapshot2); |
| 148 | + |
| 149 | + when(rotatingSaltProvider.getMetadata()).thenThrow(new CloudStorageException("The specified key does not exist: AmazonS3Exception: test-core-bucket")); |
| 150 | + when(rotatingSaltProvider.getSnapshots()).thenReturn(snapshots); |
| 151 | + when(taggableCloudStorage.list(anyString())).thenReturn(new ArrayList<>()); |
| 152 | + |
| 153 | + ArgumentCaptor<JsonObject> metadataCaptor = ArgumentCaptor.forClass(JsonObject.class); |
| 154 | + ArgumentCaptor<String> nameCaptor = ArgumentCaptor.forClass(String.class); |
| 155 | + ArgumentCaptor<CloudPath> locationCaptor = ArgumentCaptor.forClass(CloudPath.class); |
| 156 | + |
| 157 | + SaltStoreWriter saltStoreWriter = new SaltStoreWriter(config, rotatingSaltProvider, |
| 158 | + fileManager, taggableCloudStorage, versionGenerator); |
| 159 | + |
| 160 | + saltStoreWriter.upload(snapshot); |
| 161 | + verify(fileManager).uploadMetadata(metadataCaptor.capture(), nameCaptor.capture(), locationCaptor.capture()); |
| 162 | + |
| 163 | + JsonObject capturedMetadata = metadataCaptor.getValue(); |
| 164 | + JsonArray saltsArray = capturedMetadata.getJsonArray("salts"); |
| 165 | + assertEquals(1, saltsArray.size(), "Salts array should have exactly one entry, as other is removed in newest-effective logic"); |
| 166 | + JsonObject salt = saltsArray.getJsonObject(0); |
| 167 | + assertEquals(1740694476392L, salt.getLong("effective"), "Effective timestamp should match second entry"); |
| 168 | + assertEquals(10, salt.getInteger("size"), "Size should match second entries"); |
| 169 | + |
| 170 | + //Now sending snapshot2 to encrypted to verify that does the same. |
| 171 | + EncryptedSaltStoreWriter encryptedSaltStoreWriter = new EncryptedSaltStoreWriter(config, rotatingSaltProvider, |
| 172 | + fileManager, taggableCloudStorage, versionGenerator, storeScope, rotatingCloudEncryptionKeyProvider, siteId); |
| 173 | + |
| 174 | + encryptedSaltStoreWriter.upload(snapshot2); |
| 175 | + |
| 176 | + verify(fileManager,atLeastOnce()).uploadMetadata(metadataCaptor.capture(), nameCaptor.capture(), locationCaptor.capture()); |
| 177 | + |
| 178 | + capturedMetadata = metadataCaptor.getValue(); |
| 179 | + saltsArray = capturedMetadata.getJsonArray("salts"); |
| 180 | + salt = saltsArray.getJsonObject(0); |
| 181 | + assertEquals(1740694476392L, salt.getLong("effective"), "Effective timestamp should match second entry"); |
| 182 | + assertEquals(10, salt.getInteger("size"), "Size should match second entries"); |
| 183 | + verify(taggableCloudStorage,atLeastOnce()).upload(pathCaptor.capture(), cloudPathCaptor.capture(), any()); |
| 184 | + } |
127 | 185 | } |
0 commit comments