Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

multi-message subscribe fix and unit tests

  • Loading branch information...
commit 448d3100c77c38841759b8d5ea5375b2dcd23669 1 parent 3280fd3
Pandu Masabathula authored
1,059  csharp/3.4/PubNub-Messaging.Tests/EncryptionTests.cs
... ...
@@ -1,513 +1,546 @@
1  
-using System;
2  
-using System.Text;
3  
-using System.Collections.Generic;
4  
-using System.Linq;
5  
-using NUnit.Framework;
6  
-using System.ComponentModel;
7  
-using System.Threading;
8  
-using System.Collections;
9  
-using PubNubMessaging.Core;
10  
-using System.Text.RegularExpressions;
11  
-using System.Globalization;
12  
-using Newtonsoft.Json;
13  
-using Newtonsoft.Json.Linq;
14  
-
15  
-
16  
-namespace PubNubMessaging.Tests
17  
-{
18  
-    /// <summary>
19  
-    /// Custom class for testing the encryption and decryption 
20  
-    /// </summary>
21  
-    class CustomClass
22  
-    {
23  
-        public string foo = "hi!";
24  
-        public int[] bar = { 1, 2, 3, 4, 5 };
25  
-    }
26  
-    class SecretCustomClass
27  
-    {
28  
-        public string foo = "hello!";
29  
-        public int[] bar = { 10, 20, 30, 40, 50 };
30  
-    }
31  
-
32  
-    [TestFixture]
33  
-    public class EncryptionTests
34  
-    {
35  
-        /// <summary>
36  
-        /// Tests the null encryption.
37  
-        /// The input is serialized
38  
-        /// </summary>
39  
-        [Test]
40  
-        [ExpectedException(typeof(ArgumentNullException))]
41  
-        public void TestNullEncryption()
42  
-        {
43  
-            PubnubCrypto pc = new PubnubCrypto("enigma");
44  
-            //serialized string
45  
-            string message = null;
46  
-            
47  
-            //encrypt
48  
-            string encryptedMessage = pc.Encrypt(message);
49  
-        }
50  
-
51  
-        /// <summary>
52  
-        /// Tests the null decryption.
53  
-        /// Assumes that the input message is  deserialized  
54  
-        /// </summary>        
55  
-        [Test]
56  
-        [ExpectedException(typeof(ArgumentNullException))]
57  
-        public void TestNullDecryption()
58  
-        {
59  
-            PubnubCrypto pc = new PubnubCrypto("enigma");
60  
-            //deserialized string
61  
-            string message = null;
62  
-            //decrypt
63  
-            string decryptedMessage = pc.Decrypt(message);
64  
-
65  
-            Assert.AreEqual("", decryptedMessage);
66  
-        }
67  
-
68  
-        /// <summary>
69  
-        /// Tests the yay decryption.
70  
-        /// Assumes that the input message is deserialized  
71  
-        /// Decrypted string should match yay!
72  
-        /// </summary>
73  
-        [Test]
74  
-        public void TestYayDecryptionBasic()
75  
-        {
76  
-            PubnubCrypto pc = new PubnubCrypto("enigma");
77  
-            string message = "q/xJqqN6qbiZMXYmiQC1Fw==";
78  
-            //decrypt
79  
-            string decryptedMessage = pc.Decrypt(message);
80  
-            //deserialize again
81  
-            Assert.AreEqual("yay!", decryptedMessage);
82  
-        }
83  
-
84  
-        /// <summary>
85  
-        /// Tests the yay encryption.
86  
-        /// The output is not serialized
87  
-        /// Encrypted string should match q/xJqqN6qbiZMXYmiQC1Fw==
88  
-        /// </summary>
89  
-        [Test]
90  
-        public void TestYayEncryptionBasic()
91  
-        {
92  
-            PubnubCrypto pc = new PubnubCrypto("enigma");
93  
-            //deserialized string
94  
-            string message = "yay!";
95  
-            //Encrypt
96  
-            string encryptedMessage = pc.Encrypt(message);
97  
-            Assert.AreEqual("q/xJqqN6qbiZMXYmiQC1Fw==", encryptedMessage);
98  
-        }
99  
-
100  
-        /// <summary>
101  
-        /// Tests the yay decryption.
102  
-        /// Assumes that the input message is not deserialized  
103  
-        /// Decrypted and Deserialized string should match yay!
104  
-        /// </summary>
105  
-        [Test]
106  
-        public void TestYayDecryption()
107  
-        {
108  
-            PubnubCrypto pc = new PubnubCrypto("enigma");
109  
-            //string strMessage= "\"q/xJqqN6qbiZMXYmiQC1Fw==\"";
110  
-            //Non deserialized string
111  
-            string message = "\"Wi24KS4pcTzvyuGOHubiXg==\"";
112  
-            //Deserialize
113  
-            message = JsonConvert.DeserializeObject<string>(message);
114  
-            //decrypt
115  
-            string decryptedMessage = pc.Decrypt(message);
116  
-            //deserialize again
117  
-            message = JsonConvert.DeserializeObject<string>(decryptedMessage);
118  
-            Assert.AreEqual("yay!", message);
119  
-        }
120  
-        /// <summary>
121  
-        /// Tests the yay encryption.
122  
-        /// The output is not serialized
123  
-        /// Encrypted string should match Wi24KS4pcTzvyuGOHubiXg==
124  
-        /// </summary>
125  
-        [Test]
126  
-        public void TestYayEncryption()
127  
-        {
128  
-            PubnubCrypto pc = new PubnubCrypto("enigma");
129  
-            //deserialized string
130  
-            string message = "yay!";
131  
-            //serialize the string
132  
-            message = JsonConvert.SerializeObject(message);
133  
-            Console.WriteLine(message);
134  
-            //Encrypt
135  
-            string enc = pc.Encrypt(message);
136  
-            Assert.AreEqual("Wi24KS4pcTzvyuGOHubiXg==", enc);
137  
-        }
138  
-
139  
-        /// <summary>
140  
-        /// Tests the array encryption.
141  
-        /// The output is not serialized
142  
-        /// Encrypted string should match the serialized object
143  
-        /// </summary>
144  
-        [Test]
145  
-        public void TestArrayEncryption()
146  
-        {
147  
-            PubnubCrypto pc = new PubnubCrypto("enigma");
148  
-            //create an empty array object
149  
-            object[] emptyArray = { };
150  
-            //serialize
151  
-            string serializedArray = JsonConvert.SerializeObject(emptyArray);
152  
-            //Encrypt
153  
-            string encryptedMessage = pc.Encrypt(serializedArray);
154  
-
155  
-            Assert.AreEqual("Ns4TB41JjT2NCXaGLWSPAQ==", encryptedMessage);
156  
-        }
157  
-
158  
-        /// <summary>
159  
-        /// Tests the array decryption.
160  
-        /// Assumes that the input message is deserialized
161  
-        /// And the output message has to been deserialized.
162  
-        /// Decrypted string should match the serialized object
163  
-        /// </summary>
164  
-        [Test]
165  
-        public void TestArrayDecryption()
166  
-        {
167  
-            PubnubCrypto pc = new PubnubCrypto("enigma");
168  
-            //Input the deserialized string
169  
-            string message = "Ns4TB41JjT2NCXaGLWSPAQ==";
170  
-            //decrypt
171  
-            string decryptedMessage = pc.Decrypt(message);
172  
-            //create a serialized object
173  
-            object[] emptyArrayObject = { };
174  
-            string result = JsonConvert.SerializeObject(emptyArrayObject);
175  
-            //compare the serialized object and the return of the Decrypt method
176  
-            Assert.AreEqual(result, decryptedMessage);
177  
-        }
178  
-
179  
-        /// <summary>
180  
-        /// Tests the object encryption.
181  
-        /// The output is not serialized
182  
-        /// Encrypted string should match the serialized object
183  
-        /// </summary>
184  
-        [Test]
185  
-        public void TestObjectEncryption()
186  
-        {
187  
-            PubnubCrypto pc = new PubnubCrypto("enigma");
188  
-            //create an object
189  
-            Object obj = new Object();
190  
-            //serialize
191  
-            string serializedObject = JsonConvert.SerializeObject(obj);
192  
-            //encrypt
193  
-            string encryptedMessage = pc.Encrypt(serializedObject);
194  
-
195  
-            Assert.AreEqual("IDjZE9BHSjcX67RddfCYYg==", encryptedMessage);
196  
-        }
197  
-        /// <summary>
198  
-        /// Tests the object decryption.
199  
-        /// Assumes that the input message is deserialized
200  
-        /// And the output message has to be deserialized.
201  
-        /// Decrypted string should match the serialized object
202  
-        /// </summary>
203  
-        [Test]
204  
-        public void TestObjectDecryption()
205  
-        {
206  
-            PubnubCrypto pc = new PubnubCrypto("enigma");
207  
-            //Deserialized
208  
-            string message = "IDjZE9BHSjcX67RddfCYYg==";
209  
-            //Decrypt
210  
-            string decryptedMessage = pc.Decrypt(message);
211  
-            //create an object
212  
-            Object obj = new Object();
213  
-            //Serialize the object
214  
-            string result = JsonConvert.SerializeObject(obj);
215  
-
216  
-            Assert.AreEqual(result, decryptedMessage);
217  
-        }
218  
-        /// <summary>
219  
-        /// Tests my object encryption.
220  
-        /// The output is not serialized 
221  
-        /// Encrypted string should match the serialized object
222  
-        /// </summary>
223  
-        [Test]
224  
-        public void TestMyObjectEncryption()
225  
-        {
226  
-            PubnubCrypto pc = new PubnubCrypto("enigma");
227  
-            //create an object of the custom class
228  
-            CustomClass cc = new CustomClass();
229  
-            //serialize it
230  
-            string result = JsonConvert.SerializeObject(cc);
231  
-            //encrypt it
232  
-            string encryptedMessage = pc.Encrypt(result);
233  
-
234  
-            Assert.AreEqual("Zbr7pEF/GFGKj1rOstp0tWzA4nwJXEfj+ezLtAr8qqE=", encryptedMessage);
235  
-        }
236  
-        /// <summary>
237  
-        /// Tests my object decryption.
238  
-        /// The output is not deserialized
239  
-        /// Decrypted string should match the serialized object
240  
-        /// </summary>
241  
-        [Test]
242  
-        public void TestMyObjectDecryption()
243  
-        {
244  
-            PubnubCrypto pc = new PubnubCrypto("enigma");
245  
-            //Deserialized
246  
-            string message = "Zbr7pEF/GFGKj1rOstp0tWzA4nwJXEfj+ezLtAr8qqE=";
247  
-            //Decrypt
248  
-            string decryptedMessage = pc.Decrypt(message);
249  
-            //create an object of the custom class
250  
-            CustomClass cc = new CustomClass();
251  
-            //Serialize it
252  
-            string result = JsonConvert.SerializeObject(cc);
253  
-
254  
-            Assert.AreEqual(result, decryptedMessage);
255  
-        }
256  
-
257  
-        /// <summary>
258  
-        /// Tests the pub nub encryption2.
259  
-        /// The output is not serialized
260  
-        /// Encrypted string should match f42pIQcWZ9zbTbH8cyLwB/tdvRxjFLOYcBNMVKeHS54=
261  
-        /// </summary>
262  
-        [Test]
263  
-        public void TestPubNubEncryption2()
264  
-        {
265  
-            PubnubCrypto pc = new PubnubCrypto("enigma");
266  
-            //Deserialized
267  
-            string message = "Pubnub Messaging API 2";
268  
-            //serialize the message
269  
-            message = JsonConvert.SerializeObject(message);
270  
-            //encrypt
271  
-            string encryptedMessage = pc.Encrypt(message);
272  
-
273  
-            Assert.AreEqual("f42pIQcWZ9zbTbH8cyLwB/tdvRxjFLOYcBNMVKeHS54=", encryptedMessage);
274  
-        }
275  
-
276  
-        /// <summary>
277  
-        /// Tests the pub nub decryption2.
278  
-        /// Assumes that the input message is deserialized  
279  
-        /// Decrypted and Deserialized string should match Pubnub Messaging API 2
280  
-        /// </summary>
281  
-        [Test]
282  
-        public void TestPubNubDecryption2()
283  
-        {
284  
-            PubnubCrypto pc = new PubnubCrypto("enigma");
285  
-            //Deserialized string    
286  
-            string message = "f42pIQcWZ9zbTbH8cyLwB/tdvRxjFLOYcBNMVKeHS54=";
287  
-            //Decrypt
288  
-            string decryptedMessage = pc.Decrypt(message);
289  
-            //Deserialize
290  
-            message = JsonConvert.DeserializeObject<string>(decryptedMessage);
291  
-            Assert.AreEqual("Pubnub Messaging API 2", message);
292  
-        }
293  
-
294  
-        /// <summary>
295  
-        /// Tests the pub nub encryption1.
296  
-        /// The input is not serialized
297  
-        /// Encrypted string should match f42pIQcWZ9zbTbH8cyLwByD/GsviOE0vcREIEVPARR0=
298  
-        /// </summary>
299  
-        [Test]
300  
-        public void TestPubNubEncryption1()
301  
-        {
302  
-            PubnubCrypto pc = new PubnubCrypto("enigma");
303  
-            //non serialized string
304  
-            string message = "Pubnub Messaging API 1";
305  
-            //serialize
306  
-            message = JsonConvert.SerializeObject(message);
307  
-            //encrypt
308  
-            string encryptedMessage = pc.Encrypt(message);
309  
-
310  
-            Assert.AreEqual("f42pIQcWZ9zbTbH8cyLwByD/GsviOE0vcREIEVPARR0=", encryptedMessage);
311  
-        }
312  
-
313  
-        /// <summary>
314  
-        /// Tests the pub nub decryption1.
315  
-        /// Assumes that the input message is  deserialized  
316  
-        /// Decrypted and Deserialized string should match Pubnub Messaging API 1        
317  
-        /// </summary>
318  
-        [Test]
319  
-        public void TestPubNubDecryption1()
320  
-        {
321  
-            PubnubCrypto pc = new PubnubCrypto("enigma");
322  
-            //deserialized string
323  
-            string message = "f42pIQcWZ9zbTbH8cyLwByD/GsviOE0vcREIEVPARR0=";
324  
-            //decrypt
325  
-            string decryptedMessage = pc.Decrypt(message);
326  
-            //deserialize
327  
-            message = (decryptedMessage != "**DECRYPT ERROR**") ? JsonConvert.DeserializeObject<string>(decryptedMessage) : "";
328  
-            Assert.AreEqual("Pubnub Messaging API 1", message);
329  
-        }
330  
-
331  
-        /// <summary>
332  
-        /// Tests the stuff can encryption.
333  
-        /// The input is serialized
334  
-        /// Encrypted string should match zMqH/RTPlC8yrAZ2UhpEgLKUVzkMI2cikiaVg30AyUu7B6J0FLqCazRzDOmrsFsF
335  
-        /// </summary>
336  
-        [Test]
337  
-        public void TestStuffCanEncryption()
338  
-        {
339  
-            PubnubCrypto pc = new PubnubCrypto("enigma");
340  
-            //input serialized string
341  
-            string message = "{\"this stuff\":{\"can get\":\"complicated!\"}}";
342  
-            //encrypt
343  
-            string encryptedMessage = pc.Encrypt(message);
344  
-
345  
-            Assert.AreEqual("zMqH/RTPlC8yrAZ2UhpEgLKUVzkMI2cikiaVg30AyUu7B6J0FLqCazRzDOmrsFsF", encryptedMessage);
346  
-        }
347  
-
348  
-        /// <summary>
349  
-        /// Tests the stuffcan decryption.
350  
-        /// Assumes that the input message is  deserialized  
351  
-        /// </summary>
352  
-        [Test]
353  
-        public void TestStuffcanDecryption()
354  
-        {
355  
-            PubnubCrypto pc = new PubnubCrypto("enigma");
356  
-            //deserialized string
357  
-            string message = "zMqH/RTPlC8yrAZ2UhpEgLKUVzkMI2cikiaVg30AyUu7B6J0FLqCazRzDOmrsFsF";
358  
-            //decrypt
359  
-            string decryptedMessage = pc.Decrypt(message);
360  
-
361  
-            Assert.AreEqual("{\"this stuff\":{\"can get\":\"complicated!\"}}", decryptedMessage);
362  
-        }
363  
-
364  
-        /// <summary>
365  
-        /// Tests the hash encryption.
366  
-        /// The input is serialized
367  
-        /// Encrypted string should match GsvkCYZoYylL5a7/DKhysDjNbwn+BtBtHj2CvzC4Y4g=
368  
-        /// </summary>
369  
-        [Test]
370  
-        public void TestHashEncryption()
371  
-        {
372  
-            PubnubCrypto pc = new PubnubCrypto("enigma");
373  
-            //serialized string
374  
-            string message = "{\"foo\":{\"bar\":\"foobar\"}}";
375  
-            //encrypt
376  
-            string encryptedMessage = pc.Encrypt(message);
377  
-
378  
-            Assert.AreEqual("GsvkCYZoYylL5a7/DKhysDjNbwn+BtBtHj2CvzC4Y4g=", encryptedMessage);
379  
-        }
380  
-
381  
-        /// <summary>
382  
-        /// Tests the hash decryption.
383  
-        /// Assumes that the input message is  deserialized  
384  
-        /// </summary>        
385  
-        [Test]
386  
-        public void TestHashDecryption()
387  
-        {
388  
-            PubnubCrypto pc = new PubnubCrypto("enigma");
389  
-            //deserialized string
390  
-            string message = "GsvkCYZoYylL5a7/DKhysDjNbwn+BtBtHj2CvzC4Y4g=";
391  
-            //decrypt
392  
-            string decryptedMessage = pc.Decrypt(message);
393  
-
394  
-            Assert.AreEqual("{\"foo\":{\"bar\":\"foobar\"}}", decryptedMessage);
395  
-        }
396  
-
397  
-        /// <summary>
398  
-        /// Tests the unicode chars encryption.
399  
-        /// The input is not serialized
400  
-        /// </summary>
401  
-        [Test]
402  
-        public void TestUnicodeCharsEncryption()
403  
-        {
404  
-            PubnubCrypto pc = new PubnubCrypto("enigma");
405  
-            string message = "漢語";
406  
-
407  
-            message = JsonConvert.SerializeObject(message);
408  
-            Console.WriteLine(message);
409  
-            string encryptedMessage = pc.Encrypt(message);
410  
-            Console.WriteLine(encryptedMessage);
411  
-            Assert.AreEqual("+BY5/miAA8aeuhVl4d13Kg==", encryptedMessage);
412  
-        }
413  
-
414  
-        /// <summary>
415  
-        /// Tests the unicode decryption.
416  
-        /// Assumes that the input message is  deserialized  
417  
-        /// Decrypted and Deserialized string should match the unicode chars       
418  
-        /// </summary>
419  
-        [Test]
420  
-        public void TestUnicodeCharsDecryption()
421  
-        {
422  
-            PubnubCrypto pc = new PubnubCrypto("enigma");
423  
-            string message = "+BY5/miAA8aeuhVl4d13Kg==";
424  
-            //decrypt
425  
-            string decryptedMessage = pc.Decrypt(message);
426  
-            //deserialize
427  
-            message = (decryptedMessage != "**DECRYPT ERROR**") ? JsonConvert.DeserializeObject<string>(decryptedMessage) : "";
428  
-
429  
-            Assert.AreEqual("漢語", message);
430  
-        }
431  
-
432  
-        /// <summary>
433  
-        /// Tests the german chars decryption.
434  
-        /// Assumes that the input message is  deserialized  
435  
-        /// Decrypted and Deserialized string should match the unicode chars  
436  
-        /// </summary>
437  
-        [Test]
438  
-        public void TestGermanCharsDecryption()
439  
-        {
440  
-            PubnubCrypto pc = new PubnubCrypto("enigma");
441  
-            string message = "stpgsG1DZZxb44J7mFNSzg==";
442  
-            
443  
-            //decrypt
444  
-            string decryptedMessage = pc.Decrypt(message);
445  
-            //deserialize
446  
-            message = (decryptedMessage != "**DECRYPT ERROR**") ? JsonConvert.DeserializeObject<string>(decryptedMessage) : "";
447  
-
448  
-            Assert.AreEqual("ÜÖ", message);
449  
-        }
450  
-        
451  
-        /// <summary>
452  
-        /// Tests the german encryption.
453  
-        /// The input is not serialized
454  
-        /// </summary>
455  
-        [Test]
456  
-        public void TestGermanCharsEncryption()
457  
-        {
458  
-            PubnubCrypto pc = new PubnubCrypto("enigma");
459  
-            string message = "ÜÖ";
460  
-
461  
-            message = JsonConvert.SerializeObject(message);
462  
-            Console.WriteLine(message);
463  
-            string encryptedMessage = pc.Encrypt(message);
464  
-            Console.WriteLine(encryptedMessage);
465  
-            Assert.AreEqual("stpgsG1DZZxb44J7mFNSzg==", encryptedMessage);
466  
-        }
467  
-
468  
-        /// <summary>
469  
-        /// Tests the cipher.
470  
-        /// </summary>
471  
-        /*[Test]
472  
-        public void  TestCipher ()
473  
-        {
474  
-            PubnubCrypto pc = new PubnubCrypto("enigma");
475  
-
476  
-            string strCipher = pc.GetEncryptionKey();
477  
-
478  
-            Assert.AreEqual("67a4f45f0d1d9bc606486fc42dc49416", strCipher);
479  
-        }*/
480  
-
481  
-        static string EncodeNonAsciiCharacters(string value)
482  
-        {
483  
-            StringBuilder sb = new StringBuilder();
484  
-            foreach (char c in value)
485  
-            {
486  
-                if (c > 127)
487  
-                {
488  
-                    // This character is too big for ASCII
489  
-                    string encodedValue = "\\u" + ((int)c).ToString("x4");
490  
-                    sb.Append(encodedValue);
491  
-                }
492  
-                else
493  
-                {
494  
-                    sb.Append(c);
495  
-                }
496  
-            }
497  
-            return sb.ToString();
498  
-        }
499  
-
500  
-        static string DecodeEncodedNonAsciiCharacters(string value)
501  
-        {
502  
-            return Regex.Replace(
503  
-                value,
504  
-                @"\\u(?<Value>[a-zA-Z0-9]{4})",
505  
-                m =>
506  
-                {
507  
-                    return ((char)int.Parse(m.Groups["Value"].Value, NumberStyles.HexNumber)).ToString();
508  
-                });
509  
-        }
510  
-
511  
-
512  
-    }
513  
-}
  1
