Polish changes made in pull request #189

- Organize imports using shared IDEA configuration

 - Format sources, including:
   - Normalize 4-space indentation
   - Wrap at 120 char right margin
   - Remove spaces around method arguments
This commit is contained in:
Chris Beams 2015-01-06 09:50:58 +01:00
parent 02e7c28ffd
commit f34d1f49dc
No known key found for this signature in database
GPG Key ID: 3D214F8F5BC5ED73
6 changed files with 101 additions and 118 deletions

View File

@ -1,11 +1,5 @@
package test.ethereum.net; package test.ethereum.net;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import java.lang.System;
import org.ethereum.net.message.ReasonCode; import org.ethereum.net.message.ReasonCode;
import org.ethereum.net.p2p.DisconnectMessage; import org.ethereum.net.p2p.DisconnectMessage;
@ -13,7 +7,7 @@ import org.junit.Test;
import org.spongycastle.util.encoders.Hex; import org.spongycastle.util.encoders.Hex;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.*;
public class DisconnectMessageTest { public class DisconnectMessageTest {
@ -85,12 +79,12 @@ public class DisconnectMessageTest {
String disconnectMessageRaw = "C19999"; String disconnectMessageRaw = "C19999";
byte[] payload = Hex.decode(disconnectMessageRaw); byte[] payload = Hex.decode(disconnectMessageRaw);
try{ try {
DisconnectMessage disconnectMessage = new DisconnectMessage(payload); DisconnectMessage disconnectMessage = new DisconnectMessage(payload);
disconnectMessage.toString(); //throws exception disconnectMessage.toString(); //throws exception
assertTrue("Valid raw encoding for disconnectMessage", false); assertTrue("Valid raw encoding for disconnectMessage", false);
} catch (RuntimeException e) { } catch (RuntimeException e) {
assertTrue("Invalid raw encoding for disconnectMessage", true); assertTrue("Invalid raw encoding for disconnectMessage", true);
} }
} }
@ -100,12 +94,12 @@ public class DisconnectMessageTest {
String disconnectMessageRaw = "C28081"; String disconnectMessageRaw = "C28081";
byte[] payload = Hex.decode(disconnectMessageRaw); byte[] payload = Hex.decode(disconnectMessageRaw);
try{ try {
DisconnectMessage disconnectMessage = new DisconnectMessage(payload); DisconnectMessage disconnectMessage = new DisconnectMessage(payload);
disconnectMessage.toString(); //throws exception disconnectMessage.toString(); //throws exception
assertTrue("Valid raw encoding for disconnectMessage", false); assertTrue("Valid raw encoding for disconnectMessage", false);
} catch (RuntimeException e) { } catch (RuntimeException e) {
assertTrue("Invalid raw encoding for disconnectMessage", true); assertTrue("Invalid raw encoding for disconnectMessage", true);
} }
} }
} }

View File

