diff options
author | Mathias Hall-Andersen <mathias@hall-andersen.dk> | 2017-06-24 15:34:17 +0200 |
---|---|---|
committer | Mathias Hall-Andersen <mathias@hall-andersen.dk> | 2017-06-24 15:34:44 +0200 |
commit | 25190e43369a79dc77a740dc8cd28b8a9fcb235e (patch) | |
tree | b7057627e0710fe9ef40c077a204904c78bed9cc /src/noise_test.go | |
parent | 521e77fd54fba275405affd790ac91f7998e4559 (diff) |
Restructuring of noise impl.
Diffstat (limited to 'src/noise_test.go')
-rw-r--r-- | src/noise_test.go | 91 |
1 files changed, 73 insertions, 18 deletions
diff --git a/src/noise_test.go b/src/noise_test.go index b3ea54f..8d6a0fa 100644 --- a/src/noise_test.go +++ b/src/noise_test.go @@ -1,38 +1,93 @@ package main import ( + "bytes" + "encoding/binary" "testing" ) -func TestHandshake(t *testing.T) { - var dev1 Device - var dev2 Device - - var err error - - dev1.privateKey, err = newPrivateKey() +func assertNil(t *testing.T, err error) { if err != nil { t.Fatal(err) } +} + +func assertEqual(t *testing.T, a []byte, b []byte) { + if bytes.Compare(a, b) != 0 { + t.Fatal(a, "!=", b) + } +} - dev2.privateKey, err = newPrivateKey() +func TestCurveWrappers(t *testing.T) { + sk1, err := newPrivateKey() + assertNil(t, err) + + sk2, err := newPrivateKey() + assertNil(t, err) + + pk1 := sk1.publicKey() + pk2 := sk2.publicKey() + + ss1 := sk1.sharedSecret(pk2) + ss2 := sk2.sharedSecret(pk1) + + if ss1 != ss2 { + t.Fatal("Failed to compute shared secet") + } +} + +func newDevice(t *testing.T) *Device { + var device Device + sk, err := newPrivateKey() if err != nil { t.Fatal(err) } + device.Init() + device.SetPrivateKey(sk) + return &device +} + +func TestNoiseHandshake(t *testing.T) { + + dev1 := newDevice(t) + dev2 := newDevice(t) - var peer1 Peer - var peer2 Peer + peer1 := dev2.NewPeer(dev1.privateKey.publicKey()) + peer2 := dev1.NewPeer(dev2.privateKey.publicKey()) - peer1.publicKey = dev1.privateKey.publicKey() - peer2.publicKey = dev2.privateKey.publicKey() + assertEqual( + t, + peer1.handshake.precomputedStaticStatic[:], + peer2.handshake.precomputedStaticStatic[:], + ) + + /* simulate handshake */ + + // Initiation message + + msg1, err := dev1.CreateMessageInitial(peer2) + assertNil(t, err) + + packet := make([]byte, 0, 256) + writer := bytes.NewBuffer(packet) + err = binary.Write(writer, binary.LittleEndian, msg1) + peer := dev2.ConsumeMessageInitial(msg1) + if peer == nil { + t.Fatal("handshake failed at initiation message") + } - var handshake1 Handshake - var handshake2 Handshake + assertEqual( + t, + peer1.handshake.chainKey[:], + peer2.handshake.chainKey[:], + ) - handshake1.device = &dev1 - handshake2.device = &dev2 + assertEqual( + t, + peer1.handshake.hash[:], + peer2.handshake.hash[:], + ) - handshake1.peer = &peer2 - handshake2.peer = &peer1 + // Response message } |