|
18 | 18 | */ |
19 | 19 |
|
20 | 20 | /* |
21 | | - * Copyright (c) 2014, 2022, Oracle and/or its affiliates. All rights reserved. |
| 21 | + * Copyright (c) 2014, 2023, Oracle and/or its affiliates. All rights reserved. |
22 | 22 | * Portions Copyright (c) 2018, 2020, Chris Fraire <[email protected]>. |
23 | 23 | * Portions Copyright (c) 2020, 2023, Ric Harris <[email protected]>. |
24 | 24 | */ |
|
28 | 28 | import static org.junit.jupiter.api.Assertions.assertFalse; |
29 | 29 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
30 | 30 | import static org.junit.jupiter.api.Assertions.assertNull; |
31 | | -import static org.junit.jupiter.api.Assertions.assertThrows; |
32 | 31 | import static org.junit.jupiter.api.Assertions.assertTrue; |
33 | 32 | import static org.opengrok.indexer.condition.RepositoryInstalled.Type.MERCURIAL; |
34 | 33 | import static org.opengrok.indexer.condition.RepositoryInstalled.Type.SCCS; |
|
38 | 37 | import java.io.File; |
39 | 38 | import java.io.FileOutputStream; |
40 | 39 | import java.io.IOException; |
| 40 | +import java.io.OutputStream; |
41 | 41 | import java.nio.charset.StandardCharsets; |
42 | 42 | import java.nio.file.Files; |
43 | 43 | import java.nio.file.Path; |
44 | 44 | import java.nio.file.Paths; |
45 | 45 | import java.nio.file.attribute.FileTime; |
| 46 | +import java.util.ArrayList; |
| 47 | +import java.util.Collections; |
46 | 48 | import java.util.Date; |
47 | 49 | import java.util.Iterator; |
48 | 50 | import java.util.LinkedList; |
49 | 51 | import java.util.List; |
50 | 52 | import java.util.Map; |
51 | 53 | import java.util.Set; |
52 | 54 |
|
| 55 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 56 | +import com.fasterxml.jackson.databind.ObjectWriter; |
| 57 | +import com.fasterxml.jackson.dataformat.smile.SmileFactory; |
| 58 | +import com.fasterxml.jackson.dataformat.smile.SmileParser; |
| 59 | +import com.fasterxml.jackson.dataformat.smile.databind.SmileMapper; |
53 | 60 | import org.apache.commons.lang3.time.DateUtils; |
54 | 61 | import org.eclipse.jgit.api.Git; |
55 | 62 | import org.junit.jupiter.api.AfterEach; |
@@ -196,7 +203,7 @@ void testStoreTouchGet() throws Exception { |
196 | 203 | /* |
197 | 204 | * The history should not be disturbed. |
198 | 205 | * Make sure that get() retrieved the history from cache. Mocking/spying static methods |
199 | | - * (FileHistoryCache#readCache() in this case) is tricky so use the cache hits metric. |
| 206 | + * (FileHistoryCache#readHistory() in this case) is tricky so use the cache hits metric. |
200 | 207 | */ |
201 | 208 | double cacheHitsBeforeGet = cache.getFileHistoryCacheHits(); |
202 | 209 | History historyAfterReindex = cache.get(file, repo, false); |
@@ -251,7 +258,7 @@ void testStoreAndGetIncrementalTags() throws Exception { |
251 | 258 | // Avoid uncommitted changes. |
252 | 259 | MercurialRepositoryTest.runHgCommand(reposRoot, "revert", "--all"); |
253 | 260 |
|
254 | | - // Add bunch of changesets with file based changes and tags. |
| 261 | + // Add a bunch of changesets with file based changes and tags. |
255 | 262 | MercurialRepositoryTest.runHgCommand(reposRoot, "import", |
256 | 263 | Paths.get(getClass().getResource("/history/hg-export-tag.txt").toURI()).toString()); |
257 | 264 |
|
@@ -396,14 +403,15 @@ void testStoreAndGet() throws Exception { |
396 | 403 | /** |
397 | 404 | * Check how incremental reindex behaves when indexing changesets that |
398 | 405 | * rename+change file. |
399 | | - * |
| 406 | + * <p> |
400 | 407 | * The scenario goes as follows: |
401 | 408 | * - create Mercurial repository |
402 | 409 | * - perform full reindex |
403 | 410 | * - add changesets which renamed and modify a file |
404 | 411 | * - perform incremental reindex |
405 | 412 | * - change+rename the file again |
406 | 413 | * - incremental reindex |
| 414 | + * </p> |
407 | 415 | */ |
408 | 416 | @EnabledOnOs({OS.LINUX, OS.MAC, OS.SOLARIS, OS.AIX, OS.OTHER}) |
409 | 417 | @EnabledForRepository(MERCURIAL) |
@@ -984,59 +992,40 @@ void testStoreAndTryToGetIgnored() throws Exception { |
984 | 992 | assertNotNull(retrievedHistory, "history for Makefile should not be null"); |
985 | 993 | } |
986 | 994 |
|
987 | | - @ParameterizedTest |
988 | | - @ValueSource(strings = { |
989 | | - "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + |
990 | | - "<java version=\"11.0.8\" class=\"java.beans.XMLDecoder\">\n" + |
991 | | - " <object class=\"java.lang.Runtime\" method=\"getRuntime\">\n" + |
992 | | - " <void method=\"exec\">\n" + |
993 | | - " <array class=\"java.lang.String\" length=\"2\">\n" + |
994 | | - " <void index=\"0\">\n" + |
995 | | - " <string>/usr/bin/nc</string>\n" + |
996 | | - " </void>\n" + |
997 | | - " <void index=\"1\">\n" + |
998 | | - " <string>-l</string>\n" + |
999 | | - " </void>\n" + |
1000 | | - " </array>\n" + |
1001 | | - " </void>\n" + |
1002 | | - " </object>\n" + |
1003 | | - "</java>", |
1004 | | - "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + |
1005 | | - "<java version=\"11.0.8\" class=\"java.beans.XMLDecoder\">\n" + |
1006 | | - " <object class=\"java.lang.ProcessBuilder\">\n" + |
1007 | | - " <array class=\"java.lang.String\" length=\"1\" >\n" + |
1008 | | - " <void index=\"0\"> \n" + |
1009 | | - " <string>/usr/bin/curl https://oracle.com</string>\n" + |
1010 | | - " </void>\n" + |
1011 | | - " </array>\n" + |
1012 | | - " <void method=\"start\"/>\n" + |
1013 | | - " </object>\n" + |
1014 | | - "</java>", |
1015 | | - "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + |
1016 | | - "<java version=\"11.0.8\" class=\"java.beans.XMLDecoder\">\n" + |
1017 | | - " <object class = \"java.io.FileOutputStream\"> \n" + |
1018 | | - " <string>opengrok_test.txt</string>\n" + |
1019 | | - " <method name = \"write\">\n" + |
1020 | | - " <array class=\"byte\" length=\"3\">\n" + |
1021 | | - " <void index=\"0\"><byte>96</byte></void>\n" + |
1022 | | - " <void index=\"1\"><byte>96</byte></void>\n" + |
1023 | | - " <void index=\"2\"><byte>96</byte></void>\n" + |
1024 | | - " </array>\n" + |
1025 | | - " </method>\n" + |
1026 | | - " <method name=\"close\"/>\n" + |
1027 | | - " </object>\n" + |
1028 | | - "</java>" |
1029 | | - }) |
1030 | | - void testDeserializationOfNotWhiteListedClassThrowsError(final String exploit) { |
1031 | | - assertThrows(IllegalAccessError.class, () -> FileHistoryCache.readCache(exploit)); |
1032 | | - } |
1033 | | - |
| 995 | + // TODO |
1034 | 996 | @Test |
1035 | | - void testReadCacheValid() throws IOException { |
1036 | | - File testFile = new File(FileHistoryCacheTest.class.getClassLoader(). |
1037 | | - getResource("history/FileHistoryCache.java.gz").getFile()); |
1038 | | - History history = FileHistoryCache.readCache(testFile); |
1039 | | - assertNotNull(history); |
1040 | | - assertEquals(30, history.getHistoryEntries().size()); |
| 997 | + void testSmile() throws Exception { |
| 998 | + ObjectMapper mapper = new SmileMapper(); |
| 999 | + File outputFile = new File("/tmp/histentry"); |
| 1000 | + ObjectWriter objectWriter = mapper.writer().forType(HistoryEntry.class); |
| 1001 | + |
| 1002 | + try (OutputStream outputStream = new FileOutputStream(outputFile)) { |
| 1003 | + HistoryEntry historyEntry = new HistoryEntry("1.1.1", "1", |
| 1004 | + new Date(1245446973L / 60 * 60 * 1000), |
| 1005 | + "xyz", |
| 1006 | + "Return failure when executed with no arguments", |
| 1007 | + true, Collections.emptyList()); |
| 1008 | + byte[] bytes = objectWriter.writeValueAsBytes(historyEntry); |
| 1009 | + outputStream.write(bytes); |
| 1010 | + |
| 1011 | + historyEntry = new HistoryEntry("2.2.2", "2", |
| 1012 | + new Date(1245446973L / 60 * 60 * 1000), |
| 1013 | + "xyz", |
| 1014 | + "Return failure when executed with no arguments", |
| 1015 | + true, Collections.emptyList()); |
| 1016 | + bytes = objectWriter.writeValueAsBytes(historyEntry); |
| 1017 | + outputStream.write(bytes); |
| 1018 | + } |
| 1019 | + |
| 1020 | + SmileFactory factory = new SmileFactory(); |
| 1021 | + List<HistoryEntry> historyEntryList = new ArrayList<>(); |
| 1022 | + try (SmileParser parser = factory.createParser(outputFile)) { |
| 1023 | + parser.setCodec(mapper); |
| 1024 | + Iterator<HistoryEntry> historyEntryIterator = parser.readValuesAs(HistoryEntry.class); |
| 1025 | + historyEntryIterator.forEachRemaining(historyEntryList::add); |
| 1026 | + } |
| 1027 | + |
| 1028 | + History history = new History(historyEntryList); |
| 1029 | + System.out.println(history); |
1041 | 1030 | } |
1042 | 1031 | } |
0 commit comments