Added tests for Task.WhenAll w/ empty list
[mono.git] / mcs / class / Mono.Security / Mono.Security.Protocol.Ntlm / MessageBase.cs
1 //
2 // Mono.Security.Protocol.Ntlm.MessageBase
3 //      abstract class for all NTLM messages
4 //
5 // Author:
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // Copyright (C) 2003 Motus Technologies Inc. (http://www.motus.com)
9 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
10 //
11 // References
12 // a.   NTLM Authentication Scheme for HTTP, Ronald Tschalär
13 //      http://www.innovation.ch/java/ntlm.html
14 // b.   The NTLM Authentication Protocol, Copyright © 2003 Eric Glass
15 //      http://davenport.sourceforge.net/ntlm.html
16 //
17 // Permission is hereby granted, free of charge, to any person obtaining
18 // a copy of this software and associated documentation files (the
19 // "Software"), to deal in the Software without restriction, including
20 // without limitation the rights to use, copy, modify, merge, publish,
21 // distribute, sublicense, and/or sell copies of the Software, and to
22 // permit persons to whom the Software is furnished to do so, subject to
23 // the following conditions:
24 // 
25 // The above copyright notice and this permission notice shall be
26 // included in all copies or substantial portions of the Software.
27 // 
28 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 //
36
37 using System;
38 using System.Globalization;
39
40 namespace Mono.Security.Protocol.Ntlm {
41
42         public abstract class MessageBase {
43
44                 static private byte[] header = { 0x4e, 0x54, 0x4c, 0x4d, 0x53, 0x53, 0x50, 0x00 };
45                 
46                 private int _type;
47                 private NtlmFlags _flags;
48
49                 protected MessageBase (int messageType) 
50                 {
51                         _type = messageType;
52                 }
53                 
54                 public NtlmFlags Flags {
55                         get { return _flags; }
56                         set { _flags = value; }
57                 }
58
59                 public int Type { 
60                         get { return _type; }
61                 }
62
63                 protected byte[] PrepareMessage (int messageSize) 
64                 {
65                         byte[] message = new byte [messageSize];
66                         Buffer.BlockCopy (header, 0, message, 0, 8);
67                         
68                         message [ 8] = (byte) _type;
69                         message [ 9] = (byte)(_type >> 8);
70                         message [10] = (byte)(_type >> 16);
71                         message [11] = (byte)(_type >> 24);
72
73                         return message;
74                 }
75
76                 protected virtual void Decode (byte[] message) 
77                 {
78                         if (message == null)
79                                 throw new ArgumentNullException ("message");
80
81                         if (message.Length < 12) {
82                                 string msg = Locale.GetText ("Minimum message length is 12 bytes.");
83                                 throw new ArgumentOutOfRangeException ("message", message.Length, msg);
84                         }
85
86                         if (!CheckHeader (message)) {
87                                 string msg = String.Format (Locale.GetText ("Invalid Type{0} message."), _type);
88                                 throw new ArgumentException (msg, "message");
89                         }
90                 }
91
92
93                 protected bool CheckHeader (byte[] message) 
94                 {
95                         for (int i=0; i < header.Length; i++) {
96                                 if (message [i] != header [i])
97                                         return false;
98                         }
99                         return (BitConverterLE.ToUInt32 (message, 8) == _type);
100                 }
101
102                 public abstract byte[] GetBytes ();
103         }
104 }