Fixed tests
This commit is contained in:
parent
4f97b52ea6
commit
5871ec04de
@ -28,8 +28,8 @@ func (tun *DummyTUN) MTU() (int, error) {
|
|||||||
return tun.mtu, nil
|
return tun.mtu, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *DummyTUN) Write(d []byte) (int, error) {
|
func (tun *DummyTUN) Write(d []byte, offset int) (int, error) {
|
||||||
tun.packets <- d
|
tun.packets <- d[offset:]
|
||||||
return len(d), nil
|
return len(d), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,9 +41,9 @@ func (tun *DummyTUN) Events() chan TUNEvent {
|
|||||||
return tun.events
|
return tun.events
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *DummyTUN) Read(d []byte) (int, error) {
|
func (tun *DummyTUN) Read(d []byte, offset int) (int, error) {
|
||||||
t := <-tun.packets
|
t := <-tun.packets
|
||||||
copy(d, t)
|
copy(d[offset:], t)
|
||||||
return len(t), nil
|
return len(t), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,8 +31,8 @@ func TestNoiseHandshake(t *testing.T) {
|
|||||||
defer dev1.Close()
|
defer dev1.Close()
|
||||||
defer dev2.Close()
|
defer dev2.Close()
|
||||||
|
|
||||||
peer1, _ := dev2.NewPeer(dev1.privateKey.publicKey())
|
peer1, _ := dev2.NewPeer(dev1.noise.privateKey.publicKey())
|
||||||
peer2, _ := dev1.NewPeer(dev2.privateKey.publicKey())
|
peer2, _ := dev1.NewPeer(dev2.noise.privateKey.publicKey())
|
||||||
|
|
||||||
assertEqual(
|
assertEqual(
|
||||||
t,
|
t,
|
||||||
|
45
src/peer.go
45
src/peer.go
@ -154,6 +154,10 @@ func (peer *Peer) SendBuffer(buffer []byte) error {
|
|||||||
peer.device.net.mutex.RLock()
|
peer.device.net.mutex.RLock()
|
||||||
defer peer.device.net.mutex.RUnlock()
|
defer peer.device.net.mutex.RUnlock()
|
||||||
|
|
||||||
|
if peer.device.net.bind == nil {
|
||||||
|
return errors.New("No bind")
|
||||||
|
}
|
||||||
|
|
||||||
peer.mutex.RLock()
|
peer.mutex.RLock()
|
||||||
defer peer.mutex.RUnlock()
|
defer peer.mutex.RUnlock()
|
||||||
|
|
||||||
@ -161,10 +165,6 @@ func (peer *Peer) SendBuffer(buffer []byte) error {
|
|||||||
return errors.New("No known endpoint for peer")
|
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)
|
return peer.device.net.bind.Send(buffer, peer.endpoint)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -196,16 +196,19 @@ func (peer *Peer) Start() {
|
|||||||
|
|
||||||
peer.routines.mutex.Lock()
|
peer.routines.mutex.Lock()
|
||||||
defer peer.routines.mutex.Unlock()
|
defer peer.routines.mutex.Unlock()
|
||||||
|
|
||||||
|
if peer.isRunning.Get() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
peer.device.log.Debug.Println("Starting:", peer.String())
|
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.starting.Wait()
|
||||||
peer.routines.stopping.Wait()
|
peer.routines.stopping.Wait()
|
||||||
|
|
||||||
// prepare queues
|
// prepare queues and signals
|
||||||
|
|
||||||
peer.signal.newKeyPair = NewSignal()
|
peer.signal.newKeyPair = NewSignal()
|
||||||
peer.signal.handshakeBegin = NewSignal()
|
peer.signal.handshakeBegin = NewSignal()
|
||||||
@ -216,9 +219,11 @@ func (peer *Peer) Start() {
|
|||||||
peer.queue.outbound = make(chan *QueueOutboundElement, QueueOutboundSize)
|
peer.queue.outbound = make(chan *QueueOutboundElement, QueueOutboundSize)
|
||||||
peer.queue.inbound = make(chan *QueueInboundElement, QueueInboundSize)
|
peer.queue.inbound = make(chan *QueueInboundElement, QueueInboundSize)
|
||||||
|
|
||||||
// reset signal and start (new) routines
|
|
||||||
|
|
||||||
peer.routines.stop = NewSignal()
|
peer.routines.stop = NewSignal()
|
||||||
|
peer.isRunning.Set(true)
|
||||||
|
|
||||||
|
// wait for routines to start
|
||||||
|
|
||||||
peer.routines.starting.Add(PeerRoutineNumber)
|
peer.routines.starting.Add(PeerRoutineNumber)
|
||||||
peer.routines.stopping.Add(PeerRoutineNumber)
|
peer.routines.stopping.Add(PeerRoutineNumber)
|
||||||
|
|
||||||
@ -238,15 +243,28 @@ func (peer *Peer) Stop() {
|
|||||||
peer.routines.mutex.Lock()
|
peer.routines.mutex.Lock()
|
||||||
defer peer.routines.mutex.Unlock()
|
defer peer.routines.mutex.Unlock()
|
||||||
|
|
||||||
|
if !peer.isRunning.Swap(false) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
device := peer.device
|
device := peer.device
|
||||||
device.log.Debug.Println("Stopping:", peer.String())
|
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.stop.Broadcast()
|
||||||
peer.routines.starting.Wait()
|
peer.routines.starting.Wait()
|
||||||
peer.routines.stopping.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 queues
|
||||||
|
|
||||||
close(peer.queue.nonce)
|
close(peer.queue.nonce)
|
||||||
@ -274,9 +292,4 @@ func (peer *Peer) Stop() {
|
|||||||
device.indices.Delete(hs.localIndex)
|
device.indices.Delete(hs.localIndex)
|
||||||
hs.Clear()
|
hs.Clear()
|
||||||
hs.mutex.Unlock()
|
hs.mutex.Unlock()
|
||||||
|
|
||||||
// reset signal (to handle repeated stopping)
|
|
||||||
|
|
||||||
peer.routines.stop = NewSignal()
|
|
||||||
peer.isRunning.Set(false)
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user