Skip to content

Commit

Permalink
Better integration tests as requested
Browse files Browse the repository at this point in the history
  • Loading branch information
josh-richardson committed Sep 4, 2019
1 parent 07d8d08 commit 3f5db33
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,154 +12,86 @@
*/
package net.consensys.orion.acceptance.send.receive.privacyGroup;

import static io.vertx.core.Vertx.vertx;
import static java.nio.charset.StandardCharsets.UTF_8;
import static net.consensys.cava.io.Base64.decodeBytes;
import static net.consensys.cava.io.file.Files.copyResource;
import static net.consensys.orion.acceptance.NodeUtils.createPrivacyGroupTransaction;
import static net.consensys.orion.acceptance.NodeUtils.joinPathsAsTomlListEntry;
import static net.consensys.orion.http.server.HttpContentType.CBOR;
import static org.awaitility.Awaitility.await;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import net.consensys.cava.crypto.sodium.Box;
import net.consensys.cava.junit.TempDirectory;
import net.consensys.cava.junit.TempDirectoryExtension;
import net.consensys.orion.acceptance.EthClientStub;
import net.consensys.orion.acceptance.NodeUtils;
import net.consensys.orion.cmd.Orion;
import net.consensys.orion.config.Config;
import net.consensys.orion.enclave.EncryptedPayload;
import net.consensys.orion.enclave.sodium.MemoryKeyStore;
import net.consensys.orion.enclave.sodium.SodiumEnclave;
import net.consensys.orion.http.handler.privacy.PrivacyGroup;
import net.consensys.orion.http.server.HttpContentType;
import net.consensys.orion.network.ConcurrentNetworkNodes;
import net.consensys.orion.utils.Serializer;

import java.nio.file.Path;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.concurrent.TimeUnit;

import io.vertx.core.Vertx;
import io.vertx.core.http.HttpClient;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.junit.jupiter.api.AfterEach;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

