22
33import static org .junit .Assert .assertArrayEquals ;
44import static org .junit .Assert .assertEquals ;
5+ import static org .junit .Assert .assertFalse ;
56import static org .junit .Assert .assertNotEquals ;
7+ import static org .junit .Assert .assertSame ;
68import static org .junit .Assert .assertTrue ;
79
810import com .google .protobuf .ByteString ;
@@ -67,7 +69,7 @@ public void blockCapsuleSanitizeStripsBlockLevelUnknownFields() {
6769 BlockCapsule capsule = new BlockCapsule (padded );
6870 long originalSize = capsule .getData ().length ;
6971
70- capsule .sanitize ();
72+ assertTrue ( "sanitize() should report it mutated the capsule" , capsule .sanitize () );
7173
7274 assertTrue ("Block-level unknown fields should be stripped" ,
7375 capsule .getInstance ().getUnknownFields ().asMap ().isEmpty ());
@@ -85,7 +87,7 @@ public void blockCapsuleSanitizeStripsBlockHeaderOuterUnknownFields() {
8587 BlockCapsule capsule = new BlockCapsule (padded );
8688 long originalSize = capsule .getData ().length ;
8789
88- capsule .sanitize ();
90+ assertTrue ( "sanitize() should report it mutated the capsule" , capsule .sanitize () );
8991
9092 assertTrue ("BlockHeader outer unknown fields should be stripped" ,
9193 capsule .getInstance ().getBlockHeader ().getUnknownFields ().asMap ().isEmpty ());
@@ -109,11 +111,14 @@ public void blockCapsuleSanitizePreservesBlockHeaderRawData() {
109111 public void blockCapsuleSanitizeIsNoOpOnCleanBlock () {
110112 Block clean = sampleBlock ();
111113 BlockCapsule capsule = new BlockCapsule (clean );
112- byte [] before = capsule .getData ();
114+ Block beforeInstance = capsule .getInstance ();
115+ byte [] beforeData = capsule .getData ();
113116
114- capsule .sanitize ();
117+ assertFalse ( "sanitize() should report no-op on a clean block" , capsule .sanitize () );
115118
116- assertArrayEquals ("Clean block should pass through unchanged" , before , capsule .getData ());
119+ assertSame ("Underlying Block reference should not be rebuilt" ,
120+ beforeInstance , capsule .getInstance ());
121+ assertArrayEquals ("Clean block should pass through unchanged" , beforeData , capsule .getData ());
117122 }
118123
119124 // ---- TransactionCapsule.sanitize ----
@@ -124,7 +129,7 @@ public void transactionCapsuleSanitizeStripsTopLevelUnknownFields() {
124129 TransactionCapsule capsule = new TransactionCapsule (padded );
125130 long originalSize = capsule .getData ().length ;
126131
127- capsule .sanitize ();
132+ assertTrue ( "sanitize() should report it mutated the capsule" , capsule .sanitize () );
128133
129134 assertTrue ("Transaction-level unknown fields should be stripped" ,
130135 capsule .getInstance ().getUnknownFields ().asMap ().isEmpty ());
@@ -149,11 +154,14 @@ public void transactionCapsuleSanitizePreservesTransactionId() {
149154 public void transactionCapsuleSanitizeIsNoOpOnCleanTransaction () {
150155 Transaction clean = sampleTransaction ();
151156 TransactionCapsule capsule = new TransactionCapsule (clean );
152- byte [] before = capsule .getData ();
157+ Transaction beforeInstance = capsule .getInstance ();
158+ byte [] beforeData = capsule .getData ();
153159
154- capsule .sanitize ();
160+ assertFalse ( "sanitize() should report no-op on a clean transaction" , capsule .sanitize () );
155161
156- assertArrayEquals (before , capsule .getData ());
162+ assertSame ("Underlying Transaction reference should not be rebuilt" ,
163+ beforeInstance , capsule .getInstance ());
164+ assertArrayEquals (beforeData , capsule .getData ());
157165 }
158166
159167 // ---- BlockMessage.sanitize ----
@@ -177,4 +185,16 @@ public void blockMessageSanitizeUpdatesBothCapsuleAndWireBytes() throws Exceptio
177185 assertNotEquals ("msg.data should no longer match the padded wire bytes" ,
178186 paddedBytes .length , msg .getData ().length );
179187 }
188+
189+ @ Test
190+ public void blockMessageSanitizeSkipsDataRewriteOnCleanBlock () throws Exception {
191+ byte [] cleanBytes = sampleBlock ().toByteArray ();
192+ BlockMessage msg = new BlockMessage (cleanBytes );
193+ byte [] before = msg .getData ();
194+
195+ msg .sanitize ();
196+
197+ assertSame ("msg.data should not be rewritten on the no-op path" ,
198+ before , msg .getData ());
199+ }
180200}
0 commit comments