Skip to content

Commit

Permalink
Fixes issue #45 (#53)
Browse files Browse the repository at this point in the history
* Skips TLS tests if running on windows

* Fixing missing port issue

* Update of pkg names

* reverts certs.go

* Minor corrections

* Commit before renaming folder

* Cleaning and test
  • Loading branch information
PizzaWhisperer authored and nikkolasg committed Jul 16, 2018
1 parent d0e7e01 commit 39585f2
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 2 deletions.
31 changes: 29 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"os"
"path"
"path/filepath"
"regexp"
"strconv"
"strings"

"github.com/BurntSushi/toml"
Expand All @@ -28,6 +30,7 @@ var (

const gname = "group.toml"
const dpublic = "dist_key.public"
const default_port = "8080"

func banner() {
fmt.Printf("drand v%s by nikkolasg @ DEDIS\n", version)
Expand Down Expand Up @@ -192,13 +195,20 @@ func keygenCmd(c *cli.Context) error {
if !args.Present() {
slog.Fatal("Missing drand address in argument (IPv4, dns)")
}
addr := args.First()
var validID = regexp.MustCompile(`[:][0-9]+$`)
slog.Print("Testing port")
if !validID.MatchString(addr) {
slog.Print("port not ok")
addr = addr + ":" + askPort()
}
var priv *key.Pair
if c.Bool("insecure") {
slog.Info("Generating private / public key pair in INSECURE mode (no TLS).")
priv = key.NewKeyPair(args.First())
priv = key.NewKeyPair(addr)
} else {
slog.Info("Generating private / public key pair with TLS indication")
priv = key.NewTLSKeyPair(args.First())
priv = key.NewTLSKeyPair(addr)
}

config := contextToConfig(c)
Expand Down Expand Up @@ -451,3 +461,20 @@ func getGroup(c *cli.Context) *key.Group {
slog.Infof("group file loaded with %d participants", g.Len())
return g
}

func askPort() string {
slog.Print("asking for port")
for {
var port string
slog.Print("No port given. Please, choose a port number (or ENTER for default port 8080): ")
fmt.Scanf("%s\n", &port)
if port == "" {
return default_port
}
_, err := strconv.Atoi(port)
if len(port) > 2 && len(port) < 5 && err == nil {
return port
}
return askPort()
}
}
64 changes: 64 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"os/exec"
"path"
"runtime"
"strconv"
"testing"

Expand All @@ -31,6 +32,64 @@ func TestKeyGen(t *testing.T) {
require.NotNil(t, priv.Public)
}

func TestKeyGenWithoutPortNumberUsersPort(t *testing.T) {
tmp := path.Join(os.TempDir(), "drand")
defer os.RemoveAll(tmp)

installCmd := exec.Command("go", "install")
_, err := installCmd.Output()
require.NoError(t, err)

// address without port number
//os.Args = []string{"drand", "--config", tmp, "keygen", "127.0.0.1"}
cmd := exec.Command("drand", "--config", tmp, "keygen", "127.0.0.1")
in, _ := cmd.StdinPipe()
in.Write([]byte("1234\n"))
in.Close()
out, err := cmd.CombinedOutput()
fmt.Println(string(out))
require.NoError(t, err)
}

func TestKeyGenWithoutPortNumberDefaultPort(t *testing.T) {
tmp := path.Join(os.TempDir(), "drand")
defer os.RemoveAll(tmp)

installCmd := exec.Command("go", "install")
_, err := installCmd.Output()
require.NoError(t, err)

// address without port number
//os.Args = []string{"drand", "--config", tmp, "keygen", "127.0.0.1"}
cmd := exec.Command("drand", "--config", tmp, "keygen", "127.0.0.1")
in, _ := cmd.StdinPipe()
in.Write([]byte("\n"))
in.Close()
out, err := cmd.CombinedOutput()
fmt.Println(string(out))
require.NoError(t, err)
}

func TestKeyGenBadPortNumber(t *testing.T) {
tmp := path.Join(os.TempDir(), "drand")
defer os.RemoveAll(tmp)

installCmd := exec.Command("go", "install")
_, err := installCmd.Output()
require.NoError(t, err)

// address without port number
//os.Args = []string{"drand", "--config", tmp, "keygen", "127.0.0.1"}
cmd := exec.Command("drand", "--config", tmp, "keygen", "127.0.0.1")
in, _ := cmd.StdinPipe()
in.Write([]byte("0"))
in.Write([]byte("\n"))
in.Close()
out, err := cmd.CombinedOutput()
fmt.Println(string(out))
require.NoError(t, err)
}

// https://stackoverflow.com/questions/26225513/how-to-test-os-exit-scenarios-in-go
func TestKeyGenInvalid(t *testing.T) {
tmp := path.Join(os.TempDir(), "drand")
Expand Down Expand Up @@ -89,6 +148,10 @@ func TestGroupGen(t *testing.T) {
}

func TestClientTLS(t *testing.T) {
if runtime.GOOS == "windows" {
fmt.Println("Skipping TestClientTLS as operating on Windows")
t.Skip("crypto/x509: system root pool is not available on Windows")
}
tmpPath := path.Join(os.TempDir(), "drand")
os.Mkdir(tmpPath, 0777)
defer os.RemoveAll(tmpPath)
Expand Down Expand Up @@ -132,4 +195,5 @@ func TestClientTLS(t *testing.T) {
out, err := cmd.CombinedOutput()
fmt.Println(string(out))
require.NoError(t, err)

}
7 changes: 7 additions & 0 deletions net/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package net

import (
"context"
"fmt"
"net"
"os"
"path"
run "runtime"
"testing"
"time"

Expand Down Expand Up @@ -50,6 +52,7 @@ func TestListener(t *testing.T) {
peer1 := &testPeer{addr1, false}
//addr2 := "127.0.0.1:4001"
service1 := &testService{42}

lis1 := NewTCPGrpcListener(addr1, service1)
go lis1.Start()
defer lis1.Stop()
Expand All @@ -70,6 +73,10 @@ func TestListener(t *testing.T) {

// ref https://bbengfort.github.io/programmer/2017/03/03/secure-grpc.html
func TestListenerTLS(t *testing.T) {
if run.GOOS == "windows" {
fmt.Println("Skipping TestClientTLS as operating on Windows")
t.Skip("crypto/x509: system root pool is not available on Windows")
}
addr1 := "127.0.0.1:4000"
peer1 := &testPeer{addr1, true}

Expand Down

0 comments on commit 39585f2

Please sign in to comment.