@ExtendWith(TempDirectoryExtension.class)
class PushTransactionToPrivacyGroupHistoryTest {

private static final String PK_1_B_64 = "A1aVtMxLCUHmBVHXoZzzBgPbW/wj5axDpW9X8l91SGo=";
private static final String PK_2_B_64 = "Ko2bVqD+nNlNYL5EE7y3IdOnviftjiizpjRt+HTuFBs=";
class PushTransactionToPrivacyGroupHistoryTest extends PrivacyGroupAcceptanceTestBase {

private Orion firstOrionLauncher;
private Orion secondOrionLauncher;
private Vertx vertx;
private HttpClient firstHttpClient;
private MemoryKeyStore memoryKeyStore;

@BeforeEach
void setUpDualNode(@TempDirectory final Path tempDir) throws Exception {
vertx = vertx();

Path key1pub = copyResource("key1.pub", tempDir.resolve("key1.pub"));
Path key1key = copyResource("key1.key", tempDir.resolve("key1.key"));
Path key2pub = copyResource("key2.pub", tempDir.resolve("key2.pub"));
Path key2key = copyResource("key2.key", tempDir.resolve("key2.key"));

String jdbcUrl = "jdbc:h2:" + tempDir.resolve("node2").toString();
try (Connection conn = DriverManager.getConnection(jdbcUrl)) {
Statement st = conn.createStatement();
st.executeUpdate("create table if not exists store(key char(60), value binary, primary key(key))");
}

Config firstNodeConfig = NodeUtils.nodeConfig(
tempDir,
0,
"127.0.0.1",
0,
"127.0.0.1",
"node1",
joinPathsAsTomlListEntry(key1pub),
joinPathsAsTomlListEntry(key1key),
"off",
"tofu",
"tofu",
"leveldb:database/node1");

Config secondNodeConfig = NodeUtils.nodeConfig(
tempDir,
0,
"127.0.0.1",
0,
"127.0.0.1",
"node2",
joinPathsAsTomlListEntry(key2pub),
joinPathsAsTomlListEntry(key2key),
"off",
"tofu",
"tofu",
"sql:" + jdbcUrl);

firstOrionLauncher = NodeUtils.startOrion(firstNodeConfig);
firstHttpClient = vertx.createHttpClient();
secondOrionLauncher = NodeUtils.startOrion(secondNodeConfig);

Box.PublicKey pk1 = Box.PublicKey.fromBytes(decodeBytes(PK_1_B_64));
Box.PublicKey pk2 = Box.PublicKey.fromBytes(decodeBytes(PK_2_B_64));

ConcurrentNetworkNodes networkNodes =
new ConcurrentNetworkNodes(NodeUtils.url("127.0.0.1", firstOrionLauncher.nodePort()));
networkNodes.addNode(pk1, NodeUtils.url("127.0.0.1", firstOrionLauncher.nodePort()));
networkNodes.addNode(pk2, NodeUtils.url("127.0.0.1", secondOrionLauncher.nodePort()));

RequestBody partyInfoBody =
RequestBody.create(MediaType.parse(CBOR.httpHeaderValue), Serializer.serialize(CBOR, networkNodes));
OkHttpClient httpClient = new OkHttpClient();

final String firstNodeBaseUrl = NodeUtils.urlString("127.0.0.1", firstOrionLauncher.nodePort());
Request request = new Request.Builder().post(partyInfoBody).url(firstNodeBaseUrl + "/partyinfo").build();
await().atMost(5, TimeUnit.SECONDS).until(() -> getPartyInfoResponse(httpClient, request).nodeURLs().size() == 2);
void setUpKeystore() throws Exception {

memoryKeyStore = new MemoryKeyStore();
}

private ConcurrentNetworkNodes getPartyInfoResponse(OkHttpClient httpClient, Request request) throws Exception {
Response resp = httpClient.newCall(request).execute();
assertEquals(200, resp.code());
return Serializer.deserialize(HttpContentType.CBOR, ConcurrentNetworkNodes.class, resp.body().bytes());
}
@Test
void receiverCanViewWhenSentToPrivacyGroup() {
final EthClientStub firstClient = NodeUtils.client(firstOrionLauncher.clientPort(), firstHttpClient);
final EthClientStub firstNode = NodeUtils.client(firstOrionLauncher.nodePort(), firstHttpClient);

String[] addresses = new String[] {PK_1_B_64, PK_2_B_64};
final PrivacyGroup privacyGroup =
createPrivacyGroupTransaction(firstClient, addresses, PK_1_B_64, "testName", "testDescription");

@AfterEach
void tearDown() {
firstOrionLauncher.stop();
secondOrionLauncher.stop();
vertx.close();
EncryptedPayload payload = mockPayload();
var pushResult = firstNode.push(payload).orElseThrow();
var result = firstClient.pushToHistory(privacyGroup.getPrivacyGroupId(), "0xnotahash", pushResult);
assertTrue(result.isPresent() && result.get());
}

@Test
void receiverCanViewWhenSentToPrivacyGroup() {
void receiverCanViewWhenSentToLargePrivacyGroup() {
final EthClientStub firstClient = NodeUtils.client(firstOrionLauncher.clientPort(), firstHttpClient);
final EthClientStub firstNode = NodeUtils.client(firstOrionLauncher.nodePort(), firstHttpClient);

String[] addresses = new String[] {PK_1_B_64, PK_2_B_64};
final EthClientStub secondClient = NodeUtils.client(firstOrionLauncher.clientPort(), firstHttpClient);
final EthClientStub secondNode = NodeUtils.client(firstOrionLauncher.nodePort(), firstHttpClient);

String[] addresses = new String[] {PK_1_B_64, PK_2_B_64, PK_3_B_64};
final PrivacyGroup privacyGroup =
createPrivacyGroupTransaction(firstClient, addresses, PK_1_B_64, "testName", "testDescription");

EncryptedPayload payload = mockPayload();
var pushResult = firstNode.push(payload).orElseThrow();
var secondPushResult = secondNode.push(payload).orElseThrow();
var result = firstClient.pushToHistory(privacyGroup.getPrivacyGroupId(), "0xnotahash", pushResult);
var secondResult = secondClient.pushToHistory(privacyGroup.getPrivacyGroupId(), "0xnotahash", secondPushResult);
assertTrue(result.isPresent() && result.get());
assertTrue(secondResult.isPresent() && secondResult.get());
}


@Test
void errorWhenIncorrectHistoryPushedToPrivacyGroup() {
final EthClientStub firstClient = NodeUtils.client(firstOrionLauncher.clientPort(), firstHttpClient);
final EthClientStub firstNode = NodeUtils.client(firstOrionLauncher.nodePort(), firstHttpClient);

String[] addresses = new String[] {PK_1_B_64, PK_2_B_64};
final PrivacyGroup privacyGroup =
createPrivacyGroupTransaction(firstClient, addresses, PK_1_B_64, "testName", "testDescription");

EncryptedPayload payload = mockPayload();
firstNode.push(payload).orElseThrow();

var pushResult = firstClient.pushToHistory(privacyGroup.getPrivacyGroupId(), "0xnotahash", "invalid payload");
assertFalse(pushResult.isPresent());
}

private EncryptedPayload mockPayload() {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/consensys/orion/cmd/Orion.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
import net.consensys.orion.http.handler.push.PushPrivacyGroupHandler;
import net.consensys.orion.http.handler.receive.ReceiveHandler;
import net.consensys.orion.http.handler.send.SendHandler;
import net.consensys.orion.http.handler.tx.PushToHistoryHandler;
import net.consensys.orion.http.handler.set.SetPrivacyGroupHandler;
import net.consensys.orion.http.handler.tx.PushToHistoryHandler;
import net.consensys.orion.http.handler.upcheck.UpcheckHandler;
import net.consensys.orion.http.server.vertx.HttpErrorHandler;
import net.consensys.orion.network.ConcurrentNetworkNodes;
Expand Down

0 comments on commit 3f5db33

Please sign in to comment.