Custom class for testing the encryption and decryption
Пример #1
0
        public void ThenItShouldReturnSuccessCodeAndInfoForComplexMessage()
        {
            Pubnub pubnub = new Pubnub(
                Common.PublishKey,
                Common.SubscribeKey,
                "",
                "",
                false
                );
            string channel = "hello_world";
            object message = new CustomClass();

            Common common = new Common();

            pubnub.PubnubUnitTest = common.CreateUnitTestInstance("WhenAMessageIsPublished", "ThenItShouldReturnSuccessCodeAndInfoForComplexMessage");

            common.DeliveryStatus = false;
            common.Response       = null;

            pubnub.Publish(channel, message, common.DisplayReturnMessage, common.DisplayErrorMessage);
            //wait till the response is received from the server
            common.WaitForResponse();
            if (common.Response != null)
            {
                IList <object> fields = common.Response as IList <object>;
                string         sent   = fields [1].ToString();
                string         one    = fields [0].ToString();
                Assert.True(("Sent").Equals(sent));
                Assert.True(("1").Equals(one));
            }
            else
            {
                Assert.Fail("Null response");
            }
            pubnub.EndPendingRequests();
        }
        void SubscribePublishAndParseComplex(Pubnub pubnub, Common common, string channel)
        {
            CustomClass message = new CustomClass();

            pubnub.Subscribe <string> (channel, common.DisplayReturnMessage, common.DisplayReturnMessageDummy, common.DisplayReturnMessageDummy);
            Thread.Sleep(3000);

            pubnub.Publish(channel, (object)message, common.DisplayReturnMessageDummy, common.DisplayReturnMessageDummy);

            common.WaitForResponse();

            if (common.Response != null)
            {
                object[] fields = Common.Deserialize <object[]> (common.Response.ToString());

                if (fields [0] != null)
                {
                    var myObjectArray = (from item in fields
                                         select item as object).ToArray();

                    CustomClass cc = new CustomClass();

                    //If the custom class is serialized with jsonfx the response is received as a dictionary and
                    //on deserialization with Newtonsoft.Json we get an error.
                    //As a work around we parse the dictionary object.
                    var dict = myObjectArray [0] as IDictionary;

                    if ((dict != null) && (dict.Count > 1))
                    {
                        cc.foo = (string)dict ["foo"];
                        cc.bar = (int[])dict ["bar"];
                    }
                    else
                    {
                        cc = Common.Deserialize <CustomClass> (myObjectArray [0].ToString());

                        /*                                                            #if (USE_JSONFX)
                         * var reader = new JsonFx.Json.JsonReader();
                         * cc = reader.Read<CustomClass>(myObjectArray[0].ToString());
                         #else
                         * cc = JsonConvert.DeserializeObject<CustomClass>(myObjectArray[0].ToString());
                         #endif*/
                    }
                    if (cc.bar.SequenceEqual(message.bar) && cc.foo.Equals(message.foo))
                    {
                        Assert.Pass("Complex message test successful");
                    }
                    else
                    {
                        Assert.Fail("Complex message test not successful");
                    }
                }
                else
                {
                    Assert.Fail("No response");
                }
            }
            else
            {
                Assert.Fail("No response");
            }
        }
