From d793a7ecae2510688c6852a7646849026d554339 Mon Sep 17 00:00:00 2001 From: Jason Frame Date: Mon, 17 Dec 2018 10:07:06 +1000 Subject: [PATCH] Ibft message tests (#367) --- .../ibft/ibftmessage/CommitMessageTest.java | 70 +++++++++++++++++++ .../ibft/ibftmessage/NewRoundMessageTest.java | 70 +++++++++++++++++++ .../ibftmessage/PrePrepareMessageTest.java | 70 +++++++++++++++++++ .../ibft/ibftmessage/PrepareMessageTest.java | 70 +++++++++++++++++++ .../ibftmessage/RoundChangeMessageTest.java | 70 +++++++++++++++++++ 5 files changed, 350 insertions(+) create mode 100644 consensus/ibft/src/test/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/CommitMessageTest.java create mode 100644 consensus/ibft/src/test/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/NewRoundMessageTest.java create mode 100644 consensus/ibft/src/test/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/PrePrepareMessageTest.java create mode 100644 consensus/ibft/src/test/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/PrepareMessageTest.java create mode 100644 consensus/ibft/src/test/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/RoundChangeMessageTest.java diff --git a/consensus/ibft/src/test/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/CommitMessageTest.java b/consensus/ibft/src/test/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/CommitMessageTest.java new file mode 100644 index 0000000000..01ef9a5bb6 --- /dev/null +++ b/consensus/ibft/src/test/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/CommitMessageTest.java @@ -0,0 +1,70 @@ +/* + * Copyright 2018 ConsenSys AG. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package tech.pegasys.pantheon.consensus.ibft.ibftmessage; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.CommitPayload; +import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.SignedData; +import tech.pegasys.pantheon.ethereum.p2p.api.MessageData; +import tech.pegasys.pantheon.util.bytes.BytesValue; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class CommitMessageTest { + @Mock private SignedData commitPayload; + @Mock private BytesValue messageBytes; + @Mock private MessageData messageData; + @Mock private CommitMessage commitMessage; + + @Test + public void createMessageFromCommitMessageData() { + when(commitPayload.encode()).thenReturn(messageBytes); + CommitMessage commitMessage = CommitMessage.create(commitPayload); + + assertThat(commitMessage.getData()).isEqualTo(messageBytes); + assertThat(commitMessage.getCode()).isEqualTo(IbftV2.COMMIT); + verify(commitPayload).encode(); + } + + @Test + public void createMessageFromCommitMessage() { + CommitMessage message = CommitMessage.fromMessage(commitMessage); + assertThat(message).isSameAs(commitMessage); + } + + @Test + public void createMessageFromGenericMessageData() { + when(messageData.getData()).thenReturn(messageBytes); + when(messageData.getCode()).thenReturn(IbftV2.COMMIT); + CommitMessage commitMessage = CommitMessage.fromMessage(messageData); + + assertThat(commitMessage.getData()).isEqualTo(messageData.getData()); + assertThat(commitMessage.getCode()).isEqualTo(IbftV2.COMMIT); + } + + @Test + public void createMessageFailsWhenIncorrectMessageCode() { + when(messageData.getCode()).thenReturn(42); + assertThatThrownBy(() -> CommitMessage.fromMessage(messageData)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("Message has code 42 and thus is not a CommitMessage"); + } +} diff --git a/consensus/ibft/src/test/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/NewRoundMessageTest.java b/consensus/ibft/src/test/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/NewRoundMessageTest.java new file mode 100644 index 0000000000..e43cfc3b14 --- /dev/null +++ b/consensus/ibft/src/test/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/NewRoundMessageTest.java @@ -0,0 +1,70 @@ +/* + * Copyright 2018 ConsenSys AG. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package tech.pegasys.pantheon.consensus.ibft.ibftmessage; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.NewRoundPayload; +import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.SignedData; +import tech.pegasys.pantheon.ethereum.p2p.api.MessageData; +import tech.pegasys.pantheon.util.bytes.BytesValue; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class NewRoundMessageTest { + @Mock private SignedData newRoundPayload; + @Mock private BytesValue messageBytes; + @Mock private MessageData messageData; + @Mock private NewRoundMessage newRoundMessage; + + @Test + public void createMessageFromNewRoundChangeMessageData() { + when(newRoundPayload.encode()).thenReturn(messageBytes); + NewRoundMessage prepareMessage = NewRoundMessage.create(newRoundPayload); + + assertThat(prepareMessage.getData()).isEqualTo(messageBytes); + assertThat(prepareMessage.getCode()).isEqualTo(IbftV2.NEW_ROUND); + verify(newRoundPayload).encode(); + } + + @Test + public void createMessageFromNewRoundMessage() { + NewRoundMessage message = NewRoundMessage.fromMessage(newRoundMessage); + assertThat(message).isSameAs(newRoundMessage); + } + + @Test + public void createMessageFromGenericMessageData() { + when(messageData.getData()).thenReturn(messageBytes); + when(messageData.getCode()).thenReturn(IbftV2.NEW_ROUND); + NewRoundMessage newRoundMessage = NewRoundMessage.fromMessage(messageData); + + assertThat(newRoundMessage.getData()).isEqualTo(messageData.getData()); + assertThat(newRoundMessage.getCode()).isEqualTo(IbftV2.NEW_ROUND); + } + + @Test + public void createMessageFailsWhenIncorrectMessageCode() { + when(messageData.getCode()).thenReturn(42); + assertThatThrownBy(() -> NewRoundMessage.fromMessage(messageData)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("Message has code 42 and thus is not a NewRoundMessage"); + } +} diff --git a/consensus/ibft/src/test/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/PrePrepareMessageTest.java b/consensus/ibft/src/test/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/PrePrepareMessageTest.java new file mode 100644 index 0000000000..44fdecf769 --- /dev/null +++ b/consensus/ibft/src/test/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/PrePrepareMessageTest.java @@ -0,0 +1,70 @@ +/* + * Copyright 2018 ConsenSys AG. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package tech.pegasys.pantheon.consensus.ibft.ibftmessage; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.ProposalPayload; +import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.SignedData; +import tech.pegasys.pantheon.ethereum.p2p.api.MessageData; +import tech.pegasys.pantheon.util.bytes.BytesValue; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class PrePrepareMessageTest { + @Mock private SignedData prePrepareMessageData; + @Mock private BytesValue messageBytes; + @Mock private MessageData messageData; + @Mock private ProposalMessage proposalMessage; + + @Test + public void createMessageFromPrePrepareMessageData() { + when(prePrepareMessageData.encode()).thenReturn(messageBytes); + ProposalMessage proposalMessage = ProposalMessage.create(prePrepareMessageData); + + assertThat(proposalMessage.getData()).isEqualTo(messageBytes); + assertThat(proposalMessage.getCode()).isEqualTo(IbftV2.PROPOSAL); + verify(prePrepareMessageData).encode(); + } + + @Test + public void createMessageFromPrePrepareMessage() { + ProposalMessage message = ProposalMessage.fromMessage(proposalMessage); + assertThat(message).isSameAs(proposalMessage); + } + + @Test + public void createMessageFromGenericMessageData() { + when(messageData.getCode()).thenReturn(IbftV2.PROPOSAL); + when(messageData.getData()).thenReturn(messageBytes); + ProposalMessage proposalMessage = ProposalMessage.fromMessage(messageData); + + assertThat(proposalMessage.getData()).isEqualTo(messageData.getData()); + assertThat(proposalMessage.getCode()).isEqualTo(IbftV2.PROPOSAL); + } + + @Test + public void createMessageFailsWhenIncorrectMessageCode() { + when(messageData.getCode()).thenReturn(42); + assertThatThrownBy(() -> ProposalMessage.fromMessage(messageData)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("Message has code 42 and thus is not a ProposalMessage"); + } +} diff --git a/consensus/ibft/src/test/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/PrepareMessageTest.java b/consensus/ibft/src/test/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/PrepareMessageTest.java new file mode 100644 index 0000000000..5b30449977 --- /dev/null +++ b/consensus/ibft/src/test/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/PrepareMessageTest.java @@ -0,0 +1,70 @@ +/* + * Copyright 2018 ConsenSys AG. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package tech.pegasys.pantheon.consensus.ibft.ibftmessage; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.PreparePayload; +import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.SignedData; +import tech.pegasys.pantheon.ethereum.p2p.api.MessageData; +import tech.pegasys.pantheon.util.bytes.BytesValue; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class PrepareMessageTest { + @Mock private SignedData preparePayload; + @Mock private BytesValue messageBytes; + @Mock private MessageData messageData; + @Mock private PrepareMessage prepareMessage; + + @Test + public void createMessageFromPrepareMessageData() { + when(preparePayload.encode()).thenReturn(messageBytes); + PrepareMessage prepareMessage = PrepareMessage.create(preparePayload); + + assertThat(prepareMessage.getData()).isEqualTo(messageBytes); + assertThat(prepareMessage.getCode()).isEqualTo(IbftV2.PREPARE); + verify(preparePayload).encode(); + } + + @Test + public void createMessageFromPrepareMessage() { + PrepareMessage message = PrepareMessage.fromMessage(prepareMessage); + assertThat(message).isSameAs(prepareMessage); + } + + @Test + public void createMessageFromGenericMessageData() { + when(messageData.getData()).thenReturn(messageBytes); + when(messageData.getCode()).thenReturn(IbftV2.PREPARE); + PrepareMessage prepareMessage = PrepareMessage.fromMessage(messageData); + + assertThat(prepareMessage.getData()).isEqualTo(messageData.getData()); + assertThat(prepareMessage.getCode()).isEqualTo(IbftV2.PREPARE); + } + + @Test + public void createMessageFailsWhenIncorrectMessageCode() { + when(messageData.getCode()).thenReturn(42); + assertThatThrownBy(() -> PrepareMessage.fromMessage(messageData)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("Message has code 42 and thus is not a PrepareMessage"); + } +} diff --git a/consensus/ibft/src/test/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/RoundChangeMessageTest.java b/consensus/ibft/src/test/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/RoundChangeMessageTest.java new file mode 100644 index 0000000000..71bfd6fbcb --- /dev/null +++ b/consensus/ibft/src/test/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/RoundChangeMessageTest.java @@ -0,0 +1,70 @@ +/* + * Copyright 2018 ConsenSys AG. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package tech.pegasys.pantheon.consensus.ibft.ibftmessage; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.RoundChangePayload; +import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.SignedData; +import tech.pegasys.pantheon.ethereum.p2p.api.MessageData; +import tech.pegasys.pantheon.util.bytes.BytesValue; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class RoundChangeMessageTest { + @Mock private SignedData roundChangePayload; + @Mock private BytesValue messageBytes; + @Mock private MessageData messageData; + @Mock private RoundChangeMessage roundChangeMessage; + + @Test + public void createMessageFromRoundChangeMessageData() { + when(roundChangePayload.encode()).thenReturn(messageBytes); + RoundChangeMessage roundChangeMessage = RoundChangeMessage.create(roundChangePayload); + + assertThat(roundChangeMessage.getData()).isEqualTo(messageBytes); + assertThat(roundChangeMessage.getCode()).isEqualTo(IbftV2.ROUND_CHANGE); + verify(roundChangePayload).encode(); + } + + @Test + public void createMessageFromRoundChangeMessage() { + RoundChangeMessage message = RoundChangeMessage.fromMessage(roundChangeMessage); + assertThat(message).isSameAs(roundChangeMessage); + } + + @Test + public void createMessageFromGenericMessageData() { + when(messageData.getData()).thenReturn(messageBytes); + when(messageData.getCode()).thenReturn(IbftV2.ROUND_CHANGE); + RoundChangeMessage roundChangeMessage = RoundChangeMessage.fromMessage(messageData); + + assertThat(roundChangeMessage.getData()).isEqualTo(messageData.getData()); + assertThat(roundChangeMessage.getCode()).isEqualTo(IbftV2.ROUND_CHANGE); + } + + @Test + public void createMessageFailsWhenIncorrectMessageCode() { + when(messageData.getCode()).thenReturn(42); + assertThatThrownBy(() -> RoundChangeMessage.fromMessage(messageData)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("Message has code 42 and thus is not a RoundChangeMessage"); + } +}