check_request_test.go 2.99 KB
Newer Older
1
package entities
Caro Axel's avatar
Caro Axel committed
2 3 4 5 6

import (
	"bytes"
	"testing"

7
	cAPI "dfss/dfssc/api"
8
	"github.com/stretchr/testify/assert"
Caro Axel's avatar
Caro Axel committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
)

func TestIsRequestValid(t *testing.T) {
	// TODO
	// This requires the use of a real Alert message
}

func TestIsPromiseSignedByPlatform(t *testing.T) {
	promise := &cAPI.Promise{
		Context: &cAPI.Context{
			SignatureUUID: "toto",
			Signers:       [][]byte{},
		},
	}

	valid, _, _ := IsPromiseSignedByPlatform(promise)
	assert.Equal(t, valid, false)

	promise.Context.SignatureUUID = signatureUUID
	valid, sigID, _ := IsPromiseSignedByPlatform(promise)
	assert.Equal(t, valid, false)

	promise.Context.Signers = append(promise.Context.Signers, []byte{0})
	valid, sigID, _ = IsPromiseSignedByPlatform(promise)
	assert.Equal(t, valid, false)

	promise.Context.Signers = signers
36 37
	valid, sigID, _ = IsPromiseSignedByPlatform(promise)
	assert.Equal(t, valid, false)
Caro Axel's avatar
Caro Axel committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 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 107 108 109 110 111 112 113
	assert.Equal(t, sigID, signatureUUIDBson)
}

func TestGetSenderHashFromContext(t *testing.T) {
	// TODO
	// This requires the use of a real Alert message
}

func TestGetIndexOfSigner(t *testing.T) {
	promise := &cAPI.Promise{
		Context: &cAPI.Context{
			Signers: signers,
		},
	}

	hash := []byte{}
	i, err := GetIndexOfSigner(promise, hash)
	assert.Equal(t, err.Error(), "Signer's hash couldn't be matched")
	assert.Equal(t, i, uint32(0))

	hash = signers[1]
	i, err = GetIndexOfSigner(promise, hash)
	assert.Equal(t, err, nil)
	assert.Equal(t, i, uint32(1))
}

func TestIsSignatureUUIDValid(t *testing.T) {
	promise := &cAPI.Promise{
		Context: &cAPI.Context{
			SignatureUUID: "toto",
		},
	}
	b, _ := IsSignatureUUIDValid(promise)
	assert.Equal(t, b, false)

	promise.Context.SignatureUUID = signatureUUID
	b, id := IsSignatureUUIDValid(promise)
	assert.Equal(t, id, signatureUUIDBson)
	assert.Equal(t, b, true)
}

func TestAreSignersHashesValid(t *testing.T) {
	promise := &cAPI.Promise{
		Context: &cAPI.Context{
			Signers: [][]byte{},
		},
	}

	b, signerss := AreSignersHashesValid(promise)
	assert.Equal(t, b, false)
	assert.Equal(t, len(signerss), 0)

	promise.Context.Signers = append(promise.Context.Signers, []byte{0})
	b, signerss = AreSignersHashesValid(promise)
	assert.Equal(t, b, false)
	assert.Equal(t, len(signerss), 0)

	promise.Context.Signers = signers
	b, signerss = AreSignersHashesValid(promise)
	assert.Equal(t, b, true)
	assert.Equal(t, len(signerss), len(signers))
	for i, v := range signerss {
		assert.Equal(t, v.Hash, signers[i])
	}
}

func TestIsSignerHashValid(t *testing.T) {
	hash := []byte{}
	b, signer := IsSignerHashValid(hash)
	assert.Equal(t, b, false)

	b, signer = IsSignerHashValid(signers[0])
	assert.Equal(t, b, true)
	assert.Equal(t, bytes.Equal(signer.Hash, signers[0]), true)
}

114
func TestIsPlatformSealValid(t *testing.T) {
Caro Axel's avatar
Caro Axel committed
115 116 117 118 119 120
	promise := &cAPI.Promise{
		Context: &cAPI.Context{
			ContractDocumentHash: contractDocumentHash,
			Sequence:             sequence,
			Signers:              signers,
			SignatureUUID:        signatureUUID,
121
			Seal:                 seal,
Caro Axel's avatar
Caro Axel committed
122 123 124
		},
	}

125 126
	b := IsPlatformSealValid(promise)
	assert.Equal(t, b, false)
Caro Axel's avatar
Caro Axel committed
127
}