|
16 | 16 |
|
17 | 17 | package com.hedera.node.app.version;
|
18 | 18 |
|
| 19 | +import static com.hedera.node.app.version.HederaSoftwareVersion.RELEASE_027_VERSION; |
19 | 20 | import static org.assertj.core.api.Assertions.assertThat;
|
| 21 | +import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 23 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
20 | 24 |
|
21 | 25 | import com.hedera.hapi.node.base.SemanticVersion;
|
22 | 26 | import com.hedera.node.config.converter.SemanticVersionConverter;
|
| 27 | +import com.swirlds.common.constructable.ClassConstructorPair; |
| 28 | +import com.swirlds.common.constructable.ConstructableRegistry; |
| 29 | +import com.swirlds.common.constructable.ConstructableRegistryException; |
23 | 30 | import com.swirlds.common.io.streams.SerializableDataInputStream;
|
24 | 31 | import com.swirlds.common.io.streams.SerializableDataOutputStream;
|
25 | 32 | import edu.umd.cs.findbugs.annotations.NonNull;
|
26 | 33 | import java.io.ByteArrayInputStream;
|
27 | 34 | import java.io.ByteArrayOutputStream;
|
28 | 35 | import java.io.IOException;
|
| 36 | +import java.io.InputStream; |
29 | 37 | import java.util.ArrayList;
|
30 | 38 | import java.util.Collections;
|
31 | 39 | import java.util.Random;
|
@@ -66,6 +74,73 @@ void compareTo(@NonNull final String a, @NonNull final String b, final String ex
|
66 | 74 | }
|
67 | 75 | }
|
68 | 76 |
|
| 77 | + @Test |
| 78 | + void serializationRoundTripWithConfigVersionTest() throws IOException, ConstructableRegistryException { |
| 79 | + ConstructableRegistry.getInstance() |
| 80 | + .registerConstructable( |
| 81 | + new ClassConstructorPair(HederaSoftwareVersion.class, HederaSoftwareVersion::new)); |
| 82 | + |
| 83 | + final HederaSoftwareVersion v1 = new HederaSoftwareVersion( |
| 84 | + new SemanticVersion(0, 48, 0, "alpha.5", ""), new SemanticVersion(0, 48, 0, "", ""), 1); |
| 85 | + |
| 86 | + final HederaSoftwareVersion v2 = new HederaSoftwareVersion( |
| 87 | + new SemanticVersion(0, 48, 0, "alpha.5", ""), new SemanticVersion(0, 48, 0, "", ""), 1); |
| 88 | + |
| 89 | + assertEquals(0, v1.compareTo(v2)); |
| 90 | + |
| 91 | + final ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); |
| 92 | + final SerializableDataOutputStream out = new SerializableDataOutputStream(byteOut); |
| 93 | + out.writeSerializable(v1, true); |
| 94 | + |
| 95 | + final SerializableDataInputStream in = |
| 96 | + new SerializableDataInputStream(new ByteArrayInputStream(byteOut.toByteArray())); |
| 97 | + final HederaSoftwareVersion v3 = in.readSerializable(); |
| 98 | + |
| 99 | + assertEquals(0, v1.compareTo(v3)); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + void byteFormatDoesNotChangeAfterMigration() throws IOException, ConstructableRegistryException { |
| 104 | + ConstructableRegistry.getInstance() |
| 105 | + .registerConstructable( |
| 106 | + new ClassConstructorPair(HederaSoftwareVersion.class, HederaSoftwareVersion::new)); |
| 107 | + |
| 108 | + /* |
| 109 | + // The following code was used to generate the serialized software version on disk. |
| 110 | + // File was generated using the branch release/0.47. |
| 111 | +
|
| 112 | + final HederaSoftwareVersion version = new HederaSoftwareVersion(semver("1.2.3"), semver("4.5.6")); |
| 113 | + final FileOutputStream fos = new FileOutputStream("hederaSoftwareVersion_27.dat"); |
| 114 | + final SerializableDataOutputStream out = new SerializableDataOutputStream(fos); |
| 115 | + out.writeSerializable(version, true); |
| 116 | + out.close(); |
| 117 | + */ |
| 118 | + |
| 119 | + final byte[] legacyBytes; |
| 120 | + try (final InputStream legacyFile = |
| 121 | + HederaSoftwareVersion.class.getClassLoader().getResourceAsStream("hederaSoftwareVersion_27.dat")) { |
| 122 | + assertNotNull(legacyFile); |
| 123 | + legacyBytes = legacyFile.readAllBytes(); |
| 124 | + } |
| 125 | + |
| 126 | + final SerializableDataInputStream legacyIn = |
| 127 | + new SerializableDataInputStream(new ByteArrayInputStream(legacyBytes)); |
| 128 | + final HederaSoftwareVersion deserializedVersion = legacyIn.readSerializable(); |
| 129 | + |
| 130 | + assertEquals(RELEASE_027_VERSION, deserializedVersion.getVersion()); |
| 131 | + assertEquals(semver("1.2.3"), deserializedVersion.getHapiVersion()); |
| 132 | + assertEquals(semver("4.5.6"), deserializedVersion.getServicesVersion()); |
| 133 | + |
| 134 | + // Write the deserialized version back to a byte array. It should exactly match the original byte array. |
| 135 | + final ByteArrayOutputStream newBytes = new ByteArrayOutputStream(); |
| 136 | + final SerializableDataOutputStream newOut = new SerializableDataOutputStream(newBytes); |
| 137 | + newOut.writeSerializable(deserializedVersion, true); |
| 138 | + newOut.close(); |
| 139 | + final byte[] newBytesArray = newBytes.toByteArray(); |
| 140 | + |
| 141 | + assertArrayEquals(legacyBytes, newBytesArray); |
| 142 | + } |
| 143 | + |
69 | 144 | @Test
|
70 | 145 | @DisplayName("Sorting HederaSoftwareVersions")
|
71 | 146 | void sorting() {
|
|
0 commit comments