2009-09-22 Michael Barker <mike@middlesoft.co.uk>
[mono.git] / mcs / class / System.Messaging / Test / System.Messaging / BasicMessagingTest.cs
1 //
2 // Test.Mono.Messaging.RabbitMQ
3 //
4 // Authors:
5 //        Michael Barker (mike@middlesoft.co.uk)
6 //
7 // (C) 2008 Michael Barker
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System;
32 using System.Messaging;
33 using System.Reflection;
34 using System.Threading;
35 using System.Text.RegularExpressions;
36
37 using NUnit.Framework;
38
39 namespace MonoTests.System.Messaging
40 {
41         [TestFixture]
42         public class BasicMessageTest {
43
44                 [Test]
45                 public void SendReceiveBinaryMessage ()
46                 {
47                         MessageQueue mq = MQUtil.GetQueue ();
48                         String s = "Test: " + DateTime.Now;
49                         Message m = new Message (s, new BinaryMessageFormatter ());
50                         m.CorrelationId = Guid.NewGuid () + "\\0";
51                         mq.MessageReadPropertyFilter.SetAll ();
52
53                         mq.Send (m);
54
55                         Message m2 = mq.Receive ();
56                         m2.Formatter = new BinaryMessageFormatter ();
57                         Assert.AreEqual (s, m2.Body);
58
59                         //Assert.IsTrue (DateTime.MinValue == m.ArrivedTime);
60                         Assert.IsNotNull(m2.Id, "Id is null");
61                         Assert.IsTrue (Guid.Empty.ToString () !=  m2.Id, "Id is Empty");
62                         Assert.IsTrue (DateTime.MinValue != m2.ArrivedTime, "Arrived Time is not set");
63                         Assert.AreEqual (Acknowledgment.None, m2.Acknowledgment, "Acknowledgment");
64                         Assert.AreEqual (m.CorrelationId, m2.CorrelationId, "CorrelationId not set properly");
65                         Assert.IsTrue (0 != m2.SenderVersion);
66                         // TODO: This is not supported on a workgroup installation.
67                         //Assert.IsNotNull (m2.SourceMachine, "SourceMachine is null");
68                         Assert.AreEqual (mq.QueueName, m2.DestinationQueue.QueueName, "Destination Queue not set");
69                         
70                         mq.Close ();
71                 }
72                 
73                 [Test]
74                 public void SendMessageWithLabel ()
75                 {
76                         MessageQueue mq = MQUtil.GetQueue ();
77                         String label = "mylabel";
78                         String s = "Test: " + DateTime.Now;
79                         Message m = new Message (s, new BinaryMessageFormatter ());
80                         m.CorrelationId = Guid.NewGuid () + "\\0" ;
81
82                         mq.Send (m, label);
83
84                         Message m2 = mq.Receive ();
85                         m2.Formatter = new BinaryMessageFormatter ();
86                         Assert.AreEqual (s, m2.Body, "Message not passed correctly");
87                         Assert.AreEqual (label, m2.Label, "Label not passed correctly");
88                 }
89                 
90                 
91                 [Test]
92                 public void CheckDefaults ()
93                 {
94                         Message m = new Message ("Test", new BinaryMessageFormatter ());
95                         Assert.AreEqual (true, m.AttachSenderId, "AttachSenderId has incorrect default");
96                         Assert.AreEqual (Guid.Empty.ToString () + "\\0", m.Id, "Id has incorrect default");
97                         Assert.AreEqual ("Microsoft Base Cryptographic Provider, Ver. 1.0", 
98                                          m.AuthenticationProviderName, 
99                                          "AuthenticationProviderName has incorrect default"); 
100                         Assert.AreEqual (0, m.Extension.Length, "Extension has incorrect default");
101                         Assert.AreEqual ("", m.Label, "Label has incorrect default");
102                         Assert.IsFalse (m.Recoverable, "Recoverable has incorrect default");
103                         Assert.IsFalse (m.IsFirstInTransaction, "IsFirstInTransaction has incorrect default");
104                         Assert.IsFalse (m.IsLastInTransaction, "IsLastInTransaction has incorrect default");
105                         Assert.AreEqual ("", m.TransactionId, "TransactionId has incorrect default");
106                         Assert.AreEqual (MessagePriority.Normal, m.Priority, "MessagePriority has incorrect default");
107                 }
108                 
109                 private static void CheckInvalidOperation (Message m, String property)
110                 {
111                         PropertyInfo pi = m.GetType ().GetProperty (property);
112                         try {
113                                 Assert.IsNotNull (pi, "Property not defined: " + property);
114                                 object o = pi.GetValue (m, null);
115                                 Assert.Fail (property + ": " + o);
116                         } catch (InvalidOperationException) {
117                         } catch (TargetInvocationException e) {
118                                 Assert.AreEqual (typeof (InvalidOperationException), 
119                                                  e.InnerException.GetType ());
120                         }
121                 }
122                 
123                 [Test]
124                 public void CheckInvalidPropertyOperations ()
125                 {
126                         Message m = new Message ("Test", new BinaryMessageFormatter ());
127                         CheckInvalidOperation (m, "Acknowledgment");
128                         CheckInvalidOperation (m, "ArrivedTime");
129                         CheckInvalidOperation (m, "Authenticated");
130                         CheckInvalidOperation (m, "DestinationQueue");
131                         //CheckInvalidOperation (m, "Id");
132                         //CheckInvalidOperation (m, "IsFirstInTransaction");
133                         //CheckInvalidOperation (m, "IsLastInTransaction");
134                         // TODO: Support 2.0 features.
135                         //CheckInvalidOperation (m, "LookupId");
136                         CheckInvalidOperation (m, "MessageType");
137                         CheckInvalidOperation (m, "SenderId");
138                         CheckInvalidOperation (m, "SenderVersion");
139                         CheckInvalidOperation (m, "SentTime");
140                         CheckInvalidOperation (m, "SourceMachine");
141                         //CheckInvalidOperation (m, "TransactionId");
142                 }
143                 
144                 private static void CheckArgumentInvalid(Message m, String property, Type exceptionType)
145                 {
146                         PropertyInfo pi = m.GetType().GetProperty(property);
147                         try {
148                                 Assert.IsNotNull(pi, "Property not defined: " + property);
149                                 pi.SetValue(m, null, null);
150                                 Assert.Fail(property);
151                         } catch (InvalidOperationException) {
152                         } catch (TargetInvocationException e) {
153                                 Assert.AreEqual(exceptionType,
154                                                 e.InnerException.GetType(),
155                                                 property);
156                         }
157                 }
158
159                 [Test]
160                 public void CheckArgumentInvalidForProperties ()
161                 {
162                         Message m = new Message ("Stuff");
163                         CheckArgumentInvalid (m, "DestinationSymmetricKey", typeof (ArgumentNullException));
164                         CheckArgumentInvalid (m, "DigitalSignature", typeof(ArgumentNullException));
165                         CheckArgumentInvalid (m, "Extension", typeof(ArgumentNullException));
166                 }
167
168                 [Test]
169                 public void SendReceiveBinaryMessageWithAllPropertiesSet ()
170                 {
171                         MessageQueue mq = MQUtil.GetQueue ();
172                         mq.MessageReadPropertyFilter.SetAll ();
173                         
174                         MessageQueue adminQ = MQUtil.GetQueue (@".\private$\myadmin");
175                         MessageQueue responseQ = MQUtil.GetQueue (@".\private$\myresponse");
176                         Guid connectorType = Guid.NewGuid ();
177                         String s = "Test: " + DateTime.Now;
178                         
179                         Message m = new Message (s, new BinaryMessageFormatter ());
180                         m.CorrelationId = Guid.NewGuid () + "\\0";
181                         m.AcknowledgeType = AcknowledgeTypes.PositiveArrival;
182                         m.AdministrationQueue = adminQ;
183                         m.AppSpecific = 5;
184                         //m.AuthenticationProviderName = "Test Provider Name";
185                         //m.AuthenticationProviderType = CryptographicProviderType.None;
186                         //m.ConnectorType = connectorType;
187                         //m.DestinationSymmetricKey = new byte[] { 0x0A, 0x0B, 0x0C };
188                         //m.DigitalSignature = new byte[] { 0x0C, 0x0D, 0x0E };
189                         //m.EncryptionAlgorithm = EncryptionAlgorithm.Rc4;
190                         m.Extension = new byte[] { 0x01, 0x02, 0x03 };
191                         //m.HashAlgorithm = HashAlgorithm.Sha;
192                         m.Label = "MyLabel";
193                         m.Priority = MessagePriority.AboveNormal;
194                         m.Recoverable = true;
195                         m.ResponseQueue = responseQ;
196                         m.SenderCertificate = new byte[] { 0x04, 0x05, 0x06 };
197                         m.TimeToBeReceived = new TimeSpan(0, 0, 10);
198                         m.TimeToReachQueue = new TimeSpan(0, 0, 5);
199                         //m.UseAuthentication = true;
200                         m.UseDeadLetterQueue = true;
201                         //m.UseEncryption = true;
202                         
203                         mq.Send (m);
204                         
205                         Message m2 = mq.Receive ();
206                         
207                         m2.Formatter = new BinaryMessageFormatter ();
208                         Assert.AreEqual (s, m2.Body);
209                         
210                         Assert.AreEqual (AcknowledgeTypes.PositiveArrival, m2.AcknowledgeType, 
211                                          "AcknowledgeType not passed correctly");
212                         Assert.AreEqual (adminQ.QueueName, m2.AdministrationQueue.QueueName, 
213                                          "AdministrationQueue not passed correctly");
214                         Assert.AreEqual (5, m2.AppSpecific, "AppSpecific not passed correctly");
215                         //Assert.AreEqual (m.AuthenticationProviderName, m2.AuthenticationProviderName,
216                         //                 "AuthenticationProviderName not passed correctly");
217                         //Assert.AreEqual (m.AuthenticationProviderType, m2.AuthenticationProviderType,
218                         //                 "AuthenticationProviderType not passed correctly");
219                         //Assert.AreEqual (connectorType, m2.ConnectorType, 
220                         //                 "ConnectorType not passed correctly");
221                         Assert.AreEqual (m.CorrelationId, m2.CorrelationId, 
222                                          "CorrelationId not passed correctly");
223                         //AreEqual (m.DestinationSymmetricKey, m2.DestinationSymmetricKey, 
224                         //          "DestinationSymmetricKey not passed correctly");
225                         //AreEqual (m.DigitalSignature, m2.DigitalSignature,
226                         //          "DigitalSignature not passed properly");
227                         //Assert.AreEqual (EncryptionAlgorithm.Rc4, m2.EncryptionAlgorithm,
228                         //                 "EncryptionAlgorithm not passed properly");
229                         AreEqual (m.Extension, m2.Extension, "Extension not passed properly");
230                         //Assert.AreEqual (m.HashAlgorithm, m2.HashAlgorithm,
231                         //                 "HashAlgorithm not passed properly");
232                         Assert.AreEqual (m.Label, m2.Label, "Label not passed correctly");
233                         Assert.AreEqual (MessagePriority.AboveNormal, m2.Priority,
234                                          "Priority not passed properly");
235                         Assert.AreEqual (true, m2.Recoverable, "Recoverable not passed properly");
236                         Assert.AreEqual (responseQ.QueueName, m2.ResponseQueue.QueueName,
237                                          "ResponseQueue not passed properly");
238                         AreEqual (m.SenderCertificate, m2.SenderCertificate,
239                                   "SenderCertificate not passed properly");
240                         Assert.AreEqual (m.TimeToBeReceived, m2.TimeToBeReceived,
241                                          "TimeToBeReceived not passed properly");
242                         Assert.AreEqual (m.TimeToReachQueue, m2.TimeToReachQueue,
243                                          "TimeToReachQueue not passed properly");
244                         //Assert.IsTrue (m2.UseAuthentication,
245                         //               "UseAuthentication not passed properly");
246                         Assert.IsTrue (m2.UseDeadLetterQueue,
247                                        "UseDeadLetterQueue not passed properly");
248                         //Assert.IsTrue (m2.UseEncryption, "UseEncryption not pass properly");
249                         //Assert.AreEqual ();
250                         
251                         Assert.IsNotNull (m2.Id, "Id is null");
252                         Assert.IsTrue (Guid.Empty.ToString () !=  m2.Id, "Id is Empty");
253                         Assert.IsTrue (DateTime.MinValue != m2.ArrivedTime, "Arrived Time is not set");
254                         Assert.AreEqual (Acknowledgment.None, m2.Acknowledgment, "Acknowledgment");
255                         Assert.IsTrue (0 != m2.SenderVersion);
256                         
257                         //Assert.IsNotNull (m2.SourceMachine, "SourceMachine is null");
258                         
259                 }
260                 
261                 private static void AreEqual(byte[] expected, byte[] actual, string message)
262                 {
263                         Assert.AreEqual (expected.Length, actual.Length, message);
264                         for (int i = 0; i < expected.Length; i++)
265                                 Assert.AreEqual (expected[i], actual[i], message);
266                 }
267                 
268                 //[Test]
269                 // No supported by Rabbit
270                 public void SendPriorityMessages ()
271                 {
272                         MessageQueue mq = MQUtil.GetQueue ();
273                         Message sent1 = new Message ("Highest", new BinaryMessageFormatter ());
274                         sent1.Priority = MessagePriority.Highest;
275                         Message sent2 = new Message ("Lowest", new BinaryMessageFormatter ());
276                         sent2.Priority = MessagePriority.Lowest;
277                         
278                         mq.Send (sent1);
279                         mq.Send (sent2);
280                         
281                         Message received1 = mq.Receive ();
282                         Message received2 = mq.Receive ();
283                         
284                         Assert.AreEqual (MessagePriority.Highest, received2.Priority,
285                                          "Priority delivery incorrect");
286                         Assert.AreEqual (MessagePriority.Lowest, received1.Priority,
287                                          "Priority delivery incorrect");
288                 }
289                 
290                 [Test]
291                 public void SendReceiveXmlMessage ()
292                 {
293                         MessageQueue mq = MQUtil.GetQueue ();
294                         String s = "Test: " + DateTime.Now;
295                         Message m = new Message (s, new XmlMessageFormatter (new Type[] { typeof (string) }));
296                         mq.MessageReadPropertyFilter.SetAll();
297                    
298                         mq.Send (m);
299                         Message m2 = mq.Receive ();
300                         m2.Formatter = new XmlMessageFormatter (new Type[] { typeof (string) });
301                         Assert.AreEqual (s, m2.Body);
302                    
303                         Assert.AreEqual (Acknowledgment.None, m2.Acknowledgment, "Acknowledgment");
304                         Assert.IsNotNull (m2.ArrivedTime, "Acknowledgment");
305                         Assert.IsNotNull (m2.Id, "Id");
306                 }
307            
308                 [Test]
309                 public void SendBinaryText ()
310                 {
311                         string body = "This is a test";
312                    
313                         MessageQueue q = MQUtil.GetQueue ();
314
315                         q.Formatter = new BinaryMessageFormatter ();
316
317                         q.Send (body);
318
319                         Message m2 = q.Receive ();
320                    
321                         Assert.IsNotNull (m2.Formatter, "Formatter is null");
322                         Assert.AreEqual (typeof (BinaryMessageFormatter), m2.Formatter.GetType ());
323                         Assert.AreEqual (body, m2.Body);
324                 }
325
326                 [Test]
327                 public void SendDefaultText ()
328                 {
329                         string body = "This is a test";
330
331                         MessageQueue q = MQUtil.GetQueue (MQUtil.CreateQueueName (), new XmlMessageFormatter ());
332
333                         q.Send (body);
334
335                         Message m2 = q.Receive ();
336                         XmlMessageFormatter xmlf = (XmlMessageFormatter) q.Formatter;
337                         Assert.AreEqual (typeof (XmlMessageFormatter), m2.Formatter.GetType ());
338                         Assert.AreEqual (body, m2.Body);
339                         Assert.AreEqual (0, m2.BodyType);
340                 }
341
342                 [Test]
343                 public void SendBinaryObject ()
344                 {
345                         Thingy body = new Thingy ();
346                         body.MyProperty1 = 42;
347                         body.MyProperty2 = "Something";
348                         body.MyProperty3 = "Something else";
349
350                         MessageQueue q = MQUtil.GetQueue ();
351
352                         q.Formatter = new BinaryMessageFormatter ();
353
354                         q.Send (body);
355                    
356                         Message m2 = q.Receive ();
357                         Thingy body2 = (Thingy) m2.Body;
358
359                         Assert.AreEqual (typeof (BinaryMessageFormatter), m2.Formatter.GetType ());
360                         Assert.AreEqual (body.MyProperty1, body2.MyProperty1);
361                         Assert.AreEqual (body.MyProperty2, body2.MyProperty2);
362                         Assert.AreEqual (body.MyProperty3, body2.MyProperty3);
363                         Assert.AreEqual (768, m2.BodyType);
364                 }
365
366                 [Test]
367                 public void SendDefaultObject ()
368                 {
369                         string path = MQUtil.CreateQueueName ();
370                         Thingy body = new Thingy();
371                         body.MyProperty1 = 42;
372                         body.MyProperty2 = "Something";
373                         body.MyProperty3 = "Something else";
374
375                         MessageQueue q = MQUtil.GetQueue (path, new XmlMessageFormatter ());
376
377                         q.Send (body);
378
379                         MessageQueue q2 = MQUtil.GetQueue (path);
380                         q2.Formatter = new XmlMessageFormatter (new Type[] { typeof(Thingy) });
381
382                         Message m2 = q2.Receive ();
383                         Thingy body2 = (Thingy) m2.Body;
384
385                         Assert.AreEqual (typeof (XmlMessageFormatter), m2.Formatter.GetType ());
386                         Assert.AreEqual (body.MyProperty1, body2.MyProperty1);
387                         Assert.AreEqual (body.MyProperty2, body2.MyProperty2);
388                         Assert.AreEqual (body.MyProperty3, body2.MyProperty3);
389                 }
390
391                 [Test]
392                 public void SendBinaryMessage ()
393                 {
394                         Thingy body = new Thingy ();
395                         body.MyProperty1 = 42;
396                         body.MyProperty2 = "Something";
397                         body.MyProperty3 = "Something else";
398                         Message m1 = new Message (body);
399                         m1.Formatter = new BinaryMessageFormatter ();
400
401                         MessageQueue q = MQUtil.GetQueue ();
402
403                         q.Send (m1);
404
405                         Message m2 = q.Receive ();
406                         m2.Formatter = new BinaryMessageFormatter ();
407                         Assert.IsNotNull (m2.Formatter);
408                         Assert.AreEqual (typeof (BinaryMessageFormatter), m2.Formatter.GetType ());
409                         Thingy body2 = (Thingy) m2.Formatter.Read (m2);
410
411                         Assert.AreEqual (body.MyProperty1, body2.MyProperty1);
412                         Assert.AreEqual (body.MyProperty2, body2.MyProperty2);
413                         Assert.AreEqual (body.MyProperty3, body2.MyProperty3);
414                 }
415
416                 [Test]
417                 public void SendDefaultMessage ()
418                 {
419                         string path = MQUtil.CreateQueueName ();
420                         Thingy body = new Thingy ();
421                         body.MyProperty1 = 42;
422                         body.MyProperty2 = "Something";
423                         body.MyProperty3 = "Something else";
424                         Message m1 = new Message (body);
425                         Assert.IsNull (m1.Formatter);
426
427                         MessageQueue q = MQUtil.GetQueue (path, new XmlMessageFormatter ());
428
429                         q.Send (m1);
430
431                         Message m2 = q.Receive ();
432                         m2.Formatter = new XmlMessageFormatter (new Type[] { typeof (Thingy) });
433                         Assert.IsNotNull (m2.Formatter);
434                         Assert.AreEqual (typeof (XmlMessageFormatter), m2.Formatter.GetType ());
435                         Thingy body2 = (Thingy) m2.Formatter.Read (m2);
436
437                         Assert.AreEqual (body.MyProperty1, body2.MyProperty1);
438                         Assert.AreEqual (body.MyProperty2, body2.MyProperty2);
439                         Assert.AreEqual (body.MyProperty3, body2.MyProperty3);
440                 }                         
441         }
442
443         [Serializable]
444         public class Thingy
445         {
446                 private int myVar1;
447
448                 public int MyProperty1
449                 {
450                         get { return myVar1; }
451                         set { myVar1 = value; }
452                 }
453
454                 private string myVar2;
455
456                 public string MyProperty2
457                 {
458                         get { return myVar2; }
459                         set { myVar2 = value; }
460                 }
461
462                 private string myVar3;
463
464                 public string MyProperty3
465                 {
466                         get { return myVar3; }
467                         set { myVar3 = value; }
468                 }
469         }
470 }