@ -1,11 +1,13 @@
package test.ethereum.net; package test.ethereum.net;
import static org.junit.Assert.assertEquals; import org.ethereum.net.p2p.GetPeersMessage;
import org.ethereum.net.p2p.P2pMessageCodes;
import org.junit.Test;
import org.spongycastle.util.encoders.Hex; import org.spongycastle.util.encoders.Hex;
import org.ethereum.net.p2p.P2pMessageCodes;
import org.ethereum.net.p2p.GetPeersMessage; import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class GetPeersMessageTest { public class GetPeersMessageTest {
@ -13,24 +15,21 @@ public class GetPeersMessageTest {
@Test @Test
public void testGetPeers() { public void testGetPeers() {
//Init //Init
GetPeersMessage getPeersMessage = new GetPeersMessage(); GetPeersMessage getPeersMessage = new GetPeersMessage();
//System.out.println(getPeersMessage.getEncoded());
//toString //toString
assertEquals("[GET_PEERS]", getPeersMessage.toString()); assertEquals("[GET_PEERS]", getPeersMessage.toString());
//getEncoded //getEncoded
assertEquals("C104", Hex.toHexString( getPeersMessage.getEncoded() ).toUpperCase() ); assertEquals("C104", Hex.toHexString(getPeersMessage.getEncoded()).toUpperCase());
//getAnswerMessage //getAnswerMessage
assertEquals(null, getPeersMessage.getAnswerMessage()); assertEquals(null, getPeersMessage.getAnswerMessage());
//getCommand //getCommand
assertEquals(P2pMessageCodes.GET_PEERS, getPeersMessage.getCommand()); assertEquals(P2pMessageCodes.GET_PEERS, getPeersMessage.getCommand());
} }
} }

View File

@ -1,17 +1,12 @@
package test.ethereum.net; package test.ethereum.net;
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
import org.ethereum.net.client.Capability; import org.ethereum.net.client.Capability;
import org.ethereum.net.eth.EthHandler; import org.ethereum.net.eth.EthHandler;
import org.ethereum.net.p2p.HelloMessage; import org.ethereum.net.p2p.HelloMessage;
import org.ethereum.net.p2p.P2pMessageCodes;
import org.ethereum.net.p2p.P2pHandler; import org.ethereum.net.p2p.P2pHandler;
import org.ethereum.net.p2p.P2pMessageCodes;
import org.ethereum.net.shh.ShhHandler; import org.ethereum.net.shh.ShhHandler;
import org.junit.Test; import org.junit.Test;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -19,6 +14,9 @@ import org.slf4j.LoggerFactory;
import org.spongycastle.util.encoders.Hex; import org.spongycastle.util.encoders.Hex;
import java.util.Arrays;
import java.util.List;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
public class HelloMessageTest { public class HelloMessageTest {
@ -41,12 +39,10 @@ public class HelloMessageTest {
assertEquals(2, helloMessage.getCapabilities().size()); assertEquals(2, helloMessage.getCapabilities().size());
assertEquals(992, helloMessage.getListenPort()); assertEquals(992, helloMessage.getListenPort());
assertEquals( assertEquals(
"1fbf1e41f08078918c9f7b6734594ee56d7f538614f602c71194db0a1af5a77f9b86eb14669fe7a8a46a2dd1b7d070b94e463f4ecd5b337c8b4d31bbf8dd5646", "1fbf1e41f08078918c9f7b6734594ee56d7f538614f602c71194db0a1af5a77f9b86eb14669fe7a8a46a2dd1b7d070b94e463f4ecd5b337c8b4d31bbf8dd5646",
helloMessage.getPeerId()); helloMessage.getPeerId());
} }
//Instantiate from constructor //Instantiate from constructor
@Test @Test
public void test2() { public void test2() {
@ -54,9 +50,9 @@ public class HelloMessageTest {
byte version = 2; byte version = 2;
String clientStr = "Ethereum(++)/v0.7.9/Release/Linux/g++"; String clientStr = "Ethereum(++)/v0.7.9/Release/Linux/g++";
List<Capability> capabilities = Arrays.asList( List<Capability> capabilities = Arrays.asList(
new Capability( Capability.ETH, EthHandler.VERSION), new Capability(Capability.ETH, EthHandler.VERSION),
new Capability( Capability.SHH, ShhHandler.VERSION), new Capability(Capability.SHH, ShhHandler.VERSION),
new Capability( Capability.P2P, P2pHandler.VERSION) ); new Capability(Capability.P2P, P2pHandler.VERSION));
int listenPort = 992; int listenPort = 992;
String peerId = "1fbf1e41f08078918c9f7b6734594ee56d7f538614f602c71194db0a1af5a"; String peerId = "1fbf1e41f08078918c9f7b6734594ee56d7f538614f602c71194db0a1af5a";
@ -66,10 +62,10 @@ public class HelloMessageTest {
assertEquals(P2pMessageCodes.HELLO, helloMessage.getCommand()); assertEquals(P2pMessageCodes.HELLO, helloMessage.getCommand());
assertEquals(version, helloMessage.getP2PVersion()); assertEquals(version, helloMessage.getP2PVersion());
assertEquals(clientStr, helloMessage.getClientId()); assertEquals(clientStr, helloMessage.getClientId());
assertEquals( 3 , helloMessage.getCapabilities().size()); assertEquals(3, helloMessage.getCapabilities().size());
assertEquals( listenPort , helloMessage.getListenPort()); assertEquals(listenPort, helloMessage.getListenPort());
assertEquals( peerId , helloMessage.getPeerId()); assertEquals(peerId, helloMessage.getPeerId());
//TODO tostring? //TODO tostring?
} }
@ -80,21 +76,20 @@ public class HelloMessageTest {
byte version = -1; //invalid version byte version = -1; //invalid version
String clientStr = ""; //null id String clientStr = ""; //null id
List<Capability> capabilities = Arrays.asList( List<Capability> capabilities = Arrays.asList(
new Capability( null, (byte) 0 ), new Capability(null, (byte) 0),
new Capability( null, (byte) 0 ), new Capability(null, (byte) 0),
null, //null here causes NullPointerException when using toString null, //null here causes NullPointerException when using toString
new Capability( null, (byte) 0 ) ); //encoding null capabilities new Capability(null, (byte) 0)); //encoding null capabilities
int listenPort = 99999; //invalid port int listenPort = 99999; //invalid port
String peerId = ""; //null id String peerId = ""; //null id
HelloMessage helloMessage = new HelloMessage(version, clientStr, capabilities, listenPort, peerId); HelloMessage helloMessage = new HelloMessage(version, clientStr, capabilities, listenPort, peerId);
//logger.info(helloMessage.toString());
assertEquals(P2pMessageCodes.HELLO, helloMessage.getCommand()); assertEquals(P2pMessageCodes.HELLO, helloMessage.getCommand());
assertEquals(version, helloMessage.getP2PVersion()); assertEquals(version, helloMessage.getP2PVersion());
assertEquals(clientStr, helloMessage.getClientId()); assertEquals(clientStr, helloMessage.getClientId());
assertEquals( 4 , helloMessage.getCapabilities().size()); assertEquals(4, helloMessage.getCapabilities().size());
assertEquals( listenPort , helloMessage.getListenPort()); assertEquals(listenPort, helloMessage.getListenPort());
assertEquals( peerId , helloMessage.getPeerId()); assertEquals(peerId, helloMessage.getPeerId());
} }
} }

View File

@ -1,54 +1,57 @@
package test.ethereum.net; package test.ethereum.net;
import static org.junit.Assert.assertEquals;
import org.ethereum.net.client.Capability; import org.ethereum.net.client.Capability;
import org.ethereum.net.p2p.Peer; import org.ethereum.net.p2p.Peer;
import org.spongycastle.util.encoders.Hex;
import java.net.InetAddress;
import java.util.List;
import java.util.ArrayList;
import org.junit.Test; import org.junit.Test;
import org.spongycastle.util.encoders.Hex;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.assertEquals;
public class PeerTest { public class PeerTest {
/* PEER */ /* PEER */
@Test @Test
public void testPeer() { public void testPeer() {
//Init //Init
InetAddress address = InetAddress.getLoopbackAddress(); InetAddress address = InetAddress.getLoopbackAddress();
List<Capability> capabilities = new ArrayList<>(); List<Capability> capabilities = new ArrayList<>();
int port = 1010; int port = 1010;
String peerId = "1010"; String peerId = "1010";
Peer peerCopy = new Peer(address, port, peerId ); Peer peerCopy = new Peer(address, port, peerId);
//Peer //Peer
Peer peer = new Peer(address, port, peerId ); Peer peer = new Peer(address, port, peerId);
//getAddress //getAddress
assertEquals( "127.0.0.1" , peer.getAddress().getHostAddress() ); assertEquals("127.0.0.1", peer.getAddress().getHostAddress());
//getPort //getPort
assertEquals( port , peer.getPort() ); assertEquals(port, peer.getPort());
//getPeerId //getPeerId
assertEquals( peerId , peer.getPeerId() ); assertEquals(peerId, peer.getPeerId());
//getCapabilities //getCapabilities
assertEquals( capabilities , peer.getCapabilities() ); assertEquals(capabilities, peer.getCapabilities());
//getEncoded //getEncoded
assertEquals("CC847F0000018203F2821010C0", Hex.toHexString( peer.getEncoded() ).toUpperCase() ); assertEquals("CC847F0000018203F2821010C0", Hex.toHexString(peer.getEncoded()).toUpperCase());
//toString //toString
assertEquals("[ip=" + address.getHostAddress() + " port=" + Integer.toString( port ) + " peerId=" + peerId + "]", peer.toString() ); assertEquals("[ip=" + address.getHostAddress() + " port=" + Integer.toString(port) + " peerId=" + peerId + "]", peer.toString());
//equals //equals
assertEquals(true, peer.equals( peerCopy ) ); assertEquals(true, peer.equals(peerCopy));
assertEquals(false, peer.equals( null ) ); assertEquals(false, peer.equals(null));
//hashCode //hashCode
assertEquals(-1, peer.hashCode()); assertEquals(-1, peer.hashCode());

View File

@ -1,17 +1,5 @@
package test.ethereum.net; package test.ethereum.net;
import static org.junit.Assert.assertEquals;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.ethereum.net.client.Capability; import org.ethereum.net.client.Capability;
import org.ethereum.net.p2p.GetPeersMessage; import org.ethereum.net.p2p.GetPeersMessage;
import org.ethereum.net.p2p.P2pMessageCodes; import org.ethereum.net.p2p.P2pMessageCodes;
@ -25,7 +13,14 @@ import org.slf4j.LoggerFactory;
import org.spongycastle.util.encoders.Hex; import org.spongycastle.util.encoders.Hex;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.List;
import java.util.Set;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
@ -66,20 +61,19 @@ public class PeersMessageTest {
peer.getPeerId()); peer.getPeerId());
} }
@Test /* PeersMessage 1 from constructor */ @Test /* PeersMessage 1 from constructor */
public void testPeers_2() { public void testPeers_2() {
//Init //Init
InetAddress address = InetAddress.getLoopbackAddress(); InetAddress address = InetAddress.getLoopbackAddress();
List<Capability> capabilities = new ArrayList<>(); List<Capability> capabilities = new ArrayList<>();
int port = 112; int port = 112;
String peerId = "36659c3656c488437cceb11abeb9b9fc69b8055144a7e7db3584d03e606083f90e" + String peerId = "36659c3656c488437cceb11abeb9b9fc69b8055144a7e7db3584d03e606083f90e" +
"17a1d3021d674579407cdaaafdfeef485872ab719db9f2b6283f498bb90a71"; "17a1d3021d674579407cdaaafdfeef485872ab719db9f2b6283f498bb90a71";
Set<Peer> peers = new HashSet<>(); Set<Peer> peers = new HashSet<>();
peers.add(new Peer(address, port, peerId )); peers.add(new Peer(address, port, peerId));
PeersMessage peersMessage= new PeersMessage(peers); PeersMessage peersMessage = new PeersMessage(peers);
logger.info(peersMessage.toString()); logger.info(peersMessage.toString());
assertEquals(1, peersMessage.getPeers().size()); assertEquals(1, peersMessage.getPeers().size());
@ -91,7 +85,7 @@ public class PeersMessageTest {
assertEquals("127.0.0.1", peer.getAddress().getHostAddress()); assertEquals("127.0.0.1", peer.getAddress().getHostAddress());
assertEquals(112, peer.getPort()); assertEquals(112, peer.getPort());
assertEquals("36659c3656c488437cceb11abeb9b9fc69b8055144a7e7db3584d03e6" + assertEquals("36659c3656c488437cceb11abeb9b9fc69b8055144a7e7db3584d03e6" +
"06083f90e17a1d3021d674579407cdaaafdfeef485872ab719db9f2b6283f498bb90a71", peer.getPeerId()); "06083f90e17a1d3021d674579407cdaaafdfeef485872ab719db9f2b6283f498bb90a71", peer.getPeerId());
} }
@Test /* failing test */ @Test /* failing test */
@ -99,16 +93,16 @@ public class PeersMessageTest {
//Init //Init
InetAddress address = InetAddress.getLoopbackAddress(); InetAddress address = InetAddress.getLoopbackAddress();
List<Capability> capabilities = Arrays.asList( List<Capability> capabilities = Arrays.asList(
new Capability( null, (byte) 0 ), new Capability(null, (byte) 0),
null //null here can cause NullPointerException when using toString null //null here can cause NullPointerException when using toString
); //encoding null capabilities ); //encoding null capabilities
int port = -1; //invalid port int port = -1; //invalid port
String peerId = ""; //invalid peerid String peerId = ""; //invalid peerid
Set<Peer> peers = new HashSet<>(); Set<Peer> peers = new HashSet<>();
peers.add(new Peer(address, port, peerId )); peers.add(new Peer(address, port, peerId));
PeersMessage peersMessage= new PeersMessage(peers); PeersMessage peersMessage = new PeersMessage(peers);
logger.info(peersMessage.toString()); logger.info(peersMessage.toString());
assertEquals(1, peersMessage.getPeers().size()); assertEquals(1, peersMessage.getPeers().size());
@ -119,6 +113,6 @@ public class PeersMessageTest {
assertEquals(P2pMessageCodes.PEERS, peersMessage.getCommand()); assertEquals(P2pMessageCodes.PEERS, peersMessage.getCommand());
assertEquals("127.0.0.1", peer.getAddress().getHostAddress()); assertEquals("127.0.0.1", peer.getAddress().getHostAddress());
assertEquals(-1, peer.getPort()); assertEquals(-1, peer.getPort());
assertEquals( "" , peer.getPeerId()); assertEquals("", peer.getPeerId());
} }
} }

View File

@ -2,8 +2,6 @@ package test.ethereum.net;
import org.ethereum.net.eth.StatusMessage; import org.ethereum.net.eth.StatusMessage;
import java.math.BigInteger;
import org.junit.Test; import org.junit.Test;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -11,6 +9,8 @@ import org.slf4j.LoggerFactory;
import org.spongycastle.util.encoders.Hex; import org.spongycastle.util.encoders.Hex;
import java.math.BigInteger;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
public class StatusMessageTest { public class StatusMessageTest {
@ -35,21 +35,22 @@ public class StatusMessageTest {
} }
@Test //from constructor @Test //from constructor
public void test2(){ public void test2() {
//Init //Init
byte version = 39; byte version = 39;
byte netId = 0; byte netId = 0;
byte[] difficulty = new BigInteger("25c60144", 16).toByteArray(); byte[] difficulty = new BigInteger("25c60144", 16).toByteArray();
byte[] bestHash = new BigInteger("832056d3c93ff2739ace7199952e5365aa29f18805be05634c4db125c5340216",16).toByteArray(); byte[] bestHash =
byte[] genesisHash = new BigInteger("955f36d073ccb026b78ab3424c15cf966a7563aa270413859f78702b9e8e22cb",16).toByteArray(); new BigInteger("832056d3c93ff2739ace7199952e5365aa29f18805be05634c4db125c5340216", 16).toByteArray();
byte[] genesisHash =
new BigInteger("955f36d073ccb026b78ab3424c15cf966a7563aa270413859f78702b9e8e22cb", 16).toByteArray();
StatusMessage statusMessage = new StatusMessage(version, netId, difficulty, bestHash, genesisHash); StatusMessage statusMessage = new StatusMessage(version, netId, difficulty, bestHash, genesisHash);
logger.info(statusMessage.toString()); logger.info(statusMessage.toString());
assertEquals(39, statusMessage.getProtocolVersion()); assertEquals(39, statusMessage.getProtocolVersion());
assertEquals("25c60144", assertEquals("25c60144", Hex.toHexString(statusMessage.getTotalDifficulty()));
Hex.toHexString(statusMessage.getTotalDifficulty()));
assertEquals("00832056d3c93ff2739ace7199952e5365aa29f18805be05634c4db125c5340216", assertEquals("00832056d3c93ff2739ace7199952e5365aa29f18805be05634c4db125c5340216",
Hex.toHexString(statusMessage.getBestHash())); Hex.toHexString(statusMessage.getBestHash()));
assertEquals("00955f36d073ccb026b78ab3424c15cf966a7563aa270413859f78702b9e8e22cb", assertEquals("00955f36d073ccb026b78ab3424c15cf966a7563aa270413859f78702b9e8e22cb",
@ -57,25 +58,22 @@ public class StatusMessageTest {
} }
@Test //fail test @Test //fail test
public void test3(){ public void test3() {
//Init //Init
byte version = -1; //invalid version byte version = -1; //invalid version
byte netId = -1; //invalid netid byte netId = -1; //invalid netid
byte[] difficulty = new BigInteger("-1000000", 16).toByteArray(); //negative difficulty byte[] difficulty = new BigInteger("-1000000", 16).toByteArray(); //negative difficulty
byte[] bestHash = new BigInteger("-100000000000000000000000000",16).toByteArray(); //invalid hash byte[] bestHash = new BigInteger("-100000000000000000000000000", 16).toByteArray(); //invalid hash
byte[] genesisHash = new BigInteger("-1000000000000000000000000000000",16).toByteArray(); //invalid hash byte[] genesisHash = new BigInteger("-1000000000000000000000000000000", 16).toByteArray(); //invalid hash
StatusMessage statusMessage = new StatusMessage(version, netId, difficulty, bestHash, genesisHash); StatusMessage statusMessage = new StatusMessage(version, netId, difficulty, bestHash, genesisHash);
logger.info(statusMessage.toString()); logger.info(statusMessage.toString());
assertEquals(-1, statusMessage.getProtocolVersion()); assertEquals(-1, statusMessage.getProtocolVersion());
assertEquals("ff000000", assertEquals("ff000000", Hex.toHexString(statusMessage.getTotalDifficulty()));
Hex.toHexString(statusMessage.getTotalDifficulty())); assertEquals("ff00000000000000000000000000", Hex.toHexString(statusMessage.getBestHash()));
assertEquals("ff00000000000000000000000000", assertEquals("ff000000000000000000000000000000", Hex.toHexString(statusMessage.getGenesisHash()));
Hex.toHexString(statusMessage.getBestHash()));
assertEquals("ff000000000000000000000000000000",
Hex.toHexString(statusMessage.getGenesisHash()));
} }
} }