Fixed build

This commit is contained in:
Jaime Freire
2023-11-25 08:18:02 +01:00
parent db9bb4ccdf
commit 789a5f6abc
141 changed files with 3259 additions and 2957 deletions

View File

@ -43,25 +43,25 @@ import java.security.spec.PKCS8EncodedKeySpec;
// this class is constructed by reflection in GossipManager.
public class JacksonProtocolManager implements ProtocolManager {
private final ObjectMapper objectMapper;
private final PrivateKey privKey;
private final Meter signed;
private final Meter unsigned;
/** required for reflection to work! */
public JacksonProtocolManager(GossipSettings settings, String id, MetricRegistry registry) {
// set up object mapper.
objectMapper = buildObjectMapper(settings);
// set up message signing.
if (settings.isSignMessages()){
if (settings.isSignMessages()) {
File privateKey = new File(settings.getPathToKeyStore(), id);
File publicKey = new File(settings.getPathToKeyStore(), id + ".pub");
if (!privateKey.exists()){
if (!privateKey.exists()) {
throw new IllegalArgumentException("private key not found " + privateKey);
}
if (!publicKey.exists()){
if (!publicKey.exists()) {
throw new IllegalArgumentException("public key not found " + publicKey);
}
try (FileInputStream keyfis = new FileInputStream(privateKey)) {
@ -77,15 +77,39 @@ public class JacksonProtocolManager implements ProtocolManager {
} else {
privKey = null;
}
signed = registry.meter(PassiveGossipConstants.SIGNED_MESSAGE);
unsigned = registry.meter(PassiveGossipConstants.UNSIGNED_MESSAGE);
}
public static ObjectMapper buildObjectMapper(GossipSettings settings) {
ObjectMapper om = new ObjectMapper();
om.enableDefaultTyping();
// todo: should be specified in the configuration.
om.registerModule(new CrdtModule());
om.configure(JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS, false);
return om;
}
private static byte[] sign(byte[] bytes, PrivateKey pk) {
Signature dsa;
try {
dsa = Signature.getInstance("SHA1withDSA", "SUN");
dsa.initSign(pk);
dsa.update(bytes);
return dsa.sign();
} catch (NoSuchAlgorithmException
| NoSuchProviderException
| InvalidKeyException
| SignatureException e) {
throw new RuntimeException(e);
}
}
@Override
public byte[] write(Base message) throws IOException {
byte[] json_bytes;
if (privKey == null){
if (privKey == null) {
json_bytes = objectMapper.writeValueAsBytes(message);
} else {
SignedPayload p = new SignedPayload();
@ -99,7 +123,7 @@ public class JacksonProtocolManager implements ProtocolManager {
@Override
public Base read(byte[] buf) throws IOException {
Base activeGossipMessage = objectMapper.readValue(buf, Base.class);
if (activeGossipMessage instanceof SignedPayload){
if (activeGossipMessage instanceof SignedPayload) {
SignedPayload s = (SignedPayload) activeGossipMessage;
signed.mark();
return objectMapper.readValue(s.getData(), Base.class);
@ -108,25 +132,4 @@ public class JacksonProtocolManager implements ProtocolManager {
return activeGossipMessage;
}
}
public static ObjectMapper buildObjectMapper(GossipSettings settings) {
ObjectMapper om = new ObjectMapper();
om.enableDefaultTyping();
// todo: should be specified in the configuration.
om.registerModule(new CrdtModule());
om.configure(JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS, false);
return om;
}
private static byte[] sign(byte [] bytes, PrivateKey pk){
Signature dsa;
try {
dsa = Signature.getInstance("SHA1withDSA", "SUN");
dsa.initSign(pk);
dsa.update(bytes);
return dsa.sign();
} catch (NoSuchAlgorithmException | NoSuchProviderException | InvalidKeyException | SignatureException e) {
throw new RuntimeException(e);
}
}
}

View File

@ -20,6 +20,12 @@ package org.apache.gossip.protocol.json;
import com.codahale.metrics.MetricRegistry;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import org.apache.gossip.GossipSettings;
import org.apache.gossip.Member;
import org.apache.gossip.crdt.LwwSet;
@ -29,16 +35,10 @@ import org.apache.gossip.crdt.TwoPhaseSet;
import org.apache.gossip.manager.GossipManager;
import org.apache.gossip.manager.GossipManagerBuilder;
import org.apache.gossip.protocol.ProtocolManager;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import static org.junit.jupiter.api.Assertions.*;
public class JacksonTest {
@ -56,64 +56,66 @@ public class JacksonTest {
}
// formerly of SignedMessageTest.
@Test(expected = IllegalArgumentException.class)
public void ifSignMustHaveKeys()
throws URISyntaxException, UnknownHostException, InterruptedException {
@Test()
public void ifSignMustHaveKeys() throws URISyntaxException {
String cluster = UUID.randomUUID().toString();
GossipSettings settings = withSigning(simpleSettings(new GossipSettings()));
List<Member> startupMembers = new ArrayList<>();
URI uri = new URI("udp://" + "127.0.0.1" + ":" + (30000 + 1));
GossipManager gossipService = GossipManagerBuilder.newBuilder()
GossipManager gossipService =
GossipManagerBuilder.newBuilder()
.cluster(cluster)
.uri(uri)
.id(1 + "")
.gossipMembers(startupMembers)
.gossipSettings(settings)
.build();
gossipService.init();
assertThrows(IllegalArgumentException.class, gossipService::init);
}
@Test
public void jacksonSerialTest() throws InterruptedException, URISyntaxException, IOException {
ObjectMapper objectMapper = JacksonProtocolManager.buildObjectMapper(simpleSettings(new GossipSettings()));
public void jacksonSerialTest() throws IOException {
ObjectMapper objectMapper =
JacksonProtocolManager.buildObjectMapper(simpleSettings(new GossipSettings()));
OrSet<Integer> i = new OrSet<Integer>(new OrSet.Builder<Integer>().add(1).remove(1));
String s = objectMapper.writeValueAsString(i);
@SuppressWarnings("unchecked")
OrSet<Integer> back = objectMapper.readValue(s, OrSet.class);
Assert.assertEquals(back, i);
Assertions.assertEquals(back, i);
}
void jacksonCrdtSeDeTest(Object value, Class<?> cl){
ObjectMapper objectMapper = JacksonProtocolManager.buildObjectMapper(simpleSettings(new GossipSettings()));
void jacksonCrdtSeDeTest(Object value, Class<?> cl) {
ObjectMapper objectMapper =
JacksonProtocolManager.buildObjectMapper(simpleSettings(new GossipSettings()));
try {
String valueS = objectMapper.writeValueAsString(value);
@SuppressWarnings("unchecked")
Object parsedValue = objectMapper.readValue(valueS, cl);
Assert.assertEquals(value, parsedValue);
assertEquals(value, parsedValue);
} catch (Exception e) {
Assert.fail("Jackson se/de error");
Assertions.fail("Jackson se/de error");
}
}
@Test
public void jacksonOrSetTest(){
public void jacksonOrSetTest() {
jacksonCrdtSeDeTest(new OrSet<>("1", "2", "3").remove("2"), OrSet.class);
}
@Test
public void jacksonLWWSetTest(){
public void jacksonLWWSetTest() {
jacksonCrdtSeDeTest(new LwwSet<>("1", "2", "3").remove("2"), LwwSet.class);
}
@Test
public void jacksonMaxChangeSetTest(){
public void jacksonMaxChangeSetTest() {
jacksonCrdtSeDeTest(new MaxChangeSet<>("1", "2", "3").remove("2"), MaxChangeSet.class);
}
@Test
public void jacksonTwoPhaseSetTest(){
public void jacksonTwoPhaseSetTest() {
jacksonCrdtSeDeTest(new TwoPhaseSet<>("1", "2", "3").remove("2"), TwoPhaseSet.class);
}
@ -121,30 +123,34 @@ public class JacksonTest {
public void testMessageEqualityAssumptions() {
long timeA = System.nanoTime();
long timeB = System.nanoTime();
Assert.assertNotEquals(timeA, timeB);
assertNotEquals(timeA, timeB);
TestMessage messageA0 = new TestMessage(Long.toHexString(timeA));
TestMessage messageA1 = new TestMessage(Long.toHexString(timeA));
TestMessage messageB = new TestMessage(Long.toHexString(timeB));
Assert.assertEquals(messageA0, messageA1);
Assert.assertFalse(messageA0 == messageA1);
Assert.assertNotEquals(messageA0, messageB);
Assert.assertNotEquals(messageA1, messageB);
assertEquals(messageA0, messageA1);
assertNotSame(messageA0, messageA1);
assertNotEquals(messageA0, messageB);
assertNotEquals(messageA1, messageB);
}
// ideally, we would test the serializability of every message type, but we just want to make sure this works in
// ideally, we would test the serializability of every message type, but we just want to make sure
// this works in
// basic cases.
@Test
public void testMessageSerializationRoundTrip() throws Exception {
ProtocolManager mgr = new JacksonProtocolManager(simpleSettings(new GossipSettings()), "foo", new MetricRegistry());
ProtocolManager mgr =
new JacksonProtocolManager(
simpleSettings(new GossipSettings()), "foo", new MetricRegistry());
for (int i = 0; i < 100; i++) {
TestMessage a = new TestMessage(Long.toHexString(System.nanoTime()));
byte[] bytes = mgr.write(a);
TestMessage b = (TestMessage) mgr.read(bytes);
Assert.assertFalse(a == b);
Assert.assertEquals(a, b);
Assert.assertEquals(a.getMapOfThings(), b.getMapOfThings()); // concerned about that one, so explicit check.
assertNotSame(a, b);
assertEquals(a, b);
assertEquals(
a.getMapOfThings(), b.getMapOfThings()); // concerned about that one, so explicit check.
}
}
}

View File

@ -17,6 +17,8 @@
*/
package org.apache.gossip.protocol.json;
import lombok.Data;
import lombok.Getter;
import org.apache.gossip.model.Base;
import org.apache.gossip.udp.Trackable;
@ -30,6 +32,7 @@ import java.util.Objects;
* Note that there are no Jackson annotations.
* getters and setters are the keys to making this work without the Jackson annotations.
*/
@Data
class TestMessage extends Base implements Trackable {
private String unique;
private String from;
@ -41,9 +44,8 @@ class TestMessage extends Base implements Trackable {
private Object[] arrayOfThings;
private Map<String, String> mapOfThings = new HashMap<>();
@SuppressWarnings("unused")//Used by ObjectMapper
private TestMessage() {
}
@SuppressWarnings("unused") // Used by ObjectMapper
private TestMessage() {}
TestMessage(String unique) {
this.unique = unique;
@ -53,9 +55,8 @@ class TestMessage extends Base implements Trackable {
otherThing = new Subclass(Integer.toHexString(derivedField.hashCode()));
floatValue = (float) unique.hashCode() / (float) from.hashCode();
doubleValue = (double) uuid.hashCode() / (double) derivedField.hashCode();
arrayOfThings = new Object[]{
this.unique, from, uuid, derivedField, otherThing, floatValue, doubleValue
};
arrayOfThings =
new Object[] {this.unique, from, uuid, derivedField, otherThing, floatValue, doubleValue};
String curThing = unique;
for (int i = 0; i < 100; i++) {
@ -91,18 +92,14 @@ class TestMessage extends Base implements Trackable {
if (this == o) return true;
if (!(o instanceof TestMessage)) return false;
TestMessage that = (TestMessage) o;
return Objects.equals(unique, that.unique) &&
Objects.equals(from, that.from) &&
Objects.equals(getUuid(), that.getUuid()) &&
Objects.equals(derivedField, that.derivedField) &&
Objects.equals(floatValue, that.floatValue) &&
Objects.equals(doubleValue, that.doubleValue) &&
Arrays.equals(arrayOfThings, that.arrayOfThings) &&
Objects.equals(mapOfThings, that.mapOfThings);
}
public String getUnique() {
return unique;
return Objects.equals(unique, that.unique)
&& Objects.equals(from, that.from)
&& Objects.equals(getUuid(), that.getUuid())
&& Objects.equals(derivedField, that.derivedField)
&& Objects.equals(floatValue, that.floatValue)
&& Objects.equals(doubleValue, that.doubleValue)
&& Arrays.equals(arrayOfThings, that.arrayOfThings)
&& Objects.equals(mapOfThings, that.mapOfThings);
}
public void setUnique(String unique) {
@ -167,14 +164,21 @@ class TestMessage extends Base implements Trackable {
@Override
public int hashCode() {
return Objects.hash(unique, getUriFrom(), getUuid(), derivedField, floatValue, doubleValue, arrayOfThings, mapOfThings);
return Objects.hash(
unique,
getUriFrom(),
getUuid(),
derivedField,
floatValue,
doubleValue,
arrayOfThings,
mapOfThings);
}
static class Subclass {
private String thing;
public Subclass() {
}
public Subclass() {}
public Subclass(String thing) {
this.thing = thing;
@ -197,4 +201,4 @@ class TestMessage extends Base implements Trackable {
return thing;
}
}
}
}