From 5871ec04deb8f4715cab37146940baa35c08cbee Mon Sep 17 00:00:00 2001 From: Mathias Hall-Andersen Date: Fri, 2 Feb 2018 20:45:25 +0100 Subject: Fixed tests --- src/helper_test.go | 8 ++++---- src/noise_test.go | 4 ++-- src/peer.go | 45 +++++++++++++++++++++++++++++---------------- 3 files changed, 35 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/helper_test.go b/src/helper_test.go index 8548121..41e6b72 100644 --- a/src/helper_test.go +++ b/src/helper_test.go @@ -28,8 +28,8 @@ func (tun *DummyTUN) MTU() (int, error) { return tun.mtu, nil } -func (tun *DummyTUN) Write(d []byte) (int, error) { - tun.packets <- d +func (tun *DummyTUN) Write(d []byte, offset int) (int, error) { + tun.packets <- d[offset:] return len(d), nil } @@ -41,9 +41,9 @@ func (tun *DummyTUN) Events() chan TUNEvent { return tun.events } -func (tun *DummyTUN) Read(d []byte) (int, error) { +func (tun *DummyTUN) Read(d []byte, offset int) (int, error) { t := <-tun.packets - copy(d, t) + copy(d[offset:], t) return len(t), nil } diff --git a/src/noise_test.go b/src/noise_test.go index 0d7f0e9..5e9d44b 100644 --- a/src/noise_test.go +++ b/src/noise_test.go @@ -31,8 +31,8 @@ func TestNoiseHandshake(t *testing.T) { defer dev1.Close() defer dev2.Close() - peer1, _ := dev2.NewPeer(dev1.privateKey.publicKey()) - peer2, _ := dev1.NewPeer(dev2.privateKey.publicKey()) + peer1, _ := dev2.NewPeer(dev1.noise.privateKey.publicKey()) + peer2, _ := dev1.NewPeer(dev2.noise.privateKey.publicKey()) assertEqual( t, diff --git a/src/peer.go b/src/peer.go index 7776b71..dc04811 100644 --- a/src/peer.go +++ b/src/peer.go @@ -154,6 +154,10 @@ func (peer *Peer) SendBuffer(buffer []byte) error { peer.device.net.mutex.RLock() defer peer.device.net.mutex.RUnlock() + if peer.device.net.bind == nil { + return errors.New("No bind") + } + peer.mutex.RLock() defer peer.mutex.RUnlock() @@ -161,10 +165,6 @@ func (peer *Peer) SendBuffer(buffer []byte) error { return errors.New("No known endpoint for peer") } - if peer.device.net.bind == nil { - return errors.New("No bind") - } - return peer.device.net.bind.Send(buffer, peer.endpoint) } @@ -196,16 +196,19 @@ func (peer *Peer) Start() { peer.routines.mutex.Lock() defer peer.routines.mutex.Unlock() + + if peer.isRunning.Get() { + return + } + peer.device.log.Debug.Println("Starting:", peer.String()) - // stop & wait for ongoing routines (if any) + // sanity check : these should be 0 - peer.isRunning.Set(false) - peer.routines.stop.Broadcast() peer.routines.starting.Wait() peer.routines.stopping.Wait() - // prepare queues + // prepare queues and signals peer.signal.newKeyPair = NewSignal() peer.signal.handshakeBegin = NewSignal() @@ -216,9 +219,11 @@ func (peer *Peer) Start() { peer.queue.outbound = make(chan *QueueOutboundElement, QueueOutboundSize) peer.queue.inbound = make(chan *QueueInboundElement, QueueInboundSize) - // reset signal and start (new) routines - peer.routines.stop = NewSignal() + peer.isRunning.Set(true) + + // wait for routines to start + peer.routines.starting.Add(PeerRoutineNumber) peer.routines.stopping.Add(PeerRoutineNumber) @@ -238,15 +243,28 @@ func (peer *Peer) Stop() { peer.routines.mutex.Lock() defer peer.routines.mutex.Unlock() + if !peer.isRunning.Swap(false) { + return + } + device := peer.device device.log.Debug.Println("Stopping:", peer.String()) - // stop & wait for ongoing peer routines (if any) + // stop & wait for ongoing peer routines peer.routines.stop.Broadcast() peer.routines.starting.Wait() peer.routines.stopping.Wait() + // stop timers + + peer.timer.keepalivePersistent.Stop() + peer.timer.keepalivePassive.Stop() + peer.timer.zeroAllKeys.Stop() + peer.timer.handshakeNew.Stop() + peer.timer.handshakeDeadline.Stop() + peer.timer.handshakeTimeout.Stop() + // close queues close(peer.queue.nonce) @@ -274,9 +292,4 @@ func (peer *Peer) Stop() { device.indices.Delete(hs.localIndex) hs.Clear() hs.mutex.Unlock() - - // reset signal (to handle repeated stopping) - - peer.routines.stop = NewSignal() - peer.isRunning.Set(false) } -- cgit v1.2.3