<commit_before>/*******************************************************************************
 * Copyright (c) 2011 Intalio, Inc.
 * ======================================================================
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Apache License v2.0 which accompanies this distribution.
 *
 *   The Eclipse Public License is available at
 *   http://www.eclipse.org/legal/epl-v10.html
 *
 *   The Apache License v2.0 is available at
 *   http://www.opensource.org/licenses/apache2.0.php
 *
 * You may elect to redistribute this code under either of these licenses.
 *******************************************************************************/
package org.eclipse.jetty.websocket.server;

import java.io.IOException;
import java.util.Arrays;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ssl.SslSelectChannelConnector;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.eclipse.jetty.websocket.api.WebSocketAdapter;
import org.eclipse.jetty.websocket.api.WebSocketConnection;
import org.eclipse.jetty.websocket.server.examples.MyEchoSocket;
import org.junit.After;
import org.junit.Assert;
import org.junit.Test;

public class WebSocketOverSSLTest
{
    private Server _server;
    private int _port;
    private QueuedThreadPool _threadPool;
    // private WebSocketClientFactory _wsFactory;
    private WebSocketConnection _connection;

    @After
    public void destroy() throws Exception
    {
        if (_connection != null)
        {
            _connection.close();
        }

        // if (_wsFactory != null)
        // _wsFactory.stop();

        if (_threadPool != null)
        {
            _threadPool.stop();
        }

        if (_server != null)
        {
            _server.stop();
            _server.join();
        }
    }

    private void startClient(final Object webSocket) throws Exception
    {
        Assert.assertTrue(_server.isStarted());

        _threadPool = new QueuedThreadPool();
        _threadPool.setName("wsc-" + _threadPool.getName());
        _threadPool.start();

        // _wsFactory = new WebSocketClientFactory(_threadPool, new ZeroMasker());
        // SslContextFactory cf = _wsFactory.getSslContextFactory();
        // cf.setKeyStorePath(MavenTestingUtils.getTestResourceFile("keystore").getAbsolutePath());
        // cf.setKeyStorePassword("<PASSWORD>");
        // cf.setKeyManagerPassword("<PASSWORD>");
        // _wsFactory.start();

        // WebSocketClient client = new WebSocketClient(_wsFactory);
        // _connection = client.open(new URI("wss://localhost:" + _port), webSocket).get(5, TimeUnit.SECONDS);
    }

    private void startServer(final Object websocket) throws Exception
    {
        _server = new Server();
        SslSelectChannelConnector connector = new SslSelectChannelConnector();
        _server.addConnector(connector);
        SslContextFactory cf = connector.getSslContextFactory();
        cf.setKeyStorePath(MavenTestingUtils.getTestResourceFile("keystore").getAbsolutePath());
        cf.setKeyStorePassword("<PASSWORD>");
        cf.setKeyManagerPassword("<PASSWORD>");
        _server.setHandler(new WebSocketHandler.Simple(websocket.getClass()));
        _server.start();
        _port = connector.getLocalPort();
    }

    @Test
    public void testManyMessages() throws Exception
    {
        startServer(MyEchoSocket.class);
        int count = 1000;
        final CountDownLatch clientLatch = new CountDownLatch(count);
        startClient(new WebSocketAdapter()
        {
            @Override
            public void onWebSocketText(String message)
            {
                clientLatch.countDown();
            }
        });

        char[] chars = new char[256];
        Arrays.fill(chars,'x');
        String message = new String(chars);
        for (int i = 0; i < count; ++i)
        {
            _connection.write(message);
        }

        Assert.assertTrue(clientLatch.await(20,TimeUnit.SECONDS));

        // While messages may have all arrived, the SSL close alert
        // may be in the way so give some time for it to be processed.
        TimeUnit.SECONDS.sleep(1);
    }

