2015-06-05 11:28:09 +00:00
|
|
|
import OpenSSL
|
|
|
|
|
|
|
|
from netlib import tcp
|
2015-07-14 21:02:14 +00:00
|
|
|
from netlib.http import http2
|
|
|
|
from netlib.http.http2.frame import *
|
|
|
|
from ... import tutils, tservers
|
2015-06-05 11:28:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
class EchoHandler(tcp.BaseHandler):
|
|
|
|
sni = None
|
|
|
|
|
|
|
|
def handle(self):
|
2015-06-05 18:49:03 +00:00
|
|
|
while True:
|
|
|
|
v = self.rfile.safe_read(1)
|
|
|
|
self.wfile.write(v)
|
|
|
|
self.wfile.flush()
|
2015-06-05 11:28:09 +00:00
|
|
|
|
|
|
|
|
2015-06-22 02:52:23 +00:00
|
|
|
class TestCheckALPNMatch(tservers.ServerTestBase):
|
2015-06-05 11:28:09 +00:00
|
|
|
handler = EchoHandler
|
|
|
|
ssl = dict(
|
|
|
|
alpn_select=http2.HTTP2Protocol.ALPN_PROTO_H2,
|
|
|
|
)
|
|
|
|
|
|
|
|
if OpenSSL._util.lib.Cryptography_HAS_ALPN:
|
|
|
|
|
|
|
|
def test_check_alpn(self):
|
|
|
|
c = tcp.TCPClient(("127.0.0.1", self.port))
|
|
|
|
c.connect()
|
|
|
|
c.convert_to_ssl(alpn_protos=[http2.HTTP2Protocol.ALPN_PROTO_H2])
|
|
|
|
protocol = http2.HTTP2Protocol(c)
|
|
|
|
assert protocol.check_alpn()
|
|
|
|
|
|
|
|
|
2015-06-22 02:52:23 +00:00
|
|
|
class TestCheckALPNMismatch(tservers.ServerTestBase):
|
2015-06-05 11:28:09 +00:00
|
|
|
handler = EchoHandler
|
|
|
|
ssl = dict(
|
|
|
|
alpn_select=None,
|
|
|
|
)
|
|
|
|
|
|
|
|
if OpenSSL._util.lib.Cryptography_HAS_ALPN:
|
|
|
|
|
|
|
|
def test_check_alpn(self):
|
|
|
|
c = tcp.TCPClient(("127.0.0.1", self.port))
|
|
|
|
c.connect()
|
|
|
|
c.convert_to_ssl(alpn_protos=[http2.HTTP2Protocol.ALPN_PROTO_H2])
|
|
|
|
protocol = http2.HTTP2Protocol(c)
|
|
|
|
tutils.raises(NotImplementedError, protocol.check_alpn)
|
|
|
|
|
|
|
|
|
2015-06-22 02:52:23 +00:00
|
|
|
class TestPerformServerConnectionPreface(tservers.ServerTestBase):
|
2015-06-11 13:38:32 +00:00
|
|
|
class handler(tcp.BaseHandler):
|
|
|
|
|
|
|
|
def handle(self):
|
|
|
|
# send magic
|
2015-06-12 12:41:54 +00:00
|
|
|
self.wfile.write(
|
2015-06-11 13:38:32 +00:00
|
|
|
'505249202a20485454502f322e300d0a0d0a534d0d0a0d0a'.decode('hex'))
|
|
|
|
self.wfile.flush()
|
|
|
|
|
|
|
|
# send empty settings frame
|
|
|
|
self.wfile.write('000000040000000000'.decode('hex'))
|
|
|
|
self.wfile.flush()
|
|
|
|
|
|
|
|
# check empty settings frame
|
|
|
|
assert self.rfile.read(9) ==\
|
|
|
|
'000000040000000000'.decode('hex')
|
|
|
|
|
|
|
|
# check settings acknowledgement
|
|
|
|
assert self.rfile.read(9) == \
|
|
|
|
'000000040100000000'.decode('hex')
|
|
|
|
|
|
|
|
# send settings acknowledgement
|
|
|
|
self.wfile.write('000000040100000000'.decode('hex'))
|
|
|
|
self.wfile.flush()
|
|
|
|
|
|
|
|
def test_perform_server_connection_preface(self):
|
|
|
|
c = tcp.TCPClient(("127.0.0.1", self.port))
|
|
|
|
c.connect()
|
|
|
|
protocol = http2.HTTP2Protocol(c)
|
|
|
|
protocol.perform_server_connection_preface()
|
|
|
|
|
|
|
|
|
2015-06-22 02:52:23 +00:00
|
|
|
class TestPerformClientConnectionPreface(tservers.ServerTestBase):
|
2015-06-05 11:28:09 +00:00
|
|
|
class handler(tcp.BaseHandler):
|
|
|
|
|
|
|
|
def handle(self):
|
|
|
|
# check magic
|
|
|
|
assert self.rfile.read(24) ==\
|
|
|
|
'505249202a20485454502f322e300d0a0d0a534d0d0a0d0a'.decode('hex')
|
|
|
|
|
|
|
|
# check empty settings frame
|
|
|
|
assert self.rfile.read(9) ==\
|
|
|
|
'000000040000000000'.decode('hex')
|
|
|
|
|
|
|
|
# send empty settings frame
|
|
|
|
self.wfile.write('000000040000000000'.decode('hex'))
|
|
|
|
self.wfile.flush()
|
|
|
|
|
|
|
|
# check settings acknowledgement
|
|
|
|
assert self.rfile.read(9) == \
|
|
|
|
'000000040100000000'.decode('hex')
|
|
|
|
|
|
|
|
# send settings acknowledgement
|
|
|
|
self.wfile.write('000000040100000000'.decode('hex'))
|
|
|
|
self.wfile.flush()
|
|
|
|
|
2015-06-11 13:38:32 +00:00
|
|
|
def test_perform_client_connection_preface(self):
|
2015-06-05 11:28:09 +00:00
|
|
|
c = tcp.TCPClient(("127.0.0.1", self.port))
|
|
|
|
c.connect()
|
|
|
|
protocol = http2.HTTP2Protocol(c)
|
2015-06-11 13:38:32 +00:00
|
|
|
protocol.perform_client_connection_preface()
|
2015-06-05 11:28:09 +00:00
|
|
|
|
|
|
|
|
2015-06-12 13:21:23 +00:00
|
|
|
class TestClientStreamIds():
|
2015-06-05 11:28:09 +00:00
|
|
|
c = tcp.TCPClient(("127.0.0.1", 0))
|
|
|
|
protocol = http2.HTTP2Protocol(c)
|
|
|
|
|
2015-06-12 13:21:23 +00:00
|
|
|
def test_client_stream_ids(self):
|
2015-06-05 11:28:09 +00:00
|
|
|
assert self.protocol.current_stream_id is None
|
|
|
|
assert self.protocol.next_stream_id() == 1
|
|
|
|
assert self.protocol.current_stream_id == 1
|
|
|
|
assert self.protocol.next_stream_id() == 3
|
|
|
|
assert self.protocol.current_stream_id == 3
|
|
|
|
assert self.protocol.next_stream_id() == 5
|
|
|
|
assert self.protocol.current_stream_id == 5
|
|
|
|
|
|
|
|
|
2015-06-12 13:21:23 +00:00
|
|
|
class TestServerStreamIds():
|
|
|
|
c = tcp.TCPClient(("127.0.0.1", 0))
|
|
|
|
protocol = http2.HTTP2Protocol(c, is_server=True)
|
|
|
|
|
|
|
|
def test_server_stream_ids(self):
|
|
|
|
assert self.protocol.current_stream_id is None
|
|
|
|
assert self.protocol.next_stream_id() == 2
|
|
|
|
assert self.protocol.current_stream_id == 2
|
|
|
|
assert self.protocol.next_stream_id() == 4
|
|
|
|
assert self.protocol.current_stream_id == 4
|
|
|
|
assert self.protocol.next_stream_id() == 6
|
|
|
|
assert self.protocol.current_stream_id == 6
|
|
|
|
|
|
|
|
|
2015-06-22 02:52:23 +00:00
|
|
|
class TestApplySettings(tservers.ServerTestBase):
|
2015-06-05 11:28:09 +00:00
|
|
|
class handler(tcp.BaseHandler):
|
|
|
|
|
|
|
|
def handle(self):
|
|
|
|
# check settings acknowledgement
|
|
|
|
assert self.rfile.read(9) == '000000040100000000'.decode('hex')
|
|
|
|
self.wfile.write("OK")
|
|
|
|
self.wfile.flush()
|
|
|
|
|
|
|
|
ssl = True
|
|
|
|
|
|
|
|
def test_apply_settings(self):
|
|
|
|
c = tcp.TCPClient(("127.0.0.1", self.port))
|
|
|
|
c.connect()
|
|
|
|
c.convert_to_ssl()
|
|
|
|
protocol = http2.HTTP2Protocol(c)
|
|
|
|
|
|
|
|
protocol._apply_settings({
|
|
|
|
SettingsFrame.SETTINGS.SETTINGS_ENABLE_PUSH: 'foo',
|
|
|
|
SettingsFrame.SETTINGS.SETTINGS_MAX_CONCURRENT_STREAMS: 'bar',
|
|
|
|
SettingsFrame.SETTINGS.SETTINGS_INITIAL_WINDOW_SIZE: 'deadbeef',
|
|
|
|
})
|
|
|
|
|
|
|
|
assert c.rfile.safe_read(2) == "OK"
|
|
|
|
|
|
|
|
assert protocol.http2_settings[
|
|
|
|
SettingsFrame.SETTINGS.SETTINGS_ENABLE_PUSH] == 'foo'
|
|
|
|
assert protocol.http2_settings[
|
|
|
|
SettingsFrame.SETTINGS.SETTINGS_MAX_CONCURRENT_STREAMS] == 'bar'
|
|
|
|
assert protocol.http2_settings[
|
|
|
|
SettingsFrame.SETTINGS.SETTINGS_INITIAL_WINDOW_SIZE] == 'deadbeef'
|
|
|
|
|
|
|
|
|
|
|
|
class TestCreateHeaders():
|
|
|
|
c = tcp.TCPClient(("127.0.0.1", 0))
|
|
|
|
|
|
|
|
def test_create_headers(self):
|
|
|
|
headers = [
|
|
|
|
(b':method', b'GET'),
|
|
|
|
(b':path', b'index.html'),
|
|
|
|
(b':scheme', b'https'),
|
|
|
|
(b'foo', b'bar')]
|
|
|
|
|
|
|
|
bytes = http2.HTTP2Protocol(self.c)._create_headers(
|
|
|
|
headers, 1, end_stream=True)
|
|
|
|
assert b''.join(bytes) ==\
|
|
|
|
'000014010500000001824488355217caf3a69a3f87408294e7838c767f'\
|
|
|
|
.decode('hex')
|
|
|
|
|
|
|
|
bytes = http2.HTTP2Protocol(self.c)._create_headers(
|
|
|
|
headers, 1, end_stream=False)
|
|
|
|
assert b''.join(bytes) ==\
|
|
|
|
'000014010400000001824488355217caf3a69a3f87408294e7838c767f'\
|
|
|
|
.decode('hex')
|
|
|
|
|
|
|
|
# TODO: add test for too large header_block_fragments
|
|
|
|
|
|
|
|
|
|
|
|
class TestCreateBody():
|
|
|
|
c = tcp.TCPClient(("127.0.0.1", 0))
|
|
|
|
protocol = http2.HTTP2Protocol(c)
|
|
|
|
|
|
|
|
def test_create_body_empty(self):
|
|
|
|
bytes = self.protocol._create_body(b'', 1)
|
|
|
|
assert b''.join(bytes) == ''.decode('hex')
|
|
|
|
|
|
|
|
def test_create_body_single_frame(self):
|
|
|
|
bytes = self.protocol._create_body('foobar', 1)
|
|
|
|
assert b''.join(bytes) == '000006000100000001666f6f626172'.decode('hex')
|
|
|
|
|
|
|
|
def test_create_body_multiple_frames(self):
|
|
|
|
pass
|
|
|
|
# bytes = self.protocol._create_body('foobar' * 3000, 1)
|
|
|
|
# TODO: add test for too large frames
|
|
|
|
|
|
|
|
|
|
|
|
class TestCreateRequest():
|
|
|
|
c = tcp.TCPClient(("127.0.0.1", 0))
|
|
|
|
|
|
|
|
def test_create_request_simple(self):
|
|
|
|
bytes = http2.HTTP2Protocol(self.c).create_request('GET', '/')
|
|
|
|
assert len(bytes) == 1
|
2015-06-15 21:36:14 +00:00
|
|
|
assert bytes[0] == '00000d0105000000018284874188089d5c0b8170dc07'.decode('hex')
|
2015-06-05 11:28:09 +00:00
|
|
|
|
|
|
|
def test_create_request_with_body(self):
|
|
|
|
bytes = http2.HTTP2Protocol(self.c).create_request(
|
|
|
|
'GET', '/', [(b'foo', b'bar')], 'foobar')
|
|
|
|
assert len(bytes) == 2
|
|
|
|
assert bytes[0] ==\
|
2015-06-15 21:36:14 +00:00
|
|
|
'0000150104000000018284874188089d5c0b8170dc07408294e7838c767f'.decode('hex')
|
2015-06-05 11:28:09 +00:00
|
|
|
assert bytes[1] ==\
|
|
|
|
'000006000100000001666f6f626172'.decode('hex')
|
|
|
|
|
|
|
|
|
2015-06-22 02:52:23 +00:00
|
|
|
class TestReadResponse(tservers.ServerTestBase):
|
2015-06-05 11:28:09 +00:00
|
|
|
class handler(tcp.BaseHandler):
|
|
|
|
|
|
|
|
def handle(self):
|
|
|
|
self.wfile.write(
|
|
|
|
b'00000801040000000188628594e78c767f'.decode('hex'))
|
|
|
|
self.wfile.write(
|
|
|
|
b'000006000100000001666f6f626172'.decode('hex'))
|
|
|
|
self.wfile.flush()
|
|
|
|
|
|
|
|
ssl = True
|
|
|
|
|
|
|
|
def test_read_response(self):
|
|
|
|
c = tcp.TCPClient(("127.0.0.1", self.port))
|
|
|
|
c.connect()
|
|
|
|
c.convert_to_ssl()
|
|
|
|
protocol = http2.HTTP2Protocol(c)
|
|
|
|
|
2015-07-16 20:50:24 +00:00
|
|
|
resp = protocol.read_response()
|
2015-06-05 11:28:09 +00:00
|
|
|
|
2015-07-16 20:50:24 +00:00
|
|
|
assert resp.httpversion == "HTTP/2"
|
|
|
|
assert resp.status_code == "200"
|
|
|
|
assert resp.msg == ""
|
|
|
|
assert resp.headers == {':status': '200', 'etag': 'foobar'}
|
2015-07-19 16:17:30 +00:00
|
|
|
assert resp.body == b'foobar'
|
2015-06-12 13:21:23 +00:00
|
|
|
|
|
|
|
|
2015-06-22 02:52:23 +00:00
|
|
|
class TestReadEmptyResponse(tservers.ServerTestBase):
|
2015-06-12 13:21:23 +00:00
|
|
|
class handler(tcp.BaseHandler):
|
|
|
|
|
|
|
|
def handle(self):
|
|
|
|
self.wfile.write(
|
|
|
|
b'00000801050000000188628594e78c767f'.decode('hex'))
|
|
|
|
self.wfile.flush()
|
|
|
|
|
|
|
|
ssl = True
|
|
|
|
|
|
|
|
def test_read_empty_response(self):
|
|
|
|
c = tcp.TCPClient(("127.0.0.1", self.port))
|
|
|
|
c.connect()
|
|
|
|
c.convert_to_ssl()
|
|
|
|
protocol = http2.HTTP2Protocol(c)
|
|
|
|
|
2015-07-16 20:50:24 +00:00
|
|
|
resp = protocol.read_response()
|
2015-06-12 13:21:23 +00:00
|
|
|
|
2015-07-17 07:37:57 +00:00
|
|
|
assert resp.stream_id
|
2015-07-16 20:50:24 +00:00
|
|
|
assert resp.httpversion == "HTTP/2"
|
|
|
|
assert resp.status_code == "200"
|
|
|
|
assert resp.msg == ""
|
|
|
|
assert resp.headers == {':status': '200', 'etag': 'foobar'}
|
2015-07-19 16:17:30 +00:00
|
|
|
assert resp.body == b''
|
2015-06-12 13:21:23 +00:00
|
|
|
|
|
|
|
|
2015-06-22 02:52:23 +00:00
|
|
|
class TestReadRequest(tservers.ServerTestBase):
|
2015-06-12 13:21:23 +00:00
|
|
|
class handler(tcp.BaseHandler):
|
|
|
|
|
|
|
|
def handle(self):
|
|
|
|
self.wfile.write(
|
|
|
|
b'000003010400000001828487'.decode('hex'))
|
|
|
|
self.wfile.write(
|
|
|
|
b'000006000100000001666f6f626172'.decode('hex'))
|
|
|
|
self.wfile.flush()
|
|
|
|
|
|
|
|
ssl = True
|
|
|
|
|
|
|
|
def test_read_request(self):
|
|
|
|
c = tcp.TCPClient(("127.0.0.1", self.port))
|
|
|
|
c.connect()
|
|
|
|
c.convert_to_ssl()
|
|
|
|
protocol = http2.HTTP2Protocol(c, is_server=True)
|
|
|
|
|
2015-07-17 07:37:57 +00:00
|
|
|
resp = protocol.read_request()
|
2015-06-12 13:21:23 +00:00
|
|
|
|
2015-07-17 07:37:57 +00:00
|
|
|
assert resp.stream_id
|
|
|
|
assert resp.headers == {':method': 'GET', ':path': '/', ':scheme': 'https'}
|
2015-07-19 16:17:30 +00:00
|
|
|
assert resp.body == b'foobar'
|
2015-06-12 13:21:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestCreateResponse():
|
|
|
|
c = tcp.TCPClient(("127.0.0.1", 0))
|
|
|
|
|
2015-06-15 15:31:08 +00:00
|
|
|
def test_create_response_simple(self):
|
2015-06-12 13:21:23 +00:00
|
|
|
bytes = http2.HTTP2Protocol(self.c, is_server=True).create_response(200)
|
|
|
|
assert len(bytes) == 1
|
|
|
|
assert bytes[0] ==\
|
|
|
|
'00000101050000000288'.decode('hex')
|
|
|
|
|
2015-06-15 15:31:08 +00:00
|
|
|
def test_create_response_with_body(self):
|
2015-06-12 13:21:23 +00:00
|
|
|
bytes = http2.HTTP2Protocol(self.c, is_server=True).create_response(
|
2015-06-15 15:31:08 +00:00
|
|
|
200, 1, [(b'foo', b'bar')], 'foobar')
|
2015-06-12 13:21:23 +00:00
|
|
|
assert len(bytes) == 2
|
|
|
|
assert bytes[0] ==\
|
2015-06-15 15:31:08 +00:00
|
|
|
'00000901040000000188408294e7838c767f'.decode('hex')
|
2015-06-12 13:21:23 +00:00
|
|
|
assert bytes[1] ==\
|
2015-06-15 15:31:08 +00:00
|
|
|
'000006000100000001666f6f626172'.decode('hex')
|