Skip to content

Commit

Permalink
Compatibility test for old Secure Session API
Browse files Browse the repository at this point in the history
All these dynamic checks in session.New() warrant a test which verifies
that we actually can handle the old interface values and reject invalid
ones.

This test can be removed together with the old SessionCallbacks API.
  • Loading branch information
ilammy committed Mar 12, 2019
1 parent ee6022f commit 5b820e1
Showing 1 changed file with 70 additions and 16 deletions.
86 changes: 70 additions & 16 deletions gothemis/session/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,25 @@ func (clb *testCallbacks) StateChanged(ss *SecureSession, state int) {

}

// TODO: remove together with deprecated SessionCallbacks interface
type testCallbacksOld struct {
a *keys.Keypair
b *keys.Keypair
}

func (clb *testCallbacksOld) GetPublicKeyForId(ss *SecureSession, id []byte) *keys.PublicKey {
if bytes.Equal(clientID, id) {
return clb.a.Public
} else if bytes.Equal(serverID, id) {
return clb.b.Public
}
return nil
}

func (clb *testCallbacksOld) StateChanged(ss *SecureSession, state int) {

}

func genRandData() ([]byte, error) {
dataLength, err := rand.Int(rand.Reader, big.NewInt(2048))
if nil != err {
Expand Down Expand Up @@ -162,21 +181,8 @@ func serverService(server *SecureSession, ch chan []byte, finCh chan int, t *tes
}
}

func testSession(keytype int, t *testing.T) {
kpa, err := keys.New(keytype)
if nil != err {
t.Error(err)
return
}

kpb, err := keys.New(keytype)
if nil != err {
t.Error(err)
return
}

clb := &testCallbacks{kpa, kpb}

func testSession(kpa *keys.Keypair, kpb *keys.Keypair, clb interface{}, t *testing.T) {
var err error
emptyKey := keys.PrivateKey{Value: []byte{}}

_, err = New(clientID, nil, clb)
Expand Down Expand Up @@ -224,5 +230,53 @@ func testSession(keytype int, t *testing.T) {
}

func TestSession(t *testing.T) {
testSession(keys.TypeEC, t)
kpa, err := keys.New(keys.TypeEC)
if nil != err {
t.Error(err)
return
}

kpb, err := keys.New(keys.TypeEC)
if nil != err {
t.Error(err)
return
}

clb := &testCallbacks{kpa, kpb}

testSession(kpa, kpb, clb, t)
}

// TODO: remove together with deprecated SessionCallbacks interface
func TestSessionOldAPI(t *testing.T) {
kpa, err := keys.New(keys.TypeEC)
if nil != err {
t.Error(err)
return
}

kpb, err := keys.New(keys.TypeEC)
if nil != err {
t.Error(err)
return
}

clb := &testCallbacksOld{kpa, kpb}

testSession(kpa, kpb, clb, t)
}

// TODO: remove together with deprecated SessionCallbacks interface
func TestSessionInvalidCallbacks(t *testing.T) {
keyPair, err := keys.New(keys.TypeEC)
if err != nil {
t.Error(err)
return
}

_, err = New(serverID, keyPair.Private, "definitely not a callback")
if err == nil {
t.Error("Expected secure.New() to fail for invalid type")
return
}
}

0 comments on commit 5b820e1

Please sign in to comment.