<commit_before>import socket
import select
import threading
import os.path
import pprint

from openssl import *

RESOURCES = os.path.dirname(os.path.abspath(__file__)) + '/resources'
PORT = 9898


def run_one_server(ctx, port=PORT):
    if type(ctx) is int:
        ctx = init_contexts()[ctx]
    s = socket.socket()
    s.bind( ('127.100.100.1', port) )
    s.listen(300)
    c, a = s.accept()
    assert c.getpeername(), "accepted socket closed"
    c.send('hello!')
    print "coonstructing server socket"
    c2 = Socket(s, ctx, server_side=True, do_handshake_on_connect=False)
    c2.set_auto_retry(True)
    print "FLAGS"
    pprint.pprint(c2.ssl_mode_dict())
    print "STATE: " +  c2.state_string_long()
    try:
        c2.do_handshake()
    except:
        print "STATE: " + c2.state_string_long()
        try:
            c.getpeername()
            print "accepted socket open"
        except:
            print "accepted socket closed"
        raise
    c2.send('hello world!')


def run_one_client(ctx, port=PORT):
    s = socket.create_connection( ('127.100.100.1', port) )
    assert s.getpeername(), "connected socket closed"
    print "constructing client socket"
    print "recieved plain data", s.recv(1024)
    s2 = Socket(s, ctx, do_handshake_on_connect=False)
    s2.do_handshake()
    s2.send('hello!')
    print "client recieved", s2.recv(1024)


def thread_network_test(ctx):
    server = threading.Thread(target=run_one_server, args=(ctx,))
    server.daemon = True
    server.start()
    run_one_client(ctx)


def google_client_test(ctx):
    s = socket.create_connection(('google.<EMAIL>', 443))
    s2 = Socket(s, ctx)


def init_contexts():
    contexts = []
    for s in ['1k', '2k', '4k']:
        ctx = Context('TLSv1')
        ctx.set_password('<PASSWORD>')
        ctx.use_certificate_chain_file('{0}/cert{1}.pem'.format(RESOURCES, s))
        ctx.load_client_CA_list('{0}/cert{1}.pem'.format(RESOURCES, s))
        ctx.load_verify_locations('{0}/cert{1}.pem'.format(RESOURCES, s))
        ctx.use_privatekey_file('{0}/key{1}.pem'.format(RESOURCES, s))
        ctx.check_privatekey()
        contexts.append(ctx)
    return contexts


def encryption():
    import timeit

    dur = timeit.timeit(lambda: aes_gcm_encrypt('abc', 'a' * 16, 'a' * 12), number=1000)
    print dur * 1000, "us per aes gcm encrypt"

    plaintext = "hello world!"<commit_msg>still getting SSLWantRead on every server handshake<commit_after>import socket
import select
import threading
import os.path
import time
import pprint

from openssl import *

RESOURCES = os.path.dirname(os.path.abspath(__file__)) + '/resources'
PORT = 9898


def run_one_server(ctx, port=PORT):
    if type(ctx) is int:
        ctx = init_contexts()[ctx]
    s = socket.socket()
    s.bind( ('127.100.100.1', port) )
    s.listen(300)
    c, a = s.accept()
    assert c.getpeername(), "accepted socket closed"
    c.send('hello!')
    print "coonstructing server socket"
    c2 = Socket(s, ctx, server_side=True, do_handshake_on_connect=False)
    c2.set_auto_retry(True)
    print "FLAGS"
    pprint.pprint(c2.ssl_mode_dict())
    print "STATE: " +  c2.state_string_long()
    try:
        c2.do_handshake()
    except:
        print "STATE: " + c2.state_string_long()
        try:
            c.getpeername()
            print "accepted socket open"
        except:
            print "accepted socket closed"
        raise
    c2.send('hello world!')


def run_one_client(ctx, port=PORT):
    s = socket.create_connection( ('127.100.100.1', port) )
    assert s.getpeername(), "connected socket closed"
    print "constructing client socket"
    print "recieved plain data", s.recv(1024)
    s2 = Socket(s, ctx, do_handshake_on_connect=False)
    s2.do_handshake()
    s2.send('hello!')
    print "client recieved", s2.recv(1024)


def thread_network_test(ctx):
    server = threading.Thread(target=run_one_server, args=(ctx,))
    server.daemon = True
    server.start()
    time.sleep(0.25)  # give the server time to start
    run_one_client(ctx)


def google_client_test(ctx):
    s = socket.create_connection(('google.com', 443))
    s2 = Socket(s, ctx)


def init_contexts():
    contexts = []
    for s in ['1k', '2k', '4k']:
        ctx = Context('TLSv1')
        ctx.set_password('<PASSWORD>')
        ctx.use_certificate_chain_file('{0}/cert{1}.pem'.format(RESOURCES, s))
        ctx.load_client_CA_list('{0}/cert{1}.pem'.format(RESOURCES, s))
        ctx.load_verify_locations('{0}/cert{1}.pem'.format(RESOURCES, s))
        ctx.use_privatekey_file('{0}/key{1}.pem'.format(RESOURCES, s))
        ctx.check_privatekey()
        contexts.append(ctx)
    return contexts


def encryption():
    import timeit

    dur = timeit.timeit(lambda: aes_gcm_encrypt('abc', 'a' * 16, 'a' * 12), number=1000)
    print dur * 1000, "us per aes gcm encrypt"

    plaintext = "hello world!"
{'fraction_non_alphanumeric': 0.1393106278210915, 'fraction_numerical': 0.03098071399261387, 'mean_word_length': 3.1881443298969074, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, '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': '452128', 'n_tokens_mistral': 1845, 'n_tokens_neox': 1722, 'n_words': 360}