~ubuntu-branches/ubuntu/quantal/smuxi/quantal

« back to all changes in this revision

Viewing changes to lib/jabber-net/test/bedrock/net/SSLAsyncSocketTest.cs

  • Committer: Package Import Robot
  • Author(s): Mirco Bauer
  • Date: 2012-01-07 12:13:22 UTC
  • mfrom: (1.1.9) (22.1.4 sid)
  • Revision ID: package-import@ubuntu.com-20120107121322-x3tav345lpck4ty2
Tags: 0.8.9.1-3
* [7cd3a08] Ignore secur32(.dll) ModuleRef for dh_clideps
* [0055fc3] Added missing ncurses dllmap for smuxi-frontend-stfl.exe

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* --------------------------------------------------------------------------
 
2
 * Copyrights
 
3
 *
 
4
 * Portions created by or assigned to Cursive Systems, Inc. are
 
5
 * Copyright (c) 2002-2008 Cursive Systems, Inc.  All Rights Reserved.  Contact
 
6
 * information for Cursive Systems, Inc. is available at
 
7
 * http://www.cursive.net/.
 
8
 *
 
9
 * License
 
10
 *
 
11
 * Jabber-Net is licensed under the LGPL.
 
12
 * See LICENSE.txt for details.
 
13
 * --------------------------------------------------------------------------*/
 
14
using System;
 
15
 
 
16
using System.Threading;
 
17
using NUnit.Framework;
 
18
using bedrock.net;
 
19
using bedrock.util;
 
20
using System.Security.Cryptography.X509Certificates;
 
21
 
 
22
namespace test.bedrock.net
 
