-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
2,059 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
on: | ||
push: | ||
branches: [ main ] | ||
pull_request: | ||
workflow_dispatch: | ||
|
||
name: Continuous integration | ||
|
||
jobs: | ||
|
||
lints: | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
rust: | ||
- stable | ||
- nightly | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- uses: dtolnay/rust-toolchain@master | ||
id: toolchain | ||
with: | ||
toolchain: ${{ matrix.rust }} | ||
components: rustfmt, clippy | ||
|
||
- run: cargo +${{steps.toolchain.outputs.name}} fmt --all -- --check | ||
- run: cargo +${{steps.toolchain.outputs.name}} clippy --all | ||
|
||
build_only: | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
rust: | ||
- stable | ||
- nightly | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- uses: dtolnay/rust-toolchain@master | ||
id: toolchain | ||
with: | ||
toolchain: ${{ matrix.rust }} | ||
targets: x86_64-unknown-linux-gnu | ||
|
||
- run: cargo +${{steps.toolchain.outputs.name}} build --target x86_64-unknown-linux-gnu | ||
|
||
tests: | ||
needs: [build_only] | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
rust: | ||
- stable | ||
- nightly | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- name: Install 32-bit build dependencies | ||
run: | | ||
sudo apt update | ||
sudo apt install -y libc6-dev-i386 | ||
- uses: dtolnay/rust-toolchain@master | ||
id: toolchain | ||
with: | ||
toolchain: ${{ matrix.rust }} | ||
targets: "x86_64-unknown-linux-gnu,i686-unknown-linux-gnu" | ||
|
||
- run: cargo +${{steps.toolchain.outputs.name}} build --target x86_64-unknown-linux-gnu | ||
- run: cargo +${{steps.toolchain.outputs.name}} test --target x86_64-unknown-linux-gnu | ||
- run: cargo +${{steps.toolchain.outputs.name}} doc --target x86_64-unknown-linux-gnu | ||
|
||
- run: cargo clean | ||
|
||
- run: cargo +${{steps.toolchain.outputs.name}} build --target i686-unknown-linux-gnu | ||
- run: cargo +${{steps.toolchain.outputs.name}} test --target i686-unknown-linux-gnu |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
on: | ||
push: | ||
tags: | ||
- "v0.[0-9]+.[0-9]+" | ||
workflow_dispatch: | ||
|
||
name: Publish release tag to crates.io | ||
|
||
jobs: | ||
|
||
publish: | ||
name: Publish | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- name: Check package version | ||
run: | | ||
cat Cargo.toml | gawk -v ver="$GITHUB_REF_NAME" -F= 'BEGIN {res=1;p=0} /^\[/ {p=0} /^\[package\]/ {p=1} /^version/ {if (p) {gsub(/[" ]/,"", $2); fver="v"$2; if (fver==ver) {res=0}}} END {exit res}' | ||
- uses: dtolnay/rust-toolchain@stable | ||
id: toolchain | ||
targets: x86_64-unknown-linux-gnu | ||
|
||
- run: cargo +${{steps.toolchain.outputs.name}} publish --target x86_64-unknown-linux-gnu | ||
env: | ||
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Generated by Cargo | ||
# will have compiled files and executables | ||
/target/ | ||
|
||
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries | ||
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html | ||
Cargo.lock | ||
|
||
# These are backup files generated by rustfmt | ||
**/*.rs.bk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Changelog | ||
All notable changes to this project will be documented in this file. | ||
|
||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), | ||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). | ||
|
||
## [Unreleased] | ||
|
||
## [0.1.0] - 2024-04-06 | ||
|
||
First version. | ||
|
||
[Unreleased]: https://github.com/vitalyvb/usbd-class-tester/compare/v0.1.0...HEAD | ||
[0.1.0]: https://github.com/vitalyvb/usbd-class-tester/releases/tag/v0.1.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
[package] | ||
name = "usbd-class-tester" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
description = "Library for testing usb-device device classes." | ||
authors = ["Vitalii Bursov <[email protected]>"] | ||
readme = "README.md" | ||
license = "MIT" | ||
keywords = ["usb-device", "embedded", "testing"] | ||
repository = "https://github.com/vitalyvb/usbd-class-tester" | ||
exclude = [ | ||
".github", | ||
] | ||
|
||
[dependencies.usb-device] | ||
version = "0.3.2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
# usbd-class-tester | ||
|
||
[![Crates.io](https://img.shields.io/crates/v/usbd-class-tester.svg)](https://crates.io/crates/usbd-class-tester) [![Docs.rs](https://docs.rs/usbd-class-tester/badge.svg)](https://docs.rs/usbd-class-tester) | ||
|
||
A library for running tests of `usb-device` classes on | ||
developer's system natively. | ||
|
||
## About | ||
|
||
Testing is difficult, and if it's even more difficult | ||
when it involves a dedicated hardware and doing | ||
the test manually. Often a lot of stuff needs to be | ||
re-tested even after small code changes. | ||
|
||
This library aims to help testing the implementation of | ||
protocols in USB devices which are based on `usb-device` | ||
crate by providing a means of simulating Host's accesses | ||
to the device. | ||
|
||
Initial implementation was done for tests in `usbd-dfu` | ||
crate. This library is based on that idea, but extends | ||
it a lot. For example it adds a set of convenience | ||
functions for Control transfers, while originally this | ||
was done via plain `u8` arrays only. | ||
|
||
### Supported operations | ||
|
||
* IN and OUT EP0 control transfers | ||
|
||
### Not supported operations | ||
|
||
Almost everything else, including but not limited to: | ||
|
||
* Endpoints other than EP0 in `EmulatedUsbBus::poll()` | ||
* Endpoint allocation in `EmulatedUsbBus::alloc_ep()` | ||
* Reset | ||
* Suspend and Resume | ||
* Interrupt transfers | ||
* Bulk transfers | ||
* Iso transfers | ||
* ... | ||
|
||
## License | ||
|
||
This project is licensed under [MIT License](https://opensource.org/licenses/MIT) | ||
([LICENSE](https://github.com/vitalyvb/usbd-class-tester/blob/main/LICENSE)). | ||
|
||
### Contribution | ||
|
||
Unless you explicitly state otherwise, any contribution intentionally | ||
submitted for inclusion in the work by you shall be licensed as above, | ||
without any additional terms or conditions. | ||
|
||
## Example | ||
|
||
The example defines an empty `UsbClass` implementation for `TestUsbClass`. | ||
Normally this would also include things like endpoint allocations, | ||
device-specific descriptor generation, and the code handling everything. | ||
This is not in the scope of this example. | ||
|
||
A minimal `TestCtx` creates `TestUsbClass` that will be passed to | ||
a test case. In general, `TestCtx` allows some degree of environment | ||
customization, like choosing EP0 transfer size, or redefining how | ||
`UsbDevice` is created. | ||
|
||
Check crate tests directory for more examples. | ||
|
||
Also see the documentation for `usb-device`. | ||
|
||
``` | ||
use usb_device::class_prelude::*; | ||
use usbd_class_tester::prelude::*; | ||
// `UsbClass` under the test. | ||
pub struct TestUsbClass {} | ||
impl<B: UsbBus> UsbClass<B> for TestUsbClass {} | ||
// Context to create a testable instance of `TestUsbClass` | ||
struct TestCtx {} | ||
impl UsbDeviceCtx<EmulatedUsbBus, TestUsbClass> for TestCtx { | ||
fn create_class<'a>( | ||
&mut self, | ||
alloc: &'a UsbBusAllocator<EmulatedUsbBus>, | ||
) -> AnyResult<TestUsbClass> { | ||
Ok(TestUsbClass {}) | ||
} | ||
} | ||
#[test] | ||
fn test_interface_get_status() { | ||
with_usb(TestCtx {}, |mut cls, mut dev| { | ||
let st = dev.interface_get_status(&mut cls, 0).expect("status"); | ||
assert_eq!(st, 0); | ||
}) | ||
.expect("with_usb"); | ||
} | ||
``` | ||
|
Oops, something went wrong.