Пример #3
0
        private static void CommonComplexMessageSubscribeShouldReturnReceivedMessageBasedOnParams(string secretKey, string cipherKey, bool ssl, out bool receivedMessage)
        {
            if (PubnubCommon.PAMServerSideRun && string.IsNullOrEmpty(secretKey))
            {
                Assert.Ignore("Ignored for Server side run");
            }
            bool        internalReceivedMessage = false;
            bool        receivedErrorMessage    = false;
            CustomClass publishedMessage        = new CustomClass();

            PNConfiguration config = new PNConfiguration
            {
                PublishKey   = PubnubCommon.PublishKey,
                SubscribeKey = PubnubCommon.SubscribeKey,
                CipherKey    = cipherKey,
                Uuid         = "mytestuuid",
                Secure       = ssl
            };

            if (PubnubCommon.PAMServerSideRun)
            {
                config.SecretKey = secretKey;
            }
            else if (!string.IsNullOrEmpty(authKey) && !PubnubCommon.SuppressAuthKey)
            {
                config.AuthKey = authKey;
            }

            server.RunOnHttps(ssl);

            ManualResetEvent subscribeManualEvent = new ManualResetEvent(false);

            SubscribeCallback listenerSubCallack = new SubscribeCallbackExt(
                (o, m) =>
            {
                Debug.WriteLine(pubnub.JsonPluggableLibrary.SerializeToJsonString(m));
                if (m != null)
                {
                    Debug.WriteLine("SubscribeCallback: PNMessageResult: {0}", pubnub.JsonPluggableLibrary.SerializeToJsonString(m.Message));
                    if (pubnub.JsonPluggableLibrary.SerializeToJsonString(publishedMessage) == m.Message.ToString())
                    {
                        internalReceivedMessage = true;
                    }
                    subscribeManualEvent.Set();
                }
            },
                (o, p) => {
                subscribeManualEvent.Set();
            },
                (o, s) => {
                Debug.WriteLine(string.Format("{0} {1} {2}", s.Operation, s.Category, s.StatusCode));
                if (s.StatusCode != 200 || s.Error)
                {
                    receivedErrorMessage = true;
                    if (s.ErrorData != null)
                    {
                        Debug.WriteLine(s.ErrorData.Information);
                    }
                    subscribeManualEvent.Set();
                }
                else if (s.StatusCode == 200 && s.Category == PNStatusCategory.PNConnectedCategory)
                {
                    internalReceivedMessage = true;
                    subscribeManualEvent.Set();
                }
            });

            pubnub = createPubNubInstance(config);
            pubnub.AddListener(listenerSubCallack);

            string channel = "hello_my_channel";

            manualResetEventWaitTimeout = 310 * 1000;


            string expected = "{\"t\":{\"t\":\"14836303477713304\",\"r\":7},\"m\":[]}";

            server.AddRequest(new Request()
                              .WithMethod("GET")
                              .WithPath(String.Format("/v2/subscribe/{0}/{1}/0", PubnubCommon.SubscribeKey, channel))
                              .WithParameter("auth", authKey)
                              .WithParameter("heartbeat", "300")
                              .WithParameter("pnsdk", PubnubCommon.EncodedSDK)
                              .WithParameter("requestid", "myRequestId")
                              .WithParameter("timestamp", "1356998400")
                              .WithParameter("tt", "0")
                              .WithParameter("uuid", config.Uuid)
                              .WithParameter("signature", "zJpO1HpSZxGkOr3EALbOk-vQgjIZTZ6AU5svzNU9l_A=")
                              .WithResponse(expected)
                              .WithStatusCode(System.Net.HttpStatusCode.OK));

            server.AddRequest(new Request()
                              .WithMethod("GET")
                              .WithPath(String.Format("/v2/subscribe/{0}/{1}/0", PubnubCommon.SubscribeKey, channel))
                              .WithParameter("auth", authKey)
                              .WithParameter("heartbeat", "300")
                              .WithParameter("pnsdk", PubnubCommon.EncodedSDK)
                              .WithParameter("requestid", "myRequestId")
                              .WithParameter("timestamp", "1356998400")
                              .WithParameter("tt", "0")
                              .WithParameter("uuid", config.Uuid)
                              .WithResponse(expected)
                              .WithStatusCode(System.Net.HttpStatusCode.OK));

            expected = "{}";

            server.AddRequest(new Request()
                              .WithMethod("GET")
                              .WithPath(String.Format("/v2/subscribe/{0}/{1}/0", PubnubCommon.SubscribeKey, channel))
                              .WithResponse(expected)
                              .WithStatusCode(System.Net.HttpStatusCode.OK));

            pubnub.Subscribe <string>().Channels(new [] { channel }).Execute();
            subscribeManualEvent.WaitOne(manualResetEventWaitTimeout); //Wait for Connect Status


            expected = "[1,\"Sent\",\"14836234233392078\"]";

            server.AddRequest(new Request()
                              .WithMethod("GET")
                              .WithPath(string.Format("/publish/demo-36/{0}/0/{1}/0/{2}", PubnubCommon.SubscribeKey, channel, "%7B%22foo%22%3A%22hi%21%22%2C%22bar%22%3A%5B1%2C2%2C3%2C4%2C5%5D%7D"))
                              .WithResponse(expected)
                              .WithStatusCode(System.Net.HttpStatusCode.OK));

            server.AddRequest(new Request()
                              .WithMethod("GET")
                              .WithPath(string.Format("/publish/demo-36/{0}/0/{1}/0/{2}", PubnubCommon.SubscribeKey, channel, "%22Zbr7pEF%2FGFGKj1rOstp0tWzA4nwJXEfj%2BezLtAr8qqE%3D%22"))
                              .WithResponse(expected)
                              .WithStatusCode(System.Net.HttpStatusCode.OK));

            Thread.Sleep(1000);

            if (!receivedErrorMessage)
            {
                ManualResetEvent publishManualEvent = new ManualResetEvent(false);
                pubnub.Publish().Channel(channel).Message(publishedMessage)
                .Execute(new PNPublishResultExt((r, s) =>
                {
                    if (r != null && s.StatusCode == 200 && !s.Error)
                    {
                        internalReceivedMessage = true;
                    }
                    publishManualEvent.Set();
                }));

                publishManualEvent.WaitOne(manualResetEventWaitTimeout);

                pubnub.Unsubscribe <string>().Channels(new[] { channel }).Execute();

                Thread.Sleep(1000);
            }

            receivedMessage = internalReceivedMessage;
            pubnub.RemoveListener(listenerSubCallack);
            pubnub.Destroy();
            pubnub.PubnubUnitTest = null;
            pubnub = null;
        }
