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