]> Cypherpunks.ru repositories - govpn.git/blob - encless_test.go
Raise copyright years
[govpn.git] / encless_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         "bytes"
22         "encoding/binary"
23         "io"
24         "testing"
25         "testing/quick"
26 )
27
28 var (
29         testKey = new([32]byte)
30 )
31
32 func init() {
33         io.ReadFull(Rand, testKey[:])
34 }
35
36 func TestEnclessSymmetric(t *testing.T) {
37         nonce := new([16]byte)
38         f := func(pktNum uint64, in []byte) bool {
39                 binary.BigEndian.PutUint64(nonce[8:], pktNum)
40                 encoded, err := EnclessEncode(testKey, nonce, in)
41                 if err != nil {
42                         return false
43                 }
44                 decoded, err := EnclessDecode(testKey, nonce, encoded)
45                 if err != nil {
46                         return false
47                 }
48                 return bytes.Compare(decoded, in) == 0
49         }
50         if err := quick.Check(f, nil); err != nil {
51                 t.Error(err)
52         }
53 }
54
55 func BenchmarkEnclessEncode(b *testing.B) {
56         nonce := new([16]byte)
57         data := make([]byte, 128)
58         io.ReadFull(Rand, nonce[8:])
59         io.ReadFull(Rand, data)
60         b.ResetTimer()
61         for i := 0; i < b.N; i++ {
62                 EnclessEncode(testKey, nonce, data)
63         }
64 }
65
66 func BenchmarkEnclessDecode(b *testing.B) {
67         nonce := new([16]byte)
68         data := make([]byte, 128)
69         io.ReadFull(Rand, nonce[8:])
70         io.ReadFull(Rand, data)
71         encoded, _ := EnclessEncode(testKey, nonce, data)
72         b.ResetTimer()
73         for i := 0; i < b.N; i++ {
74                 EnclessDecode(testKey, nonce, encoded)
75         }
76 }