Пример #4
0
        public static void ThenUnencryptObjectPublishShouldReturnSuccessCodeAndInfo()
        {
            server.ClearRequests();

            bool receivedPublishMessage = false;

            long publishTimetoken = 0;

            string channel = "hello_my_channel";
            object message = new CustomClass();

            PNConfiguration config = new PNConfiguration
            {
                PublishKey   = PubnubCommon.PublishKey,
                SubscribeKey = PubnubCommon.SubscribeKey,
                Uuid         = "mytestuuid",
            };

            server.RunOnHttps(true);

            pubnub = createPubNubInstance(config);

            string expected = "[1,\"Sent\",\"14715286132003364\"]";

            server.AddRequest(new Request()
                              .WithMethod("GET")
                              .WithPath(String.Format("/publish/{0}/{1}/0/{2}/0/{3}", PubnubCommon.PublishKey, PubnubCommon.SubscribeKey, channel, "%7B%22foo%22%3A%22hi%21%22%2C%22bar%22%3A%5B1%2C2%2C3%2C4%2C5%5D%7D"))
                              .WithParameter("pnsdk", PubnubCommon.EncodedSDK)
                              .WithParameter("requestid", "myRequestId")
                              .WithParameter("uuid", config.Uuid)
                              .WithResponse(expected)
                              .WithStatusCode(System.Net.HttpStatusCode.OK));

            manualResetEventWaitTimeout = 310 * 1000;

            ManualResetEvent publishManualEvent = new ManualResetEvent(false);

            pubnub.Publish().Channel(channel).Message(message)
            .Async(new PNPublishResultExt((r, s) =>
            {
                if (r != null && s.StatusCode == 200 && !s.Error)
                {
                    publishTimetoken       = r.Timetoken;
                    receivedPublishMessage = true;
                }
                publishManualEvent.Set();
            }));
            publishManualEvent.WaitOne(manualResetEventWaitTimeout);

            if (!receivedPublishMessage)
            {
                Assert.IsTrue(receivedPublishMessage, "Unencrypt Publish Failed");
            }
            else
            {
                receivedPublishMessage = false;

                if (!PubnubCommon.EnableStubTest)
                {
                    Thread.Sleep(1000);
                }

                expected = "[[{\"foo\":\"hi!\",\"bar\":[1,2,3,4,5]}],14715286132003364,14715286132003364]";

                server.AddRequest(new Request()
                                  .WithMethod("GET")
                                  .WithPath(String.Format("/v2/history/sub-key/{0}/channel/{1}", PubnubCommon.SubscribeKey, channel))
                                  .WithParameter("count", "100")
                                  .WithParameter("end", "14715286132003364")
                                  .WithParameter("include_token", "true")
                                  .WithParameter("pnsdk", PubnubCommon.EncodedSDK)
                                  .WithParameter("requestid", "myRequestId")
                                  .WithParameter("uuid", config.Uuid)
                                  .WithResponse(expected)
                                  .WithStatusCode(System.Net.HttpStatusCode.OK));

                ManualResetEvent historyManualEvent = new ManualResetEvent(false);

                pubnub.History().Channel(channel)
                .End(PubnubCommon.EnableStubTest ? 14715286132003364 : publishTimetoken)
                .Reverse(false)
                .IncludeTimetoken(true)
                .Async(new PNHistoryResultExt(
                           (r, s) =>
                {
                    Console.WriteLine(pubnub.JsonPluggableLibrary.SerializeToJsonString(r));
                    receivedPublishMessage = true;
                    historyManualEvent.Set();
                }));

                historyManualEvent.WaitOne(manualResetEventWaitTimeout);

                Assert.IsTrue(receivedPublishMessage, "Unable to match the successful unencrypt object Publish");
            }
            pubnub.Destroy();
            pubnub.PubnubUnitTest = null;
            pubnub = null;
        }
        void SubscribePublishAndParseComplexObject(Pubnub pubnub, Common common, string channel)
        {
            CustomClass message = new CustomClass();

            Random r = new Random();

            channel = "hello_world_sub" + r.Next(1000);

            pubnub.Subscribe <object> (channel, common.DisplayReturnMessage, common.DisplayReturnMessageDummy, common.DisplayReturnMessageDummy);
            Thread.Sleep(3000);

            pubnub.Publish(channel, (object)message, common.DisplayReturnMessageDummy, common.DisplayReturnMessageDummy);

            common.WaitForResponse();

            if (common.Response != null)
            {
                List <object> lst = common.Response as List <object>;
                Console.WriteLine(lst [0].ToString());
                Console.WriteLine(lst [1].ToString());
                if (lst.Count == 3)
                {
                    Console.WriteLine(lst [2].ToString());
                }
                if (lst.Count == 4)
                {
                    Console.WriteLine(lst [3].ToString());
                }
                Console.WriteLine();

                if (lst [0] != null)
                {
                    CustomClass cc = new CustomClass();

                    var dict = lst [0] as IDictionary;

                    if ((dict != null) && (dict.Count > 1))
                    {
                        cc.foo = (string)dict ["foo"];
                        cc.bar = (int[])dict ["bar"];
                    }
                    else
                    {
                        cc = lst[0] as CustomClass;
                    }
                    if (cc.bar.SequenceEqual(message.bar) && cc.foo.Equals(message.foo))
                    {
                        Assert.Pass("Complex message test successful");
                    }
                    else
                    {
                        Assert.Fail("Complex message test not successful");
                    }
                }
                else
                {
                    Assert.Fail("No response1: " + common.ErrorResponse);
                }
            }
            else
            {
                Assert.Fail("No response: " + common.ErrorResponse);
            }
            common.DeliveryStatus = false;
            common.Response       = null;

            pubnub.Unsubscribe <string> (channel, common.DisplayReturnMessageDummy, common.DisplayReturnMessageDummy, common.DisplayReturnMessage, common.DisplayReturnMessageDummy);

            common.WaitForResponse(20);

            pubnub.EndPendingRequests();
        }
