New test.
[mono.git] / mcs / class / Mono.Security / Test / tools / tlstest / tlsmulti.cs
1 //
2 // tlsmulti.cs: Multi-sessions TLS/SSL Test Program with async HttpWebRequest
3 //      based on tlstest.cs
4 //
5 // Author:
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // Copyright (C) 2004-2005 Novell (http://www.novell.com)
9 //
10
11 using System;
12 using System.Collections;
13 using System.Globalization;
14 using System.IO;
15 using System.Net;
16 using System.Net.Sockets;
17 using System.Reflection;
18 using System.Security.Cryptography.X509Certificates;
19 using System.Text;
20 using System.Threading;
21
22 using Mono.Security.Protocol.Tls;
23
24 public class State {
25
26         static ArrayList handleList = new ArrayList ();
27
28         private int id;
29         private HttpWebRequest request;
30         private ManualResetEvent handle;
31
32         public State (int id, HttpWebRequest req)
33         {
34                 this.id = id;
35                 request = req;
36                 handle = new ManualResetEvent (false);
37                 handleList.Add (handle);
38         }
39
40         public int Id {
41                 get { return id; }
42         }
43
44         public HttpWebRequest Request {
45                 get { return request; }
46         }
47
48         public void Complete ()
49         {
50                 handle.Set ();
51         }
52
53         static public void WaitAll ()
54         {
55                 if (handleList.Count > 0) {
56                         WaitHandle[] handles = (WaitHandle[]) handleList.ToArray (typeof (WaitHandle));
57                         WaitHandle.WaitAll (handles);
58                         handleList.Clear ();
59                 }
60         }
61 }
62
63 public class MultiTest {
64
65         static bool alone;
66
67         public static void Main (string[] args) 
68         {
69                 if (args.Length == 0) {
70                         Console.WriteLine ("usage: mono tlsmulti.exe url1 [url ...]");
71                         return;
72                 } else if (args.Length > 64) {
73                         Console.WriteLine ("WaitHandle has a limit of 64 handles so you cannot process {0} URLs.", args.Length);
74                         return;
75                 }
76
77                 alone = (args.Length == 1);
78                 ServicePointManager.CertificatePolicy = new TestCertificatePolicy ();
79
80                 int id = 1;
81                 foreach (string url in args) {
82                         Console.WriteLine ("GET #{0} at {1}", id, url);
83                         HttpWebRequest wreq = (HttpWebRequest) WebRequest.Create (url);
84                         State s = new State (id++, wreq);
85                         wreq.BeginGetResponse (new AsyncCallback (ResponseCallback), s);
86                 }
87
88                 State.WaitAll ();
89         }
90
91         private static void ResponseCallback (IAsyncResult result)
92         {
93                 State state = ((State) result.AsyncState);
94                 Console.WriteLine ("END #{0}", state.Id);
95                 HttpWebResponse response = (HttpWebResponse) state.Request.EndGetResponse (result);
96
97                 Stream stream = response.GetResponseStream ();
98                 StreamReader sr = new StreamReader (stream, Encoding.UTF8);
99                 string data = sr.ReadToEnd ();
100
101                 if (alone)
102                         Console.WriteLine (data);
103                 state.Complete ();
104         }
105
106         public class TestCertificatePolicy : ICertificatePolicy {
107
108                 public bool CheckValidationResult (ServicePoint sp, X509Certificate certificate, WebRequest request, int error)
109                 {
110                         // whatever the reason we do not stop the SSL connection
111                         return true;
112                 }
113         }
114 }