forked from bitcoinj/bitcoinj
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
StoredBlockTest: add tests for serializeCompact(), deserializeCompact()
- Loading branch information
1 parent
c12fa3b
commit 6281b3f
Showing
1 changed file
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright by the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.bitcoinj.core; | ||
|
||
import org.junit.Test; | ||
|
||
import java.math.BigInteger; | ||
import java.nio.ByteBuffer; | ||
import java.time.Instant; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class StoredBlockTest { | ||
|
||
@Test | ||
public void roundtripSerializeCompact_zeroChainWork() { | ||
roundtripSerializeCompact(BigInteger.ZERO); | ||
} | ||
|
||
@Test | ||
public void roundtripSerializeCompact_largeChainWork() { | ||
roundtripSerializeCompact(BigInteger.valueOf(Long.MAX_VALUE)); | ||
} | ||
|
||
@Test | ||
public void roundtripSerializeCompact_veryLargeChainWork() { | ||
BigInteger chainWork = new BigInteger(/* 12 bytes */ "ffffffffffffffffffffffff", 16); | ||
roundtripSerializeCompact(chainWork); | ||
} | ||
|
||
@Test(expected = RuntimeException.class) | ||
public void roundtripSerializeCompact_tooLargeChainWork_shouldFail() { | ||
BigInteger chainWork = new BigInteger(/* 13 bytes */ "ffffffffffffffffffffffffff", 16); | ||
roundtripSerializeCompact(chainWork); | ||
} | ||
|
||
@Test(expected = RuntimeException.class) | ||
public void roundtripSerializeCompact_negativeChainWork_shouldFail() { | ||
roundtripSerializeCompact(BigInteger.valueOf(-1)); | ||
} | ||
|
||
// Just a block | ||
private static final Block BLOCK = Block.createGenesis(Instant.now(), Block.EASIEST_DIFFICULTY_TARGET); | ||
|
||
private void roundtripSerializeCompact(BigInteger chainWork) { | ||
StoredBlock block = new StoredBlock(BLOCK, chainWork, 0); | ||
ByteBuffer buf = ByteBuffer.allocate(StoredBlock.COMPACT_SERIALIZED_SIZE); | ||
block.serializeCompact(buf); | ||
buf.rewind(); | ||
assertEquals(StoredBlock.deserializeCompact(buf), block); | ||
} | ||
} |