]> Cypherpunks.ru repositories - nncp.git/blob - src/mth_test.go
Note about buildability with 1.22
[nncp.git] / src / mth_test.go
1 // NNCP -- Node to Node copy, utilities for store-and-forward data exchange
2 // Copyright (C) 2016-2024 Sergey Matveev <stargrave@stargrave.org>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, version 3 of the License.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 package nncp
17
18 import (
19         "bytes"
20         "io"
21         "testing"
22         "testing/quick"
23
24         "lukechampine.com/blake3"
25 )
26
27 func TestMTHSeqSymmetric(t *testing.T) {
28         xof := blake3.New(32, nil).XOF()
29         f := func(size uint32, offset uint32) bool {
30                 size %= 2 * 1024 * 1024
31                 data := make([]byte, int(size), int(size)+1)
32                 if _, err := io.ReadFull(xof, data); err != nil {
33                         panic(err)
34                 }
35                 offset = offset % size
36
37                 mth := MTHSeqNew(int64(size), 0)
38                 if _, err := io.Copy(mth, bytes.NewReader(data)); err != nil {
39                         panic(err)
40                 }
41                 hsh0 := mth.Sum(nil)
42
43                 mth = MTHSeqNew(int64(size), int64(offset))
44                 if _, err := io.Copy(mth, bytes.NewReader(data[int(offset):])); err != nil {
45                         panic(err)
46                 }
47                 if _, err := mth.PreaddFrom(bytes.NewReader(data), "", false); err != nil {
48                         panic(err)
49                 }
50                 if !bytes.Equal(hsh0, mth.Sum(nil)) {
51                         return false
52                 }
53
54                 mth = MTHSeqNew(0, 0)
55                 mth.Write(data)
56                 if !bytes.Equal(hsh0, mth.Sum(nil)) {
57                         return false
58                 }
59
60                 data = append(data, 0)
61                 mth = MTHSeqNew(int64(size)+1, 0)
62                 if _, err := io.Copy(mth, bytes.NewReader(data)); err != nil {
63                         panic(err)
64                 }
65                 hsh00 := mth.Sum(nil)
66                 if bytes.Equal(hsh0, hsh00) {
67                         return false
68                 }
69
70                 mth = MTHSeqNew(int64(size)+1, int64(offset))
71                 if _, err := io.Copy(mth, bytes.NewReader(data[int(offset):])); err != nil {
72                         panic(err)
73                 }
74                 if _, err := mth.PreaddFrom(bytes.NewReader(data), "", false); err != nil {
75                         panic(err)
76                 }
77                 if !bytes.Equal(hsh00, mth.Sum(nil)) {
78                         return false
79                 }
80
81                 mth = MTHSeqNew(0, 0)
82                 mth.Write(data)
83                 return bytes.Equal(hsh00, mth.Sum(nil))
84         }
85         if err := quick.Check(f, nil); err != nil {
86                 t.Error(err)
87         }
88 }
89
90 func TestMTHSeqAndFatEqual(t *testing.T) {
91         xof := blake3.New(32, nil).XOF()
92         f := func(size uint32, offset uint32) bool {
93                 size %= 10 * 1024 * 1024
94                 data := make([]byte, int(size), int(size)+1)
95                 if _, err := io.ReadFull(xof, data); err != nil {
96                         panic(err)
97                 }
98                 fat := MTHFatNew()
99                 if _, err := io.Copy(fat, bytes.NewReader(data)); err != nil {
100                         panic(err)
101                 }
102                 hshFat := fat.Sum(nil)
103                 seq := MTHSeqNew(int64(size), 0)
104                 if _, err := io.Copy(seq, bytes.NewReader(data)); err != nil {
105                         panic(err)
106                 }
107                 return bytes.Equal(hshFat, seq.Sum(nil))
108         }
109         if err := quick.Check(f, nil); err != nil {
110                 t.Error(err)
111         }
112 }
113
114 func TestMTHNull(t *testing.T) {
115         fat := MTHFatNew()
116         if _, err := fat.Write(nil); err != nil {
117                 t.Error(err)
118         }
119         hshFat := fat.Sum(nil)
120
121         seq := MTHSeqNew(0, 0)
122         if _, err := seq.Write(nil); err != nil {
123                 t.Error(err)
124         }
125         if !bytes.Equal(hshFat, seq.Sum(nil)) {
126                 t.FailNow()
127         }
128 }