Expose Codec via an unstable flag (#49)

Exposes `Codec` using an unstable flag. This is useful for testing.
This commit is contained in:
Carl Lerche
2017-09-03 16:17:05 -07:00
committed by GitHub
parent c122e97127
commit 88d1de2da0
33 changed files with 222 additions and 85 deletions

View File

@@ -13,8 +13,11 @@ matrix:
- travis-cargo doc-upload
script:
# Run tests
- cargo test
# Test examples in readme
- rustdoc --test README.md -L target/debug/deps
# Generate docs
- cargo doc --no-deps
env:

View File

@@ -3,6 +3,16 @@ name = "h2"
version = "0.1.0"
authors = ["Carl Lerche <me@carllerche.com>"]
[features]
# Enables **unstable** APIs. Any API exposed by this feature has no backwards
# compatibility guarantees. In other words, you should not use this feature for
# anything besides experimentation. Definitely **do not** publish a crate that
# depends on this feature.
unstable = []
[workspace]
[dependencies]
futures = "0.1"
tokio-io = "0.1.3"
@@ -16,7 +26,14 @@ slab = "0.4.0"
string = { git = "https://github.com/carllerche/string" }
[dev-dependencies]
mock-io = { git = "https://github.com/carllerche/mock-io", branch = "experiments" }
# Support code for tests. Ideally this wouldn't be released to crates.io, but
# until rust-lang/cargo#4466 is resolved, we just have to publish this junk crate.
#
# The dependency is set on a fixed version as the `h2-test-support` offers no
# guarantees of backwards compatibility across minor versions. The version of
# `h2-test-support` should always match the current version of `h2`.
h2-test-support = { version = "= 0.1.0", path = "tests/support" }
# Fuzzing
quickcheck = "0.4.1"

View File

@@ -2,7 +2,6 @@ extern crate h2;
extern crate http;
extern crate futures;
extern crate rustls;
extern crate tokio_io;
extern crate tokio_core;
extern crate tokio_rustls;
extern crate webpki_roots;

View File

@@ -2,12 +2,10 @@ extern crate h2;
extern crate http;
extern crate bytes;
extern crate futures;
extern crate tokio_io;
extern crate tokio_core;
extern crate io_dump;
extern crate env_logger;
use h2::*;
use h2::client::{Client, Body};
use http::*;

View File

@@ -2,9 +2,7 @@ extern crate h2;
extern crate http;
extern crate bytes;
extern crate futures;
extern crate tokio_io;
extern crate tokio_core;
extern crate io_dump;
extern crate env_logger;
use h2::server::Server;

View File

@@ -2,9 +2,7 @@ extern crate h2;
extern crate http;
extern crate bytes;
extern crate futures;
extern crate tokio_io;
extern crate tokio_core;
extern crate io_dump;
extern crate env_logger;
use h2::server::Server;

View File

@@ -258,3 +258,14 @@ impl<T: AsyncRead, B> AsyncRead for FramedWrite<T, B> {
self.inner.prepare_uninitialized_buffer(buf)
}
}
#[cfg(feature = "unstable")]
mod unstable {
use super::*;
impl<T, B> FramedWrite<T, B> {
pub fn get_ref(&self) -> &T {
&self.inner
}
}
}

View File

@@ -65,6 +65,11 @@ impl<T, B> Codec<T, B> {
self.inner.get_ref().max_frame_size()
}
#[cfg(feature = "unstable")]
pub fn get_ref(&self) -> &T {
self.inner.get_ref().get_ref()
}
pub fn get_mut(&mut self) -> &mut T {
self.inner.get_mut().get_mut()
}
@@ -146,3 +151,12 @@ impl<T, B> Sink for Codec<T, B>
Ok(Async::Ready(()))
}
}
// TODO: remove (or improve) this
impl<T> From<T> for Codec<T, ::std::io::Cursor<::bytes::Bytes>>
where T: AsyncRead + AsyncWrite,
{
fn from(src: T) -> Self {
Self::new(src)
}
}

View File

