Commit 389f72dd authored by Loïck Bonniot's avatar Loïck Bonniot

[tests] Add scenario for contract creation

parent dae88815
Pipeline #313 passed with stage
......@@ -43,6 +43,8 @@ Integration tests:
tags:
- golang
- mongo
services:
- "lesterpig/mongo:latest"
script:
- "ln -s -f $(pwd) $GOPATH/src/dfss"
- "./build/deps.sh"
......
......@@ -13,8 +13,8 @@ func newContract() {
passphrase, filepath, comment, signers := getContractInfo()
err := sign.NewCreateManager(fca, fcert, fkey, addrPort, passphrase, filepath, comment, signers)
if err != nil {
fmt.Println(err)
os.Exit(4)
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
......
......@@ -9,7 +9,6 @@ import (
var dbURI string
var dbManager *mgdb.MongoManager
// EraseDatabase drops the test database.
func eraseDatabase() {
_ = dbManager.Database.DropDatabase()
}
......@@ -25,3 +24,11 @@ func getRegistrationToken(mail string) string {
}
return user.RegToken
}
func getContract(file string, skip int) *entities.Contract {
var contract entities.Contract
_ = dbManager.Get("contracts").Collection.Find(bson.M{
"file.name": file,
}).Sort("_id").Skip(skip).One(&contract)
return &contract
}
package tests
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/bmizerany/assert"
)
// TestNewContract tries to creates new contracts on the platform.
//
// CLASSIC SCENARIO
// 1. client1 registers on the platform
// 2. client1 sends a new contract on the platform, but client2 is not here yet
// 3. client2 registers on the platform
// 4. client2 sends a new contract on the platform, and everyone is here
//
// BAD CASES
// 1. client2 sends a new contract with a wrong password
// 2. client3 sends a new contract without authentication
// 3. client1 sends a new contract with an invalid filepath
func TestNewContract(t *testing.T) {
// Cleanup
eraseDatabase()
// Start the platform
workingDir, err := ioutil.TempDir("", "dfss_")
assert.Equal(t, nil, err)
platform, ca, err := startPlatform(workingDir)
assert.Equal(t, nil, err)
defer func() {
_ = platform.Process.Kill()
_ = os.RemoveAll(workingDir)
}()
time.Sleep(2 * time.Second)
// Register client1
client1, err := createClient(workingDir, ca)
assert.Equal(t, nil, err)
err = registerAndAuth(client1, "client1@example.com", "password", "", true, true)
assert.Equal(t, nil, err)
// Create contract
client1 = newClient(client1)
setLastArg(client1, "new", true)
client1.Stdin = strings.NewReader(
"password\n" +
filepath.Join("testdata", "contract.txt") + "\n" +
"A very nice comment\n" +
"client1@example.com\n" +
"client2@example.com\n" +
"\n",
)
err = checkStderr(t, client1, "Operation succeeded with a warning message: Some users are not ready yet\n")
assert.NotEqual(t, nil, err)
// Check database
contract := getContract("contract.txt", 0)
assert.Equal(t, false, contract.Ready)
assert.Equal(t, "A very nice comment", contract.Comment)
assert.Equal(t, "6a95f6bcd6282186a7b1175fbaab4809ca5f665f7c4d55675de2399c83e67252069d741a88c766b1a79206d6dfbd5552cd7f9bc69b43bee161d1337228b4a4a8", contract.File.Hash)
assert.Equal(t, 2, len(contract.Signers))
assert.Equal(t, "client1@example.com", contract.Signers[0].Email)
assert.Equal(t, "client2@example.com", contract.Signers[1].Email)
assert.T(t, len(contract.Signers[0].Hash) > 0)
assert.T(t, len(contract.Signers[1].Hash) == 0)
// Register second signer
client2, err := createClient(workingDir, ca)
assert.Equal(t, nil, err)
err = registerAndAuth(client2, "client2@example.com", "password2", "", true, true)
assert.Equal(t, nil, err)
// Check database²
contract = getContract("contract.txt", 0)
assert.Equal(t, true, contract.Ready)
assert.T(t, len(contract.Signers[0].Hash) > 0)
assert.T(t, len(contract.Signers[1].Hash) > 0)
// Create a second contract
client2 = newClient(client2)
setLastArg(client2, "new", true)
client2.Stdin = strings.NewReader(
"password2\n" +
filepath.Join("testdata", "contract.txt") + "\n" +
"Another comment with some accents héhé\n" +
"client1@example.com\n" +
"client2@example.com\n" +
"\n",
)
err = checkStderr(t, client2, "")
assert.Equal(t, nil, err)
// Check database³
contract = getContract("contract.txt", 1)
assert.Equal(t, true, contract.Ready)
assert.Equal(t, "Another comment with some accents héhé", contract.Comment)
assert.T(t, len(contract.Signers[0].Hash) > 0)
assert.T(t, len(contract.Signers[1].Hash) > 0)
// Bad case: wrong password
client2 = newClient(client2)
setLastArg(client2, "new", true)
client2.Stdin = strings.NewReader(
"wrongPwd\n" +
filepath.Join("testdata", "contract.txt") + "\n" +
"\n" +
"client1@example.com\n" +
"client2@example.com\n" +
"\n",
)
err = checkStderr(t, client2, "x509: decryption password incorrect\n")
assert.NotEqual(t, nil, err)
// Bad case: no authentication
client3, err := createClient(workingDir, ca)
setLastArg(client3, "new", false)
client3.Stdin = strings.NewReader(
"\n" +
filepath.Join("testdata", "contract.txt") + "\n" +
"\n" +
"client1@example.com\n" +
"\n",
)
err = client3.Run()
assert.NotEqual(t, nil, err)
// Bad case: bad filepath
client1 = newClient(client1)
setLastArg(client1, "new", true)
client1.Stdin = strings.NewReader(
"password\n" +
"invalidFile\n" +
"client1@example.com\n" +
"\n",
)
err = checkStderr(t, client1, "open invalidFile: no such file or directory\n")
assert.NotEqual(t, nil, err)
// Check number of stored contracts
assert.Equal(t, 2, dbManager.Get("contracts").Count())
}
......@@ -23,6 +23,9 @@ import (
// client5 : wrong key size
// client6 : wrong mail during auth
// client7 : wrong token during auth
//
// TODO Add expired accounts test
// TODO Add Stderr test
func TestRegisterAuth(t *testing.T) {
// Cleanup
eraseDatabase()
......
package tests
import (
"bytes"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strconv"
"testing"
"github.com/bmizerany/assert"
)
const testPort = "9090"
var currentClient = 0
// StartPlatform creates root certificate for the platform and starts the platform.
// startPlatform creates root certificate for the platform and starts the platform.
func startPlatform(tmpDir string) (*exec.Cmd, []byte, error) {
path := filepath.Join(os.Getenv("GOPATH"), "bin", "dfssp")
......@@ -44,7 +48,7 @@ func startPlatform(tmpDir string) (*exec.Cmd, []byte, error) {
return cmd, ca, err
}
// CreateClient creates a new working directory for a client, creating ca.pem.
// createClient creates a new working directory for a client, creating ca.pem.
// It returns a ready-to-run command, but you probably want to set the last argument of the command.
func createClient(tmpDir string, ca []byte) (*exec.Cmd, error) {
path := filepath.Join(os.Getenv("GOPATH"), "bin", "dfssc")
......@@ -71,12 +75,11 @@ func createClient(tmpDir string, ca []byte) (*exec.Cmd, error) {
// The last argument is up to you!
cmd := exec.Command(path, "-ca", caPath, "-cert", certPath, "-host", "localhost:"+testPort, "-key", keyPath, "-v")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd, nil
}
// newClient clones the current command to another one.
// It's very useful when doing several commands on the same client.
func newClient(old *exec.Cmd) *exec.Cmd {
cmd := exec.Command(old.Path)
cmd.Args = old.Args
......@@ -85,10 +88,26 @@ func newClient(old *exec.Cmd) *exec.Cmd {
return cmd
}
// SetLastArg sets or updates the last argument of a command.
// setLastArg sets or updates the last argument of a command.
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)
}
// 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()
}
Important contract here.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment