starters_test.go 3.67 KB
Newer Older
1 2 3
package tests

import (
4
	"bytes"
5 6 7 8 9
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
	"strconv"
10
	"testing"
11
	"time"
12 13

	"github.com/bmizerany/assert"
14 15 16 17 18 19
)

const testPort = "9090"

var currentClient = 0

20
// startPlatform creates root certificate for the platform and the TTP, and starts both modules
21
func startPlatform(tmpDir string) (platform, ttp, demo *exec.Cmd, stop func(), ca []byte, err error) {
22
	path := filepath.Join(os.Getenv("GOPATH"), "bin", "dfssp")
23
	ttpPath := filepath.Join(os.Getenv("GOPATH"), "bin", "dfsst")
24
	demoPath := filepath.Join(os.Getenv("GOPATH"), "bin", "dfssd")
25 26 27 28

	// Create temporary directory for platform
	dir, err := ioutil.TempDir(tmpDir, "p_")
	if err != nil {
29
		return
30 31 32
	}

	// Init
33
	cmd := exec.Command(path, "-path", dir, "-v", "init")
34 35
	err = cmd.Run()
	if err != nil {
36 37 38 39 40 41 42 43
		return
	}

	// Create TTP working directory
	cmd = exec.Command(path, "-path", dir, "-v", "-cn", "ttp", "ttp")
	err = cmd.Run()
	if err != nil {
		return
44 45 46
	}

	// Get root certificate
47
	ca, err = ioutil.ReadFile(filepath.Join(dir, "dfssp_rootCA.pem"))
48
	if err != nil {
49
		return
50 51
	}

52 53 54 55 56 57 58 59 60 61 62 63 64
	// Start platform
	platform = exec.Command(path, "-db", dbURI, "-path", dir, "-p", testPort, "-v", "start")
	platform.Stdout = os.Stdout
	platform.Stderr = os.Stderr
	err = platform.Start()

	// Start TTP
	ttp = exec.Command(ttpPath, "-db", dbURI, "-p", "9098", "start")
	ttp.Dir = filepath.Join(dir, "ttp")
	ttp.Stdout = os.Stdout
	ttp.Stderr = os.Stderr
	_ = ioutil.WriteFile(filepath.Join(ttp.Dir, "ca.pem"), ca, 0600)
	err = ttp.Start()
65

66
	// Start demonstrator
67
	demo = exec.Command(demoPath, "-p", "9099", "nogui")
68 69 70 71 72 73 74 75 76 77 78 79
	demo.Stdout = os.Stdout
	demo.Stderr = os.Stderr
	err = demo.Start()

	stop = func() {
		_ = platform.Process.Kill()
		_ = ttp.Process.Kill()
		_ = os.RemoveAll(tmpDir)
		time.Sleep(2 * time.Second)
		_ = demo.Process.Kill()
	}

80
	return
81 82
}

83
// createClient creates a new working directory for a client, creating ca.pem.
84
// It returns a ready-to-run command, but you probably want to set the last argument of the command.
Loïck Bonniot's avatar
Loïck Bonniot committed
85
func createClient(tmpDir string, ca []byte, port int) (*exec.Cmd, error) {
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
	path := filepath.Join(os.Getenv("GOPATH"), "bin", "dfssc")

	// Create temporary directory for client
	clientID := strconv.Itoa(currentClient)
	currentClient++
	dir, err := ioutil.TempDir(tmpDir, "c"+clientID+"_")
	if err != nil {
		return nil, err
	}

	caPath := filepath.Join(dir, "ca.pem")
	certPath := filepath.Join(dir, "cert.pem")
	keyPath := filepath.Join(dir, "key.pem")

	// Write the CA
	err = ioutil.WriteFile(caPath, ca, os.ModePerm)
	if err != nil {
		return nil, err
	}

	// Prepare the client command.
	// The last argument is up to you!
108
	cmd := exec.Command(path, "-ca", caPath, "-cert", certPath, "-host", "127.0.0.1:"+testPort, "-key", keyPath, "-port", strconv.Itoa(port), "-v", "-d", "localhost:9099")
109

110 111
	return cmd, nil
}
112

113 114
// newClient clones the current command to another one.
// It's very useful when doing several commands on the same client.
115 116 117 118 119 120 121 122
func newClient(old *exec.Cmd) *exec.Cmd {
	cmd := exec.Command(old.Path)
	cmd.Args = old.Args
	cmd.Stdout = old.Stdout
	cmd.Stderr = old.Stderr
	return cmd
}

123
// setLastArg sets or updates the last argument of a command.
124 125 126 127 128 129
func setLastArg(cmd *exec.Cmd, str string, override bool) {
	if override {
		cmd.Args = cmd.Args[:(len(cmd.Args) - 1)]
	}
	cmd.Args = append(cmd.Args, str)
}
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145

// checkStderr runs the provided command and compares the stderr output with the given one.
// It returns the value of cmd.Wait()
func checkStderr(t *testing.T, cmd *exec.Cmd, value string) error {
	cmd.Stderr = nil
	stderr, _ := cmd.StderrPipe()
	err := cmd.Start()
	assert.Equal(t, nil, err)

	buf := new(bytes.Buffer)
	_, _ = buf.ReadFrom(stderr)
	s := buf.String()
	assert.Equal(t, value, s)

	return cmd.Wait()
}