Skip to content

Commit

Permalink
Return original Themis status code
Browse files Browse the repository at this point in the history
If we have a status code, add it as a "code" property to the Error
object we throw. Verify the code in tests where appropriate.
  • Loading branch information
ilammy committed Feb 18, 2019
1 parent 67b1299 commit 27fe0dc
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/wrappers/themis/jsthemis/addon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

#include <node.h>
#include "errors.hpp"
#include "secure_message.hpp"
#include "secure_keygen.hpp"
#include "secure_session.hpp"
Expand All @@ -24,6 +25,7 @@
#include "secure_comparator.hpp"

void InitAll(v8::Handle<v8::Object> exports) {
jsthemis::Errors::Init(exports);
jsthemis::SecureMessage::Init(exports);
jsthemis::KeyPair::Init(exports);
jsthemis::SecureSession::Init(exports);
Expand Down
32 changes: 29 additions & 3 deletions src/wrappers/themis/jsthemis/errors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,29 @@
#include <string>

#include <nan.h>
#include <node.h>

namespace jsthemis {

static inline void ExportStatusCode(v8::Handle<v8::Object>& exports, const char* name, themis_status_t status) {
exports->Set(Nan::New(name).ToLocalChecked(), Nan::New(status));
}

void Errors::Init(v8::Handle<v8::Object> exports) {
ExportStatusCode(exports, "SUCCESS", THEMIS_SUCCESS);
ExportStatusCode(exports, "FAIL", THEMIS_FAIL);
ExportStatusCode(exports, "INVALID_PARAMETER", THEMIS_INVALID_PARAMETER);
ExportStatusCode(exports, "NO_MEMORY", THEMIS_NO_MEMORY);
ExportStatusCode(exports, "BUFFER_TOO_SMALL", THEMIS_BUFFER_TOO_SMALL);
ExportStatusCode(exports, "DATA_CORRUPT", THEMIS_DATA_CORRUPT);
ExportStatusCode(exports, "INVALID_SIGNATURE", THEMIS_INVALID_SIGNATURE);
ExportStatusCode(exports, "NOT_SUPPORTED", THEMIS_NOT_SUPPORTED);
ExportStatusCode(exports, "SSESSION_KA_NOT_FINISHED", THEMIS_SSESSION_KA_NOT_FINISHED);
ExportStatusCode(exports, "SSESSION_TRANSPORT_ERROR", THEMIS_SSESSION_TRANSPORT_ERROR);
ExportStatusCode(exports, "SSESSION_GET_PUB_FOR_ID_CALLBACK_ERROR", THEMIS_SSESSION_GET_PUB_FOR_ID_CALLBACK_ERROR);
ExportStatusCode(exports, "SCOMPARE_NOT_READY", THEMIS_SCOMPARE_NOT_READY);
}

static const char* ErrorDescription(themis_status_t status) {
switch (status) {
case THEMIS_SUCCESS:
Expand Down Expand Up @@ -75,12 +95,18 @@ namespace jsthemis {
}
}

static v8::Local<v8::Value> WithStatus(v8::Local<v8::Value> error, themis_status_t status) {
v8::Local<v8::Object> object = error.As<v8::Object>();
object->Set(Nan::New("code").ToLocalChecked(), Nan::New(status));
return error;
}

void ThrowError(const char* domain, themis_status_t status) {
std::string message;
message += domain;
message += ": ";
message += ErrorDescription(status);
Nan::ThrowError(message.c_str());
Nan::ThrowError(WithStatus(Nan::Error(message.c_str()), status));
}

void ThrowError(const char* domain, const char* description) {
Expand All @@ -96,15 +122,15 @@ namespace jsthemis {
message += domain;
message += ": ";
message += ErrorDescriptionSecureSession(status);
Nan::ThrowError(message.c_str());
Nan::ThrowError(WithStatus(Nan::Error(message.c_str()), status));
}

void ThrowSecureComparatorError(const char* domain, themis_status_t status) {
std::string message;
message += domain;
message += ": ";
message += ErrorDescriptionSecureComparator(status);
Nan::ThrowError(message.c_str());
Nan::ThrowError(WithStatus(Nan::Error(message.c_str()), status));
}

} // namespace jsthemis
9 changes: 8 additions & 1 deletion src/wrappers/themis/jsthemis/errors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#ifndef JSTHEMIS_ERRORS_HPP_
#define JSTHEMIS_ERRORS_HPP_

#include <nan.h>

#include <themis/themis.h>

namespace jsthemis {
Expand All @@ -28,6 +30,11 @@ namespace jsthemis {

void ThrowSecureComparatorError(const char* domain, themis_status_t status);

}
namespace Errors {

void Init(v8::Handle<v8::Object> exports);

} // namespace Errors
} // namespace jsthemis

#endif /* JSTHEMIS_ERRORS_HPP_ */
9 changes: 7 additions & 2 deletions tests/jsthemis/test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
var addon = require('jsthemis');
var assert = require('assert');

function expect_code(code) {
return function(err) {
return err.code == code
}
}

describe("jsthemis", function(){
describe("secure message", function(){
Expand All @@ -16,14 +21,14 @@ describe("jsthemis", function(){

encrypted_message = encrypter.encrypt(message);
assert.equal(message.toString(), decrypter.decrypt(encrypted_message).toString());
assert.throws(function(){intruder_decrypter.decrypt(encrypted_message);});
assert.throws(function(){intruder_decrypter.decrypt(encrypted_message);}, expect_code(addon.FAIL));
});
it("sign/verify", function(){
signed_message=encrypter.sign(message);
assert.equal(message.toString(), decrypter.verify(signed_message).toString());
assert.equal(message.toString(), intruder_decrypter.verify(signed_message).toString());
signed_message[2]++;
assert.throws(function(){decrypter.verify(signed_message);});
assert.throws(function(){decrypter.verify(signed_message);}, expect_code(addon.INVALID_PARAMETER));
})
it("empty keys", function(){
encrypted_message = encrypter.encrypt(message);
Expand Down

0 comments on commit 27fe0dc

Please sign in to comment.