Skip to content

Commit

Permalink
Refactor tests (comment some tests which will be rewritten later)
Browse files Browse the repository at this point in the history
  • Loading branch information
vqhuy committed Dec 20, 2017
1 parent f5d2eec commit c9491a4
Show file tree
Hide file tree
Showing 24 changed files with 435 additions and 827 deletions.
2 changes: 1 addition & 1 deletion application/bots/twitterbot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestCannotUnmarshallRequest(t *testing.T) {
func TestInvalidRequestType(t *testing.T) {
username := "alice"
request, _ := json.Marshal(&protocol.Request{
Type: protocol.KeyLookupType,
Type: protocol.KeyLookupInEpochType,
Request: &protocol.RegistrationRequest{
Username: username + "@twitter",
Key: []byte{1, 2, 3},
Expand Down
20 changes: 16 additions & 4 deletions application/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,15 @@ func UnmarshalResponse(t int, msg []byte) *protocol.Response {
// DirectoryResponse is omitempty for the places
// where Error is in Errors
if res.DirectoryResponse == nil {
if !protocol.Errors[res.Error] {
response := &protocol.Response{
Error: res.Error,
}
if err := response.Validate(); err != nil {
return &protocol.Response{
Error: protocol.ErrMalformedMessage,
}
}
return &protocol.Response{
Error: res.Error,
}
return response
}

switch t {
Expand All @@ -92,6 +93,17 @@ func UnmarshalResponse(t int, msg []byte) *protocol.Response {
Error: res.Error,
DirectoryResponse: response,
}
case protocol.STRType:
response := new(protocol.STRHistoryRange)
if err := json.Unmarshal(res.DirectoryResponse, &response); err != nil {
return &protocol.Response{
Error: protocol.ErrMalformedMessage,
}
}
return &protocol.Response{
Error: res.Error,
DirectoryResponse: response,
}
default:
panic("Unknown request type")
}
Expand Down
12 changes: 6 additions & 6 deletions application/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ func TestUnmarshalMalformedErrorResponse(t *testing.T) {
}

func TestUnmarshalSampleMessage(t *testing.T) {
d, _ := directory.NewTestDirectory(t, true)
res := d.Register(&protocol.RegistrationRequest{
Username: "alice",
Key: []byte("key")})
d := directory.NewTestDirectory(t)
res := d.GetSTRHistory(&protocol.STRHistoryRequest{
StartEpoch: 0,
EndEpoch: 0})
msg, _ := MarshalResponse(res)
response := UnmarshalResponse(protocol.RegistrationType, []byte(msg))
str := response.DirectoryResponse.(*protocol.DirectoryProof).STR[0]
response := UnmarshalResponse(protocol.STRType, []byte(msg))
str := response.DirectoryResponse.(*protocol.STRHistoryRange).STR[0]
if !bytes.Equal(d.LatestSTR().Serialize(), str.Serialize()) {
t.Error("Cannot unmarshal Associate Data properly")
}
Expand Down
28 changes: 28 additions & 0 deletions crypto/testutil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package crypto

import (
"bytes"

"github.com/coniks-sys/coniks-go/crypto/sign"
"github.com/coniks-sys/coniks-go/crypto/vrf"
)

// NewStaticTestVRFKey returns a static VRF private key for _tests_.
func NewStaticTestVRFKey() vrf.PrivateKey {
sk, err := vrf.GenerateKey(bytes.NewReader(
[]byte("deterministic tests need 256 bit")))
if err != nil {
panic(err)
}
return sk
}

// NewStaticTestSigningKey returns a static private signing key for _tests_.
func NewStaticTestSigningKey() sign.PrivateKey {
sk, err := sign.GenerateKey(bytes.NewReader(
[]byte("deterministic tests need 256 bit")))
if err != nil {
panic(err)
}
return sk
}
31 changes: 11 additions & 20 deletions merkletree/merkletree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,11 @@ import (
"bytes"
"testing"

"github.com/coniks-sys/coniks-go/crypto/vrf"
"github.com/coniks-sys/coniks-go/utils"
"golang.org/x/crypto/sha3"
)

var vrfPrivKey1, _ = vrf.GenerateKey(bytes.NewReader(
[]byte("deterministic tests need 256 bit")))

var vrfPrivKey2, _ = vrf.GenerateKey(bytes.NewReader(
[]byte("deterministic tests need 32 byte")))

// TODO: When #178 is merged, 3 tests below should be removed.
func TestOneEntry(t *testing.T) {
m, err := NewMerkleTree()
if err != nil {
Expand All @@ -26,7 +20,7 @@ func TestOneEntry(t *testing.T) {

key := "key"
val := []byte("value")
index := vrfPrivKey1.Compute([]byte(key))
index := staticVRFKey.Compute([]byte(key))
if err := m.Set(index, key, val); err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -90,10 +84,10 @@ func TestTwoEntries(t *testing.T) {
}

key1 := "key1"
index1 := vrfPrivKey1.Compute([]byte(key1))
index1 := staticVRFKey.Compute([]byte(key1))
val1 := []byte("value1")
key2 := "key2"
index2 := vrfPrivKey1.Compute([]byte(key2))
index2 := staticVRFKey.Compute([]byte(key2))
val2 := []byte("value2")

if err := m.Set(index1, key1, val1); err != nil {
Expand Down Expand Up @@ -130,13 +124,13 @@ func TestThreeEntries(t *testing.T) {
}

key1 := "key1"
index1 := vrfPrivKey1.Compute([]byte(key1))
index1 := staticVRFKey.Compute([]byte(key1))
val1 := []byte("value1")
key2 := "key2"
index2 := vrfPrivKey1.Compute([]byte(key2))
index2 := staticVRFKey.Compute([]byte(key2))
val2 := []byte("value2")
key3 := "key3"
index3 := vrfPrivKey1.Compute([]byte(key3))
index3 := staticVRFKey.Compute([]byte(key3))
val3 := []byte("value3")

if err := m.Set(index1, key1, val1); err != nil {
Expand Down Expand Up @@ -191,13 +185,10 @@ func TestThreeEntries(t *testing.T) {
}

func TestInsertExistedKey(t *testing.T) {
m, err := NewMerkleTree()
if err != nil {
t.Fatal(err)
}
m := newEmptyTreeForTest(t)

key1 := "key"
index1 := vrfPrivKey1.Compute([]byte(key1))
index1 := staticVRFKey.Compute([]byte(key1))
val1 := append([]byte(nil), "value"...)

if err := m.Set(index1, key1, val1); err != nil {
Expand Down Expand Up @@ -241,10 +232,10 @@ func TestInsertExistedKey(t *testing.T) {

func TestTreeClone(t *testing.T) {
key1 := "key1"
index1 := vrfPrivKey1.Compute([]byte(key1))
index1 := staticVRFKey.Compute([]byte(key1))
val1 := []byte("value1")
key2 := "key2"
index2 := vrfPrivKey1.Compute([]byte(key2))
index2 := staticVRFKey.Compute([]byte(key2))
val2 := []byte("value2")

m1, err := NewMerkleTree()
Expand Down
Loading

0 comments on commit c9491a4

Please sign in to comment.