Skip to content

Commit

Permalink
implements api changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ata-no-one committed Apr 26, 2024
1 parent ccd18c3 commit 7349096
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 69 deletions.
2 changes: 0 additions & 2 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,5 @@ pub use crate::vaas::Vaas;
pub use builder::Builder;
pub use cancellation::CancellationToken;
pub use connection::Connection;
pub use message::Detection;
pub use message::LibMagic;
pub use sha256::Sha256;
pub use vaas_verdict::VaasVerdict;
12 changes: 0 additions & 12 deletions rust/src/message/detection.rs

This file was deleted.

10 changes: 0 additions & 10 deletions rust/src/message/lib_magic.rs

This file was deleted.

5 changes: 4 additions & 1 deletion rust/src/message/message_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ mod tests {
"guid": "9dae843d-e947-41db-ad39-ec73704529ed",
"verdict": "Clean",
"url": null,
"upload_token": null
"upload_token": null,
"detection": "",
"file_type": "",
"mime_type": ""
}
"#
.to_string();
Expand Down
4 changes: 0 additions & 4 deletions rust/src/message/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
mod auth_request;
mod auth_response;
mod detection;
mod error;
mod kind;
mod lib_magic;
mod message_type;
mod open_id_connect_token_response;
mod upload_url;
Expand All @@ -17,9 +15,7 @@ mod verdict_response;

pub(super) use auth_request::AuthRequest;
pub(super) use auth_response::AuthResponse;
pub use detection::Detection;
pub(super) use error::ErrorResponse;
pub use lib_magic::LibMagic;
pub(super) use message_type::MessageType;
pub(super) use open_id_connect_token_response::OpenIdConnectTokenResponse;
pub(super) use upload_url::UploadUrl;
Expand Down
29 changes: 8 additions & 21 deletions rust/src/message/verdict_response.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use super::{Detection, LibMagic};
use crate::error::Error;
use serde::{Deserialize, Serialize};
use std::convert::TryFrom;
Expand All @@ -10,8 +9,9 @@ pub struct VerdictResponse {
pub verdict: String,
pub url: Option<String>,
pub upload_token: Option<String>,
pub detections: Option<Vec<Detection>>,
pub lib_magic: Option<LibMagic>,
pub detection: Option<String>,
pub file_type: Option<String>,
pub mime_type: Option<String>,
}

