Crate test_assembler [] [src]

A set of types for building complex binary streams.

For testing code that consumes binary data, it is necessary to be able to generate test inputs consisting of various valid and invalid permutations. The Section struct defined in this crate allows for easily building a stream of bytes in any desired endianness.

This crate defines two useful structs: Section and Label. A Section is simply a stream of bytes which can be written to, and a Label is a placeholder for a value that can be computed based on other values, but can be written to a Section without knowing its value at that time.

Based on Jim Blandy's fantastic C++ implementation in Google Breakpad.

Examples

The Section methods for writing data all consume and return the Section so that they can be chained:

use test_assembler::Section;

let mut section = Section::new();
assert_eq!(section.D8(0xFF).L16(0xABCD).B32(0x12345678)
            .get_contents().unwrap(),
           &[0xFF, 0xCD, 0xAB, 0x12, 0x34, 0x56, 0x78]);

Labels can be appended to a section as placeholders for values that are not yet known using the same methods.

use test_assembler::*;

let l = Label::new();
let mut s = Section::with_endian(Endian::Little);
s = s.D32(&l);
// Now assign a value to l.
l.set_const(0x12345678);
assert_eq!(s.get_contents().unwrap(),
           &[0x78, 0x56, 0x34, 0x12]);

Labels can also be set to the current length of the Section by calling mark:

use test_assembler::*;

let l = Label::new();
let s = Section::with_endian(Endian::Little);
let start = s.start();
s.append_repeated(0, 10)
    .mark(&l)
    .append_repeated(0, 10);
assert_eq!(&l - &start, 10);

Structs

Label

A Label represents a value not yet known that is stored in a Section.

RealLabel

The inner workings of Label. Don't instanitate this, instantiate Label.

Section

A section is a sequence of bytes, constructed by appending bytes to the end.

Enums

Endian

Possible byte orders

LabelOrNum

An enum to hold Labels or Nums.

Constants

DEFAULT_ENDIAN

The default endianness for this system.

Traits

LabelMaker

Methods for creating a Label (or a RealLabel, but don't do that).

Num

A marker trait for number types.

ToLabelOrNum

A trait to allow passing numbers or Labels.