Пример #6
0
        public void ThenItShouldReturnReceivedMessageCipherForComplexMessage()
        {
            Pubnub pubnub = new Pubnub(
                "demo",
                "demo",
                "",
                "enigma",
                false);
            string channel = "hello_world";

            Common common = new Common();

            common.DeliveryStatus = false;
            common.Response       = null;

            pubnub.PubnubUnitTest = common.CreateUnitTestInstance("WhenSubscribedToAChannel", "ThenItShouldReturnReceivedMessageCipherForComplexMessage");

            CustomClass message = new CustomClass();

            pubnub.Subscribe(channel, common.DisplayReturnMessage, common.DisplayReturnMessageDummy, common.DisplayReturnMessageDummy);
            Thread.Sleep(3000);

            pubnub.Publish(channel, (object)message, common.DisplayReturnMessageDummy, common.DisplayReturnMessageDummy);

            common.WaitForResponse();

            if (common.Response != null)
            {
                IList <object> fields = common.Response as IList <object>;

                if (fields [0] != null)
                {
                    var myObjectArray = (from item in fields select item as object).ToArray();

                    CustomClass cc = new CustomClass();

                    //If the custom class is serialized with jsonfx the response is received as a dictionary and
                    //on deserialization with Newtonsoft.Json we get an error.
                    //As a work around we parse the dictionary object.
                    var dict = myObjectArray [0] as IDictionary;

                    if ((dict != null) && (dict.Count > 1))
                    {
                        cc.foo = (string)dict["foo"];
                        cc.bar = (int[])dict["bar"];
                    }
                    else
                    {
                        cc = Common.Deserialize <CustomClass>(myObjectArray[0].ToString());

/*            #if (USE_JSONFX)
 *                      var reader = new JsonFx.Json.JsonReader();
 *                      cc = reader.Read<CustomClass>(myObjectArray[0].ToString());
 #else
 *                      cc = JsonConvert.DeserializeObject<CustomClass>(myObjectArray[0].ToString());
 #endif*/
                    }
                    if (cc.bar.SequenceEqual(message.bar) && cc.foo.Equals(message.foo))
                    {
                        Assert.True(true, "Complex message test successful");
                    }
                    else
                    {
                        Assert.Fail("Complex message test not successful");
                    }
                }
                else
                {
                    Assert.Fail("No response");
                }
            }
            else
            {
                Assert.Fail("No response");
            }
        }
