create_test.go 7.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
package contract_test

import (
	"crypto/sha512"
	"io/ioutil"
	"path/filepath"
	"testing"
	"time"

	"dfss/auth"
	"dfss/dfssp/api"
	"dfss/dfssp/entities"
	"dfss/net"
	"github.com/bmizerany/assert"
	"golang.org/x/net/context"
)

var user1, user2, user3 *entities.User
var defaultHash = sha512.Sum512([]byte{0})

func createDataset() {

	user1 = entities.NewUser() // Regular user
	user2 = entities.NewUser() // Regular user
	user3 = entities.NewUser() // Non-auth user

	user1.Email = "user1@example.com"
	user1.Expiration = time.Now().AddDate(1, 0, 0)
	user1.Certificate = "Certificate1"
30
	user1.CertHash = []byte{0x01}
31 32 33 34

	user2.Email = "user2@example.com"
	user2.Expiration = time.Now().AddDate(1, 0, 0)
	user2.Certificate = "Certificate2"
35
	user2.CertHash = []byte{0x02}
36 37 38 39

	user3.Email = "user3@example.com"
	user3.Expiration = time.Now().AddDate(0, 0, -1)
	user3.Certificate = "Certificate3"
40
	user3.CertHash = []byte{0x03}
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55

	_, _ = manager.Get("users").Insert(user1)
	_, _ = manager.Get("users").Insert(user2)
	_, _ = manager.Get("users").Insert(user3)

}

func dropDataset() {

	_ = manager.Get("users").Drop()
	_ = manager.Get("contracts").Drop()

}

func clientTest(t *testing.T) api.PlatformClient {
56 57 58 59 60 61 62 63 64 65
	// TODO if anyone needs this function in another test suite, please put it in a separate file
	// to avoid code duplication
	caData, _ := ioutil.ReadFile(filepath.Join("..", "testdata", "dfssp_rootCA.pem"))
	certData, _ := ioutil.ReadFile(filepath.Join("..", "..", "dfssc", "testdata", "cert.pem"))
	keyData, _ := ioutil.ReadFile(filepath.Join("..", "..", "dfssc", "testdata", "key.pem"))
	ca, _ := auth.PEMToCertificate(caData)
	cert, _ := auth.PEMToCertificate(certData)
	key, _ := auth.EncryptedPEMToPrivateKey(keyData, "password")

	conn, err := net.Connect("localhost:9090", cert, key, ca)
66
	if err != nil {
67
		t.Fatal("Unable to connect:", err)
68 69
	}

70 71
	return api.NewPlatformClient(conn)
}
72

73 74 75 76
func TestAddContractBadAuth(t *testing.T) {
	caData, _ := ioutil.ReadFile(filepath.Join("..", "testdata", "dfssp_rootCA.pem"))
	ca, _ := auth.PEMToCertificate(caData)
	conn, err := net.Connect("localhost:9090", nil, nil, ca)
77 78 79
	if err != nil {
		t.Fatal("Unable to connect:", err)
	}
80 81 82 83
	client := api.NewPlatformClient(conn)
	errorCode, err := client.PostContract(context.Background(), &api.PostContractRequest{})
	assert.Equal(t, nil, err)
	assert.Equal(t, api.ErrorCode_BADAUTH, errorCode.Code)
84 85 86 87 88 89 90 91
}

func TestAddContract(t *testing.T) {
	dropDataset()
	createDataset()

	client := clientTest(t)
	errorCode, err := client.PostContract(context.Background(), &api.PostContractRequest{
92
		Hash:     defaultHash[:],
93 94 95 96
		Filename: "ContractFilename",
		Signer:   []string{user1.Email, user2.Email},
		Comment:  "ContractComment",
	})
97 98
	assert.Equal(t, nil, err)
	assert.Equal(t, api.ErrorCode_SUCCESS, errorCode.Code)
99 100 101 102 103 104 105 106 107

	// Check database content
	var contracts []entities.Contract
	err = manager.Get("contracts").FindAll(nil, &contracts)
	if err != nil {
		t.Fatal("Unexpected db error:", err)
	}

	assert.Equal(t, 1, len(contracts))
108
	assert.Equal(t, defaultHash[:], contracts[0].File.Hash)
109 110 111 112
	assert.Equal(t, "ContractFilename", contracts[0].File.Name)
	assert.Equal(t, "ContractComment", contracts[0].Comment)
	assert.T(t, contracts[0].Ready)

113
	assert.Equal(t, 2, len(contracts[0].Signers))
114 115 116 117 118 119 120 121 122 123 124 125 126 127
	assert.Equal(t, user1.ID, contracts[0].Signers[0].UserID)
	assert.Equal(t, user1.CertHash, contracts[0].Signers[0].Hash)
	assert.Equal(t, user1.Email, contracts[0].Signers[0].Email)
	assert.Equal(t, user2.ID, contracts[0].Signers[1].UserID)
	assert.Equal(t, user2.CertHash, contracts[0].Signers[1].Hash)
	assert.Equal(t, user2.Email, contracts[0].Signers[1].Email)
}

