Skip to content

Commit

Permalink
alpkit: Add tests for from/to JSON serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
jirutka committed Aug 9, 2023
1 parent 8a57792 commit f9df4c6
Show file tree
Hide file tree
Showing 5 changed files with 325 additions and 69 deletions.
84 changes: 76 additions & 8 deletions alpkit/src/apkbuild.test.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use indoc::indoc;
use serde_json::json;

use super::*;
use crate::internal::test_utils::{assert, assert_let, dependency, S};
use crate::internal::test_utils::{assert, assert_from_to_json, assert_let, dependency, S};

#[test]
fn read_apkbuild() {
let fixture = Path::new("../fixtures/aports/sample/APKBUILD");

let expected = Apkbuild {
fn sample_apkbuild() -> Apkbuild {
Apkbuild {
maintainer: Some(S!("Jakub Jirutka <jakub@jirutka.cz>")),
contributors: vec![
S!("Francesco Colista <fcolista@alpinelinux.org>"),
Expand Down Expand Up @@ -64,9 +62,13 @@ fn read_apkbuild() {
Secfix::new("1.2.3-r2", vec![S!("CVE-2022-12347"), S!("CVE-2022-12346")]),
Secfix::new("1.2.0-r0", vec![S!("CVE-2021-12345")]),
]
};
}
}

assert!(ApkbuildReader::new().read_apkbuild(fixture).unwrap() == expected);
#[test]
fn read_apkbuild() {
let fixture = Path::new("../fixtures/aports/sample/APKBUILD");
assert!(ApkbuildReader::new().read_apkbuild(fixture).unwrap() == sample_apkbuild());
}

#[test]
Expand Down Expand Up @@ -175,3 +177,69 @@ fn test_decode_source_and_sha512sums() {
"error message should contain name of the missing checksum"
);
}

#[test]
#[ignore = "FIXME: serialization of Dependency with conflict"]
fn apkbuild_json() {
assert_from_to_json!(
sample_apkbuild(),
json!({
"maintainer": "Jakub Jirutka <jakub@jirutka.cz>",
"contributors": [
"Francesco Colista <fcolista@alpinelinux.org>",
"Natanael Copa <ncopa@alpinelinux.org>"
],
"pkgname": "sample",
"pkgver": "1.2.3",
"pkgrel": 2,
"pkgdesc": "A sample aport for testing",
"url": "https://example.org/sample",
"arch": [ "aarch64", "armhf", "armv7", "ppc64le", "x86", "x86_64" ],
"license": "ISC and BSD-2-Clause and BSD-3-Clause",
"depends": {
"ruby": ">= 3.0",
"!sample-legacy": "*"
},
"makedepends": {
"openssl-dev": "> 3",
"zlib-dev": "*"
},
"makedepends_build": {},
"makedepends_host": {},
"checkdepends": {
"ruby-rspec": "*"
},
"install_if": {},
"pkgusers": [],
"pkggroups": [],
"provides": {
"sample2": "= 1.2.3-r2"
},
"provider_priority": 100,
"replaces": {
"sample2": "*"
},
"install": [ "sample.post-install", "sample.post-upgrade" ],
"triggers": [ "sample.trigger=/usr/share/sample/*" ],
"subpackages": [ "sample-doc", "sample-dev" ],
"sources": [{
"name": "sample-1.2.3.tar.gz",
"uri": "https://example.org/sample/sample-1.2.3.tar.gz",
"checksum": "54286070812a47b629f68757046d3c9a1bdd2b5d1c3b84a5c8e4cb92f1331afa745443f7238175835d8cfbe5b8dd442e00c75c3a5b5b8f8efd8d2ec8f636dad4"
}, {
"name": "sample.initd",
"uri": "sample.initd",
"checksum": "b512bcb8bae11853a3006e2122d7e652806d4bf2234638d8809fd823375b5b0bd590f7d6a90412baffcc3b7b6a0f197a10986728a70f24fe628f91bfb651d266"
}, {
"name": "sample.confd",
"uri": "sample.confd",
"checksum": "6eda39920cccb1238b104bb90ac4be2c32883897c72363560d8d39345819cdeff535680e78396052b2b8f981e169ad9b3c30da724def80a1501785d82ce7fa25"
}],
"options": [ "!check" ],
"secfixes": {
"1.2.3-r2": [ "CVE-2022-12347", "CVE-2022-12346" ],
"1.2.0-r0": [ "CVE-2021-12345" ]
}
}),
);
}
35 changes: 35 additions & 0 deletions alpkit/src/internal/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,41 @@ pub(crate) use assert2::{assert, let_assert as assert_let};

use crate::dependency::Dependency;

macro_rules! assert_from_to_json {
($strukt:expr, $json:expr $(,)?) => {{
fn assert<T: ::serde::de::DeserializeOwned + ::serde::ser::Serialize>(
strukt: T,
json: ::serde_json::Value,
) {
let config = ::assert_json_diff::Config::new(::assert_json_diff::CompareMode::Strict);

if let Err(error) =
::assert_json_diff::assert_json_matches_no_panic(&strukt, &json, config.clone())
{
panic!(
"\n\nStruct to JSON: {}\n\nActual JSON:\n{}\n\n",
error,
::serde_json::to_string_pretty(&strukt).unwrap()
);
}
if let Err(error) = ::assert_json_diff::assert_json_matches_no_panic(
&::serde_json::from_str::<T>(&json.to_string())
.expect("failed to deserialize from JSON"),
&strukt,
config,
) {
panic!(
"\n\nJSON to struct: {}\n\nGiven JSON:\n{}\n\n",
error,
::serde_json::to_string_pretty(&json).unwrap()
);
}
}
assert($strukt, $json);
}};
}
pub(crate) use assert_from_to_json;

macro_rules! S {
($s:expr) => {
String::from($s)
Expand Down
6 changes: 6 additions & 0 deletions alpkit/src/package/fileinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,9 @@ where
{
io::Error::new(io::ErrorKind::Other, error.into())
}

////////////////////////////////////////////////////////////////////////////////

#[cfg(test)]
#[path = "fileinfo.test.rs"]
mod test;
95 changes: 95 additions & 0 deletions alpkit/src/package/fileinfo.test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
use serde_json::json;

use super::*;
use crate::internal::test_utils::{assert_from_to_json, S};

#[test]
fn fileinfo_json_regular() {
assert_from_to_json!(
FileInfo {
path: PathBuf::from("/etc/shadow"),
file_type: FileType::Regular,
uname: S!("root"),
gname: S!("shadow"),
size: Some(926),
mode: 0o640,
digest: Some(S!("7f2f7c17ca2a0e67d74dd09caba7c20a079e7563")),
..Default::default()
},
json!({
"path": "/etc/shadow",
"type": "r",
"gname": "shadow",
"size": 926,
"mode": "0640",
"digest": "7f2f7c17ca2a0e67d74dd09caba7c20a079e7563"
}),
);
}

#[test]
fn fileinfo_json_dir() {
assert_from_to_json!(
FileInfo {
path: PathBuf::from("/etc"),
file_type: FileType::Directory,
mode: 0o755,
..Default::default()
},
json!({
"path": "/etc",
"type": "d",
"mode": "0755"
}),
);
}

#[test]
fn fileinfo_json_symlink() {
assert_from_to_json!(
FileInfo {
path: PathBuf::from("/usr/bin/killall"),
file_type: FileType::Symlink,
link_target: Some(PathBuf::from("/bin/busybox")),
size: Some(0),
digest: Some(S!("d371f2e400ee8b8c2f186801514b146668373666")),
..Default::default()
},
json!({
"path": "/usr/bin/killall",
"type": "l",
"link_target": "/bin/busybox",
"size": 0,
"mode": "0644",
"digest": "d371f2e400ee8b8c2f186801514b146668373666"
}),
)
}

#[test]
fn fileinfo_json_xattrs() {
assert_from_to_json!(
FileInfo {
path: PathBuf::from("/usr/bin/something"),
file_type: FileType::Regular,
size: Some(926),
mode: 0o4755,
digest: Some(S!("7f2f7c17ca2a0e67d74dd09caba7c20a079e7563")),
xattrs: vec![Xattr {
name: S!("user.pax.flags"),
value: b"epm".to_vec()
},],
..Default::default()
},
json!({
"path": "/usr/bin/something",
"type": "r",
"size": 926,
"mode": "04755",
"digest": "7f2f7c17ca2a0e67d74dd09caba7c20a079e7563",
"xattrs": {
"user.pax.flags": "ZXBt"
}
}),
);
}
Loading

0 comments on commit f9df4c6

Please sign in to comment.