]> Cypherpunks.ru repositories - nncp.git/blob - src/mth_test.go
MTH
[nncp.git] / src / mth_test.go
1 /*
2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2021 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 nncp
19
20 import (
21         "bytes"
22         "io"
23         "testing"
24         "testing/quick"
25
26         "lukechampine.com/blake3"
27 )
28
29 func TestMTHSymmetric(t *testing.T) {
30         xof := blake3.New(32, nil).XOF()
31         f := func(size uint32, offset uint32) bool {
32                 size %= 2 * 1024 * 1024
33                 data := make([]byte, int(size), int(size)+1)
34                 if _, err := io.ReadFull(xof, data); err != nil {
35                         panic(err)
36                 }
37                 offset = offset % size
38
39                 mth := MTHNew(int64(size), 0)
40                 if _, err := io.Copy(mth, bytes.NewReader(data)); err != nil {
41                         panic(err)
42                 }
43                 hsh0 := mth.Sum(nil)
44
45                 mth = MTHNew(int64(size), int64(offset))
46                 if _, err := io.Copy(mth, bytes.NewReader(data[int(offset):])); err != nil {
47                         panic(err)
48                 }
49                 if _, err := mth.PrependFrom(bytes.NewReader(data)); err != nil {
50                         panic(err)
51                 }
52                 if bytes.Compare(hsh0, mth.Sum(nil)) != 0 {
53                         return false
54                 }
55
56                 mth = MTHNew(0, 0)
57                 mth.Write(data)
58                 if bytes.Compare(hsh0, mth.Sum(nil)) != 0 {
59                         return false
60                 }
61
62                 data = append(data, 0)
63                 mth = MTHNew(int64(size)+1, 0)
64                 if _, err := io.Copy(mth, bytes.NewReader(data)); err != nil {
65                         panic(err)
66                 }
67                 hsh00 := mth.Sum(nil)
68                 if bytes.Compare(hsh0, hsh00) == 0 {
69                         return false
70                 }
71
72                 mth = MTHNew(int64(size)+1, int64(offset))
73                 if _, err := io.Copy(mth, bytes.NewReader(data[int(offset):])); err != nil {
74                         panic(err)
75                 }
76                 if _, err := mth.PrependFrom(bytes.NewReader(data)); err != nil {
77                         panic(err)
78                 }
79                 if bytes.Compare(hsh00, mth.Sum(nil)) != 0 {
80                         return false
81                 }
82
83                 mth = MTHNew(0, 0)
84                 mth.Write(data)
85                 if bytes.Compare(hsh00, mth.Sum(nil)) != 0 {
86                         return false
87                 }
88
89                 return true
90         }
91         if err := quick.Check(f, nil); err != nil {
92                 t.Error(err)
93         }
94 }
95
96 func TestMTHNull(t *testing.T) {
97         mth := MTHNew(0, 0)
98         if _, err := mth.Write(nil); err != nil {
99                 t.Error(err)
100         }
101         mth.Sum(nil)
102 }