]> Cypherpunks.ru repositories - govpn.git/blob - src/govpn/peer_test.go
40f6b1d4970210c8e6d0ccd45d4a2629ac24110d
[govpn.git] / src / 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         peer       *Peer
29         plaintext  []byte
30         ciphertext []byte
31         peerId     *PeerId
32         conf       *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         MTU = 1500
48         id := new([IDSize]byte)
49         peerId := PeerId(*id)
50         conf = &PeerConf{
51                 Id:      &peerId,
52                 Timeout: time.Second * time.Duration(TimeoutDefault),
53                 Noise:   false,
54                 CPR:     0,
55         }
56         peer = newPeer(true, "foo", Dummy{&ciphertext}, conf, new([SSize]byte))
57         plaintext = make([]byte, 789)
58 }
59
60 func TestSymmetric(t *testing.T) {
61         peerd := newPeer(true, "foo", Dummy{nil}, conf, new([SSize]byte))
62         f := func(payload []byte) bool {
63                 if len(payload) == 0 {
64                         return true
65                 }
66                 peer.EthProcess(payload)
67                 return peerd.PktProcess(ciphertext, Dummy{nil}, true)
68         }
69         if err := quick.Check(f, nil); err != nil {
70                 t.Error(err)
71         }
72 }
73
74 func TestSymmetricNoise(t *testing.T) {
75         peerd := newPeer(true, "foo", Dummy{nil}, conf, new([SSize]byte))
76         peer.NoiseEnable = true
77         peerd.NoiseEnable = true
78         f := func(payload []byte) bool {
79                 if len(payload) == 0 {
80                         return true
81                 }
82                 peer.EthProcess(payload)
83                 return peerd.PktProcess(ciphertext, Dummy{nil}, true)
84         }
85         if err := quick.Check(f, nil); err != nil {
86                 t.Error(err)
87         }
88         peer.NoiseEnable = true
89 }
90
91 func TestSymmetricEncLess(t *testing.T) {
92         peerd := newPeer(true, "foo", Dummy{nil}, conf, new([SSize]byte))
93         peer.EncLess = true
94         peer.NoiseEnable = true
95         peerd.EncLess = true
96         peerd.NoiseEnable = true
97         f := func(payload []byte) bool {
98                 if len(payload) == 0 {
99                         return true
100                 }
101                 peer.EthProcess(payload)
102                 return peerd.PktProcess(ciphertext, Dummy{nil}, true)
103         }
104         if err := quick.Check(f, nil); err != nil {
105                 t.Error(err)
106         }
107         peer.NoiseEnable = false
108         peer.EncLess = false
109 }
110
111 func BenchmarkEnc(b *testing.B) {
112         b.ResetTimer()
113         for i := 0; i < b.N; i++ {
114                 peer.EthProcess(plaintext)
115         }
116 }
117
118 func BenchmarkDec(b *testing.B) {
119         peer = newPeer(true, "foo", Dummy{&ciphertext}, conf, new([SSize]byte))
120         peer.EthProcess(plaintext)
121         peer = newPeer(true, "foo", Dummy{nil}, conf, new([SSize]byte))
122         orig := make([]byte, len(ciphertext))
123         copy(orig, ciphertext)
124         b.ResetTimer()
125         for i := 0; i < b.N; i++ {
126                 peer.nonceBucket0 = make(map[uint64]struct{}, 1)
127                 peer.nonceBucket1 = make(map[uint64]struct{}, 1)
128                 copy(ciphertext, orig)
129                 if !peer.PktProcess(ciphertext, Dummy{nil}, true) {
130                         b.Fail()
131                 }
132         }
133 }