sign_test.go 3.7 KB
Newer Older
1 2 3 4 5 6 7
package tests

import (
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
Loïck Bonniot's avatar
Loïck Bonniot committed
8
	"regexp"
9 10 11 12 13
	"strings"
	"testing"
	"time"

	"dfss/dfssp/contract"
14
	"github.com/stretchr/testify/assert"
15 16
)

17 18 19 20 21 22 23 24 25 26 27
// TestSignContract unroll the whole signature process.
//
// GOOD CASE
// - Start platform
// - Register client1, client2 and client3
// - Create contract `contract.txt`
// - Sign it
// - Check if all proof files are present
//
// TODO BAD CASES

28 29 30 31 32 33 34
func TestSignContract(t *testing.T) {
	// Cleanup
	eraseDatabase()

	// Start the platform
	workingDir, err := ioutil.TempDir("", "dfss_")
	assert.Equal(t, nil, err)
35
	_, _, _, stop, ca, err := startPlatform(workingDir)
36
	assert.Equal(t, nil, err)
37
	defer stop()
38 39 40 41 42

	time.Sleep(2 * time.Second)

	// Register clients
	clients := make([]*exec.Cmd, 3)
43
	client1, err := createClient(workingDir, ca, 9091)
44 45 46
	assert.Equal(t, nil, err)
	err = registerAndAuth(client1, "client1@example.com", "password", "", true, true)
	assert.Equal(t, nil, err)
47
	client2, err := createClient(workingDir, ca, 9092)
48 49 50
	assert.Equal(t, nil, err)
	err = registerAndAuth(client2, "client2@example.com", "password", "", true, true)
	assert.Equal(t, nil, err)
51
	client3, err := createClient(workingDir, ca, 9093)
52 53 54 55 56 57 58
	assert.Equal(t, nil, err)
	err = registerAndAuth(client3, "client3@example.com", "password", "", true, true)
	assert.Equal(t, nil, err)

	// Create contract
	client1 = newClient(client1)
	setLastArg(client1, "new", true)
59
	contractFilePath := filepath.Join("testdata", "contract.txt")
60 61
	client1.Stdin = strings.NewReader(
		"password\n" +
62
			contractFilePath + "\n" +
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
			"\n" +
			"client1@example.com\n" +
			"client2@example.com\n" +
			"client3@example.com\n" +
			"\n",
	)
	err = checkStderr(t, client1, "")
	assert.Equal(t, nil, err)

	// Get contract file
	contractEntity := getContract("contract.txt", 0)
	contractData, err := contract.GetJSON(contractEntity, nil)
	assert.Equal(t, nil, err)
	contractPath := filepath.Join(workingDir, "c.dfss")
	err = ioutil.WriteFile(contractPath, contractData, os.ModePerm)
	assert.Equal(t, nil, err)

80 81 82 83 84 85 86 87
	// Test with wrong file, should abort
	wrongFileClient := newClient(client1)
	setLastArg(wrongFileClient, "sign", true)
	setLastArg(wrongFileClient, contractPath, false)
	wrongFileClient.Stdin = strings.NewReader("wrongfile.txt\npassword\nyes\n")
	_, err = wrongFileClient.Output()
	assert.NotNil(t, err)

88 89 90 91
	// Sign!
	clients[0] = newClient(client1)
	clients[1] = newClient(client2)
	clients[2] = newClient(client3)
Loïck Bonniot's avatar
Loïck Bonniot committed
92 93

	closeChannel := make(chan []byte, 3)
94 95 96 97 98
	for i := 0; i < 3; i++ {
		setLastArg(clients[i], "sign", true)
		setLastArg(clients[i], contractPath, false)
		go func(c *exec.Cmd, i int) {
			time.Sleep(time.Duration(i*2) * time.Second)
99
			c.Stdin = strings.NewReader(contractFilePath + "\npassword\nyes\n")
100
			c.Stderr = os.Stderr
101 102
			output, err1 := c.Output()
			if err1 != nil {
Loïck Bonniot's avatar
Loïck Bonniot committed
103 104 105
				output = nil
			}
			closeChannel <- output
106 107 108
		}(clients[i], i)
	}

Loïck Bonniot's avatar
Loïck Bonniot committed
109 110 111 112
	regexes := []*regexp.Regexp{
		regexp.MustCompile(`Everybody is ready, starting the signature [a-f0-9]+`),
		regexp.MustCompile(`Do you REALLY want to sign contract\.txt\? Type 'yes' to confirm:`),
	}
113
	for i := 0; i < 3; i++ {
Loïck Bonniot's avatar
Loïck Bonniot committed
114 115 116
		output := <-closeChannel
		assert.NotEqual(t, nil, output, "The return error should be null")
		for _, r := range regexes {
117
			assert.True(t, r.Match(output), "Regex is not satisfied: ", r.String())
Loïck Bonniot's avatar
Loïck Bonniot committed
118
		}
119
	}
120 121 122 123 124 125 126 127 128 129

	// Ensure that all the files are present
	proofFile := regexp.MustCompile(`client[0-9]+@example.com.*\.proof`)
	files, _ := ioutil.ReadDir("./")

	matches := 0
	for _, file := range files {
		if proofFile.Match([]byte(file.Name())) {
			matches++
			err = os.Remove("./" + file.Name())
130
			assert.True(t, err == nil, "Cannot remove .proof matching file")
131 132
		}
	}
133
	assert.True(t, matches == 3, "Missing proof file ?")
134 135

	time.Sleep(time.Second)
136
}