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 Oct 25, 2017
1 parent ec6be0d commit 31c2259
Show file tree
Hide file tree
Showing 18 changed files with 226 additions and 563 deletions.
2 changes: 1 addition & 1 deletion coniksbots/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
32 changes: 22 additions & 10 deletions coniksclient/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,48 @@ func UnmarshalResponse(t int, msg []byte) *protocol.Response {
Error protocol.ErrorCode
DirectoryResponse json.RawMessage
}
var res Response
if err := json.Unmarshal(msg, &res); err != nil {
var resp Response
if err := json.Unmarshal(msg, &resp); err != nil {
return &protocol.Response{
Error: protocol.ErrMalformedMessage,
}
}

// DirectoryResponse is omitempty for the places
// where Error is in Errors
if res.DirectoryResponse == nil {
if !protocol.Errors[res.Error] {
if resp.DirectoryResponse == nil {
response := &protocol.Response{
Error: resp.Error,
}
if err := response.Validate(); err != nil {
return &protocol.Response{
Error: protocol.ErrMalformedMessage,
}
}
return &protocol.Response{
Error: res.Error,
}
return response
}

switch t {
case protocol.RegistrationType, protocol.KeyLookupType, protocol.KeyLookupInEpochType, protocol.MonitoringType:
case protocol.RegistrationType, protocol.KeyLookupInEpochType, protocol.MonitoringType:
response := new(protocol.DirectoryProof)
if err := json.Unmarshal(res.DirectoryResponse, &response); err != nil {
if err := json.Unmarshal(resp.DirectoryResponse, &response); err != nil {
return &protocol.Response{
Error: protocol.ErrMalformedMessage,
}
}
return &protocol.Response{
Error: resp.Error,
DirectoryResponse: response,
}
case protocol.STRType:
response := new(protocol.STRHistoryRange)
if err := json.Unmarshal(resp.DirectoryResponse, &response); err != nil {
return &protocol.Response{
Error: protocol.ErrMalformedMessage,
}
}
return &protocol.Response{
Error: res.Error,
Error: resp.Error,
DirectoryResponse: response,
}
default:
Expand Down
12 changes: 6 additions & 6 deletions coniksclient/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,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, _ := coniksserver.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
29 changes: 29 additions & 0 deletions crypto/testutil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package crypto

import (
"bytes"
"testing"

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

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

// StaticSigning returns a static private signing key for _tests_.
func StaticSigning(t *testing.T) sign.PrivateKey {
sk, err := sign.GenerateKey(bytes.NewReader(
[]byte("deterministic tests need 256 bit")))
if err != nil {
t.Fatal(err)
}
return sk
}
28 changes: 28 additions & 0 deletions merkletree/testutil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package merkletree

import (
"testing"

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

// StaticPAD returns a pad with a static initial STR for _tests_.
func StaticPAD(t *testing.T, ad AssocData) *PAD {
pad, err := NewPAD(ad, crypto.StaticSigning(t), crypto.StaticVRF(t), 10)
if err != nil {
t.Fatal(err)
}
str := NewSTR(pad.signKey, pad.ad, staticTree(t), 0, []byte{})
pad.latestSTR = str
pad.snapshots[0] = pad.latestSTR
return pad
}

func staticTree(t *testing.T) *MerkleTree {
m, err := NewMerkleTree()
if err != nil {
t.Fatal(err)
}
m.nonce = []byte{}
return m
}
38 changes: 19 additions & 19 deletions protocol/auditlog/auditlog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func TestInsertEmptyHistory(t *testing.T) {
// create basic test directory and audit log with 1 STR
_, _, _ = NewTestAuditLog(t, 0)
NewTestAuditLog(t, 0)
}

func TestUpdateHistory(t *testing.T) {
Expand All @@ -32,7 +32,7 @@ func TestUpdateHistory(t *testing.T) {

func TestInsertPriorHistory(t *testing.T) {
// create basic test directory and audit log with 11 STRs
_, _, _ = NewTestAuditLog(t, 10)
NewTestAuditLog(t, 10)
}

func TestInsertExistingHistory(t *testing.T) {
Expand Down Expand Up @@ -74,16 +74,16 @@ func TestAuditLogBadEpochRange(t *testing.T) {
dirInitHash := auditor.ComputeDirectoryIdentity(hist[0])
h, _ := aud.get(dirInitHash)

err1 := h.Audit(resp)
if err1 != nil {
t.Fatalf("Error occurred while auditing STR history: %s", err1.Error())
err := h.Audit(resp)
if err != nil {
t.Fatalf("Error occurred while auditing STR history: %s", err.Error())
}

// now try to audit the same range again: should fail because the
// verified epoch is at 1
err1 = h.Audit(resp)
if err1 != protocol.CheckBadSTR {
t.Fatalf("Expecting CheckBadSTR, got %s", err1.Error())
err = h.Audit(resp)
if err != protocol.CheckBadSTR {
t.Fatalf("Expecting CheckBadSTR, got %s", err.Error())
}
}

Expand Down Expand Up @@ -172,8 +172,8 @@ func TestGetObservedSTRMultipleEpochs(t *testing.T) {
h, _ := aud.get(dirInitHash)
resp := protocol.NewSTRHistoryRange([]*protocol.DirSTR{d.LatestSTR()})

err1 := h.Audit(resp)
if err1 != nil {
err := h.Audit(resp)
if err != nil {
t.Fatal("Error occurred updating audit log after auditing request")
}

Expand Down Expand Up @@ -339,9 +339,9 @@ func TestSTRHistoryRequestLatest(t *testing.T) {
dirInitHash := auditor.ComputeDirectoryIdentity(hist[0])
h, _ := aud.get(dirInitHash)

err1 := h.Audit(resp)
if err1 != nil {
t.Fatalf("Error occurred auditing the latest STR: %s", err1.Error())
err := h.Audit(resp)
if err != nil {
t.Fatalf("Error occurred auditing the latest STR: %s", err.Error())
}
}

Expand Down Expand Up @@ -375,9 +375,9 @@ func TestSTRHistoryRequestRangeLatest(t *testing.T) {
dirInitHash := auditor.ComputeDirectoryIdentity(hist[0])
h, _ := aud.get(dirInitHash)

err1 := h.Audit(resp)
if err1 != nil {
t.Fatalf("Error occurred auditing the latest STR: %s", err1.Error())
err := h.Audit(resp)
if err != nil {
t.Fatalf("Error occurred auditing the latest STR: %s", err.Error())
}
}

Expand Down Expand Up @@ -411,8 +411,8 @@ func TestSTRHistoryRequestInEpoch(t *testing.T) {
dirInitHash := auditor.ComputeDirectoryIdentity(hist[0])
h, _ := aud.get(dirInitHash)

err1 := h.Audit(resp)
if err1 != nil {
t.Fatalf("Error occurred auditing the latest STR: %s", err1.Error())
err := h.Audit(resp)
if err != nil {
t.Fatalf("Error occurred auditing the latest STR: %s", err.Error())
}
}
14 changes: 8 additions & 6 deletions protocol/auditlog/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package auditlog
import (
"testing"

"github.com/coniks-sys/coniks-go/crypto"
"github.com/coniks-sys/coniks-go/protocol"
"github.com/coniks-sys/coniks-go/protocol/directory"
)
Expand All @@ -15,21 +16,22 @@ import (
// STRs as it always includes the STR after the last directory update
func NewTestAuditLog(t *testing.T, numEpochs int) (
*directory.ConiksDirectory, ConiksAuditLog, []*protocol.DirSTR) {
d, pk := directory.NewTestDirectory(t, true)
d := directory.NewTestDirectory(t)
aud := New()

var hist []*protocol.DirSTR
var snaps []*protocol.DirSTR
for ep := 0; ep < numEpochs; ep++ {
hist = append(hist, d.LatestSTR())
snaps = append(snaps, d.LatestSTR())
d.Update()
}
// always include the actual latest STR
hist = append(hist, d.LatestSTR())
snaps = append(snaps, d.LatestSTR())

err := aud.InitHistory("test-server", pk, hist)
pk, _ := crypto.StaticSigning(t).Public()
err := aud.InitHistory("test-server", pk, snaps)
if err != nil {
t.Fatalf("Error inserting a new history with %d STRs", numEpochs+1)
}

return d, aud, hist
return d, aud, snaps
}
4 changes: 0 additions & 4 deletions protocol/auditor/auditor.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,6 @@ func (a *AudState) CheckSTRAgainstVerified(str *protocol.DirSTR) error {
// Maybe it has something to do w/ #81 and client
// transitioning between epochs.
// Try to verify w/ what's been saved

// FIXME: we are returning the error immediately
// without saving the inconsistent STR
// see: https://github.com/coniks-sys/coniks-go/pull/74#commitcomment-19804686
switch {
case str.Epoch == a.verifiedSTR.Epoch:
// Checking an STR in the same epoch
Expand Down
17 changes: 9 additions & 8 deletions protocol/auditor/auditor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package auditor
import (
"testing"

"github.com/coniks-sys/coniks-go/crypto"
"github.com/coniks-sys/coniks-go/protocol"
"github.com/coniks-sys/coniks-go/protocol/directory"
)

func TestAuditBadSTRSignature(t *testing.T) {
// create basic test directory
d, pk := directory.NewTestDirectory(t, true)
d := directory.NewTestDirectory(t)
pk, _ := crypto.StaticSigning(t).Public()

// create a generic auditor state
aud := New(pk, d.LatestSTR())
Expand All @@ -35,8 +36,8 @@ func TestAuditBadSTRSignature(t *testing.T) {

// used to be TestVerifyWithError in consistencychecks_test.go
func TestAuditBadSameEpoch(t *testing.T) {
// create basic test directory
d, pk := directory.NewTestDirectory(t, true)
d := directory.NewTestDirectory(t)
pk, _ := crypto.StaticSigning(t).Public()

// create a generic auditor state
aud := New(pk, d.LatestSTR())
Expand All @@ -57,8 +58,8 @@ func TestAuditBadSameEpoch(t *testing.T) {
}

func TestAuditBadNewSTREpoch(t *testing.T) {
// create basic test directory
d, pk := directory.NewTestDirectory(t, true)
d := directory.NewTestDirectory(t)
pk, _ := crypto.StaticSigning(t).Public()

// create a generic auditor state
aud := New(pk, d.LatestSTR())
Expand Down Expand Up @@ -95,8 +96,8 @@ func TestAuditBadNewSTREpoch(t *testing.T) {
}

func TestAuditMalformedSTRRange(t *testing.T) {
// create basic test directory
d, pk := directory.NewTestDirectory(t, true)
d := directory.NewTestDirectory(t)
pk, _ := crypto.StaticSigning(t).Public()

// create a generic auditor state
aud := New(pk, d.LatestSTR())
Expand Down
34 changes: 20 additions & 14 deletions protocol/auditor/common_test.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,29 @@
package auditor

import (
"bytes"
"encoding/hex"
"testing"

"github.com/coniks-sys/coniks-go/crypto"
"github.com/coniks-sys/coniks-go/protocol"
"github.com/coniks-sys/coniks-go/protocol/directory"
)

func TestComputeDirectoryIdentity(t *testing.T) {
d, _ := directory.NewTestDirectory(t, true)
// str0 := d.LatestSTR()
d := directory.NewTestDirectory(t)
str0 := d.LatestSTR()
d.Update()
str1 := d.LatestSTR()
var unknown [crypto.HashSizeByte]byte
type args struct {
str *protocol.DirSTR
}

tests := []struct {
name string
args args
want [crypto.HashSizeByte]byte
str *protocol.DirSTR
want []byte
}{
// {"normal", args{str0}, ""},
{"panic", args{str1}, unknown},
{"normal", str0, dh("fd0584f79054f8113f21e5450e0ad21c9221fc159334c7bc1644e3e2a0fb5060")},
{"panic", str1, []byte{}},
}
for _, tt := range tests {
// FIXME: Refactor testing. See #18.
t.Run(tt.name, func(t *testing.T) {
if tt.name == "panic" {
defer func() {
Expand All @@ -35,9 +32,18 @@ func TestComputeDirectoryIdentity(t *testing.T) {
}
}()
}
if got := ComputeDirectoryIdentity(tt.args.str); got != tt.want {
t.Errorf("ComputeDirectoryIdentity() = %v, want %v", got, tt.want)
if got, want := ComputeDirectoryIdentity(tt.str), tt.want; !bytes.Equal(got[:], want) {
t.Errorf("ComputeDirectoryIdentity() = %#v, want %#v", got, want)
}
})
}
}

// decode hex string to byte array
func dh(h string) []byte {
result, err := hex.DecodeString(h)
if err != nil {
panic(err)
}
return result
}
Loading

0 comments on commit 31c2259

Please sign in to comment.