impexp_test.go 4.71 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
package tests

import (
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
	"testing"

Caro Axel's avatar
Caro Axel committed
11
	"dfss/dfssc/common"
12
	"github.com/stretchr/testify/assert"
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
)

// TestExport tries to export the certificate and pricate key of the client
//
// CLASSIC SCENARIO
// 1. Export is node successfully
//
// BAD CASES
// 1. Wrong passphrase for unlocking private key
// 2. Missing certificate for the client
func TestExport(t *testing.T) {
	// Initialize the directory and files
	workingDir, err := ioutil.TempDir("", "dfss_")
	assert.Equal(t, nil, err)

	certPath := filepath.Join(workingDir, "cert.pem")
	certFixture, _ := common.ReadFile(filepath.Join(os.Getenv("GOPATH"), "src", "dfss", "tests", "testdata", "cert.pem"))
	err = common.SaveToDisk(certFixture, certPath)
	assert.Equal(t, nil, err)

	keyPath := filepath.Join(workingDir, "key.pem")
	keyFixture, _ := common.ReadFile(filepath.Join(os.Getenv("GOPATH"), "src", "dfss", "tests", "testdata", "key.pem"))
	err = common.SaveToDisk(keyFixture, keyPath)

	confPath := filepath.Join(workingDir, "dfssc.conf")
	common.DeleteQuietly(confPath)
	assert.Equal(t, nil, err)
	path := filepath.Join(os.Getenv("GOPATH"), "bin", "dfssc")

	// Basic command
ElyKar's avatar
ElyKar committed
43
	cmd := exec.Command(path, "--cert", certPath, "--key", keyPath, "export", confPath)
44 45 46 47 48 49 50 51

	// Export the configuration
	cmd.Stdin = strings.NewReader(
		"pass\n" +
			"password\n",
	)
	err = cmd.Run()
	assert.Equal(t, nil, err)
52
	assert.True(t, common.FileExists(confPath))
53 54

	// Bad case 1 : Wrong passphrase for private key
ElyKar's avatar
ElyKar committed
55
	badCmd1 := exec.Command(path, "--cert", certPath, "--key", keyPath, "export", confPath)
56 57 58 59 60 61
	common.DeleteQuietly(confPath)
	badCmd1.Stdin = strings.NewReader(
		"passphrase\n" +
			"wrong passphrase\n",
	)
	_ = badCmd1.Run()
62 63
	// assert.Equal(t, nil, err, "%x", err)
	assert.True(t, !common.FileExists(confPath))
64 65

	// Bad case 2 : Missing certificate
ElyKar's avatar
ElyKar committed
66
	badCmd2 := exec.Command(path, "--cert", certPath, "--key", keyPath, "export", confPath)
67 68 69 70 71 72 73
	common.DeleteQuietly(certPath)
	badCmd2.Stdin = strings.NewReader(
		"passphrase\n" +
			"password\n",
	)
	_ = badCmd2.Run()
	// assert.Equal(t, nil, err)
74
	assert.True(t, !common.FileExists(confPath))
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
}

// TestImport tries to import the certificate and private key of a user
//
// CLASSIC SCENARIO
// Import is done without problem
//
// BAD CASES
// 1. There is already a private key
// 2. Wrong passphrase for the configuration
// 3. Wrong passphrase for the private key
func TestImport(t *testing.T) {
	// Initialize the directory and files
	workingDir, err := ioutil.TempDir("", "dfss_")
	assert.Equal(t, nil, err)

	certPath := filepath.Join(workingDir, "cert.pem")
	certFixture, _ := common.ReadFile(filepath.Join(os.Getenv("GOPATH"), "src", "dfss", "tests", "testdata", "cert.pem"))
	err = common.SaveToDisk(certFixture, certPath)
	assert.Equal(t, nil, err)

	keyPath := filepath.Join(workingDir, "key.pem")
	keyFixture, _ := common.ReadFile(filepath.Join(os.Getenv("GOPATH"), "src", "dfss", "tests", "testdata", "key.pem"))
	err = common.SaveToDisk(keyFixture, keyPath)
	assert.Equal(t, nil, err)

	confPath := filepath.Join(workingDir, "dfssc.conf")
	common.DeleteQuietly(confPath)
	assert.Equal(t, nil, err)
	path := filepath.Join(os.Getenv("GOPATH"), "bin", "dfssc")

	// Create the config file
ElyKar's avatar
ElyKar committed
107
	cmd := exec.Command(path, "--cert", certPath, "--key", keyPath, "export", confPath)
108 109 110 111 112 113 114

	cmd.Stdin = strings.NewReader(
		"pass\n" +
			"password\n",
	)
	err = cmd.Run()
	assert.Equal(t, nil, err)
115
	assert.True(t, common.FileExists(confPath))
116 117 118 119 120 121 122 123 124 125 126 127

	// Nominal case
	common.DeleteQuietly(certPath)
	common.DeleteQuietly(keyPath)

	cmd = exec.Command(path, "import", confPath)
	cmd.Stdin = strings.NewReader(
		"pass\n" +
			"password\n",
	)
	err = cmd.Run()
	assert.Equal(t, nil, err)
128 129
	assert.True(t, common.FileExists(certPath))
	assert.True(t, common.FileExists(keyPath))
130 131 132 133 134 135 136 137 138 139

	// Bad case 1 : There is already the key file
	common.DeleteQuietly(certPath)

	badCmd1 := exec.Command(path, "import", confPath)
	badCmd1.Stdin = strings.NewReader(
		"pass\n" +
			"password\n",
	)
	_ = badCmd1.Run()
140 141
	// assert.Equal(t, nil, err, "%x", err)
	assert.True(t, !common.FileExists(certPath))
142 143 144 145 146 147 148 149 150 151 152

	// Bad case 2 : Wrong passphrase of the configuration
	common.DeleteQuietly(keyPath)

	badCmd2 := exec.Command(path, "import", confPath)
	badCmd2.Stdin = strings.NewReader(
		"I am a wrong passphrase\n" +
			"password\n",
	)
	_ = badCmd2.Run()
	// assert.Equal(t, nil, err)
153 154
	assert.True(t, !common.FileExists(certPath))
	assert.True(t, !common.FileExists(keyPath))
155 156 157 158 159 160 161 162 163

	// Bad case 3 : Wrong passphrase for the private key
	badCmd3 := exec.Command(path, "import", confPath)
	badCmd3.Stdin = strings.NewReader(
		"\n" +
			"I am a wrong passphrase\n",
	)
	_ = badCmd3.Run()
	// assert.Equal(t, nil, err)
164 165
	assert.True(t, !common.FileExists(certPath))
	assert.True(t, !common.FileExists(keyPath))
166
}