]> Cypherpunks.ru repositories - govpn.git/blob - src/cypherpunks.ru/govpn/peer_test.go
go vet/lint
[govpn.git] / src / cypherpunks.ru / govpn / peer_test.go
1 /*
2 GoVPN -- simple secure free software virtual private network daemon
3 Copyright (C) 2014-2017 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         testPt = make([]byte, 789)
55 }
56
57 func testPeerNew() {
58         testPeer = newPeer(true, "foo", Dummy{&testCt}, testConf, new([SSize]byte))
59 }
60
61 func TestTransportSymmetric(t *testing.T) {
62         testPeerNew()
63         peerd := newPeer(false, "foo", Dummy{nil}, testConf, new([SSize]byte))
64         f := func(payload []byte) bool {
65                 if len(payload) == 0 {
66                         return true
67                 }
68                 testPeer.EthProcess(payload)
69                 return peerd.PktProcess(testCt, Dummy{nil}, true)
70         }
71         if err := quick.Check(f, nil); err != nil {
72                 t.Error(err)
73         }
74 }
75
76 func TestTransportSymmetricNoise(t *testing.T) {
77         testPeerNew()
78         peerd := newPeer(false, "foo", Dummy{nil}, testConf, new([SSize]byte))
79         testPeer.NoiseEnable = true
80         peerd.NoiseEnable = true
81         f := func(payload []byte) bool {
82                 if len(payload) == 0 {
83                         return true
84                 }
85                 testPeer.EthProcess(payload)
86                 return peerd.PktProcess(testCt, Dummy{nil}, true)
87         }
88         if err := quick.Check(f, nil); err != nil {
89                 t.Error(err)
90         }
91         testPeer.NoiseEnable = true
92 }
93
94 func TestTransportSymmetricEncless(t *testing.T) {
95         testPeerNew()
96         peerd := newPeer(false, "foo", Dummy{nil}, testConf, new([SSize]byte))
97         testPeer.Encless = true
98         testPeer.NoiseEnable = true
99         peerd.Encless = true
100         peerd.NoiseEnable = true
101         f := func(payload []byte) bool {
102                 if len(payload) == 0 {
103                         return true
104                 }
105                 testPeer.EthProcess(payload)
106                 return peerd.PktProcess(testCt, Dummy{nil}, true)
107         }
108         if err := quick.Check(f, nil); err != nil {
109                 t.Error(err)
110         }
111         testPeer.NoiseEnable = false
112         testPeer.Encless = false
113 }
114
115 func BenchmarkEnc(b *testing.B) {
116         for i := 0; i < b.N; i++ {
117                 testPeer.EthProcess(testPt)
118         }
119 }
120
121 func BenchmarkDec(b *testing.B) {
122         testPeer = newPeer(true, "foo", Dummy{&testCt}, testConf, new([SSize]byte))
123         testPeer.EthProcess(testPt)
124         testPeer = newPeer(true, "foo", Dummy{nil}, testConf, new([SSize]byte))
125         orig := make([]byte, len(testCt))
126         copy(orig, testCt)
127         nonce := new([NonceSize]byte)
128         copy(nonce[:], testCt[len(testCt)-NonceSize:])
129         b.ResetTimer()
130         for i := 0; i < b.N; i++ {
131                 testPeer.nonceBucketL = make(map[[NonceSize]byte]struct{}, 1)
132                 testPeer.nonceBucketM = make(map[[NonceSize]byte]struct{}, 1)
133                 testPeer.nonceBucketH = make(map[[NonceSize]byte]struct{}, 1)
134                 testPeer.nonceBucketL[*nonce] = struct{}{}
135                 copy(testCt, orig)
136                 if !testPeer.PktProcess(testCt, Dummy{nil}, true) {
137                         b.Fail()
138                 }
139         }
140 }
141
142 func TestTransportBigger(t *testing.T) {
143         tmp := make([]byte, MTUMax*4)
144         Rand.Read(tmp)
145         testPeer.PktProcess(tmp, Dummy{nil}, true)
146 }