private static void threadProcHub(object testData_) { var _log = NLog.LogManager.GetLogger("HubThread"); var testData = (TestData)testData_; _log.Info("Creating keystore for Hub..."); var hubKeystore = new MemoryKeyStore(RSA.Default); hubKeystore.Load(testData.HubKeys); _log.Info("Waiting for node A to fail the connection..."); Thread.Sleep(4000); _log.Info("Creating Hub..."); var hub = new Nemesis.NemesisHub( new IPEndPoint(testData.Host, testData.Ports[0]), new IPEndPoint(testData.Host, testData.Ports[1]) ); hub.CommandReceived += Hub_CommandReceived; _log.Info("Enable encryption for nodes A and B..."); //hub.EnableEncryption(hubKeystore); hub.AllowUnknownGuid = true; var nodeAKeyStore = new MemoryKeyStore(RSA.Default); nodeAKeyStore.Load(testData.NodeAKeys); hub.NodesPublicKeys.AddOrUpdate(testData.NodeAId, nodeAKeyStore.PublicKey, (a, b) => { return(nodeAKeyStore.PublicKey); }); var nodeBKeyStore = new MemoryKeyStore(RSA.Default); nodeBKeyStore.Load(testData.NodeBKeys); hub.NodesPublicKeys.AddOrUpdate(testData.NodeBId, nodeBKeyStore.PublicKey, (a, b) => { return(nodeBKeyStore.PublicKey); }); _log.Info("Waiting for connections to establish..."); var cmdTest = "test"; _log.Info($"Sending command \"{cmdTest}\" (1/2) to Node A ({testData.NodeAId})"); Task.Run(async() => { var response2 = await hub.SendCommand(cmdTest, testData.NodeAId); _log.Info("Got response: {0}", response2); }); _log.Info($"Sending command \"{cmdTest}\" (2/2) to Node A ({testData.NodeAId})"); Task.Run(async() => { var response2 = await hub.SendCommand(cmdTest, testData.NodeAId); _log.Info("Got response: {0}", response2); }); }
private static void threadProcNodeA(object testData_) { var testData = (TestData)testData_; var _log = NLog.LogManager.GetLogger("NodeAThread"); _log.Info("Creating node A..."); var keyStore = new MemoryKeyStore(RSA.Default); keyStore.Load(testData.NodeAKeys); var nodeA = new Nemesis.NemesisNode(testData.NodeAId, testData.Ports, testData.Host.ToString(), false); nodeA.SetLogName("nNodeA"); nodeA.CommandReceived += NodeA_CommandReceived; var hubKeyStore = new MemoryKeyStore(RSA.Default); hubKeyStore.Load(testData.HubKeys); nodeA.HubPublicKey = hubKeyStore.PublicKey; nodeA.EnableEncryption(keyStore); nodeA.Connect(); var cmdTest = "n2h"; _log.Info($"Sending command \"{cmdTest}\" (1/2) from Node A to Hub"); var response = nodeA.SendCommand(cmdTest); _log.Info("Got response: {0}", response.Result); _log.Info($"Sending command \"{cmdTest}\" (2/2) from Node A to Hub"); response = nodeA.SendCommand(cmdTest); _log.Info("Got response: {0}", response.Result); }
public void SimpleNodeAndHub() { var answerTimeout = TimeSpan.FromSeconds(10); var NodeMessageReceived = new TaskCompletionSource <string>(); var testData = Shared.GetTestSettings(); var nodeAkeyStore = new MemoryKeyStore(); nodeAkeyStore.Load(testData.NodeAKeys); var hubKeystore = new MemoryKeyStore(); hubKeystore.Load(testData.HubKeys); var hubThread = new Thread(() => { var hub = new NemesisHub( new IPEndPoint(testData.Host, testData.Ports[0]), new IPEndPoint(testData.Host, testData.Ports[1]) ); hub.EnableEncryption(hubKeystore); hub.NodesPublicKeys.AddOrUpdate(testData.NodeAId, nodeAkeyStore.PublicKey, (a, b) => { return(nodeAkeyStore.PublicKey); }); hub.CommandReceived += (sender, e) => { Trace.WriteLine("Hub command received: " + e.Command); e.ResultSource.SetResult("ok"); }; var nodeResponse = hub.SendCommand("h2n", testData.NodeAId); NodeMessageReceived.SetResult(nodeResponse.Result); }); hubThread.Start(); var nodeA = new NemesisNode(testData.NodeAId, testData.Ports, testData.Host.ToString(), false); nodeA.HubPublicKey = hubKeystore.PublicKey; nodeA.EnableEncryption(nodeAkeyStore); nodeA.CommandReceived += (sender, e) => { Trace.WriteLine("nodeA command received: " + e.Command); e.ResultSource.SetResult("ok"); }; nodeA.Connect(); var hubResponse = nodeA.SendCommand("n2h"); hubResponse.Wait(answerTimeout); var hubResult = hubResponse.IsCompleted ? hubResponse.Result : "timeout"; Assert.AreEqual("ok", hubResult, "Hub -> Node command error"); NodeMessageReceived.Task.Wait(answerTimeout); var nodeResult = NodeMessageReceived.Task.IsCompleted ? NodeMessageReceived.Task.Result : "timeout"; Assert.AreEqual("ok", nodeResult, "Node -> Hub command error"); }
private static void threadProcNodeB(object testData_) { var testData = (TestData)testData_; var _log = NLog.LogManager.GetLogger("NodeBThread"); _log.Info("Creating node B..."); var ke = new RSA(); var badKeyStore = new MemoryKeyStore(ke); badKeyStore.Load(testData.NodeAKeys); // Note: The WRONG keystore var nodeBKeyStore = new MemoryKeyStore(ke); nodeBKeyStore.Load(testData.NodeAKeys); var nodeB = new Nemesis.NemesisNode(testData.NodeBId, testData.Ports, testData.Host.ToString(), false); nodeB.SetLogName("nNodeB"); nodeB.CommandReceived += NodeB_CommandReceived; //var hubKeyStore = new MemoryKeyStore(); //hubKeyStore.Load(testData.HubKeys); //nodeB.HubPublicKey = hubKeyStore.PublicKey; nodeB.HubPublicKey = badKeyStore.PublicKey; // Note: Again; the wrong key nodeB.EnableEncryption(badKeyStore); nodeB.Connect(); var cmdTest = "n2h"; _log.Info($"Sending command \"{cmdTest}\" with WRONG ENCODING from Node B to Hub"); try { var response = nodeB.SendCommand(cmdTest); var r = response.Result; // Note: Will fail } catch (Exception x) { _log.Warn("Oh no! " + x.Message); } nodeB.EnableEncryption(nodeBKeyStore); _log.Info($"Sending command \"{cmdTest}\" with the RIGHT ENCODING from Node B to Hub"); try { var response = nodeB.SendCommand(cmdTest); _log.Info("Got result: " + response.Result); // Note: Will fail } catch (Exception x) { _log.Warn("Oh no! " + x.Message); } //var hubKeyStore = new MemoryKeyStore(); //hubKeyStore.Load(testData.HubKeys); //nodeB.HubPublicKey = hubKeyStore.PublicKey; }