Skip to content

Commit 658efa6

Browse files
authored
merge: pull request #9 from namib-project/6_github-ci-migration
Add CI workflow for GitHub actions
2 parents 1df5fb2 + 0b60b47 commit 658efa6

File tree

12 files changed

+196
-27
lines changed

12 files changed

+196
-27
lines changed

.github/workflows/ci.yml

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
on:
2+
push:
3+
branches:
4+
- main
5+
pull_request:
6+
branches:
7+
- main
8+
9+
env:
10+
CARGO_TERM_COLOR: always
11+
RUST_TEST_TIME_UNIT: 60,120
12+
RUST_TEST_TIME_INTEGRATION: 60,120
13+
RUST_TEST_TIME_DOCTEST: 60,120
14+
15+
jobs:
16+
test:
17+
runs-on: ubuntu-latest
18+
strategy:
19+
fail-fast: false
20+
matrix:
21+
crate: [libcoap-sys, libcoap-rs]
22+
dtls_backend: [openssl, gnutls, tinydtls]
23+
steps:
24+
- uses: actions/checkout@v3
25+
with:
26+
submodules: true
27+
- uses: dtolnay/rust-toolchain@stable
28+
with:
29+
components: rust-src
30+
- if: matrix.dtls_backend == 'gnutls'
31+
uses: awalsh128/cache-apt-pkgs-action@latest
32+
with:
33+
packages: libgnutls28-dev libgnutls30
34+
version: 1.0
35+
- if: matrix.crate == 'libcoap-rs'
36+
run: cargo test -p ${{ matrix.crate }} --no-default-features --features dtls,tcp,vendored --features dtls_${{ matrix.dtls_backend }} --no-fail-fast -- -Z unstable-options --report-time --ensure-time
37+
- if: matrix.crate == 'libcoap-sys'
38+
run: cargo test -p ${{ matrix.crate }} --features dtls,dtls_backend_${{ matrix.dtls_backend }} --no-fail-fast -- -Z unstable-options --report-time --ensure-time
39+
40+
lint:
41+
runs-on: ubuntu-latest
42+
strategy:
43+
matrix:
44+
crate: [libcoap-sys, libcoap-rs]
45+
steps:
46+
- uses: actions/checkout@v3
47+
with:
48+
submodules: true
49+
- uses: dtolnay/rust-toolchain@stable
50+
with:
51+
components: clippy, rustfmt
52+
- uses: giraffate/clippy-action@main
53+
with:
54+
reporter: 'github-pr-check'
55+
clippy_flags: -p ${{ matrix.crate }} --no-deps
56+
level: warning
57+
tool_name: clippy (${{ matrix.crate }})
58+
59+
coverage:
60+
runs-on: ubuntu-latest
61+
strategy:
62+
fail-fast: false
63+
matrix:
64+
crate: [libcoap-rs]
65+
steps:
66+
- uses: actions/checkout@v3
67+
with:
68+
submodules: true
69+
- uses: dtolnay/rust-toolchain@stable
70+
with:
71+
components: clippy, rustfmt
72+
- uses: baptiste0928/cargo-install@v2
73+
with:
74+
crate: cargo-tarpaulin
75+
- run: cargo tarpaulin --no-fail-fast --workspace --verbose --features dtls,tcp,vendored --exclude-files libcoap-sys/tests,libcoap/tests --timeout 120 --out Xml
76+
- uses: irongut/[email protected]
77+
with:
78+
filename: ./cobertura.xml
79+
badge: true
80+
fail_below_min: false
81+
format: markdown
82+
hide_branch_rate: false
83+
hide_complexity: true
84+
indicators: true
85+
output: file
86+
- run: |
87+
# Snippet taken from https://github.com/marocchino/sticky-pull-request-comment#append-after-comment-every-time-it-runs
88+
EOF=$(dd if=/dev/urandom bs=15 count=1 status=none | base64)
89+
echo "coverage_report<<$EOF" >> "$GITHUB_ENV"
90+
echo "### Code Coverage Report" >> "$GITHUB_ENV"
91+
echo "Generated for commit ${{ github.sha }} on `date -u`." >> "$GITHUB_ENV"
92+
cat code-coverage-results.md >> "$GITHUB_ENV"
93+
echo "$EOF" >> "$GITHUB_ENV"
94+
- if: github.event_name == 'pull_request'
95+
uses: marocchino/sticky-pull-request-comment@v2
96+
with:
97+
message: ${{ env.coverage_report }}
98+
- run: cat code-coverage-results.md >> $GITHUB_STEP_SUMMARY

libcoap-sys/build.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
* See the README as well as the LICENSE file for more information.
66
*/
77

