use of co.rsk.core.RskAddress in project rskj by rsksmart.
the class BridgeSerializationUtilsTest method mockAddress.
private RskAddress mockAddress(String addr) {
RskAddress mock = PowerMockito.mock(RskAddress.class);
Mockito.when(mock.getBytes()).thenReturn(Hex.decode(addr));
return mock;
}
use of co.rsk.core.RskAddress in project rskj by rsksmart.
the class BridgeStorageProviderTest method getNewFederation.
@Test
public void getNewFederation() throws IOException {
List<Integer> calls = new ArrayList<>();
Context contextMock = mock(Context.class);
Federation newFederation = new Federation(Arrays.asList(new BtcECKey[] { BtcECKey.fromPrivate(BigInteger.valueOf(100)) }), Instant.ofEpochMilli(1000), 0L, NetworkParameters.fromID(NetworkParameters.ID_REGTEST));
PowerMockito.mockStatic(BridgeSerializationUtils.class);
Repository repositoryMock = mock(Repository.class);
BridgeStorageProvider storageProvider = new BridgeStorageProvider(repositoryMock, mockAddress("aabbccdd"), config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
Whitebox.setInternalState(storageProvider, "btcContext", contextMock);
when(repositoryMock.getStorageBytes(any(RskAddress.class), any(DataWord.class))).then((InvocationOnMock invocation) -> {
calls.add(0);
RskAddress contractAddress = invocation.getArgumentAt(0, RskAddress.class);
DataWord address = invocation.getArgumentAt(1, DataWord.class);
// Make sure the bytes are get from the correct address in the repo
Assert.assertTrue(Arrays.equals(new byte[] { (byte) 0xaa, (byte) 0xbb, (byte) 0xcc, (byte) 0xdd }, contractAddress.getBytes()));
Assert.assertEquals(new DataWord("newFederation".getBytes(StandardCharsets.UTF_8)), address);
return new byte[] { (byte) 0xaa };
});
PowerMockito.when(BridgeSerializationUtils.deserializeFederation(any(byte[].class), any(Context.class))).then((InvocationOnMock invocation) -> {
calls.add(0);
byte[] data = invocation.getArgumentAt(0, byte[].class);
Context btcContext = invocation.getArgumentAt(1, Context.class);
// Make sure we're deserializing what just came from the repo with the correct BTC context
Assert.assertTrue(Arrays.equals(new byte[] { (byte) 0xaa }, data));
Assert.assertEquals(contextMock, btcContext);
return newFederation;
});
Assert.assertEquals(newFederation, storageProvider.getNewFederation());
Assert.assertEquals(newFederation, storageProvider.getNewFederation());
// 1 for each call to deserializeFederation & getStorageBytes
Assert.assertEquals(2, calls.size());
}
use of co.rsk.core.RskAddress in project rskj by rsksmart.
the class BridgeStorageProviderTest method getFederationElection_nonNullBytes.
@Test
public void getFederationElection_nonNullBytes() throws IOException {
List<Integer> calls = new ArrayList<>();
AddressBasedAuthorizer authorizerMock = mock(AddressBasedAuthorizer.class);
ABICallElection electionMock = mock(ABICallElection.class);
PowerMockito.mockStatic(BridgeSerializationUtils.class);
Repository repositoryMock = mock(Repository.class);
BridgeStorageProvider storageProvider = new BridgeStorageProvider(repositoryMock, mockAddress("aabbccdd"), config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
when(repositoryMock.getStorageBytes(any(RskAddress.class), any(DataWord.class))).then((InvocationOnMock invocation) -> {
calls.add(0);
RskAddress contractAddress = invocation.getArgumentAt(0, RskAddress.class);
DataWord address = invocation.getArgumentAt(1, DataWord.class);
// Make sure the bytes are got from the correct address in the repo
Assert.assertTrue(Arrays.equals(new byte[] { (byte) 0xaa, (byte) 0xbb, (byte) 0xcc, (byte) 0xdd }, contractAddress.getBytes()));
Assert.assertEquals(new DataWord("federationElection".getBytes(StandardCharsets.UTF_8)), address);
return new byte[] { (byte) 0xaa };
});
PowerMockito.when(BridgeSerializationUtils.deserializeElection(any(byte[].class), any(AddressBasedAuthorizer.class))).then((InvocationOnMock invocation) -> {
calls.add(0);
byte[] data = invocation.getArgumentAt(0, byte[].class);
AddressBasedAuthorizer authorizer = invocation.getArgumentAt(1, AddressBasedAuthorizer.class);
// Make sure we're deserializing what just came from the repo with the correct AddressBasedAuthorizer
Assert.assertTrue(Arrays.equals(new byte[] { (byte) 0xaa }, data));
Assert.assertEquals(authorizerMock, authorizer);
return electionMock;
});
Assert.assertSame(electionMock, storageProvider.getFederationElection(authorizerMock));
// 1 for each call to deserializeFederation & getStorageBytes
Assert.assertEquals(2, calls.size());
}
use of co.rsk.core.RskAddress in project rskj by rsksmart.
the class BridgeStorageProviderTest method saveNewFederation.
@Test
public void saveNewFederation() throws IOException {
Federation newFederation = new Federation(Arrays.asList(new BtcECKey[] { BtcECKey.fromPrivate(BigInteger.valueOf(100)) }), Instant.ofEpochMilli(1000), 0L, NetworkParameters.fromID(NetworkParameters.ID_REGTEST));
List<Integer> storageBytesCalls = new ArrayList<>();
List<Integer> serializeCalls = new ArrayList<>();
PowerMockito.mockStatic(BridgeSerializationUtils.class);
Repository repositoryMock = mock(Repository.class);
BridgeStorageProvider storageProvider = new BridgeStorageProvider(repositoryMock, mockAddress("aabbccdd"), config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
PowerMockito.when(BridgeSerializationUtils.serializeFederation(any(Federation.class))).then((InvocationOnMock invocation) -> {
Federation federation = invocation.getArgumentAt(0, Federation.class);
Assert.assertEquals(newFederation, federation);
serializeCalls.add(0);
return new byte[] { (byte) 0xbb };
});
Mockito.doAnswer((InvocationOnMock invocation) -> {
storageBytesCalls.add(0);
RskAddress contractAddress = invocation.getArgumentAt(0, RskAddress.class);
DataWord address = invocation.getArgumentAt(1, DataWord.class);
byte[] data = invocation.getArgumentAt(2, byte[].class);
// Make sure the bytes are set to the correct address in the repo and that what's saved is what was serialized
Assert.assertTrue(Arrays.equals(new byte[] { (byte) 0xaa, (byte) 0xbb, (byte) 0xcc, (byte) 0xdd }, contractAddress.getBytes()));
Assert.assertEquals(new DataWord("newFederation".getBytes(StandardCharsets.UTF_8)), address);
Assert.assertTrue(Arrays.equals(new byte[] { (byte) 0xbb }, data));
return null;
}).when(repositoryMock).addStorageBytes(any(RskAddress.class), any(DataWord.class), any(byte[].class));
storageProvider.saveNewFederation();
// Shouldn't have tried to save nor serialize anything
Assert.assertEquals(0, storageBytesCalls.size());
Assert.assertEquals(0, serializeCalls.size());
storageProvider.setNewFederation(newFederation);
storageProvider.saveNewFederation();
Assert.assertEquals(1, storageBytesCalls.size());
Assert.assertEquals(1, serializeCalls.size());
}
use of co.rsk.core.RskAddress in project rskj by rsksmart.
the class BridgeStorageProviderTest method mockAddress.
private RskAddress mockAddress(String addr) {
RskAddress mock = PowerMockito.mock(RskAddress.class);
when(mock.getBytes()).thenReturn(Hex.decode(addr));
return mock;
}
Aggregations