Skip to content

Commit

Permalink
Refactor tests to use sealed_test
Browse files Browse the repository at this point in the history
  • Loading branch information
johnschug committed Dec 15, 2023
1 parent 567fc42 commit 2ec4599
Show file tree
Hide file tree
Showing 17 changed files with 509 additions and 343 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ maintenance = { status = "experimental" }

[dev-dependencies]
clap = { version = "4.4.11", features = ["derive"] }
tempfile = "3"
sealed_test = "1.0.0"

[dependencies]
bitflags = "2"
Expand Down
6 changes: 3 additions & 3 deletions docker/Dockerfile.static
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@ ENV SYSTEM_DEPS_LINK static
# RUN ./configure --host "$TARGET" --prefix="$PREFIX" --with-pic --enable-fast-install --disable-dependency-tracking --without-emacs --disable-java --disable-csharp --disable-c++
# RUN make -j$(nproc) install

ARG LIBGPG_ERROR_VER=1.46
ARG LIBGPG_ERROR_VER=1.47
WORKDIR /usr/src
ADD https://www.gnupg.org/ftp/gcrypt/libgpg-error/libgpg-error-${LIBGPG_ERROR_VER}.tar.bz2 ./
RUN tar -xjf libgpg-error-${LIBGPG_ERROR_VER}.tar.bz2
WORKDIR libgpg-error-$LIBGPG_ERROR_VER
RUN ./configure --host "$TARGET" --prefix="$PREFIX" --with-pic --enable-fast-install --disable-dependency-tracking --enable-static --disable-shared --disable-nls --disable-doc --disable-languages --disable-tests --enable-install-gpg-error-config
RUN make -j$(nproc) install

ARG LIBASSUAN_VER=2.5.5
ARG LIBASSUAN_VER=2.5.6
WORKDIR /usr/src
ADD https://www.gnupg.org/ftp/gcrypt/libassuan/libassuan-${LIBASSUAN_VER}.tar.bz2 ./
RUN tar -xjf libassuan-${LIBASSUAN_VER}.tar.bz2
WORKDIR libassuan-$LIBASSUAN_VER
RUN ./configure --host "$TARGET" --prefix="$PREFIX" --with-pic --enable-fast-install --disable-dependency-tracking --enable-static --disable-shared --disable-doc --with-gpg-error-prefix="$PREFIX"
RUN make -j$(nproc) install