8-
use std::io::ErrorKind;
98
use std::{
109
default::Default,
1110
env,
11+
io::ErrorKind,
1212
path::{Path, PathBuf},
1313
process::Command,
1414
};
@@ -178,16 +178,13 @@ fn main() {
178178
// libcoap doesn't support overriding the MbedTLS CFLAGS, but doesn't set those
179179
// either, so we just set CFLAGS and hope they propagate.
180180
if let Some(mbedtls_include) = env::var_os("DEP_MBEDTLS_INCLUDE") {
181+
let mbedtls_library_path = Path::new(env::var_os("DEP_MBEDTLS_CONFIG_H").unwrap().as_os_str())
182+
.parent()
183+
.unwrap()
184+
.join("build")
185+
.join("library");
181186
build_config.env("CFLAGS", format!("-I{}", mbedtls_include.to_str().unwrap()));
182-
build_config.env(
183-
"PKG_CONFIG_PATH",
184-
Path::new(mbedtls_include.as_os_str())
185-
.parent()
186-
.unwrap()
187-
.join("lib")
188-
.join("pkgconfig")
189-
.into_os_string(),
190-
);
187+
build_config.env("LDFLAGS", format!("-L{}", mbedtls_library_path.to_str().unwrap()));
191188
}
192189
},
193190
DtlsBackend::GnuTls => {

libcoap/src/context.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,17 @@ impl CoapContext<'_> {
305305
reserved: [0; 7],
306306
validate_id_call_back: Some(dtls_server_id_callback),
307307
id_call_back_arg: inner_ref.raw_context as *mut c_void,
308-
validate_sni_call_back: Some(dtls_server_sni_callback),
308+
validate_sni_call_back: {
309+
// Unsupported by TinyDTLS
310+
#[cfg(not(feature = "dtls_tinydtls"))]
311+
{
312+
Some(dtls_server_sni_callback)
313+
}
314+
#[cfg(feature = "dtls_tinydtls")]
315+
{
316+
None
317+
}
318+
},
309319
sni_call_back_arg: inner_ref.raw_context as *mut c_void,
310320
psk_info: initial_data,
311321
})),
@@ -533,6 +543,13 @@ impl CoapContext<'_> {
533543
.unwrap()
534544
.apply_to_spsk_info(&mut inner_ref.crypto_last_info_ref);
535545
Some(&inner_ref.crypto_last_info_ref.key)
546+
} else if inner_ref.crypto_default_info.is_some() {
547+
inner_ref
548+
.crypto_default_info
549+
.as_ref()
550+
.unwrap()
551+
.apply_to_spsk_info(&mut inner_ref.crypto_last_info_ref);
552+
Some(&inner_ref.crypto_last_info_ref.key)
536553
} else {
537554
None
538555
}

