]> Cypherpunks.ru repositories - govpn.git/blobdiff - src/govpn/peer_test.go
Input transport data size check
[govpn.git] / src / govpn / peer_test.go
index 0b8b917c5d44fb3bf3e95b3c398bdd10f4cd4f58..c39106029249172d91f3354b8e2bbf55df64d7ab 100644 (file)
@@ -1,7 +1,26 @@
+/*
+GoVPN -- simple secure free software virtual private network daemon
+Copyright (C) 2014-2016 Sergey Matveev <stargrave@stargrave.org>
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
 package govpn
 
 import (
        "testing"
+       "testing/quick"
        "time"
 )
 
@@ -9,7 +28,7 @@ var (
        peer       *Peer
        plaintext  []byte
        ciphertext []byte
-       peerId     *PeerId
+       peerId     PeerId
        conf       *PeerConf
 )
 
@@ -25,18 +44,68 @@ func (d Dummy) Write(b []byte) (int, error) {
 }
 
 func init() {
-       MTU = 1500
-       peerId, _ = IDDecode("ffffffffffffffffffffffffffffffff")
+       id := new([IDSize]byte)
+       peerId = PeerId(*id)
        conf = &PeerConf{
-               Id:      peerId,
+               Id:      &peerId,
+               MTU:     MTUDefault,
                Timeout: time.Second * time.Duration(TimeoutDefault),
-               Noise:   false,
-               CPR:     0,
        }
        peer = newPeer(true, "foo", Dummy{&ciphertext}, conf, new([SSize]byte))
        plaintext = make([]byte, 789)
 }
 
+func TestTransportSymmetric(t *testing.T) {
+       peerd := newPeer(true, "foo", Dummy{nil}, conf, new([SSize]byte))
+       f := func(payload []byte) bool {
+               if len(payload) == 0 {
+                       return true
+               }
+               peer.EthProcess(payload)
+               return peerd.PktProcess(ciphertext, Dummy{nil}, true)
+       }
+       if err := quick.Check(f, nil); err != nil {
+               t.Error(err)
+       }
+}
+
+func TestTransportSymmetricNoise(t *testing.T) {
+       peerd := newPeer(true, "foo", Dummy{nil}, conf, new([SSize]byte))
+       peer.NoiseEnable = true
+       peerd.NoiseEnable = true
+       f := func(payload []byte) bool {
+               if len(payload) == 0 {
+                       return true
+               }
+               peer.EthProcess(payload)
+               return peerd.PktProcess(ciphertext, Dummy{nil}, true)
+       }
+       if err := quick.Check(f, nil); err != nil {
+               t.Error(err)
+       }
+       peer.NoiseEnable = true
+}
+
+func TestTransportSymmetricEncless(t *testing.T) {
+       peerd := newPeer(true, "foo", Dummy{nil}, conf, new([SSize]byte))
+       peer.Encless = true
+       peer.NoiseEnable = true
+       peerd.Encless = true
+       peerd.NoiseEnable = true
+       f := func(payload []byte) bool {
+               if len(payload) == 0 {
+                       return true
+               }
+               peer.EthProcess(payload)
+               return peerd.PktProcess(ciphertext, Dummy{nil}, true)
+       }
+       if err := quick.Check(f, nil); err != nil {
+               t.Error(err)
+       }
+       peer.NoiseEnable = false
+       peer.Encless = false
+}
+
 func BenchmarkEnc(b *testing.B) {
        b.ResetTimer()
        for i := 0; i < b.N; i++ {
@@ -60,3 +129,9 @@ func BenchmarkDec(b *testing.B) {
                }
        }
 }
+
+func TestTransportBigger(t *testing.T) {
+       tmp := make([]byte, MTUMax*4)
+       Rand.Read(tmp)
+       peer.PktProcess(tmp, Dummy{nil}, true)
+}