]> Cypherpunks.ru repositories - govpn.git/blob - src/govpn/peer_test.go
Initial encryptionless mode support
[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 TestSymmetricEncLess(t *testing.T) {
75         peerd := newPeer(true, "foo", Dummy{nil}, conf, new([SSize]byte))
76         peer.NoiseEnable = true
77         peer.EncLess = true
78         peerd.EncLess = true
79         peerd.NoiseEnable = true
80         f := func(payload []byte) bool {
81                 if len(payload) == 0 {
82                         return true
83                 }
84                 peer.EthProcess(payload)
85                 return peerd.PktProcess(ciphertext, Dummy{nil}, true)
86         }
87         if err := quick.Check(f, nil); err != nil {
88                 t.Error(err)
89         }
90         peer.NoiseEnable = false
91         peer.EncLess = false
92 }
93
94 func BenchmarkEnc(b *testing.B) {
95         b.ResetTimer()
96         for i := 0; i < b.N; i++ {
97                 peer.EthProcess(plaintext)
98         }
99 }
100
101 func BenchmarkDec(b *testing.B) {
102         peer = newPeer(true, "foo", Dummy{&ciphertext}, conf, new([SSize]byte))
103         peer.EthProcess(plaintext)
104         peer = newPeer(true, "foo", Dummy{nil}, conf, new([SSize]byte))
105         orig := make([]byte, len(ciphertext))
106         copy(orig, ciphertext)
107         b.ResetTimer()
108         for i := 0; i < b.N; i++ {
109                 peer.nonceBucket0 = make(map[uint64]struct{}, 1)
110                 peer.nonceBucket1 = make(map[uint64]struct{}, 1)
111                 copy(ciphertext, orig)
112                 if !peer.PktProcess(ciphertext, Dummy{nil}, true) {
113                         b.Fail()
114                 }
115         }
116 }