]> Cypherpunks.ru repositories - govpn.git/blob - src/cypherpunks.ru/govpn/aont/aont_test.go
go vet/lint
[govpn.git] / src / cypherpunks.ru / govpn / aont / aont_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 aont
20
21 import (
22         "bytes"
23         "crypto/rand"
24         "io"
25         "testing"
26         "testing/quick"
27 )
28
29 var (
30         testKey = new([16]byte)
31 )
32
33 func init() {
34         io.ReadFull(rand.Reader, testKey[:])
35 }
36
37 func TestSymmetric(t *testing.T) {
38         f := func(data []byte) bool {
39                 encoded, err := Encode(testKey, data)
40                 if err != nil {
41                         return false
42                 }
43                 if len(encoded) != len(data)+16+32 {
44                         return false
45                 }
46                 decoded, err := Decode(encoded)
47                 if err != nil {
48                         return false
49                 }
50                 return bytes.Compare(decoded, data) == 0
51         }
52         if err := quick.Check(f, nil); err != nil {
53                 t.Error(err)
54         }
55 }
56
57 func TestSmallSize(t *testing.T) {
58         _, err := Decode([]byte("foobar"))
59         if err == nil {
60                 t.Fail()
61         }
62 }
63
64 func TestTampered(t *testing.T) {
65         f := func(data []byte, index int) bool {
66                 if len(data) == 0 {
67                         return true
68                 }
69                 encoded, _ := Encode(testKey, data)
70                 encoded[len(data)%index] ^= byte('a')
71                 _, err := Decode(encoded)
72                 if err == nil {
73                         return false
74                 }
75                 return true
76         }
77         if err := quick.Check(f, nil); err != nil {
78                 t.Error(err)
79         }
80 }
81
82 func BenchmarkEncode(b *testing.B) {
83         data := make([]byte, 128)
84         io.ReadFull(rand.Reader, data)
85         b.ResetTimer()
86         for i := 0; i < b.N; i++ {
87                 Encode(testKey, data)
88         }
89 }
90
91 func BenchmarkDecode(b *testing.B) {
92         data := make([]byte, 128)
93         io.ReadFull(rand.Reader, data)
94         encoded, _ := Encode(testKey, data)
95         b.ResetTimer()
96         for i := 0; i < b.N; i++ {
97                 Decode(encoded)
98         }
99 }