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