Skip to content

Commit

Permalink
Merge pull request #18 from GDATASoftwareAG/PrintStatusMessages
Browse files Browse the repository at this point in the history
Print status messages
  • Loading branch information
doxthree authored Apr 21, 2022
2 parents 018edb1 + a8291f0 commit d9f1944
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 13 deletions.
4 changes: 2 additions & 2 deletions java/src/main/java/de/gdata/vaas/Vaas.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ private void UploadFile(Path file, String url, String authToken) throws IOExcept
var response = HttpClient
.newBuilder()
.build()
.send(request, HttpResponse.BodyHandlers.discarding());
.send(request, HttpResponse.BodyHandlers.ofString());

if (response.statusCode() != 200) {
throw new IOException("Failed to upload file. HTTP Status Code: " + response.statusCode());
throw new IOException("Failed to upload file. HTTP Status Code: " + response.statusCode() + " Error: " + response.body());
}
}

Expand Down
3 changes: 3 additions & 0 deletions php/src/vaas/Exceptions/UploadFailedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@

class UploadFailedException extends Exception
{
function __construct(string $message, int $code) {
parent::__construct($message, $code);
}
}
2 changes: 1 addition & 1 deletion php/src/vaas/Vaas.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function ForFile(string $path, bool $upload = true, string $uuid = null):
'headers' => ["Authorization" => $verdictResponse->upload_token]
]);
if ($response->getStatusCode() > 399) {
throw new UploadFailedException();
throw new UploadFailedException($response->getReasonPhrase(), $response->getStatusCode());
}
$verdictResponse = $this->_waitForVerdict($verdictResponse->guid);
return $verdictResponse->verdict;
Expand Down
2 changes: 1 addition & 1 deletion rust/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ impl Connection {
let response = upload_file(file, upload_url, auth_token).await?;

if response.status() != 200 {
return Err(Error::FailedUploadFile(response.status()));
return Err(Error::FailedUploadFile(response.status(), response.text().await.expect("failed to get payload")));
}

let resp = Self::wait_for_response(guid, result_channel, ct).await?;
Expand Down
4 changes: 2 additions & 2 deletions rust/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ pub enum Error {
#[error("Failed to send file: `{0}`")]
FailedRequest(String),
/// Failed to upload the file. Server answered with an non-200 status code.
#[error("Server answered with status code: `{0}`")]
FailedUploadFile(StatusCode),
#[error("Server answered with status code: `{0}` `{1}`")]
FailedUploadFile(StatusCode, String),
/// Authentication token for the file upload in the response message is missing.
#[error("Missing authentication token for file upload")]
MissingAuthToken,
Expand Down
14 changes: 7 additions & 7 deletions typescript/src/vaas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default class Vaas {
this.callbacks = new Map<string, VerdictCallback>();
}

private static toHexString(byteArray: Uint8Array) {
public static toHexString(byteArray: Uint8Array) {
return Array.from(byteArray, function (byte) {
return ('0' + (byte & 0xFF).toString(16)).slice(-2);
}).join('')
Expand Down Expand Up @@ -67,7 +67,7 @@ export default class Vaas {
return Promise.all(promises);
}

private async forRequest(sample: string | Uint8Array, ct: CancellationToken): Promise<VerdictResponse> {
public async forRequest(sample: string | Uint8Array, ct: CancellationToken): Promise<VerdictResponse> {
return new Promise((resolve, reject) => {
if (this.connection === null) {
reject("Not connected");
Expand Down Expand Up @@ -152,17 +152,17 @@ export default class Vaas {
})
}

private async upload(verdictResponse: VerdictResponse, fileBuffer: Uint8Array) {
public async upload(verdictResponse: VerdictResponse, fileBuffer: Uint8Array) {
return new Promise(async (resolve, reject) => {
const instance = axios.default.create({
baseURL: verdictResponse.url,
timeout: 10000,
headers: {'Authorization': verdictResponse.upload_token!}
});
const response = await instance.put("/", fileBuffer);
if (response.status != 200) {
reject(`Upload failed with ${response.status}`);
}
const response = await instance.put("/", fileBuffer)
.catch((error) => {
reject(`Upload failed with ${error.response.status} - Error ${error.response.data.message}`)
});
resolve(response);
});

Expand Down
20 changes: 20 additions & 0 deletions typescript/tests/test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {expect} from 'chai';
import {describe} from 'mocha';
import * as sha256 from "fast-sha256";
import * as dotenv from "dotenv";
import Vaas from '../src/vaas';
import * as randomBytes from 'random-bytes';
Expand Down Expand Up @@ -94,6 +95,25 @@ describe('Test verdict requests', () => {
}
}).timeout(testTimeoutFileReq);

it('test if there is a mismatch between submitted hash for file and uploaded file', async () => {
dotenv.config();
const token = process.env.VAAS_TOKEN!;
const randomFileContent = await randomBytes.sync(50);
const sample = Vaas.toHexString(sha256.hash(randomFileContent));
try
{
const vaas = new Vaas();
await vaas.connect(token)
const verdictResponse = await vaas.forRequest(sample, CancellationToken.fromMilliseconds(2_000));
const otherRandomFile = await randomBytes.sync(40)
await vaas.upload(verdictResponse, otherRandomFile);
}
catch (error)
{
expect(error).to.equal("Upload failed with 400 - Error Bad request: Wrong file");
}
}).timeout(testTimeoutFileReq);

it('if a list of SHA256 is uploaded, they are detected', async () => {
dotenv.config();
const token = process.env.VAAS_TOKEN!;
Expand Down

0 comments on commit d9f1944

Please sign in to comment.