@@ -3,6 +3,7 @@ use bytes::{BufMut, Bytes, Buf};
use std::fmt;
#[derive(Eq, PartialEq)]
pub struct Data<T = Bytes> {
stream_id: StreamId,
data: T,

View File

@@ -2,7 +2,7 @@ use frame::{self, Head, Error, Kind, StreamId, Reason};
use bytes::{BufMut, BigEndian};
#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct GoAway {
last_stream_id: StreamId,
error_code: u32,

View File

@@ -15,6 +15,7 @@ use std::io::Cursor;
/// Header frame
///
/// This could be either a request or a response.
#[derive(Eq, PartialEq)]
pub struct Headers {
/// The ID of the stream with which this frame is associated.
stream_id: StreamId,
@@ -36,7 +37,7 @@ pub struct Headers {
#[derive(Copy, Clone, Eq, PartialEq)]
pub struct HeadersFlag(u8);
#[derive(Debug)]
#[derive(Debug, Eq, PartialEq)]
pub struct PushPromise {
/// The ID of the stream with which this frame is associated.
stream_id: StreamId,
@@ -63,7 +64,7 @@ pub struct Continuation {
headers: Iter,
}
#[derive(Debug, Default)]
#[derive(Debug, Default, Eq, PartialEq)]
pub struct Pseudo {
// Request
pub method: Option<Method>,

View File

@@ -61,6 +61,7 @@ pub type FrameSize = u32;
pub const HEADER_LEN: usize = 9;
#[derive(Eq, PartialEq)]
pub enum Frame<T = Bytes> {
Data(Data<T>),
Headers(Headers),

View File

@@ -5,7 +5,7 @@ const ACK_FLAG: u8 = 0x1;
pub type Payload = [u8; 8];
#[derive(Debug)]
#[derive(Debug, Eq, PartialEq)]
pub struct Ping {
ack: bool,
payload: Payload,

View File

@@ -1,12 +1,12 @@
use frame::*;
#[derive(Debug)]
#[derive(Debug, Eq, PartialEq)]
pub struct Priority {
stream_id: StreamId,
dependency: StreamDependency,
}
#[derive(Debug)]
#[derive(Debug, Eq, PartialEq)]
pub struct StreamDependency {
/// The ID of the stream dependency target
dependency_id: StreamId,

View File

@@ -2,7 +2,7 @@ use frame::{self, Head, Error, Kind, StreamId, Reason};
use bytes::{BufMut, BigEndian};
#[derive(Debug)]
#[derive(Debug, Eq, PartialEq)]
pub struct Reset {
stream_id: StreamId,
error_code: u32,

View File

@@ -29,8 +29,8 @@ pub fn strip_padding(payload: &mut Bytes) -> Result<u8, Error> {
return Err(Error::TooMuchPadding);
}
let _ = payload.split_off(pad_len);
let _ = payload.split_to(1);
let _ = payload.split_off(pad_len);
Ok(pad_len as u8)
}

View File

@@ -4,7 +4,7 @@ use bytes::{BufMut, BigEndian};
const SIZE_INCREMENT_MASK: u32 = 1 << 31;
#[derive(Copy, Clone, Debug)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct WindowUpdate {
stream_id: StreamId,
size_increment: u32,

View File

@@ -1,4 +1,4 @@
// #![deny(warnings, missing_debug_implementations)]
#![deny(warnings, missing_debug_implementations)]
#[macro_use]
extern crate futures;
@@ -28,9 +28,17 @@ mod error;
mod codec;
mod hpack;
mod proto;
#[cfg(not(feature = "unstable"))]
mod frame;
#[cfg(feature = "unstable")]
pub mod frame;
pub mod client;
pub mod server;
pub use error::{Error, Reason};
#[cfg(feature = "unstable")]
pub use codec::{Codec, SendError, RecvError, UserError};

View File

@@ -1,9 +1,8 @@
#[macro_use]
extern crate log;
#[macro_use]
pub mod support;
use support::*;
extern crate h2_test_support;
use h2_test_support::*;
#[test]
fn handshake() {

48
tests/codec_read.rs Normal file
View File

@@ -0,0 +1,48 @@
#[macro_use]
extern crate h2_test_support;
use h2_test_support::*;
#[test]
fn read_none() {
let mut codec = Codec::from(
mock_io::Builder::new()
.build());
assert_closed!(codec);
}
#[test]
fn read_data_no_padding() {
let mut codec = raw_codec! {
read => [
0, 0, 5, 0, 0, 0, 0, 0, 1,
"hello",
];
};
let data = poll_data!(codec);
assert_eq!(data.stream_id(), 1);
assert_eq!(data.payload(), &b"hello"[..]);
assert!(!data.is_end_stream());
assert_closed!(codec);
}
#[test]
fn read_data_padding() {
let mut codec = raw_codec! {
read => [
0, 0, 11, 0, 0x8, 0, 0, 0, 1,
5, // Pad length
"hello", // Data
"world", // Padding
];
};
let data = poll_data!(codec);
assert_eq!(data.stream_id(), 1);
assert_eq!(data.payload(), &b"hello"[..]);
assert!(!data.is_end_stream());
assert_closed!(codec);
}

0
tests/codec_write.rs Normal file
View File

View File

@@ -1,5 +1,5 @@
pub mod support;
use support::*;
extern crate h2_test_support;
use h2_test_support::*;
// In this case, the stream & connection both have capacity, but capacity is not
// explicitly requested.

View File

@@ -1,5 +1,7 @@
pub mod support;
use support::*;
/*
extern crate h2_test_support;
use h2_test_support::*;
*/
#[test]
#[ignore]

View File

@@ -1,5 +1,5 @@
pub mod support;
use support::*;
extern crate h2_test_support;
use h2_test_support::*;
#[test]
fn single_stream_send_large_body() {

View File

@@ -1,12 +1,5 @@
extern crate h2;
extern crate http;
extern crate futures;
extern crate mock_io;
extern crate env_logger;
use h2::server::Server;
use futures::*;
extern crate h2_test_support;
use h2_test_support::*;
const SETTINGS: &'static [u8] = &[0, 0, 0, 4, 0, 0, 0, 0, 0];
const SETTINGS_ACK: &'static [u8] = &[0, 0, 0, 4, 1, 0, 0, 0, 0];

View File

@@ -1,9 +1,8 @@
#[macro_use]
extern crate log;
#[macro_use]
pub mod support;
use support::*;
extern crate h2_test_support;
use h2_test_support::*;
#[test]
fn send_recv_headers_only() {

17
tests/support/Cargo.toml Normal file
View File

@@ -0,0 +1,17 @@
[package]
name = "h2-test-support"
version = "0.1.0"
authors = ["Carl Lerche <me@carllerche.com>"]
[dependencies]
futures = "0.1"
tokio-io = "0.1"
bytes = "0.4"
http = { git = "https://github.com/carllerche/http", rev = "2dd15d9" }
env_logger = "0.4"
mock-io = { git = "https://github.com/carllerche/mock-io", branch = "experiments" }
[dependencies.h2]
path = "../.."
features = ["unstable"]

3
tests/support/README.md Normal file
View File

@@ -0,0 +1,3 @@
**This is not a real crate**
You should not use this crate, it only exists to work around cargo limitations.

View File

@@ -0,0 +1,64 @@
#[macro_export]
macro_rules! assert_closed {
($transport:expr) => {{
assert_eq!($transport.poll().unwrap(), None.into());
}}
}
#[macro_export]
macro_rules! poll_data {
($transport:expr) => {{
use h2::frame::Frame;
use futures::Async;
match $transport.poll() {
Ok(Async::Ready(Some(Frame::Data(frame)))) => frame,
frame => panic!("expected data frame; actual={:?}", frame),
}
}}
}
#[macro_export]
macro_rules! raw_codec {
(
$(
$fn:ident => [$($chunk:expr,)+];
)*
) => {{
let mut b = $crate::mock_io::Builder::new();
$({
let mut chunk = vec![];
$(
$crate::codec::Chunk::push(&$chunk, &mut chunk);
)+
b.$fn(&chunk[..]);
})*
$crate::Codec::new(b.build())
}}
}
pub trait Chunk {
fn push(&self, dst: &mut Vec<u8>);
}
impl Chunk for u8 {
fn push(&self, dst: &mut Vec<u8>) {
dst.push(*self);
}
}
impl<'a> Chunk for &'a [u8] {
fn push(&self, dst: &mut Vec<u8>) {
dst.extend(*self)
}
}
impl<'a> Chunk for &'a str {
fn push(&self, dst: &mut Vec<u8>) {
dst.extend(self.as_bytes())
}
}

View File

@@ -1,7 +1,5 @@
//! Utilities to support tests.
#![allow(unused_extern_crates)]
pub extern crate bytes;
pub extern crate h2;
pub extern crate http;
@@ -10,6 +8,9 @@ pub extern crate futures;
pub extern crate mock_io;
pub extern crate env_logger;
#[macro_use]
pub mod codec;
pub use self::futures::{
Future,
Sink,
@@ -27,8 +28,11 @@ pub use self::http::{
HeaderMap,
};
pub use self::h2::*;
pub use self::h2::client::{self, Client};
// pub use self::h2::server;
pub use self::h2::server::{self, Server};
pub type Codec<T> = h2::Codec<T, ::std::io::Cursor<::bytes::Bytes>>;
pub use self::bytes::{
Buf,

View File

@@ -1,9 +1,8 @@
#[macro_use]
extern crate log;
#[macro_use]
pub mod support;
use support::*;
extern crate h2_test_support;
use h2_test_support::*;
#[test]
fn recv_trailers_only() {

View File

@@ -1,18 +0,0 @@
[package]
name = "h2-codec"
version = "0.1.0"
authors = ["Carl Lerche <me@carllerche.com>"]
[dependencies]
http = { git = "https://github.com/carllerche/http" }
log = "0.3.8"
fnv = "1.0.5"
bytes = "0.4"
string = { git = "https://github.com/carllerche/string" }
byteorder = "1.0"
futures = "0.1"
tokio-io = "0.1.3"
# tokio-timer = "0.1"
# http = { git = "https://github.com/carllerche/http", branch = "lower-case-header-name-parsing" }
# slab = "0.4.0"

View File

@@ -1,22 +0,0 @@
extern crate http;
extern crate fnv;
extern crate bytes;
extern crate string;
extern crate byteorder;
extern crate futures;
#[macro_use]
extern crate tokio_io;
#[macro_use]
extern crate log;
#[path = "../../../src/hpack/mod.rs"]
mod hpack;
#[path = "../../../src/frame/mod.rs"]
mod frame;
#[path = "../../../src/codec/mod.rs"]
mod codec;