diff --git a/rust/notus/src/packages/deb.rs b/rust/notus/src/packages/deb.rs index 4c1579b69..9d1a89288 100644 --- a/rust/notus/src/packages/deb.rs +++ b/rust/notus/src/packages/deb.rs @@ -1,7 +1,7 @@ // SPDX-FileCopyrightText: 2023 Greenbone AG // // SPDX-License-Identifier: GPL-2.0-or-later -use super::PackageVersion; +use super::{PackageVersion, Package}; use regex::Regex; use std::{ cmp::Ordering, @@ -27,25 +27,7 @@ impl Hash for Deb { } } -impl Deb { - fn new( - name: String, - epoch: u64, - upstream_version: String, - debian_revision: String, - full_name: String, - full_version: String, - ) -> Self { - Self { - name, - full_name, - full_version, - epoch, - upstream_version, - debian_revision, - } - } - +impl Package for Deb { /// Returns a Some struct or None if not comparable fn compare(&self, other: &Deb) -> Option { if self.name != other.name { @@ -81,7 +63,7 @@ impl Deb { s.finish() } - pub fn from_full_name(full_name: &str) -> Option { + fn from_full_name(full_name: &str) -> Option { if full_name.is_empty() { return None; } @@ -141,7 +123,7 @@ impl Deb { }) } - pub fn from_name_and_full_version(name: &str, full_version: &str) -> Option { + fn from_name_and_full_version(name: &str, full_version: &str) -> Option { if name.is_empty() || full_version.is_empty() { return None; } @@ -194,7 +176,7 @@ impl Deb { mod deb_tests { use crate::packages; - use super::Deb; + use super::{Deb,Package}; #[test] pub fn test_compare_gt() { diff --git a/rust/notus/src/packages/ebuild.rs b/rust/notus/src/packages/ebuild.rs index 82923acc0..7e07730e3 100644 --- a/rust/notus/src/packages/ebuild.rs +++ b/rust/notus/src/packages/ebuild.rs @@ -8,7 +8,7 @@ use std::{ hash::{Hash, Hasher}, }; -use super::PackageVersion; +use super::{Package, PackageVersion}; // Supported packages types. @@ -27,8 +27,8 @@ impl Hash for EBuild { } #[allow(dead_code)] -impl EBuild { - fn compare(&self, other: EBuild) -> Option { +impl Package for EBuild { + fn compare(&self, other: &EBuild) -> Option { if self.name != other.name { return None; } @@ -45,7 +45,7 @@ impl EBuild { s.finish() } - pub fn from_full_name(full_name: &str) -> Option { + fn from_full_name(full_name: &str) -> Option { if full_name.is_empty() { return None; } @@ -74,7 +74,7 @@ impl EBuild { }) } - pub fn from_name_and_full_version(name: &str, full_version: &str) -> Option { + fn from_name_and_full_version(name: &str, full_version: &str) -> Option { if name.is_empty() || full_version.is_empty() { return None; } @@ -101,6 +101,7 @@ mod ebuild_tests { }; use super::EBuild; + use super::Package; #[test] pub fn test_guard() { @@ -131,14 +132,14 @@ mod ebuild_tests { assert!(apache2 .clone() .unwrap() - .compare(apache1.clone().unwrap()) + .compare(&apache1.clone().unwrap()) .unwrap() .is_gt()); assert!(apache1 .clone() .unwrap() - .compare(apache2.clone().unwrap()) + .compare(&apache2.clone().unwrap()) .unwrap() .is_le()); @@ -148,18 +149,18 @@ mod ebuild_tests { assert!(apache2 .clone() .unwrap() - .compare(apache3.clone().unwrap()) + .compare(&apache3.clone().unwrap()) .unwrap() .is_le()); assert!(apache2 .clone() .unwrap() - .compare(apache3.clone().unwrap()) + .compare(&apache3.clone().unwrap()) .unwrap() .is_ge()); let apache4 = EBuild::from_name_and_full_version("apache", "2.4.51-r3"); assert!(apache4.is_some()); - assert!(apache4.unwrap().compare(apache3.unwrap()).unwrap().is_ne()); + assert!(apache4.unwrap().compare(&apache3.unwrap()).unwrap().is_ne()); } } diff --git a/rust/notus/src/packages/mod.rs b/rust/notus/src/packages/mod.rs index fcfea99f9..cbe88443d 100644 --- a/rust/notus/src/packages/mod.rs +++ b/rust/notus/src/packages/mod.rs @@ -131,6 +131,15 @@ impl<'a> PartialOrd for PackageVersion<'a> { } } +/// +pub trait Package { + fn compare(&self, other: &Rhs) -> Option; + fn from_full_name (a: &str) -> Option; + fn from_name_and_full_version (a: &str, b: &str) -> Option; + fn hash_calc(&self) -> u64; +} + + #[cfg(test)] mod tests { use crate::packages::PackageVersion; diff --git a/rust/notus/src/packages/rpm.rs b/rust/notus/src/packages/rpm.rs index 09610ff37..4c2254ac3 100644 --- a/rust/notus/src/packages/rpm.rs +++ b/rust/notus/src/packages/rpm.rs @@ -1,7 +1,7 @@ // SPDX-FileCopyrightText: 2023 Greenbone AG // // SPDX-License-Identifier: GPL-2.0-or-later -use super::PackageVersion; +use super::{PackageVersion, Package}; use regex::RegexBuilder; use std::{ cmp::Ordering, @@ -29,27 +29,7 @@ impl Hash for Rpm { static EXCEPTIONS: [&str; 2] = ["_fips", ".ksplice"]; -impl Rpm { - fn new( - name: String, - epoch: u64, - version: String, - release: String, - arch: String, - full_name: String, - full_version: String, - ) -> Self { - Self { - name, - full_name, - full_version, - epoch, - version, - release, - arch, - } - } - +impl Package for Rpm { /// Returns a Some struct or None if not comparable fn compare(&self, other: &Rpm) -> Option { if self.name != other.name { @@ -94,7 +74,7 @@ impl Rpm { s.finish() } - pub fn from_full_name(full_name: &str) -> Option { + fn from_full_name(full_name: &str) -> Option { if full_name.is_empty() { return None; } @@ -139,7 +119,7 @@ impl Rpm { }) } - pub fn from_name_and_full_version(name: &str, full_version: &str) -> Option { + fn from_name_and_full_version(name: &str, full_version: &str) -> Option { if name.is_empty() || full_version.is_empty() { return None; } @@ -188,7 +168,8 @@ mod rpm_tests { use crate::packages; use super::Rpm; - + use super::Package; + #[test] pub fn test_from_full_name() { assert!(Rpm::from_full_name("").is_none()); @@ -342,76 +323,76 @@ mod rpm_tests { #[test] pub fn test_compare_gt() { - let package1 = Rpm::new( - "foo-bar".to_string(), - 0, - "1.2.3".to_string(), - "4".to_string(), - "x86_64".to_string(), - "foo-bar-1.2.3-4.x86_64".to_string(), - "1.2.3-4.x86_64".to_string(), - ); - let package2 = Rpm::new( - "foo-bar".to_string(), - 0, - "1.2.4".to_string(), - "4".to_string(), - "x86_64".to_string(), - "foo-bar-1.2.4-4.x86_64".to_string(), - "1.2.4-4.x86_64".to_string(), - ); + let package1 = Rpm { + name: "foo-bar".to_string(), + epoch: 0, + version: "1.2.3".to_string(), + release: "4".to_string(), + arch: "x86_64".to_string(), + full_name: "foo-bar-1.2.3-4.x86_64".to_string(), + full_version: "1.2.3-4.x86_64".to_string(), + }; + let package2 = Rpm { + name: "foo-bar".to_string(), + epoch: 0, + version: "1.2.4".to_string(), + release: "4".to_string(), + arch: "x86_64".to_string(), + full_name: "foo-bar-1.2.4-4.x86_64".to_string(), + full_version: "1.2.4-4.x86_64".to_string(), + }; assert!(package2.compare(&package1).unwrap().is_gt()); - let package2 = Rpm::new( - "foo-bar".to_string(), - 0, - "1.2.3".to_string(), - "5".to_string(), - "x86_64".to_string(), - "foo-bar-1.2.3-5.x86_64".to_string(), - "1.2.3-5.x86_64".to_string(), - ); + let package2 = Rpm { + name: "foo-bar".to_string(), + epoch: 0, + version: "1.2.3".to_string(), + release: "5".to_string(), + arch: "x86_64".to_string(), + full_name: "foo-bar-1.2.3-5.x86_64".to_string(), + full_version: "1.2.3-5.x86_64".to_string(), + }; assert!(package2.compare(&package1).unwrap().is_gt()); } #[test] pub fn test_compare_gt_different_architecture() { - let package1 = Rpm::new( - "foo-bar".to_string(), - 0, - "1.2.3".to_string(), - "4".to_string(), - "x86_64".to_string(), - "foo-bar-1.2.3-4.x86_64".to_string(), - "1.2.3-4.x86_64".to_string(), - ); - let package2 = Rpm::new( - "foo-bar".to_string(), - 0, - "1.2.3".to_string(), - "4".to_string(), - "aarch64".to_string(), - "foo-bar-1.2.3-4.aarch64".to_string(), - "1.2.3-4.aarch64".to_string(), - ); - let package3 = Rpm::new( - "foo-bar".to_string(), - 0, - "1.2.4".to_string(), - "4".to_string(), - "aarch64".to_string(), - "foo-bar-1.2.4-4.aarch64".to_string(), - "1.2.4-4.aarch64".to_string(), - ); - let package4 = Rpm::new( - "foo-bar".to_string(), - 0, - "1.2.3".to_string(), - "5".to_string(), - "aarch64".to_string(), - "foo-bar-1.2.3-5.aarch64".to_string(), - "1.2.3-5.aarch64".to_string(), - ); + let package1 = Rpm { + name: "foo-bar".to_string(), + epoch: 0, + version: "1.2.3".to_string(), + release: "4".to_string(), + arch: "x86_64".to_string(), + full_name: "foo-bar-1.2.3-4.x86_64".to_string(), + full_version: "1.2.3-4.x86_64".to_string(), + }; + let package2 = Rpm { + name: "foo-bar".to_string(), + epoch: 0, + version: "1.2.3".to_string(), + release: "4".to_string(), + arch: "aarch64".to_string(), + full_name: "foo-bar-1.2.3-4.aarch64".to_string(), + full_version: "1.2.3-4.aarch64".to_string(), + }; + let package3 = Rpm { + name: "foo-bar".to_string(), + epoch: 0, + version: "1.2.4".to_string(), + release: "4".to_string(), + arch: "aarch64".to_string(), + full_name: "foo-bar-1.2.4-4.aarch64".to_string(), + full_version: "1.2.4-4.aarch64".to_string(), + }; + let package4 = Rpm { + name: "foo-bar".to_string(), + epoch: 0, + version: "1.2.3".to_string(), + release: "5".to_string(), + arch: "aarch64".to_string(), + full_name: "foo-bar-1.2.3-5.aarch64".to_string(), + full_version: "1.2.3-5.aarch64".to_string(), + }; //Not comparable, because different archs. Compare returns None. assert!(package2.compare(&package1).is_none()); assert!(package1.compare(&package2).is_none()); @@ -423,106 +404,106 @@ mod rpm_tests { #[test] pub fn test_compare_gt_different_epoch() { - let package1 = Rpm::new( - "foo-bar".to_string(), - 0, - "1.2.3".to_string(), - "4".to_string(), - "x86_64".to_string(), - "foo-bar-1.2.3-4.x86_64".to_string(), - "1.2.3-4.x86_64".to_string(), - ); - let package2 = Rpm::new( - "foo-bar".to_string(), - 1, - "1.2.3".to_string(), - "4".to_string(), - "x86_64".to_string(), - "foo-bar-1.2.3-4.x86_64".to_string(), - "1:1.2.3-4.x86_64".to_string(), - ); + let package1 = Rpm { + name: "foo-bar".to_string(), + epoch: 0, + version: "1.2.3".to_string(), + release: "4".to_string(), + arch: "x86_64".to_string(), + full_name: "foo-bar-1.2.3-4.x86_64".to_string(), + full_version: "1.2.3-4.x86_64".to_string(), + }; + let package2 = Rpm { + name: "foo-bar".to_string(), + epoch: 1, + version: "1.2.3".to_string(), + release: "4".to_string(), + arch: "x86_64".to_string(), + full_name: "foo-bar-1.2.3-4.x86_64".to_string(), + full_version: "1:1.2.3-4.x86_64".to_string(), + }; assert!(!package1.compare(&package2).unwrap().is_gt()); assert!(package2.compare(&package1).unwrap().is_gt()); } #[test] pub fn test_compare_gt_different_name() { - let package1 = Rpm::new( - "foo".to_string(), - 0, - "1.2.3".to_string(), - "4".to_string(), - "x86_64".to_string(), - "foo-1.2.3-4.x86_64".to_string(), - "1.2.3-4.x86_64".to_string(), - ); - let package2 = Rpm::new( - "bar".to_string(), - 0, - "1.2.3".to_string(), - "4".to_string(), - "x86_64".to_string(), - "bar-1.2.3-4.x86_64".to_string(), - "1.2.3-4.x86_64".to_string(), - ); + let package1 = Rpm { + name: "foo".to_string(), + epoch: 0, + version: "1.2.3".to_string(), + release: "4".to_string(), + arch: "x86_64".to_string(), + full_name: "foo-1.2.3-4.x86_64".to_string(), + full_version: "1.2.3-4.x86_64".to_string(), + }; + let package2 = Rpm { + name: "bar".to_string(), + epoch: 0, + version: "1.2.3".to_string(), + release: "4".to_string(), + arch: "x86_64".to_string(), + full_name: "bar-1.2.3-4.x86_64".to_string(), + full_version: "1.2.3-4.x86_64".to_string(), + }; assert!(package2.compare(&package1).is_none()); assert!(package1.compare(&package2).is_none()); } #[test] pub fn test_compare_less() { - let package1 = Rpm::new( - "foo-bar".to_string(), - 0, - "1.2.3".to_string(), - "4".to_string(), - "x86_64".to_string(), - "foo-bar-1.2.3-4.x86_64".to_string(), - "1.2.3-4.x86_64".to_string(), - ); - let package2 = Rpm::new( - "foo-bar".to_string(), - 0, - "1.2.4".to_string(), - "4".to_string(), - "x86_64".to_string(), - "foo-bar-1.2.4-4.x86_64".to_string(), - "1.2.4-4.x86_64".to_string(), - ); + let package1 = Rpm { + name: "foo-bar".to_string(), + epoch: 0, + version: "1.2.3".to_string(), + release: "4".to_string(), + arch: "x86_64".to_string(), + full_name: "foo-bar-1.2.3-4.x86_64".to_string(), + full_version: "1.2.3-4.x86_64".to_string(), + }; + let package2 = Rpm { + name: "foo-bar".to_string(), + epoch: 0, + version: "1.2.4".to_string(), + release: "4".to_string(), + arch: "x86_64".to_string(), + full_name: "foo-bar-1.2.4-4.x86_64".to_string(), + full_version: "1.2.4-4.x86_64".to_string(), + }; assert!(package1.compare(&package2).unwrap().is_lt()); - let package2 = Rpm::new( - "foo-bar".to_string(), - 0, - "1.2.3".to_string(), - "5".to_string(), - "x86_64".to_string(), - "foo-bar-1.2.3-5.x86_64".to_string(), - "1.2.3-5.x86_64".to_string(), - ); + let package2 = Rpm { + name: "foo-bar".to_string(), + epoch: 0, + version: "1.2.3".to_string(), + release: "5".to_string(), + arch: "x86_64".to_string(), + full_name: "foo-bar-1.2.3-5.x86_64".to_string(), + full_version: "1.2.3-5.x86_64".to_string(), + }; assert!(package1.compare(&package2).unwrap().is_lt()); } #[test] pub fn test_compare_equal() { - let package1 = Rpm::new( - "docker-engine".to_string(), - 0, - "18.09.0.200".to_string(), - "20.h47.28.15.eulerosv2r10".to_string(), - "x86_64".to_string(), - "docker-engine-18.09.0.200-200.h47.28.15.eulerosv2r10.x86_64".to_string(), - "18.09.0.200-200.h47.28.15.eulerosv2r10.x86_64".to_string(), - ); - let package2 = Rpm::new( - "docker-engine".to_string(), - 1, - "18.09.0".to_string(), - "20.h62.33.19.eulerosv2r10".to_string(), - "x86_64".to_string(), - "docker-engine-1:18.09.0-200.h62.33.19.eulerosv2r10.x86_64".to_string(), - "1:18.09.0-200.h62.33.19.eulerosv2r10.x86_64".to_string(), - ); + let package1 = Rpm { + name: "docker-engine".to_string(), + epoch: 0, + version: "18.09.0.200".to_string(), + release: "20.h47.28.15.eulerosv2r10".to_string(), + arch: "x86_64".to_string(), + full_name:"docker-engine-18.09.0.200-200.h47.28.15.eulerosv2r10.x86_64".to_string(), + full_version: "18.09.0.200-200.h47.28.15.eulerosv2r10.x86_64".to_string(), + }; + let package2 = Rpm { + name: "docker-engine".to_string(), + epoch: 1, + version: "18.09.0".to_string(), + release: "20.h62.33.19.eulerosv2r10".to_string(), + arch: "x86_64".to_string(), + full_name: "docker-engine-1:18.09.0-200.h62.33.19.eulerosv2r10.x86_64".to_string(), + full_version: "1:18.09.0-200.h62.33.19.eulerosv2r10.x86_64".to_string(), + }; assert!(package2.compare(&package1).unwrap().is_gt()) } diff --git a/rust/notus/src/packages/slack.rs b/rust/notus/src/packages/slack.rs index 4d452f5ed..8d5f14b5a 100644 --- a/rust/notus/src/packages/slack.rs +++ b/rust/notus/src/packages/slack.rs @@ -1,7 +1,7 @@ // SPDX-FileCopyrightText: 2023 Greenbone AG // // SPDX-License-Identifier: GPL-2.0-or-later -use super::PackageVersion; +use super::{Package, PackageVersion}; use regex::Regex; use std::{ cmp::Ordering, @@ -30,7 +30,7 @@ impl Hash for Slack { } } -impl Slack { +impl Package for Slack { /// Returns a Some struct or None if not comparable fn compare(&self, other: &Slack) -> Option { if self.name != other.name { @@ -80,7 +80,7 @@ impl Slack { s.finish() } - pub fn from_full_name(full_name: &str) -> Option { + fn from_full_name(full_name: &str) -> Option { if full_name.is_empty() { return None; } @@ -124,7 +124,7 @@ impl Slack { }) } - pub fn from_name_and_full_version(name: &str, full_version: &str) -> Option { + fn from_name_and_full_version(name: &str, full_version: &str) -> Option { if name.is_empty() || full_version.is_empty() { return None; } @@ -169,6 +169,7 @@ mod slack_tests { use crate::packages; + use super::Package; use super::Slack; #[test]