Matthias Andreas Benkard | b5d657a | 2022-02-03 21:14:30 +0100 | [diff] [blame] | 1 | // SPDX-FileCopyrightText: © 2021 Matthias Andreas Benkard <code@mail.matthias.benkard.de> |
| 2 | // |
| 3 | // SPDX-License-Identifier: LGPL-3.0-or-later |
| 4 | |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 5 | package eu.mulk.jgvariant.ostree; |
| 6 | |
| 7 | import static org.junit.jupiter.api.Assertions.assertAll; |
| 8 | import static org.junit.jupiter.api.Assertions.assertEquals; |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 9 | |
| 10 | import com.adelean.inject.resources.junit.jupiter.GivenBinaryResource; |
| 11 | import com.adelean.inject.resources.junit.jupiter.TestWithResources; |
| 12 | import eu.mulk.jgvariant.core.Signature; |
| 13 | import eu.mulk.jgvariant.core.Variant; |
| 14 | import java.nio.ByteBuffer; |
Matthias Andreas Benkard | c442ebe | 2023-12-10 17:58:38 +0100 | [diff] [blame] | 15 | import java.util.*; |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 16 | import org.junit.jupiter.api.Test; |
| 17 | |
| 18 | @TestWithResources |
Matthias Andreas Benkard | 91dbd74 | 2022-10-17 19:38:56 +0200 | [diff] [blame] | 19 | @SuppressWarnings({ |
Matthias Andreas Benkard | c442ebe | 2023-12-10 17:58:38 +0100 | [diff] [blame] | 20 | "DoubleBraceInitialization", |
Matthias Andreas Benkard | 91dbd74 | 2022-10-17 19:38:56 +0200 | [diff] [blame] | 21 | "ImmutableListOf1", |
| 22 | "ImmutableMapOf1", |
| 23 | "initialization.field.uninitialized", |
Matthias Andreas Benkard | c442ebe | 2023-12-10 17:58:38 +0100 | [diff] [blame] | 24 | "NullAway", |
Matthias Andreas Benkard | 91dbd74 | 2022-10-17 19:38:56 +0200 | [diff] [blame] | 25 | }) |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 26 | class OstreeDecoderTest { |
| 27 | |
| 28 | @GivenBinaryResource("/ostree/summary") |
| 29 | byte[] summaryBytes; |
| 30 | |
| 31 | @GivenBinaryResource( |
| 32 | "/ostree/objects/3d/3b3329dca38871f29aeda1bf5854d76c707fa269759a899d0985c91815fe6f.commit") |
| 33 | byte[] commitBytes; |
| 34 | |
| 35 | @GivenBinaryResource( |
| 36 | "/ostree/objects/14/c9b958ac59df4979095a3485b4da5a045fe8737ffdba8cfbfff24988b238f7.dirtree") |
| 37 | byte[] dirTreeBytes; |
| 38 | |
| 39 | @GivenBinaryResource( |
| 40 | "/ostree/objects/48/cc6a2ecdab284b9d1e5b0e875c905866ff32f65ee1e857df0e691285d6f14c.dirmeta") |
| 41 | byte[] dirMetaBytes; |
| 42 | |
| 43 | @GivenBinaryResource("/ostree/deltas/Mc/iDXVydLGaHpQCRyFFC0bLYU_9Bap+4G07jB1RRDVI/superblock") |
| 44 | byte[] deltaSuperblockBytes; |
| 45 | |
| 46 | @GivenBinaryResource("/ostree/deltas/Mc/iDXVydLGaHpQCRyFFC0bLYU_9Bap+4G07jB1RRDVI/0") |
| 47 | byte[] deltaPartPayloadBytes; |
| 48 | |
| 49 | @Test |
Matthias Andreas Benkard | 91dbd74 | 2022-10-17 19:38:56 +0200 | [diff] [blame] | 50 | void summaryDecoder() { |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 51 | var decoder = Summary.decoder(); |
Matthias Andreas Benkard | c442ebe | 2023-12-10 17:58:38 +0100 | [diff] [blame] | 52 | var input = ByteBuffer.wrap(summaryBytes); |
| 53 | var summary = decoder.decode(input); |
Matthias Andreas Benkard | f66fc18 | 2023-12-10 11:37:30 +0100 | [diff] [blame] | 54 | |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 55 | assertAll( |
| 56 | () -> |
| 57 | assertEquals( |
| 58 | List.of( |
| 59 | new Summary.Entry( |
| 60 | "mulkos/1.x/amd64", |
| 61 | new Summary.Entry.Value( |
| 62 | 214, |
| 63 | Checksum.ofHex( |
| 64 | "66ff167ff35ce87daac817447a9490a262ee75f095f017716a6eb1a9d9eb3350"), |
| 65 | new Metadata( |
Matthias Andreas Benkard | 329168c | 2021-12-28 01:20:05 +0100 | [diff] [blame] | 66 | Map.of( |
| 67 | "ostree.commit.timestamp", |
| 68 | new Variant(Signature.parse("t"), 1640537170L)))))), |
Matthias Andreas Benkard | f66fc18 | 2023-12-10 11:37:30 +0100 | [diff] [blame] | 69 | summary.entries()), |
| 70 | () -> |
| 71 | assertEquals( |
Matthias Andreas Benkard | c442ebe | 2023-12-10 17:58:38 +0100 | [diff] [blame] | 72 | new LinkedHashMap<String, Variant>() { |
| 73 | { |
| 74 | put("ostree.summary.mode", new Variant(Signature.parse("s"), "bare")); |
| 75 | put( |
| 76 | "ostree.summary.last-modified", |
| 77 | new Variant(Signature.parse("t"), 1640537300L)); |
| 78 | put( |
| 79 | "ostree.summary.tombstone-commits", |
| 80 | new Variant(Signature.parse("b"), false)); |
| 81 | put( |
| 82 | "ostree.static-deltas", |
| 83 | new Variant( |
| 84 | Signature.parse("a{sv}"), |
| 85 | new LinkedHashMap<String, Variant>() { |
| 86 | { |
| 87 | put( |
| 88 | "3d3b3329dca38871f29aeda1bf5854d76c707fa269759a899d0985c91815fe6f-66ff167ff35ce87daac817447a9490a262ee75f095f017716a6eb1a9d9eb3350", |
| 89 | new Variant( |
| 90 | Signature.parse("ay"), |
| 91 | bytesOfHex( |
| 92 | "03738040e28e7662e9c9d2599c530ea974e642c9f87e6c00cbaa39a0cdac8d44"))); |
| 93 | put( |
| 94 | "31c8835d5c9d2c6687a50091c85142d1b2d853ff416a9fb81b4ee30754510d52", |
| 95 | new Variant( |
| 96 | Signature.parse("ay"), |
| 97 | bytesOfHex( |
| 98 | "f481144629474bd88c106e45ac405ebd75b324b0655af1aec14b31786ae1fd61"))); |
| 99 | put( |
| 100 | "31c8835d5c9d2c6687a50091c85142d1b2d853ff416a9fb81b4ee30754510d52-3d3b3329dca38871f29aeda1bf5854d76c707fa269759a899d0985c91815fe6f", |
| 101 | new Variant( |
| 102 | Signature.parse("ay"), |
| 103 | bytesOfHex( |
| 104 | "2c6a07bc1cf4d7ce7d00f82d7d2d6d156fd0e31d476851b46dc2306b181b064a"))); |
| 105 | } |
| 106 | })); |
| 107 | put("ostree.summary.indexed-deltas", new Variant(Signature.parse("b"), true)); |
| 108 | } |
| 109 | }, |
Matthias Andreas Benkard | f66fc18 | 2023-12-10 11:37:30 +0100 | [diff] [blame] | 110 | summary.metadata().fields())); |
| 111 | |
Matthias Andreas Benkard | c442ebe | 2023-12-10 17:58:38 +0100 | [diff] [blame] | 112 | var encoded = decoder.encode(summary); |
Matthias Andreas Benkard | df853ef | 2023-12-10 20:29:35 +0100 | [diff] [blame] | 113 | |
Matthias Andreas Benkard | c442ebe | 2023-12-10 17:58:38 +0100 | [diff] [blame] | 114 | input.rewind(); |
| 115 | assertEquals(input, encoded); |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | @Test |
Matthias Andreas Benkard | 91dbd74 | 2022-10-17 19:38:56 +0200 | [diff] [blame] | 119 | void commitDecoder() { |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 120 | var decoder = Commit.decoder(); |
Matthias Andreas Benkard | c442ebe | 2023-12-10 17:58:38 +0100 | [diff] [blame] | 121 | var input = ByteBuffer.wrap(commitBytes); |
| 122 | var commit = decoder.decode(input); |
| 123 | |
| 124 | var encoded = decoder.encode(commit); |
Matthias Andreas Benkard | df853ef | 2023-12-10 20:29:35 +0100 | [diff] [blame] | 125 | |
Matthias Andreas Benkard | c442ebe | 2023-12-10 17:58:38 +0100 | [diff] [blame] | 126 | input.rewind(); |
| 127 | assertEquals(input, encoded); |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | @Test |
Matthias Andreas Benkard | 91dbd74 | 2022-10-17 19:38:56 +0200 | [diff] [blame] | 131 | void dirTreeDecoder() { |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 132 | var decoder = DirTree.decoder(); |
Matthias Andreas Benkard | c442ebe | 2023-12-10 17:58:38 +0100 | [diff] [blame] | 133 | var input = ByteBuffer.wrap(dirTreeBytes); |
| 134 | var dirTree = decoder.decode(input); |
| 135 | |
| 136 | var encoded = decoder.encode(dirTree); |
Matthias Andreas Benkard | df853ef | 2023-12-10 20:29:35 +0100 | [diff] [blame] | 137 | |
Matthias Andreas Benkard | c442ebe | 2023-12-10 17:58:38 +0100 | [diff] [blame] | 138 | input.rewind(); |
| 139 | assertEquals(input, encoded); |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | @Test |
Matthias Andreas Benkard | 91dbd74 | 2022-10-17 19:38:56 +0200 | [diff] [blame] | 143 | void dirMetaDecoder() { |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 144 | var decoder = DirMeta.decoder(); |
Matthias Andreas Benkard | c442ebe | 2023-12-10 17:58:38 +0100 | [diff] [blame] | 145 | var input = ByteBuffer.wrap(dirMetaBytes); |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 146 | var dirMeta = decoder.decode(ByteBuffer.wrap(dirMetaBytes)); |
Matthias Andreas Benkard | c442ebe | 2023-12-10 17:58:38 +0100 | [diff] [blame] | 147 | |
| 148 | var encoded = decoder.encode(dirMeta); |
Matthias Andreas Benkard | df853ef | 2023-12-10 20:29:35 +0100 | [diff] [blame] | 149 | |
Matthias Andreas Benkard | c442ebe | 2023-12-10 17:58:38 +0100 | [diff] [blame] | 150 | input.rewind(); |
| 151 | assertEquals(input, encoded); |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | @Test |
Matthias Andreas Benkard | 91dbd74 | 2022-10-17 19:38:56 +0200 | [diff] [blame] | 155 | void superblockDecoder() { |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 156 | var decoder = DeltaSuperblock.decoder(); |
Matthias Andreas Benkard | c442ebe | 2023-12-10 17:58:38 +0100 | [diff] [blame] | 157 | var input = ByteBuffer.wrap(deltaSuperblockBytes); |
| 158 | var deltaSuperblock = decoder.decode(input); |
Matthias Andreas Benkard | c442ebe | 2023-12-10 17:58:38 +0100 | [diff] [blame] | 159 | |
| 160 | var encoded = decoder.encode(deltaSuperblock); |
Matthias Andreas Benkard | df853ef | 2023-12-10 20:29:35 +0100 | [diff] [blame] | 161 | |
Matthias Andreas Benkard | c442ebe | 2023-12-10 17:58:38 +0100 | [diff] [blame] | 162 | input.rewind(); |
| 163 | assertEquals(input, encoded); |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 164 | } |
| 165 | |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 166 | @Test |
Matthias Andreas Benkard | 91dbd74 | 2022-10-17 19:38:56 +0200 | [diff] [blame] | 167 | void partPayloadDecoder() { |
Matthias Andreas Benkard | c981cde | 2021-12-30 20:37:39 +0100 | [diff] [blame] | 168 | var superblockDecoder = DeltaSuperblock.decoder(); |
| 169 | var superblock = superblockDecoder.decode(ByteBuffer.wrap(deltaSuperblockBytes)); |
| 170 | |
| 171 | var decoder = DeltaPartPayload.decoder(superblock.entries().get(0)); |
Matthias Andreas Benkard | c442ebe | 2023-12-10 17:58:38 +0100 | [diff] [blame] | 172 | var input = ByteBuffer.wrap(deltaPartPayloadBytes); |
| 173 | var deltaPartPayload = decoder.decode(input); |
Matthias Andreas Benkard | c981cde | 2021-12-30 20:37:39 +0100 | [diff] [blame] | 174 | |
Matthias Andreas Benkard | c442ebe | 2023-12-10 17:58:38 +0100 | [diff] [blame] | 175 | var encoded = decoder.encode(deltaPartPayload); |
| 176 | var decodedAgain = decoder.decode(encoded); |
| 177 | assertEquals(deltaPartPayload, decodedAgain); |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 178 | } |
Matthias Andreas Benkard | f66fc18 | 2023-12-10 11:37:30 +0100 | [diff] [blame] | 179 | |
| 180 | private static List<Byte> bytesOfHex(String hex) { |
| 181 | var bytes = HexFormat.of().parseHex(hex); |
| 182 | var byteObjects = new Byte[bytes.length]; |
| 183 | Arrays.setAll(byteObjects, i -> bytes[i]); |
| 184 | return Arrays.asList(byteObjects); |
| 185 | } |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 186 | } |