23
{
 
24
    /// <summary>
 
25
    ///  Not really async.
 
26
    /// </summary>
 
27
    [SVN(@"$Id$")]
 
28
    [Ignore("Fails due to certificate.")]
 
29
    [TestFixture]
 
30
    public class SSLAsyncSocketTest : ISocketEventListener
 
31
    {
 
32
        private static readonly System.Text.Encoding ENC = System.Text.Encoding.ASCII;
 
33
 
 
34
        private static readonly byte[] sbuf = ENC.GetBytes("01234567890123456789012345678901234567890123456789012345678901234567890123456789");
 
35
        private readonly object done = new object();
 
36
        private readonly object start = new object();
 
37
        private string success = null;
 
38
        private AsyncSocket m_listen;
 
39
        readonly Address a = new Address("localhost", 7003);
 
40
 
 
41
        private bool succeeded = true;
 
42
        private string errorMessage;
 
43
 
 
44
        [Test] public void Test_Write()
 
45
        {
 
46
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
 
47
            a.Resolve();
 
48
 
 
49
            lock(start)
 
50
            {
 
51
                new Thread(Server).Start();
 
52
                Monitor.Wait(start);
 
53
            }
 
54
 
 
55
            try
 
56
            {
 
57
                lock (done)
 
58
                {
 
59
                    new Thread(Client).Start();
 
60
                    Monitor.Wait(done);
 
61
 
 
62
                    Assert.IsTrue(succeeded, errorMessage);
 
63
                }
 
64
            }
 
65
            finally
 
66
            {
 
67
                m_listen.Close();
 
68
            }
 
69
 
 
70
            Assert.AreEqual("5678901234", success);
 
71
        }
 
72
 
 
73
        private void Client()
 
74
        {
 
75
 
 
76
            SocketWatcher c_w = new SocketWatcher(20);
 
77
            c_w.Synchronous = true;
 
78
 
 
79
            // Note: must have a client cert in your IE cert store.
 
80
            X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
 
81
            store.Open(OpenFlags.ReadOnly);
 
82
 
 
83
            if (store.Certificates.Count > 0)
 
84
            {
 
85
                c_w.LocalCertificate = store.Certificates[0];
 
86
            }
 
87
            else
 
88
            {
 
89
                lock (done)
 
90
                {
 
91
                    errorMessage = "There were no certificates in the Windows Certificate Store.";
 
92
                    succeeded = false;
 
93
                    Monitor.Pulse(done);
 
94
                }
 
95
 
 
96
                return;
 
97
            }
 
98
            c_w.CreateConnectSocket(this, a, true, "localhost");
 
99
        }
 
100
 
 
101
        private void Server()
 
102
        {
 
103
            SocketWatcher s_w = new SocketWatcher(20);
 
104
 
 
105
            //s_w.RequireClientCert = true;
 
106
 
 
107
            X509Certificate2 c2;
 
108
            X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
 
109
            store.Open(OpenFlags.ReadWrite);
 
110
            X509Certificate2Collection cert = store.Certificates.Find(X509FindType.FindBySubjectName, "localhost", true);
 
111
            if (cert.Count == 0)
 
112
            {
 
113
                c2 = new X509Certificate2("../../localhost-cert.p12", "test");
 
114
                store.Add(c2);
 
115
            }
 
116
            else
 
117
            {
 
118
                c2 = cert[0];
 
119
            }
 
120
            Assert.IsTrue(c2.HasPrivateKey);
 
121
            Assert.IsNotNull(c2.PrivateKey);
 
122
            Assert.AreEqual(typeof(X509Certificate2), c2.GetType());
 
123
 
 
124
            cert = store.Certificates.Find(X509FindType.FindByThumbprint, c2.GetCertHashString(), false);
 
125
            c2 = cert[0];
 
126
            Assert.AreEqual(typeof(X509Certificate2), c2.GetType());
 
127
            Assert.IsTrue(c2.HasPrivateKey);
 
128
            Assert.IsNotNull(c2.PrivateKey);
 
129
            store.Close();
 
130
            s_w.LocalCertificate = c2;
 
131
            s_w.Synchronous = true;
 
132
 
 
133
            m_listen = s_w.CreateListenSocket(this, a, true);
 
134
            lock(start)
 
135
            {
 
136
                Monitor.Pulse(start);
 
137
            }
 
138
 
 
139
            try
 
140
            {
 
141
                m_listen.RequestAccept();
 
142
            }
 
143
            catch (Exception ex)
 
144
            {
 
145
                lock (done)
 
146
                {
 
147
                    succeeded = false;
 
148
                    errorMessage = ex.Message;
 
149
                    Monitor.Pulse(done);
 
150
                }
 
151
            }
 
152
        }
 
153
 
 
154
        #region Implementation of ISocketEventListener
 
155
        public bool OnAccept(BaseSocket newsocket)
 
156
        {
 
157
            Assert.IsTrue(((AsyncSocket)newsocket).IsMutuallyAuthenticated);
 
158
            newsocket.RequestRead();
 
159
            return false;
 
160
        }
 
161
 
 
162
        public bool OnRead(BaseSocket sock, byte[] buf, int offset, int length)
 
163
        {
 
164
            success = ENC.GetString(buf, offset, length);
 
165
            lock(done)
 
166
            {
 
167
                Monitor.Pulse(done);
 
168
            }
 
169
            return false;
 
170
        }
 
171
 
 
172
        public void OnWrite(BaseSocket sock, byte[] buf, int offset, int length)
 
173
        {
 
174
            System.Diagnostics.Debug.WriteLine(ENC.GetString(buf, offset, length));
 
175
            sock.Close();
 
176
        }
 
177
 
 
178
        public void OnError(BaseSocket sock, Exception ex)
 
179
        {
 
180
 
 
181
        }
 
182
 
 
183
        public void OnConnect(BaseSocket sock)
 
184
        {
 
185
            sock.Write(sbuf, 5, 10);
 
186
        }
 
187
 
 
188
        public void OnClose(BaseSocket sock)
 
189
        {
 
190
 
 
191
        }
 
192
 
 
193
        public void OnInit(BaseSocket new_sock)
 
194
        {
 
195
 
 
196
        }
 
197
 
 
198
        public ISocketEventListener GetListener(BaseSocket new_sock)
 
199
        {
 
200
            return this;
 
201
        }
 
202
 
 
203
        public bool OnInvalidCertificate(BaseSocket sock,
 
204
            System.Security.Cryptography.X509Certificates.X509Certificate certificate,
 
205
            System.Security.Cryptography.X509Certificates.X509Chain chain,
 
206
            System.Net.Security.SslPolicyErrors sslPolicyErrors)
 
207
        {
 
208
            return true;
 
209
        }
 
210
 
 
211
        #endregion
 
212
 
 
213
        private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
 
214
        {
 
215
            System.Diagnostics.Debug.WriteLine(e.ExceptionObject.ToString());
 
216
        }
 
217
    }
 
218
}