]> Cypherpunks.ru repositories - govpn.git/blobdiff - src/govpn/peer_test.go
[DOC] Minimal Go version is 1.4
[govpn.git] / src / govpn / peer_test.go
index dfdb941991b88858a6e3f5adc9c7613eb4a432cd..7fa8aab8438a7fd04eaccf53972064edd91b8792 100644 (file)
@@ -20,15 +20,16 @@ package govpn
 
 import (
        "testing"
+       "testing/quick"
        "time"
 )
 
 var (
-       peer       *Peer
-       plaintext  []byte
-       ciphertext []byte
-       peerId     *PeerId
-       conf       *PeerConf
+       testPeer   *Peer
+       testPt     []byte
+       testCt     []byte
+       testPeerId PeerId
+       testConf   *PeerConf
 )
 
 type Dummy struct {
@@ -43,39 +44,94 @@ func (d Dummy) Write(b []byte) (int, error) {
 }
 
 func init() {
-       MTU = 1500
        id := new([IDSize]byte)
-       peerId := PeerId(*id)
-       conf = &PeerConf{
-               Id:      &peerId,
+       testPeerId = PeerId(*id)
+       testConf = &PeerConf{
+               Id:      &testPeerId,
+               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)
+       testPeer = newPeer(true, "foo", Dummy{&testCt}, testConf, new([SSize]byte))
+       testPt = make([]byte, 789)
+}
+
+func TestTransportSymmetric(t *testing.T) {
+       peerd := newPeer(true, "foo", Dummy{nil}, testConf, new([SSize]byte))
+       f := func(payload []byte) bool {
+               if len(payload) == 0 {
+                       return true
+               }
+               testPeer.EthProcess(payload)
+               return peerd.PktProcess(testCt, 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}, testConf, new([SSize]byte))
+       testPeer.NoiseEnable = true
+       peerd.NoiseEnable = true
+       f := func(payload []byte) bool {
+               if len(payload) == 0 {
+                       return true
+               }
+               testPeer.EthProcess(payload)
+               return peerd.PktProcess(testCt, Dummy{nil}, true)
+       }
+       if err := quick.Check(f, nil); err != nil {
+               t.Error(err)
+       }
+       testPeer.NoiseEnable = true
+}
+
+func TestTransportSymmetricEncless(t *testing.T) {
+       peerd := newPeer(true, "foo", Dummy{nil}, testConf, new([SSize]byte))
+       testPeer.Encless = true
+       testPeer.NoiseEnable = true
+       peerd.Encless = true
+       peerd.NoiseEnable = true
+       f := func(payload []byte) bool {
+               if len(payload) == 0 {
+                       return true
+               }
+               testPeer.EthProcess(payload)
+               return peerd.PktProcess(testCt, Dummy{nil}, true)
+       }
+       if err := quick.Check(f, nil); err != nil {
+               t.Error(err)
+       }
+       testPeer.NoiseEnable = false
+       testPeer.Encless = false
 }
 
 func BenchmarkEnc(b *testing.B) {
        b.ResetTimer()
        for i := 0; i < b.N; i++ {
-               peer.EthProcess(plaintext)
+               testPeer.EthProcess(testPt)
        }
 }
 
 func BenchmarkDec(b *testing.B) {
-       peer = newPeer(true, "foo", Dummy{&ciphertext}, conf, new([SSize]byte))
-       peer.EthProcess(plaintext)
-       peer = newPeer(true, "foo", Dummy{nil}, conf, new([SSize]byte))
-       orig := make([]byte, len(ciphertext))
-       copy(orig, ciphertext)
+       testPeer = newPeer(true, "foo", Dummy{&testCt}, testConf, new([SSize]byte))
+       testPeer.EthProcess(testPt)
+       testPeer = newPeer(true, "foo", Dummy{nil}, testConf, new([SSize]byte))
+       orig := make([]byte, len(testCt))
+       copy(orig, testCt)
        b.ResetTimer()
        for i := 0; i < b.N; i++ {
-               peer.nonceBucket0 = make(map[uint64]struct{}, 1)
-               peer.nonceBucket1 = make(map[uint64]struct{}, 1)
-               copy(ciphertext, orig)
-               if !peer.PktProcess(ciphertext, Dummy{nil}, true) {
+               testPeer.nonceBucket0 = make(map[uint64]struct{}, 1)
+               testPeer.nonceBucket1 = make(map[uint64]struct{}, 1)
+               copy(testCt, orig)
+               if !testPeer.PktProcess(testCt, Dummy{nil}, true) {
                        b.Fail()
                }
        }
 }
+
+func TestTransportBigger(t *testing.T) {
+       tmp := make([]byte, MTUMax*4)
+       Rand.Read(tmp)
+       testPeer.PktProcess(tmp, Dummy{nil}, true)
+}