    @Test
    public void testWebSocketOverSSL() throws Exception
    {
        final String message = "message";
        final CountDownLatch serverLatch = new CountDownLatch(1);
        startServer(new WebSocketAdapter()
        {
            private WebSocketConnection connection;

            @Override
            public void onWebSocketConnect(WebSocketConnection connection)
            {
                this.connection = connection;
            }

            @Override
            public void onWebSocketText(String message)
            {
                try
                {
                    Assert.assertEquals(message,message);
                    connection.write(message);
                    serverLatch.countDown();<commit_msg>Setting SSL tests to @Ignore (till we implement the SSL bits again)<commit_after>/*******************************************************************************
 * Copyright (c) 2011 Intalio, Inc.
 * ======================================================================
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Apache License v2.0 which accompanies this distribution.
 *
 *   The Eclipse Public License is available at
 *   http://www.eclipse.org/legal/epl-v10.html
 *
 *   The Apache License v2.0 is available at
 *   http://www.opensource.org/licenses/apache2.0.php
 *
 * You may elect to redistribute this code under either of these licenses.
 *******************************************************************************/
package org.eclipse.jetty.websocket.server;

import java.io.IOException;
import java.util.Arrays;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ssl.SslSelectChannelConnector;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.eclipse.jetty.websocket.api.WebSocketAdapter;
import org.eclipse.jetty.websocket.api.WebSocketConnection;
import org.eclipse.jetty.websocket.server.examples.MyEchoSocket;
import org.junit.After;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;

public class WebSocketOverSSLTest
{
    private Server _server;
    private int _port;
    private QueuedThreadPool _threadPool;
    // private WebSocketClientFactory _wsFactory;
    private WebSocketConnection _connection;

    @After
    public void destroy() throws Exception
    {
        if (_connection != null)
        {
            _connection.close();
        }

        // if (_wsFactory != null)
        // _wsFactory.stop();

        if (_threadPool != null)
        {
            _threadPool.stop();
        }

        if (_server != null)
        {
            _server.stop();
            _server.join();
        }
    }

    private void startClient(final Object webSocket) throws Exception
    {
        Assert.assertTrue(_server.isStarted());

        _threadPool = new QueuedThreadPool();
        _threadPool.setName("wsc-" + _threadPool.getName());
        _threadPool.start();

        // _wsFactory = new WebSocketClientFactory(_threadPool, new ZeroMasker());
        // SslContextFactory cf = _wsFactory.getSslContextFactory();
        // cf.setKeyStorePath(MavenTestingUtils.getTestResourceFile("keystore").getAbsolutePath());
        // cf.setKeyStorePassword("<PASSWORD>");
        // cf.setKeyManagerPassword("<PASSWORD>");
        // _wsFactory.start();

        // WebSocketClient client = new WebSocketClient(_wsFactory);
        // _connection = client.open(new URI("wss://localhost:" + _port), webSocket).get(5, TimeUnit.SECONDS);
    }

    private void startServer(final Object websocket) throws Exception
    {
        _server = new Server();
        SslSelectChannelConnector connector = new SslSelectChannelConnector();
        _server.addConnector(connector);
        SslContextFactory cf = connector.getSslContextFactory();
        cf.setKeyStorePath(MavenTestingUtils.getTestResourceFile("keystore").getAbsolutePath());
        cf.setKeyStorePassword("<PASSWORD>");
        cf.setKeyManagerPassword("<PASSWORD>");
        _server.setHandler(new WebSocketHandler.Simple(websocket.getClass()));
        _server.start();
        _port = connector.getLocalPort();
    }

    @Test
    @Ignore("SSL Not yet implemented")
    public void testManyMessages() throws Exception
    {
        startServer(MyEchoSocket.class);
        int count = 1000;
        final CountDownLatch clientLatch = new CountDownLatch(count);
        startClient(new WebSocketAdapter()
        {
            @Override
            public void onWebSocketText(String message)
            {
                clientLatch.countDown();
            }
        });

        char[] chars = new char[256];
        Arrays.fill(chars,'x');
        String message = new String(chars);
        for (int i = 0; i < count; ++i)
        {
            _connection.write(message);
        }

        Assert.assertTrue(clientLatch.await(20,TimeUnit.SECONDS));

        // While messages may have all arrived, the SSL close alert
        // may be in the way so give some time for it to be processed.
        TimeUnit.SECONDS.sleep(1);
    }

    @Test
    @Ignore("SSL Not yet implemented")
    public void testWebSocketOverSSL() throws Exception
    {
        final String message = "message";
        final CountDownLatch serverLatch = new CountDownLatch(1);
        startServer(new WebSocketAdapter()
        {
            private WebSocketConnection connection;

            @Override
            public void onWebSocketConnect(WebSocketConnection connection)
            {
                this.connection = connection;
            }

            @Override
            public void onWebSocketText(String message)
            {
                try
                {
                    Assert.assertEquals(message,message);
                    connection.write(message);
                    serverLatch.countDown();
{'fraction_non_alphanumeric': 0.13679735474728388, 'fraction_numerical': 0.005101558809636278, 'mean_word_length': 2.6885017421602786, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4829476', 'n_tokens_mistral': 2816, 'n_tokens_neox': 2704, 'n_words': 620}