]> Cypherpunks.ru repositories - govpn.git/blob - src/cypherpunks.ru/govpn/peer_test.go
Merge branch 'develop'
[govpn.git] / src / cypherpunks.ru / govpn / peer_test.go
1 /*
2 GoVPN -- simple secure free software virtual private network daemon
3 Copyright (C) 2014-2016 Sergey Matveev <stargrave@stargrave.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 package govpn
20
21 import (
22         "testing"
23         "testing/quick"
24         "time"
25 )
26
27 var (
28         testPeer   *Peer
29         testPt     []byte
30         testCt     []byte
31         testPeerId PeerId
32         testConf   *PeerConf
33 )
34
35 type Dummy struct {
36         dst *[]byte
37 }
38
39 func (d Dummy) Write(b []byte) (int, error) {
40         if d.dst != nil {
41                 *d.dst = b
42         }
43         return len(b), nil
44 }
45
46 func init() {
47         id := new([IDSize]byte)
48         testPeerId = PeerId(*id)
49         testConf = &PeerConf{
50                 Id:      &testPeerId,
51                 MTU:     MTUDefault,
52                 Timeout: time.Second * time.Duration(TimeoutDefault),
53         }
54         testPeer = newPeer(true, "foo", Dummy{&testCt}, testConf, new([SSize]byte))
55         testPt = make([]byte, 789)
56 }
57
58 func TestTransportSymmetric(t *testing.T) {
59         peerd := newPeer(true, "foo", Dummy{nil}, testConf, new([SSize]byte))
60         f := func(payload []byte) bool {
61                 if len(payload) == 0 {
62                         return true
63                 }
64                 testPeer.EthProcess(payload)
65                 return peerd.PktProcess(testCt, Dummy{nil}, true)
66         }
67         if err := quick.Check(f, nil); err != nil {
68                 t.Error(err)
69         }
70 }
71
72 func TestTransportSymmetricNoise(t *testing.T) {
73         peerd := newPeer(true, "foo", Dummy{nil}, testConf, new([SSize]byte))
74         testPeer.NoiseEnable = true
75         peerd.NoiseEnable = true
76         f := func(payload []byte) bool {
77                 if len(payload) == 0 {
78                         return true
79                 }
80                 testPeer.EthProcess(payload)
81                 return peerd.PktProcess(testCt, Dummy{nil}, true)
82         }
83         if err := quick.Check(f, nil); err != nil {
84                 t.Error(err)
85         }
86         testPeer.NoiseEnable = true
87 }
88
89 func TestTransportSymmetricEncless(t *testing.T) {
90         peerd := newPeer(true, "foo", Dummy{nil}, testConf, new([SSize]byte))
91         testPeer.Encless = true
92         testPeer.NoiseEnable = true
93         peerd.Encless = true
94         peerd.NoiseEnable = true
95         f := func(payload []byte) bool {
96                 if len(payload) == 0 {
97                         return true
98                 }
99                 testPeer.EthProcess(payload)
100                 return peerd.PktProcess(testCt, Dummy{nil}, true)
101         }
102         if err := quick.Check(f, nil); err != nil {
103                 t.Error(err)
104         }
105         testPeer.NoiseEnable = false
106         testPeer.Encless = false
107 }
108
109 func BenchmarkEnc(b *testing.B) {
110         b.ResetTimer()
111         for i := 0; i < b.N; i++ {
112                 testPeer.EthProcess(testPt)
113         }
114 }
115
116 func BenchmarkDec(b *testing.B) {
117         testPeer = newPeer(true, "foo", Dummy{&testCt}, testConf, new([SSize]byte))
118         testPeer.EthProcess(testPt)
119         testPeer = newPeer(true, "foo", Dummy{nil}, testConf, new([SSize]byte))
120         orig := make([]byte, len(testCt))
121         copy(orig, testCt)
122         b.ResetTimer()
123         for i := 0; i < b.N; i++ {
124                 testPeer.nonceBucket0 = make(map[uint64]struct{}, 1)
125                 testPeer.nonceBucket1 = make(map[uint64]struct{}, 1)
126                 copy(testCt, orig)
127                 if !testPeer.PktProcess(testCt, Dummy{nil}, true) {
128                         b.Fail()
129                 }
130         }
131 }
132
133 func TestTransportBigger(t *testing.T) {
134         tmp := make([]byte, MTUMax*4)
135         Rand.Read(tmp)
136         testPeer.PktProcess(tmp, Dummy{nil}, true)
137 }