func TestAddContractMissingUser(t *testing.T) {
	dropDataset()
	createDataset()

	client := clientTest(t)
	errorCode, err := client.PostContract(context.Background(), &api.PostContractRequest{
128
		Hash:     defaultHash[:],
129 130 131 132 133 134 135 136 137 138 139 140 141 142
		Filename: "ContractFilename",
		Signer:   []string{user1.Email, user3.Email},
	})
	assert.Equal(t, nil, err)
	assert.Equal(t, api.ErrorCode_WARNING, errorCode.Code)

	// Check database content
	var contracts []entities.Contract
	err = manager.Get("contracts").FindAll(nil, &contracts)
	if err != nil {
		t.Fatal("Unexpected db error:", err)
	}

	assert.Equal(t, 1, len(contracts))
143
	assert.Equal(t, defaultHash[:], contracts[0].File.Hash)
144 145 146 147
	assert.Equal(t, "ContractFilename", contracts[0].File.Name)
	assert.Equal(t, "", contracts[0].Comment)
	assert.T(t, !contracts[0].Ready)

148
	assert.Equal(t, 2, len(contracts[0].Signers))
149 150 151 152
	assert.Equal(t, user1.ID, contracts[0].Signers[0].UserID)
	assert.Equal(t, user1.CertHash, contracts[0].Signers[0].Hash)
	assert.Equal(t, user1.Email, contracts[0].Signers[0].Email)
	assert.Equal(t, "000000000000000000000000", contracts[0].Signers[1].UserID.Hex())
153
	assert.Equal(t, []byte{}, contracts[0].Signers[1].Hash)
154 155 156 157 158 159 160 161 162
	assert.Equal(t, user3.Email, contracts[0].Signers[1].Email)
}

func TestAddContractNoUser(t *testing.T) {
	dropDataset()
	createDataset()

	client := clientTest(t)
	errorCode, err := client.PostContract(context.Background(), &api.PostContractRequest{
163
		Hash:     defaultHash[:],
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
		Filename: "ContractFilename",
		Signer:   []string{},
	})
	assert.Equal(t, nil, err)
	assert.Equal(t, api.ErrorCode_INVARG, errorCode.Code)

	// Check database content
	var contracts []entities.Contract
	err = manager.Get("contracts").FindAll(nil, &contracts)
	if err != nil {
		t.Fatal("Unexpected db error:", err)
	}

	assert.Equal(t, 0, len(contracts))
}

func TestAddContractDuplicatedUser(t *testing.T) {
	dropDataset()
	createDataset()

	client := clientTest(t)
	errorCode, err := client.PostContract(context.Background(), &api.PostContractRequest{
186
		Hash:     defaultHash[:],
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
		Filename: "ContractFilename",
		Signer:   []string{user1.Email, user1.Email, user2.Email},
	})
	assert.Equal(t, nil, err)
	assert.Equal(t, api.ErrorCode_SUCCESS, errorCode.Code)

	// Check database content
	var contracts []entities.Contract
	err = manager.Get("contracts").FindAll(nil, &contracts)
	if err != nil {
		t.Fatal("Unexpected db error:", err)
	}

	assert.Equal(t, 1, len(contracts))
	assert.Equal(t, 2, len(contracts[0].Signers))
}

func TestAddContractNoFilename(t *testing.T) {
	dropDataset()
	createDataset()

	client := clientTest(t)
	errorCode, err := client.PostContract(context.Background(), &api.PostContractRequest{
210
		Hash:   defaultHash[:],
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
		Signer: []string{user1.Email},
	})
	assert.Equal(t, nil, err)
	assert.Equal(t, api.ErrorCode_INVARG, errorCode.Code)

	// Check database content
	var contracts []entities.Contract
	err = manager.Get("contracts").FindAll(nil, &contracts)
	if err != nil {
		t.Fatal("Unexpected db error:", err)
	}

	assert.Equal(t, 0, len(contracts))
}

func TestAddContractBadHash(t *testing.T) {
	dropDataset()
	createDataset()

	client := clientTest(t)
	errorCode, err := client.PostContract(context.Background(), &api.PostContractRequest{
232
		Hash:     []byte{0xba, 0xd},
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
		Filename: "ContractFilename",
		Signer:   []string{user1.Email},
	})
	assert.Equal(t, nil, err)
	assert.Equal(t, api.ErrorCode_INVARG, errorCode.Code)

	// Check database content
	var contracts []entities.Contract
	err = manager.Get("contracts").FindAll(nil, &contracts)
	if err != nil {
		t.Fatal("Unexpected db error:", err)
	}

	assert.Equal(t, 0, len(contracts))
}