public void Success_sync() { MetadefenderCoreClient metadefenderCoreClient = new MetadefenderCoreClient(GetMockApiUrl()); string existingDataId = "61dffeaa728844adbf49eb090e4ece0e"; CreateStub("/file", "POST", 200, GetJsonFromFile("MetadefenderCoreClient.test.resources.apiResponses.scanFile.scanFile_success.json")); CreateStub("/file/" + existingDataId, "GET", 200, GetJsonFromFile("MetadefenderCoreClient.test.resources.apiResponses.fetchScanResult.fetchScanResult_success.json")); using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("MetadefenderCoreClient.test.resources.testScanFile.txt")) { FileScanResult result = metadefenderCoreClient.ScanFileSync( stream, new FileScanOptions().SetFileName("fileName.txt"), 50, 1000 * 30, 4000); Assert.AreEqual("Allowed", result.process_info.result); Assert.AreEqual("Clean", result.scan_results.scan_all_result_a); Assert.Null(result.extracted_files); } HttpServer.AssertWasCalled(x => { return(x.CustomVerb("POST", "/file")); } ); HttpServer.AssertWasCalled(x => { return(x.CustomVerb("GET", "/file/" + existingDataId)); } ); }
public void ServerError() { MetadefenderCoreClient metadefenderCoreClient = new MetadefenderCoreClient(GetMockApiUrl()); CreateStub("/stat/engines", "GET", 200, GetJsonFromFile("MetadefenderCoreClient.test.resources.apiResponses.errorJson.json")); bool isException = false; try { metadefenderCoreClient.GetEngineVersions(); } catch (MetadefenderClientException) { isException = true; } Assert.True(isException); HttpServer.AssertWasCalled(x => { return(x.CustomVerb("GET", "/stat/engines")); } ); }
public void ServerError() { CreateStubForLogin(); MetadefenderCoreClient metadefenderCoreClient = new MetadefenderCoreClient(GetMockApiUrl(), "admin", "admin"); CreateStub("/admin/license", "GET", 500, GetJsonFromFile("MetadefenderCoreClient.test.resources.apiResponses.errorJson.json")); bool isException = false; try { metadefenderCoreClient.GetCurrentLicenseInformation(); } catch (MetadefenderClientException) { isException = true; } Assert.True(isException); HttpServer.AssertWasCalled(x => { return(x.CustomVerb("GET", "/admin/license")); } ); }
public void WithError() { MetadefenderCoreClient metadefenderCoreClient = new MetadefenderCoreClient(GetMockApiUrl()); CreateStub("/file", "POST", 500, GetJsonFromFile("MetadefenderCoreClient.test.resources.apiResponses.errorJson.json")); bool isException = false; using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("MetadefenderCoreClient.test.resources.testScanFile.txt")) { try { metadefenderCoreClient.ScanFile(stream, null); } catch (MetadefenderClientException) { isException = true; } } Assert.True(isException); HttpServer.AssertWasCalled(x => { return(x.CustomVerb("POST", "/file")); } ); }
public void SuccessWithFileOptions() { MetadefenderCoreClient metadefenderCoreClient = new MetadefenderCoreClient(GetMockApiUrl()); CreateStub("/file", "POST", 200, GetJsonFromFile("MetadefenderCoreClient.test.resources.apiResponses.scanFile.scanFile_success.json")); using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("MetadefenderCoreClient.test.resources.testScanFile.txt")) { string dataId = metadefenderCoreClient.ScanFile( stream, new FileScanOptions() .SetUserAgent("Java client") .SetFileName("file.txt") .SetRule("Default Rule") ); Assert.AreEqual("61dffeaa728844adbf49eb090e4ece0e", dataId); } HttpServer.AssertWasCalled(x => { var ret = x.CustomVerb("POST", "/file"); ret.WithHeader("user_agent", new EqualConstraint("Java client")); return(ret); } ); }
public void WithNotFound() { MetadefenderCoreClient metadefenderCoreClient = new MetadefenderCoreClient(GetMockApiUrl()); string nonExistingHash = "61dffeaa728844adbf49eb090e4ece0e"; CreateStub("/hash/" + nonExistingHash, "GET", 200, GetJsonFromFile("MetadefenderCoreClient.test.resources.apiResponses.fetchScanResultByHash.fetchScanResult_notFound.json")); bool isException = false; try { metadefenderCoreClient.FetchScanResultByHash(nonExistingHash); } catch (MetadefenderClientException) { isException = true; } Assert.True(isException); HttpServer.AssertWasCalled(x => { return(x.CustomVerb("GET", "/hash/" + nonExistingHash)); } ); }
public void WithError() { MetadefenderCoreClient metadefenderCoreClient = new MetadefenderCoreClient(GetMockApiUrl()); string existingDataId = "61dffeaa728844adbf49eb090e4ece0e"; CreateStub("/hash/" + existingDataId, "GET", 500, GetJsonFromFile("MetadefenderCoreClient.test.resources.apiResponses.errorJson.json")); bool isException = false; try { metadefenderCoreClient.FetchScanResultByHash(existingDataId); } catch (MetadefenderClientException) { isException = true; } Assert.True(isException); HttpServer.AssertWasCalled(x => { return(x.CustomVerb("GET", "/hash/" + existingDataId)); } ); }
private static void ScanFile(string apiUrl, string file, string rule = "") { MetadefenderCoreClient metadefenderCoreClient = new MetadefenderCoreClient(apiUrl); try { // The Core ID is usefull when using external LB // It actually points the cookie value that identify the core server the LB sent the file to string CoreId = apiUrl; Stream inputStream = File.Open(file, FileMode.Open); FileScanOptions fso = new FileScanOptions(); fso.SetFileName(GetFileNameFromPath(file)); if (!string.IsNullOrEmpty(rule)) { fso.SetRule(rule); } string dataId = metadefenderCoreClient.ScanFile(inputStream, fso, out CoreId, inputStream.Length); Console.WriteLine("File scan started. The data id is: " + dataId); if (CoreId.ToLower() != apiUrl.ToLower()) { Console.WriteLine("Core Id: {0}", CoreId); } } catch (MetadefenderClientException e) { Console.WriteLine("Error during file scan: " + e.GetDetailedMessage()); } catch (FileNotFoundException e) { Console.WriteLine("File not found: " + file + " Exception: " + e.Message); } }
public void ApiRedirectTest() { CreateStubForLogin(); MetadefenderCoreClient metadefenderCoreClient = new MetadefenderCoreClient(GetMockApiUrl(), "admin", "admin"); HttpServer.Stub(x => x.CustomVerb("/admin/license", "GET")) .AddHeader("Content-Type", "application/json; charset=utf-8") .AddHeader("Location", "/admin/licenseRedirected") .WithStatus((HttpStatusCode)302); // the redirected resource HttpServer.Stub(x => x.CustomVerb("/admin/licenseRedirected", "GET")) .Return(GetJsonFromFile("MetadefenderCoreClient.test.resources.apiResponses.getCurrentLicenseInformation.getCurrentLicenseInformation_success.json")) .AddHeader("Content-Type", "application/json; charset=utf-8") .AddHeader("Location", "/admin/licenseRedirected") .WithStatus((HttpStatusCode)200); License result = metadefenderCoreClient.GetCurrentLicenseInformation(); Assert.AreEqual(3740, result.days_left); HttpServer.AssertWasCalled(x => { return(x.CustomVerb("GET", "/admin/license")); } ); HttpServer.AssertWasCalled(x => { return(x.CustomVerb("GET", "/admin/licenseRedirected")); } ); }
public void ValidateCurrentSessionTest_notLoggedIn_1() { MetadefenderCoreClient metadefenderCoreClient = new MetadefenderCoreClient(GetMockApiUrl()); bool isLoggedIn = metadefenderCoreClient.ValidateCurrentSession(); Assert.False(isLoggedIn); }
public void LoginTest() { CreateStubForLogin(); MetadefenderCoreClient metadefenderCoreClient = new MetadefenderCoreClient(GetMockApiUrl(), "admin", "admin"); Assert.AreEqual(TEST_SESSION_ID, metadefenderCoreClient.GetSessionId()); HttpServer.AssertWasCalled(x => { var ret = x.CustomVerb("POST", "/login"); ret.WithBody("{\"user\":\"admin\",\"password\":\"admin\"}"); return(ret); }); }
public void ValidateCurrentSessionTest_loggedIn() { CreateStubForLogin(); CreateStubForVersion(); MetadefenderCoreClient metadefenderCoreClient = new MetadefenderCoreClient(GetMockApiUrl()); metadefenderCoreClient.Login("admin", "admin"); bool isLoggedIn = metadefenderCoreClient.ValidateCurrentSession(); Assert.True(isLoggedIn); }
public void ValidateCurrentSessionTest_notLoggedIn_sessionExpired() { CreateStubForLogin(); CreateStub("/version", "GET", 403, GetJsonFromFile("MetadefenderCoreClient.test.resources.apiResponses.version.getVersion_accessDenied.json")); MetadefenderCoreClient metadefenderCoreClient = new MetadefenderCoreClient(GetMockApiUrl()); metadefenderCoreClient.Login("admin", "admin"); bool isLoggedIn = metadefenderCoreClient.ValidateCurrentSession(); Assert.False(isLoggedIn); }
public void WithoutDataId() { MetadefenderCoreClient metadefenderCoreClient = new MetadefenderCoreClient(GetMockApiUrl()); bool isException = false; try { metadefenderCoreClient.FetchScanResultByHash(null); } catch (MetadefenderClientException) { isException = true; } Assert.True(isException); }
public void Success() { MetadefenderCoreClient metadefenderCoreClient = new MetadefenderCoreClient(GetMockApiUrl()); CreateStub("/stat/engines", "GET", 200, GetJsonFromFile("MetadefenderCoreClient.test.resources.apiResponses.getEngineVersions.getEngineVersions_success.json")); List <EngineVersion> result = metadefenderCoreClient.GetEngineVersions(); Assert.AreEqual(47, result.Count); HttpServer.AssertWasCalled(x => { return(x.CustomVerb("GET", "/stat/engines")); } ); }
public void Success_withNewUnknownFields() { MetadefenderCoreClient metadefenderCoreClient = new MetadefenderCoreClient(GetMockApiUrl()); CreateStub("/file/rules", "GET", 200, GetJsonFromFile("MetadefenderCoreClient.test.resources.apiResponses.getAvailableScanRules.getAvailableScanRules_withNewUnknownFieldsJson.json")); List <ScanRule> result = metadefenderCoreClient.GetAvailableScanRules(); Assert.AreEqual(2, result.Count); HttpServer.AssertWasCalled(x => { return(x.CustomVerb("GET", "/file/rules")); } ); }
private static void FetchScanResultByHash(string apiUrl, string hash) { MetadefenderCoreClient metadefenderCoreClient = new MetadefenderCoreClient(apiUrl); try { FileScanResult result = metadefenderCoreClient.FetchScanResultByHash(hash); Console.WriteLine("Fetch result by file hash: " + result.process_info.result); if (result.process_info.post_processing != null) { Console.WriteLine("post processing: " + result.process_info.post_processing); } } catch (MetadefenderClientException e) { Console.WriteLine("Error during fetch scan by hash: " + e.GetDetailedMessage()); } }
public void Success() { MetadefenderCoreClient metadefenderCoreClient = new MetadefenderCoreClient(GetMockApiUrl()); CreateStub("/file", "POST", 200, GetJsonFromFile("MetadefenderCoreClient.test.resources.apiResponses.scanFile.scanFile_success.json")); using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("MetadefenderCoreClient.test.resources.testScanFile.txt")) { string dataId = metadefenderCoreClient.ScanFile(stream, null); Assert.AreEqual("61dffeaa728844adbf49eb090e4ece0e", dataId); } HttpServer.AssertWasCalled(x => { return(x.CustomVerb("POST", "/file")); } ); }
public void Success() { MetadefenderCoreClient metadefenderCoreClient = new MetadefenderCoreClient(GetMockApiUrl()); string existingHash = "e981b537cff14c3fbbba923d7a71ff2e"; CreateStub("/hash/" + existingHash, "GET", 200, GetJsonFromFile("MetadefenderCoreClient.test.resources.apiResponses.fetchScanResultByHash.fetchScanResultByHash_success.json")); FileScanResult result = metadefenderCoreClient.FetchScanResultByHash(existingHash); Assert.AreEqual(existingHash, result.data_id); Assert.AreEqual("Allowed", result.process_info.result); Assert.AreEqual("Clean", result.scan_results.scan_all_result_a); HttpServer.AssertWasCalled(x => { return(x.CustomVerb("GET", "/hash/" + existingHash)); }); }
public void GetVersionTest() { CreateStubForLogin(); CreateStubForVersion(); MetadefenderCoreClient metadefenderCoreClient = new MetadefenderCoreClient(GetMockApiUrl(), "admin", "admin"); ApiVersion apiVersion = metadefenderCoreClient.GetVersion(); Assert.AreEqual("4.3.0.256", apiVersion.version); Assert.AreEqual("MSCL", apiVersion.product_id); HttpServer.AssertWasCalled(x => { var ret = x.CustomVerb("GET", "/version"); ret.WithHeader("apikey", new EqualConstraint(TEST_SESSION_ID)); return(ret); } ); }
public void Success_withNewUnknownFields() { CreateStubForLogin(); MetadefenderCoreClient metadefenderCoreClient = new MetadefenderCoreClient(GetMockApiUrl(), "admin", "admin"); CreateStub("/admin/license", "GET", 200, GetJsonFromFile("MetadefenderCoreClient.test.resources.apiResponses.getCurrentLicenseInformation.getCurrentLicenseInformation_withNewUnknownFields.json")); License result = metadefenderCoreClient.GetCurrentLicenseInformation(); Assert.AreEqual(3740, result.days_left); Assert.AreEqual("OPSWAT, Inc.", result.licensed_to); Assert.AreEqual(10, result.max_agent_count); HttpServer.AssertWasCalled(x => { return(x.CustomVerb("GET", "/admin/license")); } ); }
public void Success() { MetadefenderCoreClient metadefenderCoreClient = new MetadefenderCoreClient(GetMockApiUrl()); string existingDataId = "59f92cb3e3194c6381d3f8819a0d47ed"; CreateStub("/file/" + existingDataId, "GET", 200, GetJsonFromFile("MetadefenderCoreClient.test.resources.apiResponses.fetchScanResult.fetchScanResult_success.json")); FileScanResult result = metadefenderCoreClient.FetchScanResult(existingDataId); Assert.AreEqual(existingDataId, result.data_id); Assert.AreEqual("Allowed", result.process_info.result); Assert.AreEqual("Clean", result.scan_results.scan_all_result_a); Assert.Null(result.extracted_files); HttpServer.AssertWasCalled(x => { return(x.CustomVerb("GET", "/file/" + existingDataId)); } ); }
public void LogoutTest() { CreateStubForLogin(); CreateStub("/logout", "POST", 200, GetJsonFromFile("MetadefenderCoreClient.test.resources.apiResponses.logout.logout_success.json")); MetadefenderCoreClient metadefenderCoreClient = new MetadefenderCoreClient(GetMockApiUrl(), "admin", "admin"); Assert.NotNull(metadefenderCoreClient.GetSessionId()); metadefenderCoreClient.Logout(); Assert.Null(metadefenderCoreClient.GetSessionId()); HttpServer.AssertWasCalled(x => { var ret = x.CustomVerb("POST", "/logout"); ret.WithHeader("apikey", new EqualConstraint(TEST_SESSION_ID)); return(ret); } ); }
public void Success_withArchive() { MetadefenderCoreClient metadefenderCoreClient = new MetadefenderCoreClient(GetMockApiUrl()); string existingDataId = "fafb3a12b0d141909b3a3ba6b26e42c9"; CreateStub("/file/" + existingDataId, "GET", 200, GetJsonFromFile("MetadefenderCoreClient.test.resources.apiResponses.fetchScanResult.fetchScanResult_success_withArchive.json")); FileScanResult result = metadefenderCoreClient.FetchScanResult(existingDataId); Assert.AreEqual(existingDataId, result.data_id); Assert.AreEqual("Allowed", result.process_info.result); Assert.AreEqual("Clean", result.scan_results.scan_all_result_a); Assert.NotNull(result.extracted_files); Assert.AreEqual(2L, result.extracted_files.files_in_archive.Count); HttpServer.AssertWasCalled(x => { return(x.CustomVerb("GET", "/file/" + existingDataId)); } ); }
private static void ScanFile(string apiUrl, string file) { MetadefenderCoreClient metadefenderCoreClient = new MetadefenderCoreClient(apiUrl); // This is optional: using custom HttpConnector metadefenderCoreClient.SetHttpConnector(new CustomHttpConnector()); try { Stream inputStream = File.Open(file, FileMode.Open); string dataId = metadefenderCoreClient.ScanFile(inputStream, new FileScanOptions().SetFileName(GetFileNameFromPath(file))); Console.WriteLine("File scan started. The data id is: " + dataId); } catch (MetadefenderClientException e) { Console.WriteLine("Error during file scan: " + e.GetDetailedMessage()); } catch (FileNotFoundException e) { Console.WriteLine("File not found: " + file + " Exception: " + e.Message); } }
public void Success_syncTimeout() { MetadefenderCoreClient metadefenderCoreClient = new MetadefenderCoreClient(GetMockApiUrl()); string existingDataId = "61dffeaa728844adbf49eb090e4ece0e"; CreateStub("/file", "POST", 200, GetJsonFromFile("MetadefenderCoreClient.test.resources.apiResponses.scanFile.scanFile_success.json")); CreateStub("/file/" + existingDataId, "GET", 200, GetJsonFromFile("MetadefenderCoreClient.test.resources.apiResponses.fetchScanResult.fetchScanResult_inProgress.json")); bool isException = false; using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("MetadefenderCoreClient.test.resources.testScanFile.txt")) { try { // it should be a timeout, because we return in progress response every time FileScanResult result = metadefenderCoreClient.ScanFileSync(stream, new FileScanOptions().SetFileName("fileName.txt"), 50, 1000 * 30, 2000); } catch (TimeoutException) { isException = true; } Assert.True(isException); } HttpServer.AssertWasCalled(x => { return(x.CustomVerb("POST", "/file")); } ); HttpServer.AssertWasCalled(x => { return(x.CustomVerb("GET", "/file/" + existingDataId)); } ); }
private static void ScanFileSync(string apiUrl, string file) { MetadefenderCoreClient metadefenderCoreClient = new MetadefenderCoreClient(apiUrl); try { Stream inputStream = File.Open(file, FileMode.Open); FileScanResult result = metadefenderCoreClient.ScanFileSync(inputStream, new FileScanOptions().SetFileName(GetFileNameFromPath(file)), 200, 1000 * 30, 5000); Console.WriteLine("File scan finished with result: " + result.process_info.result); if (result.process_info.post_processing != null) { Console.WriteLine("post processing: " + result.process_info.post_processing); } } catch (MetadefenderClientException e) { Console.WriteLine("Error during file scan: " + e.GetDetailedMessage()); } catch (FileNotFoundException e) { Console.WriteLine("File not found: " + file + " Exception: " + e.Message); } }
private static void ScanFileSync(string apiUrl, string file, string rule = "") { int TaskTimeout = 600 * 1000; // 10 minutes MetadefenderCoreClient metadefenderCoreClient = new MetadefenderCoreClient(apiUrl); // The Core ID is usefull when using external LB // It actually points the cookie value that identify the core server the LB sent the file to string CoreId = apiUrl; try { Stream inputStream = File.Open(file, FileMode.Open); FileScanOptions fso = new FileScanOptions(); fso.SetFileName(GetFileNameFromPath(file)); if (!string.IsNullOrEmpty(rule)) { fso.SetRule(rule); } FileScanResult result = metadefenderCoreClient.ScanFileSync(inputStream, fso, 200, 1000 * 30, TaskTimeout, out CoreId); Console.WriteLine("File scan finished with result: " + result.process_info.result); if (CoreId.ToLower() != apiUrl.ToLower()) { Console.WriteLine("Core Id: {0}", CoreId); } if (result.process_info.post_processing != null) { if (!string.IsNullOrEmpty(result.process_info.post_processing.actions_ran)) { Console.WriteLine("post processing: " + result.process_info.post_processing.actions_ran); } if (result.process_info.post_processing.actions_ran.ToLower().Contains("sanitized")) { Console.WriteLine("\nFile was sanitized. Where would you like to save the sanitized file?"); Console.Write("Enter folder path: "); string destinationPath = Console.ReadLine(); if (!Directory.Exists(destinationPath)) { throw new Exception(string.Format("Destination folder {0} does not exist!", destinationPath)); } string cdrErrorMessage = null; Stream sanitizedStream = metadefenderCoreClient.DownloadSanitizedFile(result.data_id, out cdrErrorMessage); if (string.IsNullOrEmpty(cdrErrorMessage)) { string sanitizedFilename = result.process_info.post_processing.converted_destination; // save the file on disk using (var fileStream = File.Create(Path.Combine(destinationPath, sanitizedFilename))) { sanitizedStream.Seek(0, SeekOrigin.Begin); sanitizedStream.CopyTo(fileStream, 81920); } } else { Console.WriteLine("Failed to download sanitized file! Reason: {0}", cdrErrorMessage); } } } } catch (MetadefenderClientException e) { Console.WriteLine("Error during file scan: " + e.GetDetailedMessage()); } catch (FileNotFoundException e) { Console.WriteLine("File not found: " + file + " Exception: " + e.Message); } catch (Exception e) { Console.WriteLine("General error: " + file + " Exception: " + e.Message); } }
private static void ShowApiInfo(string apiUrl, string apiUser, string apiUserPass) { MetadefenderCoreClient metadefenderCoreClient; try { metadefenderCoreClient = new MetadefenderCoreClient(apiUrl, apiUser, apiUserPass); Console.WriteLine("Metadefender client created. Session id is: " + metadefenderCoreClient.GetSessionId()); } catch (MetadefenderClientException e) { Console.WriteLine("Cannot login to this API. Error message: " + e.GetDetailedMessage()); return; } try { License license = metadefenderCoreClient.GetCurrentLicenseInformation(); Console.WriteLine("Licensed to: " + license.licensed_to); } catch (MetadefenderClientException e) { Console.WriteLine("Cannot get license details: " + e.GetDetailedMessage()); } try { List <EngineVersion> result = metadefenderCoreClient.GetEngineVersions(); Console.WriteLine("Fetched engine/database versions: " + result.Count); } catch (MetadefenderClientException e) { Console.WriteLine("Cannot get engine/database versions: " + e.GetDetailedMessage()); } try { ApiVersion apiVersion = metadefenderCoreClient.GetVersion(); Console.WriteLine("Api endpoint apiVersion: " + apiVersion.version); } catch (MetadefenderClientException e) { Console.WriteLine("Cannot get api endpoint version: " + e.GetDetailedMessage()); } try { List <ScanRule> scanRules = metadefenderCoreClient.GetAvailableScanRules(); Console.WriteLine("Available scan rules: " + scanRules.Count); } catch (MetadefenderClientException e) { Console.WriteLine("Cannot get available scan rules: " + e.GetDetailedMessage()); } try { metadefenderCoreClient.Logout(); Console.WriteLine("Client successfully logged out."); } catch (MetadefenderClientException e) { Console.WriteLine("Cannot log out: " + e.GetDetailedMessage()); } }