]> Cypherpunks.ru repositories - nncp.git/blob - src/mth_test.go
Raise copyright years
[nncp.git] / src / mth_test.go
1 /*
2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2022 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 TestMTHSeqSymmetric(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 := MTHSeqNew(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 = MTHSeqNew(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.PreaddFrom(bytes.NewReader(data), "", false); err != nil {
50                         panic(err)
51                 }
52                 if bytes.Compare(hsh0, mth.Sum(nil)) != 0 {
53                         return false
54                 }
55
56                 mth = MTHSeqNew(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 = MTHSeqNew(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 = MTHSeqNew(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.PreaddFrom(bytes.NewReader(data), "", false); err != nil {
77                         panic(err)
78                 }
79                 if bytes.Compare(hsh00, mth.Sum(nil)) != 0 {
80                         return false
81                 }
82
83                 mth = MTHSeqNew(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 TestMTHSeqAndFatEqual(t *testing.T) {
97         xof := blake3.New(32, nil).XOF()
98         f := func(size uint32, offset uint32) bool {
99                 size %= 10 * 1024 * 1024
100                 data := make([]byte, int(size), int(size)+1)
101                 if _, err := io.ReadFull(xof, data); err != nil {
102                         panic(err)
103                 }
104                 fat := MTHFatNew()
105                 if _, err := io.Copy(fat, bytes.NewReader(data)); err != nil {
106                         panic(err)
107                 }
108                 hshFat := fat.Sum(nil)
109                 seq := MTHSeqNew(int64(size), 0)
110                 if _, err := io.Copy(seq, bytes.NewReader(data)); err != nil {
111                         panic(err)
112                 }
113                 return bytes.Compare(hshFat, seq.Sum(nil)) == 0
114         }
115         if err := quick.Check(f, nil); err != nil {
116                 t.Error(err)
117         }
118 }
119
120 func TestMTHNull(t *testing.T) {
121         fat := MTHFatNew()
122         if _, err := fat.Write(nil); err != nil {
123                 t.Error(err)
124         }
125         hshFat := fat.Sum(nil)
126
127         seq := MTHSeqNew(0, 0)
128         if _, err := seq.Write(nil); err != nil {
129                 t.Error(err)
130         }
131         if bytes.Compare(hshFat, seq.Sum(nil)) != 0 {
132                 t.FailNow()
133         }
134 }