public void TestBasicEncryption() { // tests key creation and exchange // - between 2 clients using (Reference <ILogger> loggerRef = Reference <ILogger> .Create(new TraceLogger(true))) { using (CoreServer server = new CoreServer(loggerRef, "UTT", NodeType.Router)) { server.Start(); using (ICoreClient sender = new CoreClientFactory(loggerRef).SetEnv("UTT").Create()) using (ICoreClient recver = new CoreClientFactory(loggerRef).SetEnv("UTT").Create()) { // check un-encrypted traffic Guid evtId1 = sender.SaveObject <TestData>(new TestData("1", 1), "test", null, TimeSpan.MaxValue); List <ICoreItem> recdList1 = recver.LoadItems <TestData>(Expr.ALL); Assert.AreEqual <int>(1, recdList1.Count); ICoreItem recdItem1 = recdList1[0]; Assert.AreEqual <Guid>(evtId1, recdItem1.Id); Assert.IsNull(recdItem1.TranspKeyId); Assert.IsNull(recdItem1.SenderKeyId); Assert.IsNull(recdItem1.RecverKeyId); // generate keys string senderKeyId = sender.CryptoManager.GenerateNewKeys(); string recverKeyId = recver.CryptoManager.GenerateNewKeys(); // send encrypted message and check receiver fails to decrypt ICoreItem sentItem2 = sender.MakeObject <TestData>(new TestData("2", 2), "test", null); sentItem2.TranspKeyId = senderKeyId; sentItem2.SenderKeyId = senderKeyId; Guid evtId2 = sender.SaveItem(sentItem2); List <ICoreItem> recdList2 = recver.LoadItems <TestData>(Expr.ALL); Assert.AreEqual <int>(1, recdList2.Count); ICoreItem recdItem2 = recdList2[0]; Assert.AreEqual <Guid>(evtId2, recdItem2.Id); Assert.AreEqual <Guid>(evtId2, recdItem2.Id); Assert.AreEqual <string>(senderKeyId, recdItem2.TranspKeyId); Assert.AreEqual <string>(senderKeyId, recdItem2.SenderKeyId); Assert.IsNull(recver.CryptoManager.GetTranspKey(senderKeyId)); // set public key at receiver and check authentication recver.CryptoManager.SetPublicKey(senderKeyId, sender.CryptoManager.GetPublicKey(senderKeyId)); Assert.IsTrue(recdItem2.IsSigned); Assert.IsFalse(recdItem2.IsSecret); // set transport key at receiver and recheck decryption recver.CryptoManager.SetTranspKey(senderKeyId, sender.CryptoManager.GetTranspKey(senderKeyId)); object data2 = recdItem2.Data; Assert.IsNotNull(data2); Assert.AreEqual <Type>(typeof(TestData), data2.GetType()); } server.Stop(); } } }
public void TestPagingTechniques() { const bool debugRequests = true; // loads/saves a very large number of objects const int maxLoopCount = 5; const int itemsPerLoop = 400; const int itemsPerPage = 154; const int totalItems = maxLoopCount * itemsPerLoop; using (Reference <ILogger> loggerRef = Reference <ILogger> .Create(new TraceLogger(true))) { NamedValueSet serverSettings = new NamedValueSet(); serverSettings.Set(CfgPropName.NodeType, (int)NodeType.Router); serverSettings.Set(CfgPropName.EnvName, "UTT"); using (CoreServer server = new CoreServer(loggerRef, serverSettings)) { server.Start(); // save using (ICoreClient client1 = new CoreClientFactory(loggerRef).SetEnv("UTT").Create()) { for (int loop = 0; loop < maxLoopCount; loop++) { ICoreItem[] items = new ICoreItem[itemsPerLoop]; for (int i = 0; i < itemsPerLoop; i++) { client1.DebugRequests = i == 0; int n = loop * itemsPerLoop + i; TestData data = new TestData(n.ToString(), n); NamedValueSet props = new NamedValueSet(new NamedValue("n", n)); items[i] = client1.MakeObject(data, "Test." + n, props); } client1.SaveItems(items); } } // load (using original paging technique) loggerRef.Target.LogDebug("---------- multi page load"); // - slow because it scans and sort the entire data set for each page using (ICoreClient client3 = new CoreClientFactory(loggerRef).SetEnv("UTT").Create()) { // count the items int itemCount1 = client3.CountObjects <TestData>(null); // now get the items in pages IExpression orderExpr = Expr.Prop("n"); client3.DebugRequests = debugRequests; int itemCount2 = 0; int startRow = 0; List <ICoreItem> page; do { page = client3.LoadItems <TestData>(null, orderExpr, startRow, itemsPerPage); itemCount2 += page.Count; startRow += page.Count; }while (page.Count == itemsPerPage); Assert.AreEqual(totalItems, itemCount1); Assert.AreEqual(totalItems, itemCount2); } // paged load (using pre-load of item header info) loggerRef.Target.LogDebug("---------- header pre-load"); // - faster because it only scans sorts the entire data set once and sorting // can use local compiled methods instead of server-side expression evaluation. using (ICoreClient client4 = new CoreClientFactory(loggerRef).SetEnv("UTT").Create()) { // get all the items (without data) List <ICoreItemInfo> itemInfoList = client4.LoadItemInfos <TestData>(null); int itemCount1 = itemInfoList.Count; // optionally sort the list here //IExpression orderExpr = Expr.Prop("n"); // now get the items in pages client4.DebugRequests = debugRequests; int itemCount2 = 0; int maxPages = ((itemCount1 / itemsPerPage) + 1); for (int pageNum = 0; pageNum < maxPages; pageNum++) { // get the item names/ids for the current page int startRow = itemsPerPage * pageNum; List <string> itemNames = new List <string>(); for (int i = 0; i < itemsPerPage; i++) { int n = startRow + i; if (n < itemCount1) { itemNames.Add(itemInfoList[n].Name); } } // now get the objects (with data) var page = client4.LoadItems <TestData>(itemNames); itemCount2 += page.Count; } Assert.AreEqual(totalItems, itemCount1); Assert.AreEqual(totalItems, itemCount2); } } } }
public void TestStressServerLoadAndRetrieve() { // return an increasing number of trades until failure Trade trade = XmlSerializerHelper.DeserializeFromString <Trade>( ResourceHelper.GetResourceWithPartialName(Assembly.GetExecutingAssembly(), "SampleSwap.xml")); NamedValueSet tradeProps = new NamedValueSet( ResourceHelper.GetResourceWithPartialName(Assembly.GetExecutingAssembly(), "SampleSwap.nvs")); using (Reference <ILogger> loggerRef = Reference <ILogger> .Create(new TraceLogger(true))) { NamedValueSet serverSettings = new NamedValueSet(); serverSettings.Set(CfgPropName.NodeType, (int)NodeType.Router); serverSettings.Set(CfgPropName.EnvName, "UTT"); using (CoreServer server = new CoreServer(loggerRef, serverSettings)) { server.Start(); using (ICoreClient client = new CoreClientFactory(loggerRef).SetEnv("UTT").SetServers("localhost:8113").Create()) //using (ICoreClient client = new CoreClientFactory(loggerRef).SetEnv(EnvId.DEV_Development).Create()) { // delete the test trades IExpression deleteExpr = Expr.StartsWith(Expr.SysPropItemName, "Test."); client.DeleteObjects <Trade>(deleteExpr); const int maxLoops = 1; const int loopRepeat = 1; const int incrementPerLoop = 1024; int itemsPerLoop = 8192 - 1024; int itemsSaved = 0; for (int loop = 0; loop < maxLoops; loop++) { for (int repeat = 0; repeat < loopRepeat; repeat++) { // create trades loggerRef.Target.LogDebug("[{0}-{1}] Making {2} trades...", loop, repeat, itemsPerLoop); List <IAsyncResult> completions = new List <IAsyncResult>(); ICoreItem[] items = new ICoreItem[itemsPerLoop]; for (int i = 0; i < itemsPerLoop; i++) { string tradeName = $"Test.{loop}.{i}"; ICoreItem item = client.MakeObject(trade, tradeName, tradeProps); item.Freeze(); // serialises items[i] = item; } loggerRef.Target.LogDebug("[{0}-{1}] Commencing save of {2} trades...", loop, repeat, itemsPerLoop); for (int i = 0; i < itemsPerLoop; i++) { completions.Add(client.SaveItemBegin(items[i])); itemsSaved++; } loggerRef.Target.LogDebug("[{0}-{1}] Completing save of {2} trades...", loop, repeat, itemsPerLoop); foreach (IAsyncResult ar in completions) { client.SaveEnd(ar); } // load all that have been saved string loopName = $"Test.{loop}."; loggerRef.Target.LogDebug("[{0}-{1}] Loading {2} trades...", loop, repeat, itemsPerLoop); List <ICoreItem> tradeItems = client.LoadItems <Trade>(Expr.StartsWith(Expr.SysPropItemName, loopName)); Assert.AreEqual(itemsPerLoop, tradeItems.Count); loggerRef.Target.LogDebug("[{0}-{1}] Unpacking {2} trades...", loop, repeat, itemsPerLoop); List <Trade> trades = new List <Trade>(); foreach (ICoreItem item in tradeItems) { trades.Add((Trade)item.Data); } loggerRef.Target.LogDebug("[{0}-{1}] Retrieved {2} trades.", loop, repeat, itemsPerLoop); // delete the test trades client.DeleteObjects <Trade>(deleteExpr); GC.Collect(); } // next loop itemsPerLoop += incrementPerLoop; } // for loop } } } }
public static void Main(string[] args) { using (Reference <ILogger> loggerRef = Reference <ILogger> .Create(new ConsoleLogger("CoreDump: "))) { loggerRef.Target.LogInfo("Running..."); const int exitCode = 0; try { // set defaults string coreAddr = null; // process arguments var filterProps = new NamedValueSet(); foreach (string arg in args) { bool argProcessed = true; // switches - begin with / if (arg.StartsWith("/")) { // arg is a switch string[] argParts = arg.Split(':'); string argName = argParts[0].Substring(1); string argValue = ""; for (int j = 1; j < argParts.Length; j++) { if (j > 1) { argValue += ':'; } argValue += argParts[j]; } switch (argName.ToLower()) { case "p": string[] propParts = argValue.Split('='); filterProps.Set(new NamedValue(propParts[0], propParts[1])); loggerRef.Target.LogInfo(" Property: {0}={1}", propParts[0], propParts[1]); break; case "s": coreAddr = argValue; loggerRef.Target.LogInfo(" Server : {0}", coreAddr); break; default: argProcessed = false; break; } } if (!argProcessed) { loggerRef.Target.LogInfo(" Dumps trade metadata and FpML content"); loggerRef.Target.LogInfo(" usage:"); loggerRef.Target.LogInfo(" CoreDump [options]"); loggerRef.Target.LogInfo(" /s:address the server address to connect to eg. /s:sydwadqds01:8213"); loggerRef.Target.LogInfo(" /p:name=value adds a filter property value e.g. /p:CounterpartyId=13142"); loggerRef.Target.LogInfo(" Examples: /p:CounterpartyId=13142 /p:OriginatingPartyId=1636 /p:ProductType=CrossCurrencySwap"); loggerRef.Target.LogInfo(" returns:"); loggerRef.Target.LogInfo(" 0: success"); loggerRef.Target.LogInfo(" 2: failed - see output for details"); throw new ArgumentException("Unknown argument '" + arg + "'"); } } // connect and query using (ICoreClient client = new CoreClientFactory(loggerRef) .SetEnv(BuildConst.BuildEnv) .SetServers(coreAddr) .SetApplication(Assembly.GetExecutingAssembly()) .Create()) { IExpression filterExpr = Expr.BoolAND(filterProps); loggerRef.Target.LogInfo("Selecting trades where: {0}", filterExpr.DisplayString()); ICoreItem[] tradeItems = client.LoadItems <Trade>(filterExpr).ToArray(); // dump results loggerRef.Target.LogInfo("Saving {0} trades...", tradeItems.Length); for (int i = 0; i < tradeItems.Length; i++) { ICoreItem tradeItem = tradeItems[i]; // save item string baseFilename = Path.GetFullPath($@".\{tradeItem.Name}.xxx"); string xmlFilename = Path.ChangeExtension(baseFilename, ".xml"); string nvsFilename = Path.ChangeExtension(xmlFilename, ".nvs"); using (var sr = new StreamWriter(xmlFilename)) { sr.Write(tradeItem.Text); } using (var sr = new StreamWriter(nvsFilename)) { sr.Write(tradeItem.AppProps.Serialise()); } // done tradeItems[i] = null; // allows early GC of decompressed/deserialised xml content if (i % 1000 == 0) { loggerRef.Target.LogInfo("Saved {0} trades...", i); GC.Collect(); } } loggerRef.Target.LogInfo("Saved {0} trades.", tradeItems.Length); } loggerRef.Target.LogInfo("Success"); Environment.ExitCode = exitCode; } catch (Exception e) { loggerRef.Target.Log(e); for (int i = 0; i < args.Length; i++) { loggerRef.Target.LogDebug(" args[{0}]='{1}'", i, args[i]); } loggerRef.Target.LogInfo("FAILED"); Environment.ExitCode = 2; } } }
public void TestSerialisingDerivedTypes() { // tests control of the serialisation type // - type b (derived from a) is saved as b, loaded as b; // - type b (derived from a) is saved as a, loaded as a (but is type b). // (in this example a = PricingStructure, b = YieldCurve) using (Reference <ILogger> loggerRef = Reference <ILogger> .Create(new TraceLogger(true))) { using (CoreServer server = new CoreServer(loggerRef, "UTT", NodeType.Router)) { // start server server.Start(); using (ICoreClient client = new CoreClientFactory(loggerRef).SetEnv("UTT").Create()) { { YieldCurve dataA = new YieldCurve { currency = new Currency { Value = "USD" }, algorithm = "FastCubicSpline" }; // - save as derived type client.SaveObject(dataA, "TestA", null, TimeSpan.MaxValue); ICoreItem test1 = client.LoadItem <YieldCurve>("TestA"); Assert.IsNotNull(test1); Assert.IsNotNull(test1.Text); Assert.AreEqual( "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<yieldCurve xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns=\"http://www.fpml.org/FpML-5/reporting\">\r\n <currency>USD</currency>\r\n <algorithm>FastCubicSpline</algorithm>\r\n</yieldCurve>", test1.Text); Assert.AreEqual(typeof(YieldCurve).FullName, test1.DataTypeName); Assert.AreEqual(typeof(YieldCurve), test1.DataType); Assert.IsNotNull(test1.Data); Assert.AreEqual(typeof(YieldCurve), test1.Data.GetType()); // - save as base type client.SaveObject <PricingStructure>(dataA, "TestA", null, TimeSpan.MaxValue); ICoreItem test2 = client.LoadItem <PricingStructure>("TestA"); Assert.IsNotNull(test2); Assert.IsNotNull(test2.Text); Assert.AreEqual( "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<PricingStructure xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:q1=\"http://www.fpml.org/FpML-5/reporting\" xsi:type=\"q1:YieldCurve\">\r\n <q1:currency>USD</q1:currency>\r\n <q1:algorithm>FastCubicSpline</q1:algorithm>\r\n</PricingStructure>", test2.Text); Assert.AreEqual(typeof(PricingStructure).FullName, test2.DataTypeName); Assert.AreEqual(typeof(PricingStructure), test2.DataType); Assert.IsNotNull(test2.Data); Assert.AreEqual(typeof(YieldCurve), test2.Data.GetType()); } { FxCurve dataB = new FxCurve() { quotedCurrencyPair = new QuotedCurrencyPair() { currency1 = new Currency() { Value = "USD" }, currency2 = new Currency() { Value = "JPY" }, quoteBasis = QuoteBasisEnum.Currency2PerCurrency1 } }; // - save as derived type client.SaveObject(dataB, "TestB", null, TimeSpan.MaxValue); ICoreItem test1 = client.LoadItem <FxCurve>("TestB"); Assert.IsNotNull(test1); Assert.IsNotNull(test1.Text); Assert.AreEqual( "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<fxCurve xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns=\"http://www.fpml.org/FpML-5/reporting\">\r\n <quotedCurrencyPair>\r\n <currency1>USD</currency1>\r\n <currency2>JPY</currency2>\r\n </quotedCurrencyPair>\r\n</fxCurve>", test1.Text); Assert.AreEqual(typeof(FxCurve).FullName, test1.DataTypeName); Assert.AreEqual(typeof(FxCurve), test1.DataType); Assert.IsNotNull(test1.Data); Assert.AreEqual(typeof(FxCurve), test1.Data.GetType()); // - save as base type client.SaveObject <PricingStructure>(dataB, "TestB", null, TimeSpan.MaxValue); ICoreItem test2 = client.LoadItem <PricingStructure>("TestB"); Assert.IsNotNull(test2); Assert.IsNotNull(test2.Text); Assert.AreEqual( "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<PricingStructure xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:q1=\"http://www.fpml.org/FpML-5/reporting\" xsi:type=\"q1:FxCurve\">\r\n <q1:quotedCurrencyPair>\r\n <q1:currency1>USD</q1:currency1>\r\n <q1:currency2>JPY</q1:currency2>\r\n </q1:quotedCurrencyPair>\r\n</PricingStructure>", test2.Text); Assert.AreEqual(typeof(PricingStructure).FullName, test2.DataTypeName); Assert.AreEqual(typeof(PricingStructure), test2.DataType); Assert.IsNotNull(test2.Data); Assert.AreEqual(typeof(FxCurve), test2.Data.GetType()); } { // load a collection of the base type and verify specific types List <ICoreItem> items = client.LoadItems <PricingStructure>(Expr.ALL); Assert.AreEqual(2, items.Count); Dictionary <string, PricingStructure> index = new Dictionary <string, PricingStructure>(); foreach (ICoreItem item in items) { index[item.Name] = (PricingStructure)item.Data; } Assert.AreEqual(typeof(YieldCurve), index["TestA"].GetType()); Assert.AreEqual(typeof(FxCurve), index["TestB"].GetType()); } } // shutdown server.Stop(); } } }