Skip to content

Commit

Permalink
Remove PartialEq on Error
Browse files Browse the repository at this point in the history
  • Loading branch information
Tehforsch committed Nov 5, 2024
1 parent 684d28b commit 9d6a97d
Show file tree
Hide file tree
Showing 20 changed files with 57 additions and 52 deletions.
4 changes: 2 additions & 2 deletions rust/src/feed/update/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use thiserror::Error;

use crate::feed::{verify, VerifyError};

#[derive(Debug, Clone, PartialEq, Error)]
#[derive(Debug, Clone, Error)]
/// Errors within feed handling
pub enum ErrorKind {
/// An InterpretError occurred while interpreting
Expand All @@ -32,7 +32,7 @@ pub enum ErrorKind {
VerifyError(#[from] verify::Error),
}

#[derive(Debug, Clone, PartialEq, Error)]
#[derive(Debug, Clone, Error)]
#[error("Error with key '{key}': {kind}")]
/// ErrorKind and key of error
pub struct Error {
Expand Down
2 changes: 1 addition & 1 deletion rust/src/nasl/builtin/cryptographic/aes_cmac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn aes_cmac(register: &Register, _: &Context) -> Result<NaslValue, NaslError> {
let data = get_data(register)?;

let mut mac = Cmac::<Aes128>::new_from_slice(key)
.map_err(|e| CryptographicError::AesCmacError(format!("{}", e)))?;
.map_err(|e| CryptographicError::AesCmacInvalidLength(e))?;
mac.update(data);

Ok(mac.finalize().into_bytes().to_vec().into())
Expand Down
6 changes: 3 additions & 3 deletions rust/src/nasl/builtin/cryptographic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ pub mod hmac;
#[cfg(test)]
mod tests;

#[derive(Debug, Clone, PartialEq, Error)]
#[derive(Debug, Clone, Error)]
pub enum CryptographicError {
#[error("Error in AesGcm: insufficient buffer size.")]
InsufficientBufferSize,
#[error("Error in AesCcm: unable to encrypt.")]
AesCcmUnableToEncrypt,
#[error("Error in AesGmac: {0}.")]
AesGmacError(String),
#[error("Error in AesCmac: {0}.")]
AesCmacError(String),
#[error("Invalid length of key in AesCmac {0}.")]
AesCmacInvalidLength(digest::InvalidLength),
}

enum Crypt {
Expand Down
5 changes: 4 additions & 1 deletion rust/src/nasl/builtin/cryptographic/tests/aes_cbc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,8 @@ fn padding() {
"#,
);
let results = t.results();
assert_eq!(results[results.len() - 2], results[results.len() - 1]);
assert_eq!(
results[results.len() - 2].as_ref().unwrap(),
results[results.len() - 1].as_ref().unwrap()
);
}
5 changes: 4 additions & 1 deletion rust/src/nasl/builtin/cryptographic/tests/aes_gcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,8 @@ fn padding() {
"#,
);
let results = t.results();
assert_eq!(results[results.len() - 2], results[results.len() - 1]);
assert_eq!(
results[results.len() - 2].as_ref().unwrap(),
results[results.len() - 1].as_ref().unwrap()
);
}
2 changes: 1 addition & 1 deletion rust/src/nasl/builtin/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::cryptographic::CryptographicError;
use super::regex::RegexError;
use super::{misc::MiscError, network::socket::SocketError, ssh::SshError, string::StringError};

#[derive(Debug, Clone, PartialEq, Error)]
#[derive(Debug, Clone, Error)]
pub enum BuiltinError {
#[error("{0}")]
Ssh(SshError),
Expand Down
2 changes: 1 addition & 1 deletion rust/src/nasl/builtin/misc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use flate2::{
read::GzDecoder, read::ZlibDecoder, write::GzEncoder, write::ZlibEncoder, Compression,
};

#[derive(Debug, Clone, PartialEq, Error)]
#[derive(Debug, Clone, Error)]
// It would be nicer to derive this using #[from] from
// thiserror, but io::Error does not impl `PartialEq`,
// `Eq` or `Clone`, so we wrap `io::ErrorKind` instead, which
Expand Down
22 changes: 6 additions & 16 deletions rust/src/nasl/builtin/misc/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,12 @@ mod tests {
#[test]
fn gunzip() {
let mut t = TestBuilder::default();
t.run_all(
r#"
z = raw_string (0x78, 0x9c, 0xab, 0x02, 0x00, 0x00, 0x7b, 0x00, 0x7b);
gunzip(data: z);
# With Header Format and data is data
gz = gzip(data: 'gz', headformat: "gzip");
gunzip(data: gz);
# Without Header format and data is a string
ngz = gzip(data: "ngz");
gunzip(data: ngz);
"#,
);
let results = t.results();
assert_eq!(results[1], Ok(NaslValue::String("z".into())));
assert_eq!(results[3], Ok(NaslValue::String("gz".into())));
assert_eq!(results[5], Ok(NaslValue::String("ngz".into())));
t.run(r#"z = raw_string (0x78, 0x9c, 0xab, 0x02, 0x00, 0x00, 0x7b, 0x00, 0x7b);"#);
t.ok(r#"gunzip(data: z);"#, "z");
t.run(r#"gz = gzip(data: 'gz', headformat: "gzip");"#);
t.ok(r#"gunzip(data: gz);"#, "gz");
t.run(r#"ngz = gzip(data: "ngz");"#);
t.ok(r#"gunzip(data: ngz);"#, "ngz");
}

#[test]
Expand Down
6 changes: 3 additions & 3 deletions rust/src/nasl/builtin/network/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ use super::{
// Number of times to resend a UDP packet, when no response is received
const NUM_TIMES_TO_RESEND: usize = 5;

#[derive(Debug, Clone, PartialEq, Error)]
#[derive(Debug, Clone, Error)]
#[error("{0}")]
// It would be nicer to derive this using #[from] from
// thiserror, but io::Error does not impl `PartialEq`,
// `Eq` or `Clone`, so we wrap `io::ErrorKind` instead, which
// thiserror, but io::Error does not impl `Clone`,
// so we wrap `io::ErrorKind` instead, which
// does not impl `Error` which is why this `From` impl exists.
pub enum SocketError {
IO(std::io::ErrorKind),
Expand Down
2 changes: 1 addition & 1 deletion rust/src/nasl/builtin/raw_ip/packet_forgery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use socket2::{Domain, Protocol, Socket};
use thiserror::Error;
use tracing::debug;

#[derive(Debug, Clone, PartialEq, Error)]
#[derive(Debug, Clone, Error)]
pub enum PacketForgeryError {
#[error("{0}")]
Custom(String),
Expand Down
2 changes: 1 addition & 1 deletion rust/src/nasl/builtin/regex/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::nasl::prelude::*;
use regex::{Regex, RegexBuilder};
use thiserror::Error;

#[derive(Debug, Clone, PartialEq, Error)]
#[derive(Debug, Clone, Error)]
pub enum RegexError {
#[error("Error building regular expression pattern: {0}")]
BuildingError(regex::Error),
Expand Down
8 changes: 4 additions & 4 deletions rust/src/nasl/builtin/regex/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ mod tests {
"#,
);
assert_eq!(
t.results()[1],
Ok(NaslValue::String("Pair 0\n Pair 2\n".to_string()))
t.results()[1].as_ref().unwrap(),
&NaslValue::String("Pair 0\n Pair 2\n".to_string())
);
}

Expand All @@ -124,8 +124,8 @@ mod tests {
"#,
);
assert_eq!(
t.results()[1],
Ok(NaslValue::String("Pair 0\n Pair 2\n".to_string()))
t.results()[1].as_ref().unwrap(),
&NaslValue::String("Pair 0\n Pair 2\n".to_string())
);
}

Expand Down
6 changes: 3 additions & 3 deletions rust/src/nasl/builtin/ssh/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use thiserror::Error;
use super::SessionId;

/// A cloneable representation of the Error type of the underlying SSH lib
#[derive(Clone, Debug, PartialEq, Error)]
#[derive(Clone, Debug, Error)]
#[error("{0}")]
pub struct LibError(String);

Expand All @@ -23,7 +23,7 @@ impl From<russh::Error> for LibError {
}
}

#[derive(Clone, Debug, PartialEq, Error)]
#[derive(Clone, Debug, Error)]
pub struct SshError {
pub kind: SshErrorKind,
id: Option<SessionId>,
Expand All @@ -46,7 +46,7 @@ impl fmt::Display for SshError {
}
}

#[derive(Clone, Debug, PartialEq, Eq, Error)]
#[derive(Clone, Debug, Error)]
pub enum SshErrorKind {
#[error("Failed to open new SSH session.")]
NewSession,
Expand Down
2 changes: 1 addition & 1 deletion rust/src/nasl/builtin/string/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::nasl::prelude::*;

use super::BuiltinError;

#[derive(Debug, Clone, PartialEq, Error)]
#[derive(Debug, Clone, Error)]
#[error("{0}")]
pub struct StringError(#[from] std::fmt::Error);

Expand Down
6 changes: 3 additions & 3 deletions rust/src/nasl/interpreter/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::nasl::InternalError;
use crate::storage::StorageError;
use thiserror::Error;

#[derive(Debug, Clone, PartialEq, Error)]
#[derive(Debug, Clone, Error)]
/// An error that occurred while calling a function
#[error("Error while calling function '{function}': {kind}")]
pub struct FunctionError {
Expand All @@ -32,7 +32,7 @@ impl FunctionError {
}
}

#[derive(Debug, Clone, PartialEq, Error)]
#[derive(Debug, Clone, Error)]
/// Is used to represent an error while interpreting
#[error("{}{kind}", self.origin.clone().map(|e| format!("{e}: ")).unwrap_or_default())]
pub struct InterpretError {
Expand All @@ -43,7 +43,7 @@ pub struct InterpretError {
pub origin: Option<Statement>,
}

#[derive(Debug, Clone, PartialEq, Error)]
#[derive(Debug, Clone, Error)]
/// Is used to give hints to the user how to react on an error while interpreting
pub enum InterpretErrorKind {
/// When returned context is a function when a value is required.
Expand Down
15 changes: 9 additions & 6 deletions rust/src/nasl/interpreter/include.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,19 @@ mod tests {
let ctx = context.build(Default::default());
let mut interpreter = CodeInterpreter::new(code, register, &ctx);
assert_eq!(
interpreter.next_statement().await,
Some(Ok(NaslValue::Null))
interpreter.next_statement().await.unwrap().unwrap(),
NaslValue::Null
);
assert_eq!(interpreter.next_statement().await, Some(Ok(12.into())));
assert_eq!(
interpreter.next_statement().await,
Some(Ok(NaslValue::Dict(HashMap::from([(
interpreter.next_statement().await.unwrap().unwrap(),
12.into()
);
assert_eq!(
interpreter.next_statement().await.unwrap().unwrap(),
NaslValue::Dict(HashMap::from([(
"hello".to_owned(),
NaslValue::Data("world".as_bytes().into())
)]))))
)]))
);
}
}
5 changes: 4 additions & 1 deletion rust/src/nasl/interpreter/tests/description.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ if(description)
t.set_variable("description", NaslValue::Number(1));
t.run_all(code);
let results = t.results();
assert_eq!(*results.last().unwrap(), Ok(NaslValue::Exit(23)));
assert_eq!(
*results.last().unwrap().as_ref().unwrap(),
NaslValue::Exit(23)
);

let mut tag = BTreeMap::new();
tag.insert(TagKey::CreationDate, 1366091481.into());
Expand Down
5 changes: 4 additions & 1 deletion rust/src/nasl/interpreter/tests/local_var.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,8 @@ if (a) {
a;
"###,
);
assert_eq!(t.results().last().unwrap(), &Ok(NaslValue::Number(1)));
assert!(matches!(
t.results().last().unwrap(),
&Ok(NaslValue::Number(1))
));
}
2 changes: 1 addition & 1 deletion rust/src/nasl/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ where
reference: &TestResult,
) -> bool {
match reference {
TestResult::Ok(val) => result.as_ref() == Ok(val),
TestResult::Ok(val) => result.as_ref().unwrap() == val,
TestResult::GenericCheck(f, _) => f(result.clone()),
TestResult::None => true,
}
Expand Down
2 changes: 1 addition & 1 deletion rust/src/nasl/utils/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::storage::StorageError;

use super::ContextType;

#[derive(Debug, Clone, PartialEq, Error)]
#[derive(Debug, Clone, Error)]
/// Descriptive kind of error that can occur while calling a function
pub enum NaslError {
/// Diagnostic string is informational and the second arg is the return value for the user
Expand Down

0 comments on commit 9d6a97d

Please sign in to comment.