libcoap/src/crypto.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ pub trait CoapClientCryptoProvider: Debug {
7979
///
8080
/// Return a CoapCryptoProviderResponse corresponding to the cryptographic information that
8181
/// should be used.
82+
///
83+
/// Note: Unsupported by the MBedTLS DTLS backend.
8284
fn provide_key_for_hint(
8385
&mut self,
8486
hint: &CoapCryptoPskIdentity,
@@ -110,6 +112,8 @@ pub trait CoapServerCryptoProvider: Debug {
110112
/// hint.
111113
///
112114
/// Return None if the provided SNI is unacceptable, i.e. you have no key for this server name.
115+
///
116+
/// Note: Unsupported by the TinyDTLS DTLS backend.
113117
#[allow(unused_variables)]
114118
fn provide_hint_for_sni(&mut self, sni: &str) -> CoapCryptoProviderResponse<CoapCryptoPskInfo> {
115119
CoapCryptoProviderResponse::UseCurrent

libcoap/src/message/request.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ impl CoapRequest {
509509
}
510510
pdu.clear_options();
511511
for opt in additional_opts {
512-
(&mut pdu).add_option(opt);
512+
pdu.add_option(opt);
513513
}
514514
if proxy_scheme.is_some() && proxy_uri.is_some() {
515515
return Err(MessageConversionError::InvalidOptionCombination(

libcoap/src/resource.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,11 @@ impl<D: Any + ?Sized + Debug> CoapResource<D> {
244244
let raw_resource = coap_resource_init(
245245
uri_path,
246246
(COAP_RESOURCE_FLAGS_RELEASE_URI
247-
| (notify_con
248-
.then(|| COAP_RESOURCE_FLAGS_NOTIFY_CON)
249-
.unwrap_or(COAP_RESOURCE_FLAGS_NOTIFY_NON))) as i32,
247+
| if notify_con {
248+
COAP_RESOURCE_FLAGS_NOTIFY_CON
249+
} else {
250+
COAP_RESOURCE_FLAGS_NOTIFY_NON
251+
}) as i32,
250252
);
251253
let inner = CoapFfiRcCell::new(CoapResourceInner {
252254
raw_resource,

libcoap/src/session/client.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ impl CoapClientSession<'_> {
5555
/// Will return a [SessionCreationError] if libcoap was unable to create a session (most likely
5656
/// because it was not possible to bind to a port).
5757
#[cfg(feature = "dtls")]
58-
pub fn connect_dtls<'a, 'b, P: 'static + CoapClientCryptoProvider>(
59-
ctx: &'b mut CoapContext<'a>,
58+
pub fn connect_dtls<'a, P: 'static + CoapClientCryptoProvider>(
59+
ctx: &mut CoapContext<'a>,
6060
addr: SocketAddr,
6161
mut crypto_provider: P,
6262
) -> Result<CoapClientSession<'a>, SessionCreationError> {
@@ -65,7 +65,17 @@ impl CoapClientSession<'_> {
6565
let client_setup_data = Box::into_raw(Box::new(coap_dtls_cpsk_t {
6666
version: COAP_DTLS_SPSK_SETUP_VERSION as u8,
6767
reserved: [0; 7],
68-
validate_ih_call_back: Some(dtls_ih_callback),
68+
validate_ih_call_back: {
69+
// Unsupported by MbedTLS
70+
#[cfg(not(feature = "dtls_mbedtls"))]
71+
{
72+
Some(dtls_ih_callback)
73+
}
74+
#[cfg(feature = "dtls_mbedtls")]
75+
{
76+
None
77+
}
78+
},
6979
ih_call_back_arg: std::ptr::null_mut(),
7080
client_sni: std::ptr::null_mut(),
7181
psk_info: coap_dtls_cpsk_info_t {

libcoap/src/transport/tcp.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
* See the README as well as the LICENSE file for more information.
66
*/
77
/// TODO
8+
#[allow(dead_code)]
89
#[cfg(feature = "tcp")]
910
pub struct CoapTcpEndpoint {}

libcoap/src/transport/tls.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
* See the README as well as the LICENSE file for more information.
66
*/
77
/// TODO
8+
#[allow(dead_code)]
89
#[cfg(feature = "tcp")]
910
pub struct CoapTlsEndpoint {}

libcoap/tests/common/mod.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ use libcoap_rs::{CoapContext, CoapRequestHandler, CoapResource};
66
use std::net::{SocketAddr, UdpSocket};
77
use std::rc::Rc;
88
use std::sync::atomic::{AtomicBool, Ordering};
9+
use std::sync::{Arc, Condvar, Mutex};
10+
use std::thread::JoinHandle;
911
use std::time::Duration;
1012

1113
pub(crate) fn get_unused_server_addr() -> SocketAddr {
@@ -25,6 +27,39 @@ pub(crate) fn get_unused_server_addr() -> SocketAddr {
2527
.expect("Failed to get server socket address")
2628
}
2729

30+
/// Spawns a test server in a new thread and waits for context_configurator to complete before
31+
/// returning.
32+
/// As the context_configurator closure is responsible for binding to sockets, this can be used to
33+
/// spawn a test server and wait for it to be ready to accept requests before returning (avoiding
34+
/// test failure due to "Connection Refused" errors).
35+
pub(crate) fn spawn_test_server<F: FnOnce(&mut CoapContext) + Send + 'static>(
36+
context_configurator: F,
37+
) -> JoinHandle<()> {
38+
let ready_condition = Arc::new((Mutex::new(false), Condvar::new()));
39+
let ready_condition2 = Arc::clone(&ready_condition);
40+
41+
let server_handle = std::thread::spawn(move || {
42+
let (ready_var, ready_cond) = &*ready_condition2;
43+
run_test_server(|context| {
44+
context_configurator(context);
45+
let mut ready_var = ready_var.lock().expect("ready condition mutex is poisoned");
46+
*ready_var = true;
47+
ready_cond.notify_all();
48+
});
49+
});
50+
51+
let (ready_var, ready_cond) = &*ready_condition;
52+
drop(
53+
ready_cond
54+
.wait_while(ready_var.lock().expect("ready condition mutex is poisoned"), |ready| {
55+
!*ready
56+
})
57+
.expect("ready condition mutex is poisoned"),
58+
);
59+
server_handle
60+
}
61+
62+
/// Configures and starts a test server in the current thread.
2863
pub(crate) fn run_test_server<F: FnOnce(&mut CoapContext)>(context_configurator: F) {
2964
let mut context = CoapContext::new().unwrap();
3065
context_configurator(&mut context);

0 commit comments

Comments
 (0)