Exemplo n.º 1
0
	void AuthenticateClientAndServer (bool server, bool client)
	{
		IPEndPoint endPoint = new IPEndPoint (IPAddress.Parse ("127.0.0.1"), 10000);
		ClientServerState state = new ClientServerState ();
		state.Client = new TcpClient ();
		state.Listener = new TcpListener (endPoint);
		state.Listener.Start ();
		state.ServerAuthenticated = new AutoResetEvent (false);
		state.ClientAuthenticated = new AutoResetEvent (false);
		state.ServerIOException = !server;
		try {
			Thread serverThread = new Thread (() => StartServerAndAuthenticate (state));
			serverThread.Start (null);
			Thread clientThread = new Thread (() => StartClientAndAuthenticate (state, endPoint));
			clientThread.Start (null);
			Assert.AreEqual (server, state.ServerAuthenticated.WaitOne (TimeSpan.FromSeconds (2)), 
				"server not authenticated");
			Assert.AreEqual (client, state.ClientAuthenticated.WaitOne (TimeSpan.FromSeconds (2)), 
				"client not authenticated");
		} finally {
			if (state.ClientStream != null)
				state.ClientStream.Dispose ();
			state.Client.Close ();
			if (state.ServerStream != null)
				state.ServerStream.Dispose ();
			if (state.ServerClient != null)
				state.ServerClient.Close ();
			state.Listener.Stop ();
		}
	}
Exemplo n.º 2
0
	private void StartClientAndAuthenticate (ClientServerState state, 
						 IPEndPoint endPoint) {
		try {
			state.Client.Connect (endPoint.Address, endPoint.Port);
			NetworkStream s = state.Client.GetStream ();
			state.ClientStream = new SslStream (s, false, 
						(a1, a2, a3, a4) => true,
						(a1, a2, a3, a4, a5) => m_clientCert);
			state.ClientStream.AuthenticateAsClient ("test_host");
			state.ClientAuthenticated.Set ();
		} catch (ObjectDisposedException) { /* this can happen when closing connection it's irrelevant for the test result*/}
	}
Exemplo n.º 3
0
 private void StartServerAndAuthenticate(ClientServerState state)
 {
     try {
         state.ServerClient = state.Listener.AcceptTcpClient();
         NetworkStream s = state.ServerClient.GetStream();
         state.ServerStream = new SslStream(s, false,
                                            (a1, a2, a3, a4) => true,
                                            (a1, a2, a3, a4, a5) => m_serverCert);
         state.ServerStream.AuthenticateAsServer(m_serverCert);
         state.ServerAuthenticated.Set();
     } catch (ObjectDisposedException) {     /* this can happen when closing connection it's irrelevant for the test result*/
     } catch (IOException) {
         // The authentication or decryption has failed.
         // ---> Mono.Security.Protocol.Tls.TlsException: Insuficient Security
         // that's fine for MismatchedCipherSuites
         if (!state.ServerIOException)
         {
             throw;
         }
     }
 }
        void AuthenticateClientAndServer(bool server, bool client)
        {
            IPEndPoint        endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 10000);
            ClientServerState state    = new ClientServerState();

            state.Client   = new TcpClient();
            state.Listener = new TcpListener(endPoint);
            state.Listener.Start();
            state.ServerAuthenticated = new AutoResetEvent(false);
            state.ClientAuthenticated = new AutoResetEvent(false);
            state.ServerIOException   = !server;
            try {
                Thread serverThread = new Thread(() => StartServerAndAuthenticate(state));
                serverThread.Start();
                Thread clientThread = new Thread(() => StartClientAndAuthenticate(state, endPoint));
                clientThread.Start();
                Assert.AreEqual(server, state.ServerAuthenticated.WaitOne(TimeSpan.FromSeconds(2)),
                                "server not authenticated");
                Assert.AreEqual(client, state.ClientAuthenticated.WaitOne(TimeSpan.FromSeconds(2)),
                                "client not authenticated");
            } finally {
                if (state.ClientStream != null)
                {
                    state.ClientStream.Dispose();
                }
                state.Client.Close();
                if (state.ServerStream != null)
                {
                    state.ServerStream.Dispose();
                }
                if (state.ServerClient != null)
                {
                    state.ServerClient.Close();
                }
                state.Listener.Stop();
            }
        }
Exemplo n.º 5
0
	private void StartServerAndAuthenticate (ClientServerState state) {
		try {
			state.ServerClient = state.Listener.AcceptTcpClient ();
			NetworkStream s = state.ServerClient.GetStream ();
			state.ServerStream = new SslStream (s, false, 
						(a1, a2, a3, a4) => true, 
						(a1, a2, a3, a4, a5) => m_serverCert);
			state.ServerStream.AuthenticateAsServer (m_serverCert);
			state.ServerAuthenticated.Set ();
		} catch (ObjectDisposedException) { /* this can happen when closing connection it's irrelevant for the test result*/
		} catch (IOException) {
			// The authentication or decryption has failed.
			// ---> Mono.Security.Protocol.Tls.TlsException: Insuficient Security
			// that's fine for MismatchedCipherSuites
			if (!state.ServerIOException)
				throw;
		}
	}
Exemplo n.º 6
0
	private void StartServerAndAuthenticate (ClientServerState state) {
		try {
			state.ServerClient = state.Listener.AcceptTcpClient ();
			NetworkStream s = state.ServerClient.GetStream ();
			state.ServerStream = new SslStream (s, false, 
						(a1, a2, a3, a4) => true, 
						(a1, a2, a3, a4, a5) => m_serverCert);
			state.ServerStream.AuthenticateAsServer (m_serverCert);
			state.ServerAuthenticated.Set ();
		} catch (ObjectDisposedException) { /* this can happen when closing connection it's irrelevant for the test result*/}
	}
Exemplo n.º 7
0
 public void Stop()
 {
     m_receivedList.Clear();
     m_task?.Dispose();
     m_state = ClientServerState.None;
 }