19
19
import static com .exonum .binding .common .proofs .list .ListProofUtils .getBranchHashCode ;
20
20
import static com .exonum .binding .common .proofs .list .ListProofUtils .getNodeHashCode ;
21
21
import static com .exonum .binding .common .proofs .list .ListProofUtils .getProofListHash ;
22
- import static com .exonum .binding .common .proofs .list .ListProofUtils .leafOf ;
23
22
import static com .google .common .collect .ImmutableMap .of ;
24
23
import static org .hamcrest .CoreMatchers .equalTo ;
25
24
import static org .hamcrest .MatcherAssert .assertThat ;
26
25
import static org .junit .jupiter .api .Assertions .assertEquals ;
27
26
28
27
import com .exonum .binding .common .hash .HashCode ;
29
- import com .exonum . binding . common . serialization . StandardSerializers ;
28
+ import com .google . protobuf . ByteString ;
30
29
import org .junit .jupiter .api .Test ;
31
30
32
31
class ListProofHashCalculatorTest {
33
32
34
- private static final String V1 = "v1" ;
35
- private static final String V2 = "v2" ;
36
- private static final String V3 = "v3" ;
37
- private static final String V4 = "v4" ;
33
+ private static final ByteString V1 = ByteString . copyFromUtf8 ( "v1" ) ;
34
+ private static final ByteString V2 = ByteString . copyFromUtf8 ( "v2" ) ;
35
+ private static final ByteString V3 = ByteString . copyFromUtf8 ( "v3" ) ;
36
+ private static final ByteString V4 = ByteString . copyFromUtf8 ( "v4" ) ;
38
37
39
38
private static final HashCode H1 = HashCode .fromString ("a1" );
40
39
private static final HashCode H2 = HashCode .fromString ("a2" );
41
40
42
- private ListProofHashCalculator < String > calculator ;
41
+ private ListProofHashCalculator calculator ;
43
42
44
43
@ Test
45
44
void visit_SingletonListProof () {
46
- ListProofNode root = leafOf (V1 );
45
+ ListProofNode root = new ListProofElement (V1 );
47
46
48
- ListProof listProof = new ListProof ( root , 1 ) ;
49
- calculator = createListProofCalculator ( listProof );
47
+ long length = 1 ;
48
+ calculator = new ListProofHashCalculator ( root , length );
50
49
51
- HashCode expectedProofListHash = getProofListHash (getNodeHashCode (V1 ), listProof . getLength () );
50
+ HashCode expectedProofListHash = getProofListHash (getNodeHashCode (V1 ), length );
52
51
53
52
assertThat (calculator .getElements (), equalTo (of (0L , V1 )));
54
53
assertEquals (expectedProofListHash , calculator .getHash ());
55
54
}
56
55
57
56
@ Test
58
57
void visit_FullProof2elements () {
59
- ListProofElement left = leafOf (V1 );
60
- ListProofElement right = leafOf (V2 );
58
+ ListProofElement left = new ListProofElement (V1 );
59
+ ListProofElement right = new ListProofElement (V2 );
61
60
ListProofBranch root = new ListProofBranch (left , right );
62
61
63
- ListProof listProof = new ListProof ( root , 2 ) ;
64
- calculator = createListProofCalculator ( listProof );
62
+ long length = 2 ;
63
+ calculator = new ListProofHashCalculator ( root , length );
65
64
66
65
// Calculate expected proof list hash
67
66
HashCode expectedRootHash = getBranchHashCode (getNodeHashCode (V1 ), getNodeHashCode (V2 ));
68
- HashCode expectedProofListHash = getProofListHash (expectedRootHash , listProof . getLength () );
67
+ HashCode expectedProofListHash = getProofListHash (expectedRootHash , length );
69
68
70
69
assertThat (calculator .getElements (), equalTo (of (0L , V1 ,
71
70
1L , V2 )));
@@ -76,23 +75,23 @@ void visit_FullProof2elements() {
76
75
void visit_FullProof4elements () {
77
76
ListProofBranch root = new ListProofBranch (
78
77
new ListProofBranch (
79
- leafOf (V1 ),
80
- leafOf (V2 )
78
+ new ListProofElement (V1 ),
79
+ new ListProofElement (V2 )
81
80
),
82
81
new ListProofBranch (
83
- leafOf (V3 ),
84
- leafOf (V4 )
82
+ new ListProofElement (V3 ),
83
+ new ListProofElement (V4 )
85
84
)
86
85
);
87
86
88
- ListProof listProof = new ListProof ( root , 4 ) ;
89
- calculator = createListProofCalculator ( listProof );
87
+ long length = 4 ;
88
+ calculator = new ListProofHashCalculator ( root , length );
90
89
91
90
// Calculate expected proof list hash
92
91
HashCode leftBranchHash = getBranchHashCode (getNodeHashCode (V1 ), getNodeHashCode (V2 ));
93
92
HashCode rightBranchHash = getBranchHashCode (getNodeHashCode (V3 ), getNodeHashCode (V4 ));
94
93
HashCode expectedRootHash = getBranchHashCode (leftBranchHash , rightBranchHash );
95
- HashCode expectedProofListHash = getProofListHash (expectedRootHash , listProof . getLength () );
94
+ HashCode expectedProofListHash = getProofListHash (expectedRootHash , length );
96
95
97
96
98
97
assertThat (calculator .getElements (),
@@ -105,15 +104,15 @@ void visit_FullProof4elements() {
105
104
106
105
@ Test
107
106
void visit_ProofLeftValue () {
108
- ListProofNode left = leafOf (V1 );
107
+ ListProofNode left = new ListProofElement (V1 );
109
108
ListProofNode right = new ListProofHashNode (H2 );
110
109
ListProofBranch root = new ListProofBranch (left , right );
111
110
112
- ListProof listProof = new ListProof ( root , 2 ) ;
113
- calculator = createListProofCalculator ( listProof );
111
+ long length = 2 ;
112
+ calculator = new ListProofHashCalculator ( root , length );
114
113
115
114
HashCode expectedRootHash = getBranchHashCode (getNodeHashCode (V1 ), H2 );
116
- HashCode expectedProofListHash = getProofListHash (expectedRootHash , listProof . getLength () );
115
+ HashCode expectedProofListHash = getProofListHash (expectedRootHash , length );
117
116
118
117
assertThat (calculator .getElements (), equalTo (of (0L , V1 )));
119
118
assertEquals (expectedProofListHash , calculator .getHash ());
@@ -122,14 +121,14 @@ void visit_ProofLeftValue() {
122
121
@ Test
123
122
void visit_ProofRightValue () {
124
123
ListProofNode left = new ListProofHashNode (H1 );
125
- ListProofNode right = leafOf (V2 );
124
+ ListProofNode right = new ListProofElement (V2 );
126
125
ListProofBranch root = new ListProofBranch (left , right );
127
126
128
- ListProof listProof = new ListProof ( root , 2 ) ;
129
- calculator = createListProofCalculator ( listProof );
127
+ long length = 2 ;
128
+ calculator = new ListProofHashCalculator ( root , length );
130
129
131
130
HashCode expectedRootHash = getBranchHashCode (H1 , getNodeHashCode (V2 ));
132
- HashCode expectedProofListHash = getProofListHash (expectedRootHash , listProof . getLength () );
131
+ HashCode expectedProofListHash = getProofListHash (expectedRootHash , length );
133
132
134
133
assertThat (calculator .getElements (), equalTo (of (1L , V2 )));
135
134
assertEquals (expectedProofListHash , calculator .getHash ());
@@ -139,22 +138,22 @@ void visit_ProofRightValue() {
139
138
void visit_FullProof3elements () {
140
139
ListProofBranch root = new ListProofBranch (
141
140
new ListProofBranch (
142
- leafOf (V1 ),
143
- leafOf (V2 )
141
+ new ListProofElement (V1 ),
142
+ new ListProofElement (V2 )
144
143
),
145
144
new ListProofBranch (
146
- leafOf (V3 ),
145
+ new ListProofElement (V3 ),
147
146
null
148
147
)
149
148
);
150
149
151
- ListProof listProof = new ListProof ( root , 3 ) ;
152
- calculator = createListProofCalculator ( listProof );
150
+ long length = 3 ;
151
+ calculator = new ListProofHashCalculator ( root , length );
153
152
154
153
HashCode leftBranchHash = getBranchHashCode (getNodeHashCode (V1 ), getNodeHashCode (V2 ));
155
154
HashCode rightBranchHash = getBranchHashCode (getNodeHashCode (V3 ), null );
156
155
HashCode expectedRootHash = getBranchHashCode (leftBranchHash , rightBranchHash );
157
- HashCode expectedProofListHash = getProofListHash (expectedRootHash , listProof . getLength () );
156
+ HashCode expectedProofListHash = getProofListHash (expectedRootHash , length );
158
157
159
158
assertThat (calculator .getElements (),
160
159
equalTo (of (
@@ -164,8 +163,4 @@ void visit_FullProof3elements() {
164
163
);
165
164
assertEquals (expectedProofListHash , calculator .getHash ());
166
165
}
167
-
168
- private ListProofHashCalculator <String > createListProofCalculator (ListProof listProof ) {
169
- return new ListProofHashCalculator <>(listProof , StandardSerializers .string ());
170
- }
171
166
}
0 commit comments