+using System;
  2
+using System.Text;
  3
+using System.Collections.Generic;
  4
+using System.Linq;
  5
+using NUnit.Framework;
  6
+using System.ComponentModel;
  7
+using System.Threading;
  8
+using System.Collections;
  9
+using PubNubMessaging.Core;
  10
+using System.Text.RegularExpressions;
  11
+using System.Globalization;
  12
+using Newtonsoft.Json;
  13
+using Newtonsoft.Json.Linq;
  14
+
  15
+using System.Xml;
  16
+
  17
+namespace PubNubMessaging.Tests
  18
+{
  19
+    /// <summary>
  20
+    /// Custom class for testing the encryption and decryption 
  21
+    /// </summary>
  22
+    class CustomClass
  23
+    {
  24
+        public string foo = "hi!";
  25
+        public int[] bar = { 1, 2, 3, 4, 5 };
  26
+    }
  27
+    class SecretCustomClass
  28
+    {
  29
+        public string foo = "hello!";
  30
+        public int[] bar = { 10, 20, 30, 40, 50 };
  31
+    }
  32
+
  33
+    class PubnubDemoObject
  34
+    {
  35
+        public double VersionID = 3.4;
  36
+        public long Timetoken = 13601488652764619;
  37
+        public string OperationName = "Publish";
  38
+        public string[] Channels = { "ch1" };
  39
+        public PubnubDemoMessage DemoMessage = new PubnubDemoMessage();
  40
+        public PubnubDemoMessage CustomMessage = new PubnubDemoMessage("Welcome to the world of Pubnub for Publish and Subscribe. Hah!");
  41
+        public XmlDocument SampleXml = new PubnubDemoMessage().TryXmlDemo();
  42
+    }
  43
+
  44
+    class PubnubDemoMessage
  45
+    {
  46
+        public string DefaultMessage = "~!@#$%^&*()_+ `1234567890-= qwertyuiop[]\\ {}| asdfghjkl;' :\" zxcvbnm,./ <>? ";
  47
+
  48
+        public PubnubDemoMessage()
  49
+        {
  50
+        }
  51
+
  52
+        public PubnubDemoMessage(string message)
  53
+        {
  54
+            DefaultMessage = message;
  55
+        }
  56
+
  57
+        public XmlDocument TryXmlDemo()
  58
+        {
  59
+            XmlDocument xmlDocument = new XmlDocument();
  60
+            xmlDocument.LoadXml("<DemoRoot><Person ID='ABCD123'><Name><First>John</First><Middle>P.</Middle><Last>Doe</Last></Name><Address><Street>123 Duck Street</Street><City>New City</City><State>New York</State><Country>United States</Country></Address></Person><Person ID='ABCD456'><Name><First>Peter</First><Middle>Z.</Middle><Last>Smith</Last></Name><Address><Street>12 Hollow Street</Street><City>Philadelphia</City><State>Pennsylvania</State><Country>United States</Country></Address></Person></DemoRoot>");
  61
+            return xmlDocument;
  62
+        }
  63
+    }
  64
+
  65
+    [TestFixture]
  66
+    public class EncryptionTests
  67
+    {
  68
+        /// <summary>
  69
+        /// Tests the null encryption.
  70
+        /// The input is serialized
  71
+        /// </summary>
  72
+        [Test]
  73
+        [ExpectedException(typeof(ArgumentNullException))]
  74
+        public void TestNullEncryption()
  75
+        {
  76
+            PubnubCrypto pc = new PubnubCrypto("enigma");
  77
+            //serialized string
  78
+            string message = null;
  79
+            
  80
+            //encrypt
  81
+            string encryptedMessage = pc.Encrypt(message);
  82
+        }
  83
+
  84
+        /// <summary>
  85
+        /// Tests the null decryption.
  86
+        /// Assumes that the input message is  deserialized  
  87
+        /// </summary>        
  88
+        [Test]
  89
+        [ExpectedException(typeof(ArgumentNullException))]
  90
+        public void TestNullDecryption()
  91
+        {
  92
+            PubnubCrypto pc = new PubnubCrypto("enigma");
  93
+            //deserialized string
  94
+            string message = null;
  95
+            //decrypt
  96
+            string decryptedMessage = pc.Decrypt(message);
  97
+
  98
+            Assert.AreEqual("", decryptedMessage);
  99
+        }
  100
+
  101
+        /// <summary>
  102
+        /// Tests the yay decryption.
  103
+        /// Assumes that the input message is deserialized  
  104
+        /// Decrypted string should match yay!
  105
+        /// </summary>
  106
+        [Test]
  107
+        public void TestYayDecryptionBasic()
  108
+        {
  109
+            PubnubCrypto pc = new PubnubCrypto("enigma");
  110
+            string message = "q/xJqqN6qbiZMXYmiQC1Fw==";
  111
+            //decrypt
  112
+            string decryptedMessage = pc.Decrypt(message);
  113
+            //deserialize again
  114
+            Assert.AreEqual("yay!", decryptedMessage);
  115
+        }
  116
+
  117
+        /// <summary>
  118
+        /// Tests the yay encryption.
  119
+        /// The output is not serialized
  120
+        /// Encrypted string should match q/xJqqN6qbiZMXYmiQC1Fw==
  121
+        /// </summary>
  122
+        [Test]
  123
+        public void TestYayEncryptionBasic()
  124
+        {
  125
+            PubnubCrypto pc = new PubnubCrypto("enigma");
  126
+            //deserialized string
  127
+            string message = "yay!";
  128
+            //Encrypt
  129
+            string encryptedMessage = pc.Encrypt(message);
  130
+            Assert.AreEqual("q/xJqqN6qbiZMXYmiQC1Fw==", encryptedMessage);
  131
+        }
  132
+
  133
+        /// <summary>
  134
+        /// Tests the yay decryption.
  135
+        /// Assumes that the input message is not deserialized  
  136
+        /// Decrypted and Deserialized string should match yay!
  137
+        /// </summary>
  138
+        [Test]
  139
+        public void TestYayDecryption()
  140
+        {
  141
+            PubnubCrypto pc = new PubnubCrypto("enigma");
  142
+            //string strMessage= "\"q/xJqqN6qbiZMXYmiQC1Fw==\"";
  143
+            //Non deserialized string
  144
+            string message = "\"Wi24KS4pcTzvyuGOHubiXg==\"";
  145
+            //Deserialize
  146
+            message = JsonConvert.DeserializeObject<string>(message);
  147
+            //decrypt
  148
+            string decryptedMessage = pc.Decrypt(message);
  149
+            //deserialize again
  150
+            message = JsonConvert.DeserializeObject<string>(decryptedMessage);
  151
+            Assert.AreEqual("yay!", message);
  152
+        }
  153
+        /// <summary>
  154
+        /// Tests the yay encryption.
  155
+        /// The output is not serialized
  156
+        /// Encrypted string should match Wi24KS4pcTzvyuGOHubiXg==
  157
+        /// </summary>
  158
+        [Test]
  159
+        public void TestYayEncryption()
  160
+        {
  161
+            PubnubCrypto pc = new PubnubCrypto("enigma");
  162
+            //deserialized string
  163
+            string message = "yay!";
  164
+            //serialize the string
  165
+            message = JsonConvert.SerializeObject(message);
  166
+            Console.WriteLine(message);
  167
+            //Encrypt
  168
+            string enc = pc.Encrypt(message);
  169
+            Assert.AreEqual("Wi24KS4pcTzvyuGOHubiXg==", enc);
  170
+        }
  171
+
  172
+        /// <summary>
  173
+        /// Tests the array encryption.
  174
+        /// The output is not serialized
  175
+        /// Encrypted string should match the serialized object
  176
+        /// </summary>
  177
+        [Test]
  178
+        public void TestArrayEncryption()
  179
+        {
  180
+            PubnubCrypto pc = new PubnubCrypto("enigma");
  181
+            //create an empty array object
  182
+            object[] emptyArray = { };
  183
+            //serialize
  184
+            string serializedArray = JsonConvert.SerializeObject(emptyArray);
  185
+            //Encrypt
  186
+            string encryptedMessage = pc.Encrypt(serializedArray);
  187
+
  188
+            Assert.AreEqual("Ns4TB41JjT2NCXaGLWSPAQ==", encryptedMessage);
  189
+        }
  190
+
  191
+        /// <summary>
  192
+        /// Tests the array decryption.
  193
+        /// Assumes that the input message is deserialized
  194
+        /// And the output message has to been deserialized.
  195
+        /// Decrypted string should match the serialized object
  196
+        /// </summary>
  197
+        [Test]
  198
+        public void TestArrayDecryption()
  199
+        {
  200
+            PubnubCrypto pc = new PubnubCrypto("enigma");
  201
+            //Input the deserialized string
  202
+            string message = "Ns4TB41JjT2NCXaGLWSPAQ==";
  203
+            //decrypt
  204
+            string decryptedMessage = pc.Decrypt(message);
  205
+            //create a serialized object
  206
+            object[] emptyArrayObject = { };
  207
+            string result = JsonConvert.SerializeObject(emptyArrayObject);
  208
+            //compare the serialized object and the return of the Decrypt method
  209
+            Assert.AreEqual(result, decryptedMessage);
  210
+        }
  211
+
  212
+        /// <summary>
  213
+        /// Tests the object encryption.
  214
+        /// The output is not serialized
  215
+        /// Encrypted string should match the serialized object
  216
+        /// </summary>
  217
+        [Test]
  218
+        public void TestObjectEncryption()
  219
+        {
  220
+            PubnubCrypto pc = new PubnubCrypto("enigma");
  221
+            //create an object
  222
+            Object obj = new Object();
  223
+            //serialize
  224
+            string serializedObject = JsonConvert.SerializeObject(obj);
  225
+            //encrypt
  226
+            string encryptedMessage = pc.Encrypt(serializedObject);
  227
+
  228
+            Assert.AreEqual("IDjZE9BHSjcX67RddfCYYg==", encryptedMessage);
  229
+        }
  230
+        /// <summary>
  231
+        /// Tests the object decryption.
  232
+        /// Assumes that the input message is deserialized
  233
+        /// And the output message has to be deserialized.
  234
+        /// Decrypted string should match the serialized object
  235
+        /// </summary>
  236
+        [Test]
  237
+        public void TestObjectDecryption()
  238
+        {
  239
+            PubnubCrypto pc = new PubnubCrypto("enigma");
  240
+            //Deserialized
  241
+            string message = "IDjZE9BHSjcX67RddfCYYg==";
  242
+            //Decrypt
  243
+            string decryptedMessage = pc.Decrypt(message);
  244
+            //create an object
  245
+            Object obj = new Object();
  246
+            //Serialize the object
  247
+            string result = JsonConvert.SerializeObject(obj);
  248
+
  249
+            Assert.AreEqual(result, decryptedMessage);
  250
+        }
  251
+        /// <summary>
  252
+        /// Tests my object encryption.
  253
+        /// The output is not serialized 
  254
+        /// Encrypted string should match the serialized object
  255
+        /// </summary>
  256
+        [Test]
  257
+        public void TestMyObjectEncryption()
  258
+        {
  259
+            PubnubCrypto pc = new PubnubCrypto("enigma");
  260
+            //create an object of the custom class
  261
+            CustomClass cc = new CustomClass();
  262
+            //serialize it
  263
+            string result = JsonConvert.SerializeObject(cc);
  264
+            //encrypt it
  265
+            string encryptedMessage = pc.Encrypt(result);
  266
+
  267
+            Assert.AreEqual("Zbr7pEF/GFGKj1rOstp0tWzA4nwJXEfj+ezLtAr8qqE=", encryptedMessage);
  268
+        }
  269
+        /// <summary>
  270
+        /// Tests my object decryption.
  271
+        /// The output is not deserialized
  272
+        /// Decrypted string should match the serialized object
  273
+        /// </summary>
  274
+        [Test]
  275
+        public void TestMyObjectDecryption()
  276
+        {
  277
+            PubnubCrypto pc = new PubnubCrypto("enigma");
  278
+            //Deserialized
  279
+            string message = "Zbr7pEF/GFGKj1rOstp0tWzA4nwJXEfj+ezLtAr8qqE=";
  280
+            //Decrypt
  281
+            string decryptedMessage = pc.Decrypt(message);
  282
+            //create an object of the custom class
  283
+            CustomClass cc = new CustomClass();
  284
+            //Serialize it
  285
+            string result = JsonConvert.SerializeObject(cc);
  286
+
  287
+            Assert.AreEqual(result, decryptedMessage);
  288
+        }
  289
+
  290
+        /// <summary>
  291
+        /// Tests the pub nub encryption2.
  292
+        /// The output is not serialized
  293
+        /// Encrypted string should match f42pIQcWZ9zbTbH8cyLwB/tdvRxjFLOYcBNMVKeHS54=
  294
+        /// </summary>
  295
+        [Test]
  296
+        public void TestPubNubEncryption2()
  297
+        {
  298
+            PubnubCrypto pc = new PubnubCrypto("enigma");
  299
+            //Deserialized
  300
+            string message = "Pubnub Messaging API 2";
  301
+            //serialize the message
  302
+            message = JsonConvert.SerializeObject(message);
  303
+            //encrypt
  304
+            string encryptedMessage = pc.Encrypt(message);
  305
+
  306
+            Assert.AreEqual("f42pIQcWZ9zbTbH8cyLwB/tdvRxjFLOYcBNMVKeHS54=", encryptedMessage);
  307
+        }
  308
+
  309
+        /// <summary>
  310
+        /// Tests the pub nub decryption2.
  311
+        /// Assumes that the input message is deserialized  
  312
+        /// Decrypted and Deserialized string should match Pubnub Messaging API 2
  313
+        /// </summary>
  314
+        [Test]
  315
+        public void TestPubNubDecryption2()
  316
+        {
  317
+            PubnubCrypto pc = new PubnubCrypto("enigma");
  318
+            //Deserialized string    
  319
+            string message = "f42pIQcWZ9zbTbH8cyLwB/tdvRxjFLOYcBNMVKeHS54=";
  320
+            //Decrypt
  321
+            string decryptedMessage = pc.Decrypt(message);
  322
+            //Deserialize
  323
+            message = JsonConvert.DeserializeObject<string>(decryptedMessage);
  324
+            Assert.AreEqual("Pubnub Messaging API 2", message);
  325
+        }
  326
+
  327
+        /// <summary>
  328
+        /// Tests the pub nub encryption1.
  329
+        /// The input is not serialized
  330
+        /// Encrypted string should match f42pIQcWZ9zbTbH8cyLwByD/GsviOE0vcREIEVPARR0=
  331
+        /// </summary>
  332
+        [Test]
  333
+        public void TestPubNubEncryption1()
  334
+        {
  335
+            PubnubCrypto pc = new PubnubCrypto("enigma");
  336
+            //non serialized string
  337
+            string message = "Pubnub Messaging API 1";
  338
+            //serialize
  339
+            message = JsonConvert.SerializeObject(message);
  340
+            //encrypt
  341
+            string encryptedMessage = pc.Encrypt(message);
  342
+
  343
+            Assert.AreEqual("f42pIQcWZ9zbTbH8cyLwByD/GsviOE0vcREIEVPARR0=", encryptedMessage);
  344
+        }
  345
+
  346
+        /// <summary>
  347
+        /// Tests the pub nub decryption1.
  348
+        /// Assumes that the input message is  deserialized  
  349
+        /// Decrypted and Deserialized string should match Pubnub Messaging API 1        
  350
+        /// </summary>
  351
+        [Test]
  352
+        public void TestPubNubDecryption1()
  353
+        {
  354
+            PubnubCrypto pc = new PubnubCrypto("enigma");
  355
+            //deserialized string
  356
+            string message = "f42pIQcWZ9zbTbH8cyLwByD/GsviOE0vcREIEVPARR0=";
  357
+            //decrypt
  358
+            string decryptedMessage = pc.Decrypt(message);
  359
+            //deserialize
  360
+            message = (decryptedMessage != "**DECRYPT ERROR**") ? JsonConvert.DeserializeObject<string>(decryptedMessage) : "";
  361
+            Assert.AreEqual("Pubnub Messaging API 1", message);
  362
+        }
  363
+
  364
+        /// <summary>
  365
+        /// Tests the stuff can encryption.
  366
+        /// The input is serialized
  367
+        /// Encrypted string should match zMqH/RTPlC8yrAZ2UhpEgLKUVzkMI2cikiaVg30AyUu7B6J0FLqCazRzDOmrsFsF
  368
+        /// </summary>
  369
+        [Test]
  370
+        public void TestStuffCanEncryption()
  371
+        {
  372
+            PubnubCrypto pc = new PubnubCrypto("enigma");
  373
+            //input serialized string
  374
+            string message = "{\"this stuff\":{\"can get\":\"complicated!\"}}";
  375
+            //encrypt
  376
+            string encryptedMessage = pc.Encrypt(message);
  377
+
  378
+            Assert.AreEqual("zMqH/RTPlC8yrAZ2UhpEgLKUVzkMI2cikiaVg30AyUu7B6J0FLqCazRzDOmrsFsF", encryptedMessage);
  379
+        }
  380
+
  381
+        /// <summary>
  382
+        /// Tests the stuffcan decryption.
  383
+        /// Assumes that the input message is  deserialized  
  384
+        /// </summary>
  385
+        [Test]
  386
+        public void TestStuffcanDecryption()
  387
+        {
  388
+            PubnubCrypto pc = new PubnubCrypto("enigma");
  389
+            //deserialized string
  390
+            string message = "zMqH/RTPlC8yrAZ2UhpEgLKUVzkMI2cikiaVg30AyUu7B6J0FLqCazRzDOmrsFsF";
  391
+            //decrypt
  392
+            string decryptedMessage = pc.Decrypt(message);
  393
+
  394
+            Assert.AreEqual("{\"this stuff\":{\"can get\":\"complicated!\"}}", decryptedMessage);
  395
+        }
  396
+
  397
+        /// <summary>
  398
+        /// Tests the hash encryption.
  399
+        /// The input is serialized
  400
+        /// Encrypted string should match GsvkCYZoYylL5a7/DKhysDjNbwn+BtBtHj2CvzC4Y4g=
  401
+        /// </summary>
  402
+        [Test]
  403
+        public void TestHashEncryption()
  404
+        {
  405
+            PubnubCrypto pc = new PubnubCrypto("enigma");
  406
+            //serialized string
  407
+            string message = "{\"foo\":{\"bar\":\"foobar\"}}";
  408
+            //encrypt
  409
+            string encryptedMessage = pc.Encrypt(message);
  410
+
  411
+            Assert.AreEqual("GsvkCYZoYylL5a7/DKhysDjNbwn+BtBtHj2CvzC4Y4g=", encryptedMessage);
  412
+        }
  413
+
  414
+        /// <summary>
  415
+        /// Tests the hash decryption.
  416
+        /// Assumes that the input message is  deserialized  
  417
+        /// </summary>        
  418
+        [Test]
  419
+        public void TestHashDecryption()
  420
+        {
  421
+            PubnubCrypto pc = new PubnubCrypto("enigma");
  422
+            //deserialized string
  423
+            string message = "GsvkCYZoYylL5a7/DKhysDjNbwn+BtBtHj2CvzC4Y4g=";
  424
+            //decrypt
  425
+            string decryptedMessage = pc.Decrypt(message);
  426
+
  427
+            Assert.AreEqual("{\"foo\":{\"bar\":\"foobar\"}}", decryptedMessage);
  428
+        }
  429
+
  430
+        /// <summary>
  431
+        /// Tests the unicode chars encryption.
  432
+        /// The input is not serialized
  433
+        /// </summary>
  434
+        [Test]
  435
+        public void TestUnicodeCharsEncryption()
  436
+        {
  437
+            PubnubCrypto pc = new PubnubCrypto("enigma");
  438
+            string message = "漢語";
  439
+
  440
+            message = JsonConvert.SerializeObject(message);
  441
+            Console.WriteLine(message);
  442
+            string encryptedMessage = pc.Encrypt(message);
  443
+            Console.WriteLine(encryptedMessage);
  444
+            Assert.AreEqual("+BY5/miAA8aeuhVl4d13Kg==", encryptedMessage);
  445
+        }
  446
+
  447
+        /// <summary>
  448
+        /// Tests the unicode decryption.
  449
+        /// Assumes that the input message is  deserialized  
  450
+        /// Decrypted and Deserialized string should match the unicode chars       
  451
+        /// </summary>
  452
+        [Test]
  453
+        public void TestUnicodeCharsDecryption()
  454
+        {
  455
+            PubnubCrypto pc = new PubnubCrypto("enigma");
  456
+            string message = "+BY5/miAA8aeuhVl4d13Kg==";
  457
+            //decrypt
  458
+            string decryptedMessage = pc.Decrypt(message);
  459
+            //deserialize
  460
+            message = (decryptedMessage != "**DECRYPT ERROR**") ? JsonConvert.DeserializeObject<string>(decryptedMessage) : "";
  461
+
  462
+            Assert.AreEqual("漢語", message);
  463
+        }
  464
+
  465
+        /// <summary>
  466
+        /// Tests the german chars decryption.
  467
+        /// Assumes that the input message is  deserialized  
  468
+        /// Decrypted and Deserialized string should match the unicode chars  
  469
+        /// </summary>
  470
+        [Test]
  471
+        public void TestGermanCharsDecryption()
  472
+        {
  473
+            PubnubCrypto pc = new PubnubCrypto("enigma");
  474
+            string message = "stpgsG1DZZxb44J7mFNSzg==";
  475
+            
  476
+            //decrypt
  477
+            string decryptedMessage = pc.Decrypt(message);
  478
+            //deserialize
  479
+            message = (decryptedMessage != "**DECRYPT ERROR**") ? JsonConvert.DeserializeObject<string>(decryptedMessage) : "";
  480
+
  481
+            Assert.AreEqual("ÜÖ", message);
  482
+        }
  483
+        
  484
+        /// <summary>
  485
+        /// Tests the german encryption.
  486
+        /// The input is not serialized
  487
+        /// </summary>
  488
+        [Test]
  489
+        public void TestGermanCharsEncryption()
  490
+        {
  491
+            PubnubCrypto pc = new PubnubCrypto("enigma");
  492
+            string message = "ÜÖ";
  493
+
  494
+            message = JsonConvert.SerializeObject(message);
  495
+            Console.WriteLine(message);
  496
+            string encryptedMessage = pc.Encrypt(message);
  497
+            Console.WriteLine(encryptedMessage);
  498
+            Assert.AreEqual("stpgsG1DZZxb44J7mFNSzg==", encryptedMessage);
  499
+        }
  500
+
  501
+        /// <summary>
  502
+        /// Tests the cipher.
  503
+        /// </summary>
  504
+        /*[Test]
  505
+        public void  TestCipher ()
  506
+        {
  507
+            PubnubCrypto pc = new PubnubCrypto("enigma");
  508
+
  509
+            string strCipher = pc.GetEncryptionKey();
  510
+
  511
+            Assert.AreEqual("67a4f45f0d1d9bc606486fc42dc49416", strCipher);
  512
+        }*/
  513
+
  514
+        static string EncodeNonAsciiCharacters(string value)
  515
+        {
  516
+            StringBuilder sb = new StringBuilder();
  517
+            foreach (char c in value)
  518
+            {
  519
+                if (c > 127)
  520
+                {
  521
+                    // This character is too big for ASCII
  522
+                    string encodedValue = "\\u" + ((int)c).ToString("x4");
  523
+                    sb.Append(encodedValue);
  524
+                }
  525
+                else
  526
+                {
  527
+                    sb.Append(c);
  528
+                }
  529
+            }
  530
+            return sb.ToString();
  531
+        }
  532
+
  533
+        static string DecodeEncodedNonAsciiCharacters(string value)
  534
+        {
  535
+            return Regex.Replace(
  536
+                value,
  537
+                @"\\u(?<Value>[a-zA-Z0-9]{4})",
  538
+                m =>
  539
+                {
  540
+                    return ((char)int.Parse(m.Groups["Value"].Value, NumberStyles.HexNumber)).ToString();
  541
+                });
  542
+        }
  543
+
  544
+
  545
+    }
  546
+}
237  csharp/3.4/PubNub-Messaging.Tests/PubNub-Messaging.Tests.csproj
... ...
@@ -1,119 +1,120 @@
1  
-<?xml version="1.0" encoding="utf-8"?>
2  
-<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3  
-  <PropertyGroup>
4  
-    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5  
-    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>