1
/* --------------------------------------------------------------------------
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/.
11
* Jabber-Net is licensed under the LGPL.
12
* See LICENSE.txt for details.
13
* --------------------------------------------------------------------------*/
16
using System.Threading;
17
using NUnit.Framework;
20
using System.Security.Cryptography.X509Certificates;
22
namespace test.bedrock.net
28
[Ignore("Fails due to certificate.")]
30
public class SSLAsyncSocketTest : ISocketEventListener
32
private static readonly System.Text.Encoding ENC = System.Text.Encoding.ASCII;
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);
41
private bool succeeded = true;
42
private string errorMessage;
44
[Test] public void Test_Write()
46
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
51
new Thread(Server).Start();
59
new Thread(Client).Start();
62
Assert.IsTrue(succeeded, errorMessage);
70
Assert.AreEqual("5678901234", success);
76
SocketWatcher c_w = new SocketWatcher(20);
77
c_w.Synchronous = true;
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);
83
if (store.Certificates.Count > 0)
85
c_w.LocalCertificate = store.Certificates[0];
91
errorMessage = "There were no certificates in the Windows Certificate Store.";
98
c_w.CreateConnectSocket(this, a, true, "localhost");
101
private void Server()
103
SocketWatcher s_w = new SocketWatcher(20);
105
//s_w.RequireClientCert = true;
108
X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
109
store.Open(OpenFlags.ReadWrite);
110
X509Certificate2Collection cert = store.Certificates.Find(X509FindType.FindBySubjectName, "localhost", true);
113
c2 = new X509Certificate2("../../localhost-cert.p12", "test");
120
Assert.IsTrue(c2.HasPrivateKey);
121
Assert.IsNotNull(c2.PrivateKey);
122
Assert.AreEqual(typeof(X509Certificate2), c2.GetType());
124
cert = store.Certificates.Find(X509FindType.FindByThumbprint, c2.GetCertHashString(), false);
126
Assert.AreEqual(typeof(X509Certificate2), c2.GetType());
127
Assert.IsTrue(c2.HasPrivateKey);
128
Assert.IsNotNull(c2.PrivateKey);
130
s_w.LocalCertificate = c2;
131
s_w.Synchronous = true;
133
m_listen = s_w.CreateListenSocket(this, a, true);
136
Monitor.Pulse(start);
141
m_listen.RequestAccept();
148
errorMessage = ex.Message;
154
#region Implementation of ISocketEventListener
155
public bool OnAccept(BaseSocket newsocket)
157
Assert.IsTrue(((AsyncSocket)newsocket).IsMutuallyAuthenticated);
158
newsocket.RequestRead();
162
public bool OnRead(BaseSocket sock, byte[] buf, int offset, int length)
164
success = ENC.GetString(buf, offset, length);
172
public void OnWrite(BaseSocket sock, byte[] buf, int offset, int length)
174
System.Diagnostics.Debug.WriteLine(ENC.GetString(buf, offset, length));
178
public void OnError(BaseSocket sock, Exception ex)
183
public void OnConnect(BaseSocket sock)
185
sock.Write(sbuf, 5, 10);
188
public void OnClose(BaseSocket sock)
193
public void OnInit(BaseSocket new_sock)
198
public ISocketEventListener GetListener(BaseSocket new_sock)
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)
213
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
215
System.Diagnostics.Debug.WriteLine(e.ExceptionObject.ToString());