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