Пример #7
0
        void SubscribePublishAndParseComplexObject(Pubnub pubnub, Common common, string channel)
        {
            Random r = new Random();

            channel = "hello_world_sub" + r.Next(1000);
            CustomClass message = new CustomClass();

            pubnub.Subscribe <object> (channel, common.DisplayReturnMessage, common.DisplayReturnMessageDummy, common.DisplayReturnMessageDummy);
            Thread.Sleep(2000);

            pubnub.Publish(channel, (object)message, common.DisplayReturnMessageDummy, common.DisplayReturnMessageDummy);

            common.WaitForResponse();

            if (common.Response != null)
            {
                Console.WriteLine("response:" + common.Response.ToString());
                List <object> lst = common.Response as List <object>;
                Console.WriteLine(lst [0].ToString());
                Console.WriteLine(lst [1].ToString());
                if (lst.Count == 3)
                {
                    Console.WriteLine(lst [2].ToString());
                }
                if (lst.Count == 4)
                {
                    Console.WriteLine(lst [3].ToString());
                }
                Console.WriteLine();

                if (lst [0] != null)
                {
                    //var myObjectArray = (from item in fields
                    //  select item as object).ToArray ();

                    CustomClass cc = new CustomClass();

                    //If the custom class is serialized with jsonfx the response is received as a dictionary and
                    //on deserialization with Newtonsoft.Json we get an error.
                    //As a work around we parse the dictionary object.
                    //var dict = myObjectArray [0] as IDictionary;
                    var dict = lst [0] as IDictionary;

                    if ((dict != null) && (dict.Count > 1))
                    {
                        cc.foo = (string)dict ["foo"];
                        cc.bar = (int[])dict ["bar"];
                    }
                    else
                    {
                        Type valueType    = lst [0].GetType();
                        var  expectedType = typeof(System.Dynamic.ExpandoObject);
                        if (expectedType.IsAssignableFrom(valueType))
                        {
                            dynamic x = lst [0];
                            cc.foo = x.foo;
                            cc.bar = x.bar;
                        }
                        else
                        {
                            cc = Common.Deserialize <CustomClass> (lst [0].ToString());
                        }
                    }
                    if (cc.bar.SequenceEqual(message.bar) && cc.foo.Equals(message.foo))
                    {
                        Assert.True(true, "Complex message test successful");
                    }
                    else
                    {
                        Assert.Fail("Complex message test not successful");
                    }
                }
                else
                {
                    Assert.Fail("No response");
                }
            }
            else
            {
                Assert.Fail("No response");
            }
            common.DeliveryStatus = false;
            common.Response       = null;

            pubnub.Unsubscribe <string> (channel, common.DisplayReturnMessageDummy, common.DisplayReturnMessageDummy, common.DisplayReturnMessage, common.DisplayReturnMessageDummy);

            common.WaitForResponse(20);

            pubnub.EndPendingRequests();
        }
