Skip to content

Commit

Permalink
rebase master
Browse files Browse the repository at this point in the history
  • Loading branch information
MathiasKoch committed Nov 15, 2020
2 parents 34701b1 + be13f97 commit c3cdf17
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 10 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/master.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Rust

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# Unreleased

## Bugfixes

* Return error for invalid version instead of panicking ([#31](https://github.com/00imvj00/mqttrs/pull/31))


# 0.3 (2020-03-23)

## API changes
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
`Mqttrs` is a [Rust](https://www.rust-lang.org/) crate (library) to write [MQTT
protocol](https://mqtt.org/) clients and servers.

It is a codec-only library with [very few dependencies](Cargo.toml) and a [straightworward and
It is a codec-only library with [very few dependencies](Cargo.toml) and a [straightforward and
composable API](https://docs.rs/mqttrs/*/mqttrs/), usable with rust's standard library or with async
frameworks like [tokio](https://tokio.rs/). It is strict when decoding (e.g. returns an error when
encountering reserved values) and encoding (the API makes it impossible to generate an illegal
Expand Down
16 changes: 16 additions & 0 deletions src/decoder_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,22 @@ fn test_half_connect() {
assert_eq!(12, data.len());
}

#[test]
fn test_connect_wrong_version() {
let mut data: &[u8] = &[
0b00010000, 39, 0x00, 0x04, 'M' as u8, 'Q' as u8, 'T' as u8, 'T' as u8, 0x01,
0b11001110, // +username, +password, -will retain, will qos=1, +last_will, +clean_session
0x00, 0x0a, // 10 sec
0x00, 0x04, 't' as u8, 'e' as u8, 's' as u8, 't' as u8, // client_id
0x00, 0x02, '/' as u8, 'a' as u8, // will topic = '/a'
0x00, 0x07, 'o' as u8, 'f' as u8, 'f' as u8, 'l' as u8, 'i' as u8, 'n' as u8,
'e' as u8, // will msg = 'offline'
0x00, 0x04, 'r' as u8, 'u' as u8, 's' as u8, 't' as u8, // username = 'rust'
0x00, 0x02, 'm' as u8, 'q' as u8, // password = 'mq'
];
assert!(decode_slice(&mut data).is_err(), "Unknown version should return error");
}

#[test]
fn test_connect() {
let mut data: &[u8] = &[
Expand Down
8 changes: 4 additions & 4 deletions src/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ use crate::{Error, Packet};
/// }.into();
///
/// // Allocate buffer (should be appropriately-sized or able to grow as needed).
/// let mut buf = BytesMut::with_capacity(1024);
/// let mut buf = [0u8; 1024];
///
/// // Write bytes corresponding to `&Packet` into the `BytesMut`.
/// encode(&packet, &mut buf).expect("failed encoding");
/// assert_eq!(&*buf, &[0b00110000, 11,
/// let len = encode_slice(&packet, &mut buf).expect("failed encoding");
/// assert_eq!(&buf[..len], &[0b00110000, 11,
/// 0, 4, 't' as u8, 'e' as u8, 's' as u8, 't' as u8,
/// 'h' as u8, 'e' as u8, 'l' as u8, 'l' as u8, 'o' as u8]);
/// ```
///
/// [Packet]: ../enum.Packet.html
/// [BufMut]: https://docs.rs/bytes/0.5.3/bytes/trait.BufMut.html
// #[cfg(feature = "std")]
// pub fn encode(packet: &Packet, buf: impl BufMut) -> Result<usize, Error> {
// pub fn encode_slice(packet: &Packet, buf: impl BufMut) -> Result<usize, Error> {
// let mut offset = 0;
// encode_slice(packet, buf.bytes_mut(), &mut offset)
// }
Expand Down
10 changes: 5 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
//! use bytes::BytesMut;
//!
//! // Allocate buffer.
//! let mut buf = BytesMut::with_capacity(1024);
//! let mut buf = [0u8; 1024];
//!
//! // Encode an MQTT Connect packet.
//! let pkt = Packet::Connect(Connect { protocol: Protocol::MQTT311,
Expand All @@ -22,15 +22,15 @@
//! last_will: None,
//! username: None,
//! password: None });
//! assert!(encode(&pkt, &mut buf).is_ok());
//! assert_eq!(&buf[14..], b"doc_client");
//! let len = encode_slice(&pkt, &mut buf).unwrap();
//! assert_eq!(&buf[14..len], b"doc_client");
//! let mut encoded = buf.clone();
//!
//! // Decode one packet. The buffer will advance to the next packet.
//! assert_eq!(Ok(Some(pkt)), decode_slice(&mut buf));
//!
//! // Example decode failures.
//! let mut incomplete = encoded.split_to(10);
//! let mut incomplete = encoded.split_at(10).0;
//! assert_eq!(Ok(None), decode_slice(&mut incomplete));
//! let mut garbage = BytesMut::from(&[0u8,0,0,0] as &[u8]);
//! assert_eq!(Err(Error::InvalidHeader), decode_slice(&mut garbage));
Expand All @@ -40,7 +40,7 @@
//! [MQTT 5]: https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html
//! [tokio]: https://tokio.rs/
//! [Packet]: enum.Packet.html
//! [encode()]: fn.encode.html
//! [encode_slice()]: fn.encode_slice.html
//! [decode_slice()]: fn.decode_slice.html
//! [bytes::BytesMut]: https://docs.rs/bytes/0.5.3/bytes/struct.BytesMut.html
Expand Down

0 comments on commit c3cdf17

Please sign in to comment.