Skip to content

Commit

Permalink
Update peer tests and add IP address flag
Browse files Browse the repository at this point in the history
  • Loading branch information
bfbachmann committed May 6, 2017
1 parent d9c9e51 commit 9807889
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
7 changes: 4 additions & 3 deletions cumuluspeer/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ import (
)

const (
CumulusPort = 8765 // Cumulus peers communicate over this TCP port
DefaultPort = 8765 // Cumulus peers communicate over this TCP port
CumulusProtocol = "/cumulate/0.0.1" // Cumulus communication protocol
DefaultIP = "127.0.0.1" // Default Host IP address if none given
)

// A basic StreamHandler for a very basic host.
Expand Down Expand Up @@ -56,7 +57,7 @@ func doCumulate(s net.Stream) {
// Create a Cumulus host.
// This may throw an error if we fail to create a key pair, a pid, or a new
// multiaddress.
func MakeBasicHost(port int) (host.Host, pstore.Peerstore, error) {
func MakeBasicHost(ip string, port int) (host.Host, pstore.Peerstore, error) {
// Make sure we received a valid port number

// Generate a key pair for this host. We will only use the pudlic key to
Expand All @@ -73,7 +74,7 @@ func MakeBasicHost(port int) (host.Host, pstore.Peerstore, error) {
}

// Create a multiaddress (IP address and TCP port for this peer).
addr, err := ma.NewMultiaddr(fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", port))
addr, err := ma.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d", ip, port))
if err != nil {
return nil, nil, err
}
Expand Down
10 changes: 9 additions & 1 deletion cumuluspeer/peer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

// Tests if we can make a basic host on a valid TCP port
func TestMakeBasicHostValidPort(t *testing.T) {
h, ps, err := cumuluspeer.MakeBasicHost(8000)
h, ps, err := cumuluspeer.MakeBasicHost(cumuluspeer.DefaultIP, 8000)
if err != nil {
t.Fail()
}
Expand All @@ -26,5 +26,13 @@ func TestMakeBasicHostValidPort(t *testing.T) {
}
}

// Make sure MakeBasicHost fails with invalid IP
func TestMakeBasicHostInvalidIP(t *testing.T) {
_, _, err := cumuluspeer.MakeBasicHost("asdfasdf", 123)
if err == nil {
t.Fail()
}
}

// TODO Test ExtractPeerInfo
// TODO Test BasicStreamHandler
10 changes: 6 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ func main() {
// Get and parse command line arguments
// targetPeer is a Multiaddr representing the target peer to connect to
// when joining the Cumulus Network.
// port is the port to communicate over (defaults to peer.CumulusPort)
// port is the port to communicate over (defaults to peer.DefaultPort).
// ip is the public IP address of the this host.
targetPeer := flag.String("t", "", "target peer to connect to")
port := flag.Int("p", cumuluspeer.CumulusPort, "TCP port to use")
port := flag.Int("p", cumuluspeer.DefaultPort, "TCP port to use for this host")
ip := flag.String("i", cumuluspeer.DefaultIP, "IP address to use for this host")
flag.Parse()

// Set up a new host on the Cumulus network
host, ps, err := cumuluspeer.MakeBasicHost(*port)
host, ps, err := cumuluspeer.MakeBasicHost(*ip, *port)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -70,5 +72,5 @@ func main() {
log.Error(err)
}

log.Infof("Read reply: %s", string(reply))
log.Info("Read reply: ", string(reply))
}

0 comments on commit 9807889

Please sign in to comment.