Пример #8
0
        void SubscribePublishAndParseComplex(Pubnub pubnub, Common common, string channel)
        {
            Random r = new Random();

            channel = "hello_world_sub" + r.Next(1000);

            CustomClass message = new CustomClass();

            pubnub.Subscribe <string> (channel, common.DisplayReturnMessage, common.DisplayReturnMessageDummy, common.DisplayReturnMessageDummy);
            Thread.Sleep(2000);

            pubnub.Publish(channel, (object)message, common.DisplayReturnMessageDummy, common.DisplayReturnMessageDummy);

            common.WaitForResponse();

            if (common.Response != null)
            {
                object[] fields = Common.Deserialize <object[]> (common.Response.ToString());

                if (fields [0] != null)
                {
                    var myObjectArray = (from item in fields
                                         select item as object).ToArray();

                    CustomClass cc = new CustomClass();

                    //If the custom class is serialized with jsonfx the response is received as a dictionary and
                    //on deserialization with Newtonsoft.Json we get an error.
                    //As a work around we parse the dictionary object.
                    var dict = myObjectArray [0] as IDictionary;

                    if ((dict != null) && (dict.Count > 1))
                    {
                        cc.foo = (string)dict ["foo"];
                        cc.bar = (int[])dict ["bar"];
                    }
                    else
                    {
                        cc = Common.Deserialize <CustomClass> (myObjectArray [0].ToString());
                    }
                    if (cc.bar.SequenceEqual(message.bar) && cc.foo.Equals(message.foo))
                    {
                        Assert.True(true, "Complex message test successful");
                    }
                    else
                    {
                        Assert.Fail("Complex message test not successful");
                    }
                }
                else
                {
                    Assert.Fail("No response");
                }
            }
            else
            {
                Assert.Fail("No response");
            }
            common.DeliveryStatus = false;
            common.Response       = null;

            pubnub.Unsubscribe <string> (channel, common.DisplayReturnMessageDummy, common.DisplayReturnMessageDummy, common.DisplayReturnMessage, common.DisplayReturnMessageDummy);

            common.WaitForResponse(20);

            pubnub.EndPendingRequests();
        }
        void SubscribePublishAndParseComplex(Pubnub pubnub, Common common, string channel)
        {
            CustomClass message = new CustomClass();

            pubnub.Subscribe <string> (channel, common.DisplayReturnMessage, common.DisplayReturnMessageDummy, common.DisplayReturnMessageDummy);
            Thread.Sleep(1500);

            pubnub.Publish(channel, (object)message, common.DisplayReturnMessageDummy, common.DisplayReturnMessageDummy);

            common.WaitForResponse();

            if (common.Response != null)
            {
                object[] fields = Common.Deserialize <object[]> (common.Response.ToString());

                if (fields [0] != null)
                {
                    var myObjectArray = (from item in fields
                                         select item as object).ToArray();

                    CustomClass cc = new CustomClass();

                    //If the custom class is serialized with jsonfx the response is received as a dictionary and
                    //on deserialization with Newtonsoft.Json we get an error.
                    //As a work around we parse the dictionary object.
                    var dict = myObjectArray [0] as IDictionary;

                    if ((dict != null) && (dict.Count > 1))
                    {
                        cc.foo = (string)dict ["foo"];
                        cc.bar = (int[])dict ["bar"];
                    }
                    else
                    {
                        Type valueType    = myObjectArray [0].GetType();
                        var  expectedType = typeof(System.Dynamic.ExpandoObject);
                        if (expectedType.IsAssignableFrom(valueType))
                        {
                            dynamic x = myObjectArray [0];
                            cc.foo = x.foo;
                            cc.bar = x.bar;
                        }
                        else
                        {
                            cc = Common.Deserialize <CustomClass>(myObjectArray [0].ToString());
                        }
                    }
                    if (cc.bar.SequenceEqual(message.bar) && cc.foo.Equals(message.foo))
                    {
                        Assert.True(true, "Complex message test successful");
                    }
                    else
                    {
                        Assert.Fail("Complex message test not successful");
                    }
                }
                else
                {
                    Assert.Fail("No response");
                }
            }
            else
            {
                Assert.Fail("No response");
            }
        }