2007-03-22 Adar Wesley <adarw@mainsoft.com>
[mono.git] / mcs / class / System.Web / Test / System.Web / HttpWorkerRequestCas.cs
1 //
2 // HttpWorkerRequestCas.cs - CAS unit tests for System.Web.HttpWorkerRequest
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using NUnit.Framework;
30
31 using System;
32 using System.Collections;
33 using System.IO;
34 using System.Reflection;
35 using System.Security;
36 using System.Security.Permissions;
37 using System.Text;
38 using System.Web;
39 using System.Web.Caching;
40
41 namespace MonoCasTests.System.Web {
42
43         class CasHttpWorkerRequest : HttpWorkerRequest {
44
45                 public override void EndOfRequest ()
46                 {
47                 }
48
49                 public override void FlushResponse (bool finalFlush)
50                 {
51                 }
52
53                 public override string GetHttpVerbName ()
54                 {
55                         return null;
56                 }
57
58                 public override string GetHttpVersion ()
59                 {
60                         return null;
61                 }
62
63                 public override string GetLocalAddress ()
64                 {
65                         return null;
66                 }
67
68                 public override int GetLocalPort ()
69                 {
70                         return 0;
71                 }
72
73                 public override string GetQueryString ()
74                 {
75                         return null;
76                 }
77
78                 public override string GetRawUrl ()
79                 {
80                         return null;
81                 }
82
83                 public override string GetRemoteAddress ()
84                 {
85                         return null;
86                 }
87
88                 public override int GetRemotePort ()
89                 {
90                         return 0;
91                 }
92
93                 public override string GetUriPath ()
94                 {
95                         return null;
96                 }
97
98                 public override void SendKnownResponseHeader (int index, string value)
99                 {
100                 }
101
102                 public override void SendResponseFromFile (IntPtr handle, long offset, long length)
103                 {
104                 }
105
106                 public override void SendResponseFromFile (string filename, long offset, long length)
107                 {
108                 }
109
110                 public override void SendResponseFromMemory (byte[] data, int length)
111                 {
112                 }
113
114                 public override void SendStatus (int statusCode, string statusDescription)
115                 {
116                 }
117
118                 public override void SendUnknownResponseHeader (string name, string value)
119                 {
120                 }
121         }
122
123         [TestFixture]
124         [Category ("CAS")]
125         public class HttpWorkerRequestCas {
126
127                 [SetUp]
128                 public virtual void SetUp ()
129                 {
130                         if (!SecurityManager.SecurityEnabled)
131                                 Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
132                 }
133
134                 [Test]
135                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
136                 public void Properties_Deny_Unrestricted ()
137                 {
138                         CasHttpWorkerRequest hwr = new CasHttpWorkerRequest ();
139                         Assert.IsNull (hwr.MachineConfigPath, "MachineConfigPath");
140                         Assert.IsNull (hwr.MachineInstallDirectory, "MachineInstallDirectory");
141 #if NET_2_0
142                         Assert.IsNotNull (hwr.RequestTraceIdentifier, "RequestTraceIdentifier");
143                         Assert.IsNull (hwr.RootWebConfigPath, "RootWebConfigPath");
144 #endif
145                 }
146
147                 private void Callback (HttpWorkerRequest wr, object extraData)
148                 {
149                 }
150
151                 [Test]
152                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
153                 public void Methods_Deny_Unrestricted ()
154                 {
155                         CasHttpWorkerRequest hwr = new CasHttpWorkerRequest ();
156                         hwr.CloseConnection ();
157                         Assert.IsNull (hwr.GetAppPath (), "GetAppPath");
158                         Assert.IsNull (hwr.GetAppPathTranslated (), "GetAppPathTranslated");
159                         Assert.IsNull (hwr.GetAppPoolID (), "GetAppPoolID");
160                         Assert.AreEqual (0, hwr.GetBytesRead (), "GetBytesRead");
161                         Assert.IsNull (hwr.GetFilePath (), "GetFilePath");
162                         Assert.IsNull (hwr.GetFilePathTranslated (), "GetGetFilePathTranslated");
163                         Assert.IsNull (hwr.GetKnownRequestHeader (0), "GetKnownRequestHeader");
164                         Assert.AreEqual (String.Empty, hwr.GetPathInfo (), "GetPathInfo");
165                         Assert.IsNull (hwr.GetPreloadedEntityBody (), "GetPreloadedEntityBody");
166                         Assert.AreEqual ("http", hwr.GetProtocol (), "GetProtocol");
167                         Assert.IsNull (hwr.GetQueryStringRawBytes (), "GetQueryStringRawBytes");
168                         Assert.AreEqual (0, hwr.GetRequestReason (), "GetRequestReason");
169                         Assert.IsNull (hwr.GetServerVariable (null), "GetServerVariable");
170                         Assert.IsNull (hwr.GetUnknownRequestHeader (null), "GetUnknownRequestHeader");
171                         Assert.IsNull (hwr.GetUnknownRequestHeaders (), "GetUnknownRequestHeaders");
172                         Assert.AreEqual (IntPtr.Zero, hwr.GetUserToken (), "GetUserToken");
173                         Assert.IsFalse (hwr.HasEntityBody (), "HasEntityBody");
174                         Assert.IsTrue (hwr.HeadersSent (), "HeadersSent");
175                         Assert.IsTrue (hwr.IsClientConnected (), "IsClientConnected");
176                         Assert.IsFalse (hwr.IsEntireEntityBodyIsPreloaded (), "IsEntireEntityBodyIsPreloaded");
177                         Assert.IsFalse (hwr.IsSecure (), "IsSecure");
178                         Assert.IsNull (hwr.MapPath (null), "MapPath");
179
180                         try {
181                                 Assert.AreEqual (0, hwr.ReadEntityBody (new byte[1], 1), "ReadEntityBody(byte[],int)");
182                         }
183                         catch (NotImplementedException) {
184                                 // mono
185                         }
186
187                         try {
188                                 hwr.SendCalculatedContentLength (0);
189                         }
190                         catch (NotImplementedException) {
191                                 // mono
192                         }
193
194                         hwr.SendResponseFromMemory (IntPtr.Zero, 0);
195                         hwr.SetEndOfSendNotification (new HttpWorkerRequest.EndOfSendNotification (Callback), null);
196
197                         Assert.IsNotNull (hwr.GetClientCertificate (), "GetClientCertificate");
198                         Assert.IsNotNull (hwr.GetClientCertificateBinaryIssuer (), "GetClientCertificateBinaryIssuer");
199                         Assert.AreEqual (0, hwr.GetClientCertificateEncoding (), "GetClientCertificateEncoding");
200                         Assert.IsNotNull (hwr.GetClientCertificatePublicKey (), "GetClientCertificatePublicKey");
201                         DateTime dt = DateTime.Now.AddMinutes (1);
202                         Assert.IsTrue (hwr.GetClientCertificateValidFrom () < dt, "GetClientCertificateValidFrom");
203                         Assert.IsTrue (hwr.GetClientCertificateValidUntil () < dt, "GetClientCertificateValidUntil");
204                         Assert.AreEqual (0, hwr.GetConnectionID (), "GetConnectionID");
205                         Assert.AreEqual (0, hwr.GetUrlContextID (), "GetUrlContextID");
206                         Assert.AreEqual (IntPtr.Zero, hwr.GetVirtualPathToken (), "GetVirtualPathToken");
207 #if NET_2_0
208                         Assert.AreEqual (0, hwr.GetPreloadedEntityBody (new byte[0], 0), "GetPreloadedEntityBody(byte[],int)");
209                         Assert.AreEqual (0, hwr.GetPreloadedEntityBodyLength (), "GetPreloadedEntityBodyLength");
210                         Assert.AreEqual (0, hwr.GetTotalEntityBodyLength (), "GetTotalEntityBodyLength");
211                         Assert.AreEqual (0, hwr.ReadEntityBody (new byte[1], 0, 1), "ReadEntityBody(byte[],int,int)");
212 #endif
213                 }
214
215                 [Test]
216                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
217                 public void StaticMethods_Deny_Unrestricted ()
218                 {
219                         Assert.AreEqual (-1, HttpWorkerRequest.GetKnownRequestHeaderIndex ("mono"), "GetKnownRequestHeaderIndex");
220                         Assert.AreEqual ("Cache-Control", HttpWorkerRequest.GetKnownRequestHeaderName (0), "GetKnownRequestHeaderName");
221                         Assert.AreEqual (-1, HttpWorkerRequest.GetKnownResponseHeaderIndex ("mono"), "GetKnownResponseHeaderIndex");
222                         Assert.AreEqual ("Cache-Control", HttpWorkerRequest.GetKnownResponseHeaderName (0), "GetKnownResponseHeaderName");
223                         Assert.AreEqual ("OK", HttpWorkerRequest.GetStatusDescription (200), "GetStatusDescription");
224                 }
225         }
226 }