impl TryFrom<&String> for VerdictResponse {
Expand All @@ -23,11 +23,11 @@ impl TryFrom<&String> for VerdictResponse {

#[cfg(test)]
mod tests {
use crate::message::{detection::Detection, lib_magic::LibMagic, VerdictResponse};
use crate::message::VerdictResponse;

#[test]
fn deserialize() {
let json = r#"{"sha256":"275a021bbfb6489e54d471899f7db9d1663fc695ec2fe2a2c4538aabf651fd0f","file_name":null,"verdict":"Malicious","upload_token":null,"url":null,"detections":[{"engine":2,"file_name":"/tmp/scan/051f699f-b21f-4d33-9cdd-d8b2f01e6118","virus":"EICAR-Test-File"},{"engine":3,"file_name":"/tmp/scan/051f699f-b21f-4d33-9cdd-d8b2f01e6118","virus":"EICAR_TEST_FILE"}],"lib_magic":{"file_type":"EICAR virus test files","mime_type":"text/plain"},"kind":"VerdictResponse","request_id":"ed7207a5-d65a-4400-b91c-673ff39cfd8b","guid":"ed7207a5-d65a-4400-b91c-673ff39cfd8b"}"#;
let json = r#"{"sha256":"275a021bbfb6489e54d471899f7db9d1663fc695ec2fe2a2c4538aabf651fd0f","file_name":null,"verdict":"Malicious","upload_token":null,"url":null,"detection":"EICAR-Test-File","file_type":"EICAR virus test files","mime_type":"text/plain","kind":"VerdictResponse","request_id":"ed7207a5-d65a-4400-b91c-673ff39cfd8b","guid":"ed7207a5-d65a-4400-b91c-673ff39cfd8b"}"#;
let verdict_response: VerdictResponse = serde_json::from_str(json).unwrap();

assert_eq!(
Expand All @@ -38,22 +38,9 @@ mod tests {
verdict: "Malicious".to_string(),
url: None,
upload_token: None,
detections: Some(vec![
Detection {
engine: 2,
file_name: "/tmp/scan/051f699f-b21f-4d33-9cdd-d8b2f01e6118".to_string(),
virus: "EICAR-Test-File".to_string()
},
Detection {
engine: 3,
file_name: "/tmp/scan/051f699f-b21f-4d33-9cdd-d8b2f01e6118".to_string(),
virus: "EICAR_TEST_FILE".to_string()
}
]),
lib_magic: Some(LibMagic {
file_type: "EICAR virus test files".to_string(),
mime_type: "text/plain".to_string()
})
detection: Some("EICAR-Test-File".to_string()),
file_type: Some("EICAR virus test files".to_string()),
mime_type: Some("text/plain".to_string()),
},
verdict_response
);
Expand Down
17 changes: 10 additions & 7 deletions rust/src/vaas_verdict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! The `VaaSVerdict` is the result of a request for a verdict. It contains the verdict itself and the SHA256 hash of the requested file.
use crate::error::Error;
use crate::message::{Detection, LibMagic, Verdict, VerdictResponse};
use crate::message::{Verdict, VerdictResponse};
use crate::sha256::Sha256;
use std::convert::TryFrom;

Expand All @@ -15,10 +15,12 @@ pub struct VaasVerdict {
pub sha256: Sha256,
/// Verdict for the file
pub verdict: Verdict,
/// Detections
pub detections: Option<Vec<Detection>>,
/// File and mime type as classified by https://www.darwinsys.com/file/
pub lib_magic: Option<LibMagic>,
/// Detection
pub detection: Option<String>,
/// File type as classified by https://www.darwinsys.com/file/
pub file_type: Option<String>,
/// mime type as classified by https://www.darwinsys.com/file/
pub mime_type: Option<String>,
}

impl TryFrom<VerdictResponse> for VaasVerdict {
Expand All @@ -27,8 +29,9 @@ impl TryFrom<VerdictResponse> for VaasVerdict {
Ok(Self {
sha256: Sha256::try_from(verdict_response.sha256.as_str())?,
verdict: Verdict::try_from(&verdict_response)?,
detections: verdict_response.detections,
lib_magic: verdict_response.lib_magic,
detection: verdict_response.detection,
file_type: verdict_response.file_type,
mime_type: verdict_response.mime_type,
})
}
}
32 changes: 20 additions & 12 deletions rust/tests/real_api_integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use reqwest::Url;
use std::convert::TryFrom;
use std::ops::Deref;
use vaas::auth::authenticators::{ClientCredentials, Password};
use vaas::{message::Verdict, CancellationToken, Connection, LibMagic, Sha256, Vaas};
use vaas::{message::Verdict, CancellationToken, Connection, Sha256, Vaas};

async fn get_vaas_with_flags(use_cache: bool, use_hash_lookup: bool) -> Connection {
let token_url: Url = dotenv::var("TOKEN_URL")
Expand Down Expand Up @@ -131,7 +131,8 @@ async fn from_http_response_stream_returns_malicious_verdict() {
}

#[tokio::test]
async fn from_http_response_stream_no_hash_lookup_no_cache_lookup_returns_malicious_verdict() {
async fn from_http_response_stream_no_hash_lookup_no_cache_lookup_returns_malicious_verdict_and_mimetype_and_detection(
) {
let result = reqwest::get("https://secure.eicar.org/eicar.com.txt").await;
let vaas = get_vaas_with_flags(false, false).await;

Expand All @@ -142,6 +143,18 @@ async fn from_http_response_stream_no_hash_lookup_no_cache_lookup_returns_malici
let verdict = vaas.for_stream(byte_stream, content_length, &ct).await;

assert_eq!(Verdict::Malicious, verdict.as_ref().unwrap().verdict);
assert_eq!(
Some("text/plain"),
verdict.as_ref().unwrap().mime_type.as_deref()
);
assert_eq!(
Some("EICAR virus test files"),
verdict.as_ref().unwrap().file_type.as_deref()
);
assert_eq!(
Some("EICAR-Test-File"),
verdict.as_ref().unwrap().detection.as_deref()
);
}

#[tokio::test]
Expand Down Expand Up @@ -291,18 +304,13 @@ async fn for_file_single_malicious_file() {

assert_eq!(Verdict::Malicious, verdict.verdict);
assert_eq!(Sha256::try_from(&tmp_file).unwrap(), verdict.sha256);
let detections = verdict.detections.unwrap();
assert_eq!(2, detections[0].engine);
assert_eq!("EICAR-Test-File".to_string(), detections[0].virus);
assert_eq!(3, detections[1].engine);
assert_eq!("EICAR_TEST_FILE".to_string(), detections[1].virus);
assert_eq!("EICAR-Test-File".to_string(), verdict.detection.unwrap());
assert_eq!(
Some(LibMagic {
file_type: "EICAR virus test files".to_string(),
mime_type: "text/plain".to_string()
}),
verdict.lib_magic
"EICAR virus test files".to_string(),
verdict.file_type.unwrap()
);
assert_eq!("text/plain".to_string(), verdict.mime_type.unwrap());

std::fs::remove_file(&tmp_file).unwrap();
}

Expand Down

0 comments on commit 7349096

Please sign in to comment.