ARG GPGME_VER=1.18.0
ARG GPGME_VER=1.23.2
WORKDIR /usr/src
ADD https://www.gnupg.org/ftp/gcrypt/gpgme/gpgme-${GPGME_VER}.tar.bz2 ./
RUN tar -xjf gpgme-${GPGME_VER}.tar.bz2
Expand Down
44 changes: 29 additions & 15 deletions src/data.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#[cfg(unix)]
use std::os::unix::io::AsRawFd;
use std::{
borrow::BorrowMut,
error::Error as StdError,
Expand All @@ -12,8 +10,11 @@ use std::{
str::Utf8Error,
};

#[cfg(unix)]
use std::os::fd::{AsRawFd, BorrowedFd};

use conv::{UnwrapOrSaturate, ValueInto};
use ffi;
use ffi::{self, gpgme_off_t};
use libc;
use static_assertions::{assert_impl_all, assert_not_impl_any};

Expand Down Expand Up @@ -202,6 +203,20 @@ impl<'data> Data<'data> {
/// [`gpgme_data_new_from_fd`](https://www.gnupg.org/documentation/manuals/gpgme/File-Based-Data-Buffers.html#index-gpgme_005fdata_005fnew_005ffrom_005ffd)
#[inline]
#[cfg(unix)]
pub fn from_borrowed_fd(fd: BorrowedFd<'data>) -> Result<Self> {
crate::init();
unsafe {
let mut data = ptr::null_mut();
convert_err(ffi::gpgme_data_new_from_fd(&mut data, fd.as_raw_fd()))?;
Ok(Data::from_raw(data))
}
}

/// Upstream documentation:
/// [`gpgme_data_new_from_fd`](https://www.gnupg.org/documentation/manuals/gpgme/File-Based-Data-Buffers.html#index-gpgme_005fdata_005fnew_005ffrom_005ffd)
#[inline]
#[cfg(unix)]
#[deprecated(note = "Use from_borrowed_fd")]
pub fn from_fd(file: &'data (impl AsRawFd + ?Sized)) -> Result<Self> {
crate::init();
unsafe {
Expand Down Expand Up @@ -433,11 +448,7 @@ impl Read for Data<'_> {
let (buf, len) = (buf.as_mut_ptr(), buf.len());
ffi::gpgme_data_read(self.as_raw(), buf.cast(), len)
};
if result >= 0 {
Ok(result as usize)
} else {
Err(Error::last_os_error().into())
}
Ok(usize::try_from(result).map_err(|_| Error::last_os_error())?)
}
}

Expand All @@ -464,7 +475,7 @@ impl Seek for Data<'_> {
io::SeekFrom::Start(off) => {
(off.try_into().unwrap_or(gpgme_off_t::MAX), libc::SEEK_SET)
}
io::SeekFrom::End(off) => (off.try_into().unwrap_or(gpgme_off_t::MAX), libc::SEEK_END),
io::SeekFrom::End(off) => (off.value_into().unwrap_or_saturate(), libc::SEEK_END),
io::SeekFrom::Current(off) => (off.value_into().unwrap_or_saturate(), libc::SEEK_CUR),
};
let result = unsafe { ffi::gpgme_data_seek(self.as_raw(), off, whence) };
Expand All @@ -487,9 +498,10 @@ unsafe extern "C" fn read_callback<S: Read>(
(*handle)
.inner
.read(slice)
.map(|n| n.try_into().unwrap_or(-1))
.map_err(Error::from)
.and_then(|n| n.try_into().or(Err(Error::EOVERFLOW)))
.unwrap_or_else(|err| {
ffi::gpgme_err_set_errno(Error::from(err).to_errno());
ffi::gpgme_err_set_errno(err.to_errno());
-1
})
}
Expand All @@ -504,9 +516,10 @@ unsafe extern "C" fn write_callback<S: Write>(
(*handle)
.inner
.write(slice)
.map(|n| n.try_into().unwrap_or(-1))
.map_err(Error::from)
.and_then(|n| n.try_into().or(Err(Error::EOVERFLOW)))
.unwrap_or_else(|err| {
ffi::gpgme_err_set_errno(Error::from(err).to_errno());
ffi::gpgme_err_set_errno(err.to_errno());
-1
})
}
Expand All @@ -529,9 +542,10 @@ unsafe extern "C" fn seek_callback<S: Seek>(
(*handle)
.inner
.seek(pos)
.map(|n| n.try_into().unwrap_or(-1))
.map_err(Error::from)
.and_then(|n| n.try_into().or(Err(Error::EOVERFLOW)))
.unwrap_or_else(|err| {
ffi::gpgme_err_set_errno(Error::from(err).to_errno());
ffi::gpgme_err_set_errno(err.to_errno());
-1
})
}
Expand Down
5 changes: 5 additions & 0 deletions tests/common/data/gpg-agent.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
disable-scdaemon
ignore-invalid-option allow-loopback-pinentry
allow-loopback-pinentry
ignore-invalid-option pinentry-mode
pinentry-mode loopback
7 changes: 7 additions & 0 deletions tests/common/data/gpg.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ignore-invalid-option no-force-v3-sigs
ignore-invalid-option allow-weak-key-signatures
# This is required for t-sig-notations.
no-force-v3-sigs

# This is required for t-edit-sign.
allow-weak-key-signatures
3 changes: 3 additions & 0 deletions tests/common/data/ownertrust.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# List of assigned trustvalues, created Mi 08 Feb 2023 09:52:04 CET
# (Use "gpg --import-ownertrust" to restore them)
A0FF4590BB6122EDEF6E3C542D727CC768697734:6:
Loading

0 comments on commit 2ec4599

Please sign in to comment.