summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorKyungwook Tak <k.tak@samsung.com>2016-07-21 16:46:00 +0900
committerKyungwook Tak <k.tak@samsung.com>2016-07-26 15:51:15 +0900
commitd6da3e3d9bc29e22103b094bee5ca68f5d8f0f61 (patch)
tree6439a58283180d86c72f4c30413d55be728f6dd4 /tests
parent6818b8559b7d4d45adaeb1937d708a154dc00fd7 (diff)
downloadlibwebappenc-d6da3e3d9bc29e22103b094bee5ca68f5d8f0f61.tar.gz
libwebappenc-d6da3e3d9bc29e22103b094bee5ca68f5d8f0f61.tar.bz2
libwebappenc-d6da3e3d9bc29e22103b094bee5ca68f5d8f0f61.zip
Add data structures
For migrated web app, we need to more fields in cache e.g., IV and is_migrated flag to handle it separately. Because cipher algorithm, iv and key size could be different between old secure storage, it depends on product implementation. So this architecture needs more flexibility. A lot of code changed because of the principle data structure is added from the bottom. Change-Id: Id6a10b9f707f4da25016dd928ab4049be619a610 Signed-off-by: Kyungwook Tak <k.tak@samsung.com>
Diffstat (limited to 'tests')
-rw-r--r--tests/internals.cpp325
-rw-r--r--tests/non-normals.cpp8
-rw-r--r--tests/normals.cpp4
-rw-r--r--tests/test-common.cpp35
-rw-r--r--tests/test-common.h7
-rw-r--r--tests/test-helper.cpp100
-rw-r--r--tests/test-helper.h4
7 files changed, 251 insertions, 232 deletions
diff --git a/tests/internals.cpp b/tests/internals.cpp
index 257447b..b5c106d 100644
--- a/tests/internals.cpp
+++ b/tests/internals.cpp
@@ -22,6 +22,7 @@
*/
#include "web_app_enc.h"
+#include <string>
#include <cstring>
#include <unistd.h>
@@ -32,6 +33,51 @@
#include "test-common.h"
+namespace {
+
+using rb_raii = std::unique_ptr<raw_buffer_s, void(*)(raw_buffer_s *)>;
+using ce_raii = std::unique_ptr<crypto_element_s, void(*)(crypto_element_s *)>;
+using map_raii = std::unique_ptr<crypto_element_map_s, void(*)(crypto_element_map_s *)>;
+
+inline rb_raii _safe(raw_buffer_s *ptr)
+{
+ return rb_raii(ptr, buffer_destroy);
+}
+
+inline ce_raii _safe(crypto_element_s *ptr)
+{
+ return ce_raii(ptr, crypto_element_destroy);
+}
+
+inline map_raii _safe(crypto_element_map_s *ptr)
+{
+ return map_raii(ptr, crypto_element_map_destroy);
+}
+
+crypto_element_s *_create_ce(void)
+{
+ raw_buffer_s *dek = buffer_create(32);
+ raw_buffer_s *iv = buffer_create(16);
+ crypto_element_s *ce = crypto_element_create(dek, iv);
+
+ if (ce == nullptr) {
+ buffer_destroy(dek);
+ buffer_destroy(iv);
+ } else if (_get_random(ce->dek) != WAE_ERROR_NONE) {
+ crypto_element_destroy(ce);
+ ce = nullptr;
+ } else if (_get_random(ce->iv) != WAE_ERROR_NONE) {
+ crypto_element_destroy(ce);
+ ce = nullptr;
+ }
+
+ BOOST_REQUIRE(ce != nullptr);
+
+ return ce;
+}
+
+}
+
BOOST_AUTO_TEST_SUITE(SYSTEM)
BOOST_AUTO_TEST_SUITE(INTERNALS)
@@ -78,29 +124,37 @@ BOOST_AUTO_TEST_CASE(encrypt_decrypt_app_dek)
"+wIDAQAB\n"
"-----END PUBLIC KEY-----";
- std::vector<unsigned char> dek(32, 0);
+ raw_buffer_s *dek = buffer_create(32);
+
+ auto _raii1 = _safe(dek);
+
+ BOOST_REQUIRE_MESSAGE(dek != nullptr && dek->size == 32, "Failed to create buffer");
+ BOOST_REQUIRE_MESSAGE(_get_random(dek) == WAE_ERROR_NONE, "Failed to get random");
+
+ raw_buffer_s pubkey;
+
+ pubkey.buf = (unsigned char *)public_key;
+ pubkey.size = strlen(public_key);
+
+ raw_buffer_s *encrypted = nullptr;
+ int ret = encrypt_app_dek(&pubkey, dek, &encrypted);
- unsigned char *_encrypted = nullptr;
- size_t _encrypted_len = 0;
- int ret = encrypt_app_dek(reinterpret_cast<const unsigned char *>(public_key),
- strlen(public_key), dek.data(), dek.size(), &_encrypted,
- &_encrypted_len);
- auto encrypted = Wae::Test::bytearr_to_vec(_encrypted, _encrypted_len);
- free(_encrypted);
+ auto _raii2 = _safe(encrypted);
BOOST_REQUIRE_MESSAGE(ret == WAE_ERROR_NONE, "Failed to encrypt_app_dek. ec: " << ret);
- unsigned char *_decrypted = nullptr;
- size_t _decrypted_len = 0;
- ret = decrypt_app_dek(reinterpret_cast<const unsigned char *>(private_key),
- strlen(private_key), nullptr, encrypted.data(), encrypted.size(),
- &_decrypted, &_decrypted_len);
- auto decrypted = Wae::Test::bytearr_to_vec(_decrypted, _decrypted_len);
- free(_decrypted);
+ raw_buffer_s prikey;
+ prikey.buf = (unsigned char *)private_key;
+ prikey.size = strlen(private_key);
+
+ raw_buffer_s *decrypted = nullptr;
+ ret = decrypt_app_dek(&prikey, nullptr, encrypted, &decrypted);
+
+ auto _raii3 = _safe(decrypted);
BOOST_REQUIRE_MESSAGE(ret == WAE_ERROR_NONE, "Failed to decrypt_app_dek. ec: " << ret);
- BOOST_REQUIRE_MESSAGE(dek == decrypted,
+ BOOST_REQUIRE_MESSAGE(Wae::Test::bytes_to_hex(dek) == Wae::Test::bytes_to_hex(decrypted),
"encrypted/decrypted dek isn't valid. "
"dek(" << Wae::Test::bytes_to_hex(dek) << ") "
"decrypted(" << Wae::Test::bytes_to_hex(decrypted) << ")");
@@ -108,37 +162,32 @@ BOOST_AUTO_TEST_CASE(encrypt_decrypt_app_dek)
BOOST_AUTO_TEST_CASE(encrypt_decrypt_aes_cbc)
{
- std::vector<unsigned char> plaintext = {
- 'a', 'b', 'c', 'a', 'b', 'c', 'x', 'y',
- 'o', 'q', '2', 'e', 'v', '0', '1', 'x'
- };
+ raw_buffer_s *data = buffer_create(16);
+
+ auto _raii1 = _safe(data);
- size_t dek_len = 32;
- std::vector<unsigned char> dek(dek_len, 0);
+ BOOST_REQUIRE_MESSAGE(data != nullptr && data->size == 16, "Failed to create buffer");
- int ret = _get_random(dek.size(), dek.data());
- BOOST_REQUIRE_MESSAGE(ret == WAE_ERROR_NONE, "Failed to get random");
+ crypto_element_s *ce = _create_ce();
- unsigned char *_encrypted = nullptr;
- size_t _encrypted_len = 0;
- ret = encrypt_aes_cbc(dek.data(), dek.size(), plaintext.data(), plaintext.size(),
- &_encrypted, &_encrypted_len);
- auto encrypted = Wae::Test::bytearr_to_vec(_encrypted, _encrypted_len);
- free(_encrypted);
+ auto _raii2 = _safe(ce);
+
+ raw_buffer_s *encrypted = nullptr;
+ int ret = encrypt_aes_cbc(ce, data, &encrypted);
+
+ auto _raii3 = _safe(encrypted);
BOOST_REQUIRE_MESSAGE(ret == WAE_ERROR_NONE, "Failed to encrypt_aes_cbc. ec: " << ret);
- unsigned char *_decrypted = nullptr;
- size_t _decrypted_len = 0;
- ret = decrypt_aes_cbc(dek.data(), dek.size(), encrypted.data(), encrypted.size(),
- &_decrypted, &_decrypted_len);
- auto decrypted = Wae::Test::bytearr_to_vec(_decrypted, _decrypted_len);
- free(_decrypted);
+ raw_buffer_s *decrypted = nullptr;
+ ret = decrypt_aes_cbc(ce, encrypted, &decrypted);
+
+ auto _raii4 = _safe(decrypted);
BOOST_REQUIRE_MESSAGE(ret == WAE_ERROR_NONE, "Failed to decrypt_aes_cbc. ec: " << ret);
- BOOST_REQUIRE_MESSAGE(plaintext == decrypted,
+ BOOST_REQUIRE_MESSAGE(Wae::Test::bytes_to_hex(data) == Wae::Test::bytes_to_hex(decrypted),
"decrypted plaintext isn't valid. "
- "plaintext(" << Wae::Test::bytes_to_hex(plaintext) << ") "
+ "plaintext(" << Wae::Test::bytes_to_hex(data) << ") "
"decrypted(" << Wae::Test::bytes_to_hex(decrypted) << ")");
}
@@ -149,90 +198,79 @@ BOOST_AUTO_TEST_CASE(cache)
const char *pkg3 = "pkg3";
const char *pkgDummy = "dummy";
- std::vector<unsigned char> dek1(32, 1);
- std::vector<unsigned char> dek2(32, 2);
- std::vector<unsigned char> dek3(32, 3);
+ auto ce1 = _create_ce();
+ auto ce2 = _create_ce();
+ auto ce3 = _create_ce();
- _initialize_cache();
+ crypto_element_map_s *map = nullptr;
- _add_app_dek_to_cache(pkg1, dek1.data());
- _add_app_dek_to_cache(pkg2, dek2.data());
- _add_app_dek_to_cache(pkg3, dek3.data());
+ int tmp = crypto_element_map_add(&map, pkg1, ce1);
+ BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NONE, "Failed to add ce to map. ret: " << tmp);
- size_t dek_len = 32;
- const unsigned char *_cached = _get_app_dek_from_cache(pkg1);
- auto cached = Wae::Test::bytearr_to_vec(_cached, dek_len);
+ tmp = crypto_element_map_add(&map, pkg2, ce2);
+ BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NONE, "Failed to add ce to map. ret: " << tmp);
- BOOST_REQUIRE_MESSAGE(cached == dek1,
- "cached dek isn't valid! "
- "dek(" << Wae::Test::bytes_to_hex(dek1) << ") "
- "cached(" << Wae::Test::bytes_to_hex(cached) << ")");
+ tmp = crypto_element_map_add(&map, pkg3, ce3);
+ BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NONE, "Failed to add ce to map. ret: " << tmp);
- _cached = _get_app_dek_from_cache(pkg2);
- cached = Wae::Test::bytearr_to_vec(_cached, dek_len);
+ BOOST_REQUIRE_MESSAGE(crypto_element_map_get(map, pkg1) == ce1,
+ "cached ce has different address with actual.");
+ BOOST_REQUIRE_MESSAGE(crypto_element_map_get(map, pkg2) == ce2,
+ "cached ce has different address with actual.");
+ BOOST_REQUIRE_MESSAGE(crypto_element_map_get(map, pkg3) == ce3,
+ "cached ce has different address with actual.");
+ BOOST_REQUIRE_MESSAGE(crypto_element_map_get(map, pkgDummy) == nullptr,
+ "something returned with pkg dummy from map which should be null.");
- BOOST_REQUIRE_MESSAGE(cached == dek2,
- "cached dek isn't valid! "
- "dek(" << Wae::Test::bytes_to_hex(dek2) << ") "
- "cached(" << Wae::Test::bytes_to_hex(cached) << ")");
+ crypto_element_map_remove(&map, pkg3);
- _cached = _get_app_dek_from_cache(pkg3);
- cached = Wae::Test::bytearr_to_vec(_cached, dek_len);
+ BOOST_REQUIRE_MESSAGE(crypto_element_map_get(map, pkg3) == nullptr,
+ "removed pkg(" << pkg3 << ") is returned from map which should be null.");
- BOOST_REQUIRE_MESSAGE(cached == dek3,
- "cached dek isn't valid! "
- "dek(" << Wae::Test::bytes_to_hex(dek3) << ") "
- "cached(" << Wae::Test::bytes_to_hex(cached) << ")");
+ auto ce4 = _create_ce();
+ tmp = crypto_element_map_add(&map, pkg1, ce4);
+ BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NONE, "Failed to update ce to map. ret: " << tmp);
- _cached = _get_app_dek_from_cache(pkgDummy);
- if (_cached) {
- cached = Wae::Test::bytearr_to_vec(_cached, dek_len);
- BOOST_REQUIRE_MESSAGE(false,
- "wrong cached val is extracted by dummy pkg id. "
- "val(" << Wae::Test::bytes_to_hex(cached) << ")");
- }
+ BOOST_REQUIRE_MESSAGE(crypto_element_map_get(map, pkg1) == ce4,
+ "cached ce has different address with actual.");
- _remove_app_dek_from_cache(pkg3);
+ crypto_element_map_destroy(map);
+}
- _cached = _get_app_dek_from_cache(pkg3);
- if (_cached) {
- cached = Wae::Test::bytearr_to_vec(_cached, dek_len);
- BOOST_REQUIRE_MESSAGE(false,
- "app dek removed from cache but it's remained! "
- "val(" << Wae::Test::bytes_to_hex(cached) << ")");
- }
+BOOST_AUTO_TEST_CASE(cache_max)
+{
+ crypto_element_map_s *map = nullptr;
- _initialize_cache();
+ for (size_t i = 0; i < MAX_MAP_ELEMENT_SIZE + 1; ++i) {
+ BOOST_REQUIRE(crypto_element_map_add(&map, std::to_string(i).c_str(), _create_ce()) == WAE_ERROR_NONE);
+ }
- _add_app_dek_to_cache(pkg1, dek1.data());
+ BOOST_REQUIRE(crypto_element_map_get(map, "0") == nullptr);
- _cached = nullptr;
- _cached = _get_app_dek_from_cache(pkg2);
- if (_cached) {
- cached = Wae::Test::bytearr_to_vec(_cached, dek_len);
- BOOST_REQUIRE_MESSAGE(false,
- "cache is initialized but something is remained! "
- "val(" << Wae::Test::bytes_to_hex(cached) << ")");
- }
+ crypto_element_map_destroy(map);
}
BOOST_AUTO_TEST_CASE(read_write_encrypted_app_dek)
{
const char *pkg_id = "write_test_pkg";
- std::vector<unsigned char> dek(256, 0);
+ raw_buffer_s *dek = buffer_create(256);
- int ret = _write_encrypted_app_dek_to_file(pkg_id, dek.data(), dek.size());
+ auto raii1 = _safe(dek);
+
+ BOOST_REQUIRE(dek != nullptr);
+ BOOST_REQUIRE(_get_random(dek) == WAE_ERROR_NONE);
+
+ int ret = _write_encrypted_app_dek_to_file(pkg_id, dek);
BOOST_REQUIRE_MESSAGE(ret == WAE_ERROR_NONE, "Failed to write_encrypted_app_dek_to_file. ec: " << ret);
- unsigned char *_readed = nullptr;
- size_t _readed_len = 0;
- ret = _read_encrypted_app_dek_from_file(pkg_id, &_readed, &_readed_len);
- auto readed = Wae::Test::bytearr_to_vec(_readed, _readed_len);
- free(_readed);
+ raw_buffer_s *readed = nullptr;
+ ret = _read_encrypted_app_dek_from_file(pkg_id, &readed);
BOOST_REQUIRE_MESSAGE(ret == WAE_ERROR_NONE, "Failed to read_encrypted_app_dek_from_file. ec: " << ret);
- BOOST_REQUIRE_MESSAGE(dek == readed,
+ auto raii2 = _safe(readed);
+
+ BOOST_REQUIRE_MESSAGE(Wae::Test::bytes_to_hex(dek) == Wae::Test::bytes_to_hex(readed),
"dek isn't match after write/read file. "
"dek(" << Wae::Test::bytes_to_hex(dek) << ") "
"readed(" << Wae::Test::bytes_to_hex(readed) << ")");
@@ -242,35 +280,23 @@ BOOST_AUTO_TEST_CASE(get_create_preloaded_app_dek_1)
{
const char *pkg_id = "TEST_PKG_ID_FOR_CREATE";
- unsigned char *_readed = nullptr;
- size_t _readed_len = 0;
- int ret = get_preloaded_app_dek(pkg_id, &_readed, &_readed_len);
- auto readed = Wae::Test::bytearr_to_vec(_readed, _readed_len);
- free(_readed);
+ const crypto_element_s *readed = nullptr;
+ int ret = get_preloaded_app_ce(pkg_id, &readed);
BOOST_REQUIRE_MESSAGE(ret == WAE_ERROR_NO_KEY,
- "preloaded app dek to create is already exist. ec: " << ret);
+ "preloaded app ce to create is already exist. ec: " << ret);
- unsigned char *_dek = nullptr;
- size_t _dek_len = 0;
- ret = create_preloaded_app_dek(pkg_id, &_dek, &_dek_len);
- auto dek = Wae::Test::bytearr_to_vec(_dek, _dek_len);
+ const crypto_element_s *ce = nullptr;
+ ret = create_preloaded_app_ce(pkg_id, &ce);
BOOST_REQUIRE_MESSAGE(ret == WAE_ERROR_NONE,
- "Failed to create_preloaded_app_dek. ec: " << ret);
-
- _readed = nullptr;
- ret = get_preloaded_app_dek(pkg_id, &_readed, &_readed_len);
- readed = Wae::Test::bytearr_to_vec(_readed, _readed_len);
- free(_readed);
+ "Failed to create_preloaded_app_ce. ec: " << ret);
+ ret = get_preloaded_app_ce(pkg_id, &readed);
BOOST_REQUIRE_MESSAGE(ret == WAE_ERROR_NONE,
- "Failed to get_preloaded_app_dek. ec: " << ret);
+ "Failed to get_preloaded_app_ce. ec: " << ret);
- BOOST_REQUIRE_MESSAGE(dek == readed,
- "created/loaded dek is not matched! "
- "created(" << Wae::Test::bytes_to_hex(dek) << ") "
- "loaded(" << Wae::Test::bytes_to_hex(readed) << ")");
+ BOOST_REQUIRE_MESSAGE(readed == ce, "cached ce address and actual is different!");
}
BOOST_AUTO_TEST_CASE(get_create_preloaded_app_dek_2)
@@ -284,67 +310,42 @@ BOOST_AUTO_TEST_CASE(get_create_preloaded_app_dek_2)
_get_preloaded_app_dek_file_path(pkg_id2, sizeof(path2), path2);
// remove old test data
- remove_app_dek(pkg_id1, WAE_PRELOADED_APP);
- remove_app_dek(pkg_id2, WAE_PRELOADED_APP);
+ remove_app_ce(pkg_id1, WAE_PRELOADED_APP);
+ remove_app_ce(pkg_id2, WAE_PRELOADED_APP);
unlink(path1);
unlink(path2);
- // create 2 deks for preloaded app
- unsigned char *_dek1 = nullptr;
- size_t _dek_len1 = 0;
- int ret = create_preloaded_app_dek(pkg_id1, &_dek1, &_dek_len1);
- auto dek1 = Wae::Test::bytearr_to_vec(_dek1, _dek_len1);
- free(_dek1);
-
+ // create 2 ces for preloaded app
+ const crypto_element_s *ce1 = nullptr;
+ int ret = create_preloaded_app_ce(pkg_id1, &ce1);
BOOST_REQUIRE_MESSAGE(ret == WAE_ERROR_NONE,
- "Failed to create_preloaded_app_dek. ec: " << ret);
-
- unsigned char *_dek2 = nullptr;
- size_t _dek_len2 = 0;
- ret = create_preloaded_app_dek(pkg_id2, &_dek2, &_dek_len2);
- auto dek2 = Wae::Test::bytearr_to_vec(_dek2, _dek_len2);
- free(_dek2);
+ "Failed to create_preloaded_app_ce. ec: " << ret);
+ const crypto_element_s *ce2 = nullptr;
+ ret = create_preloaded_app_ce(pkg_id2, &ce2);
BOOST_REQUIRE_MESSAGE(ret == WAE_ERROR_NONE,
- "Failed to create_preloaded_app_dek. ec: " << ret);
+ "Failed to create_preloaded_app_ce. ec: " << ret);
ret = load_preloaded_app_deks(true);
BOOST_REQUIRE_MESSAGE(ret == WAE_ERROR_NONE,
"Failed to load_preloaded_app_deks. ec: " << ret);
- unsigned char *_readed1 = nullptr;
- size_t _readed_len1 = 0;
- ret = get_app_dek(pkg_id1, WAE_PRELOADED_APP, &_readed1, &_readed_len1);
- auto readed1 = Wae::Test::bytearr_to_vec(_readed1, _readed_len1);
- free(_readed1);
-
+ const crypto_element_s *readed1 = nullptr;
+ ret = get_app_ce(pkg_id1, WAE_PRELOADED_APP, false, &readed1);
BOOST_REQUIRE_MESSAGE(ret == WAE_ERROR_NONE, "Failed to get_app_dek. ec: " << ret);
- unsigned char *_readed2 = nullptr;
- size_t _readed_len2 = 0;
- ret = get_app_dek(pkg_id2, WAE_PRELOADED_APP, &_readed2, &_readed_len2);
- auto readed2 = Wae::Test::bytearr_to_vec(_readed2, _readed_len2);
- free(_readed2);
-
+ const crypto_element_s *readed2 = nullptr;
+ ret = get_app_ce(pkg_id2, WAE_PRELOADED_APP, false, &readed2);
BOOST_REQUIRE_MESSAGE(ret == WAE_ERROR_NONE, "Failed to get_app_dek. ec: " << ret);
- BOOST_REQUIRE_MESSAGE(dek1 == readed1,
- "readed dek and original isn't matched! "
- "original(" << Wae::Test::bytes_to_hex(dek1) << ") "
- "readed(" << Wae::Test::bytes_to_hex(readed1) << ")");
+ BOOST_REQUIRE_MESSAGE(readed1 == ce1, "cached ce and actual address is different!");
+ BOOST_REQUIRE_MESSAGE(readed2 == ce2, "cached ce and actual address is different!");
- BOOST_REQUIRE_MESSAGE(dek2 == readed2,
- "readed dek and original isn't matched! "
- "original(" << Wae::Test::bytes_to_hex(dek2) << ") "
- "readed(" << Wae::Test::bytes_to_hex(readed2) << ")");
+ ret = remove_app_ce(pkg_id1, WAE_PRELOADED_APP);
+ BOOST_REQUIRE_MESSAGE(ret == WAE_ERROR_NONE, "Failed remove app ce. ec: " << ret);
- ret = remove_app_dek(pkg_id1, WAE_PRELOADED_APP);
- BOOST_REQUIRE_MESSAGE(ret == WAE_ERROR_NONE,
- "Failed remove app dek after used. ec: " << ret);
-
- ret = remove_app_dek(pkg_id2, WAE_PRELOADED_APP);
- BOOST_REQUIRE_MESSAGE(ret == WAE_ERROR_NONE,
- "Failed remove app dek after used. ec: " << ret);
+ ret = remove_app_ce(pkg_id2, WAE_PRELOADED_APP);
+ BOOST_REQUIRE_MESSAGE(ret == WAE_ERROR_NONE, "Failed remove app ce. ec: " << ret);
}
BOOST_AUTO_TEST_SUITE_END() // INTERNALS
diff --git a/tests/non-normals.cpp b/tests/non-normals.cpp
index 8667bfc..81c0173 100644
--- a/tests/non-normals.cpp
+++ b/tests/non-normals.cpp
@@ -32,12 +32,12 @@ BOOST_AUTO_TEST_SUITE(GLOBAL_APP)
BOOST_AUTO_TEST_CASE(add_get_remove_dek)
{
- Wae::Test::add_get_remove_dek(WAE_DOWNLOADED_GLOBAL_APP);
+ Wae::Test::add_get_remove_ce(WAE_DOWNLOADED_GLOBAL_APP);
}
BOOST_AUTO_TEST_CASE(create_app_dek)
{
- Wae::Test::create_app_dek(WAE_DOWNLOADED_GLOBAL_APP);
+ Wae::Test::create_app_ce(WAE_DOWNLOADED_GLOBAL_APP);
}
BOOST_AUTO_TEST_CASE(encrypt_decrypt)
@@ -52,12 +52,12 @@ BOOST_AUTO_TEST_SUITE(PRELOADED_APP)
BOOST_AUTO_TEST_CASE(add_get_remove_dek)
{
- Wae::Test::add_get_remove_dek(WAE_PRELOADED_APP);
+ Wae::Test::add_get_remove_ce(WAE_PRELOADED_APP);
}
BOOST_AUTO_TEST_CASE(create_app_dek)
{
- Wae::Test::create_app_dek(WAE_PRELOADED_APP);
+ Wae::Test::create_app_ce(WAE_PRELOADED_APP);
}
BOOST_AUTO_TEST_CASE(encrypt_decrypt)
diff --git a/tests/normals.cpp b/tests/normals.cpp
index 2ce272e..5d67910 100644
--- a/tests/normals.cpp
+++ b/tests/normals.cpp
@@ -32,12 +32,12 @@ BOOST_AUTO_TEST_SUITE(DOWNLOADED_APP);
BOOST_AUTO_TEST_CASE(add_get_remove_dek)
{
- Wae::Test::add_get_remove_dek(WAE_DOWNLOADED_NORMAL_APP);
+ Wae::Test::add_get_remove_ce(WAE_DOWNLOADED_NORMAL_APP);
}
BOOST_AUTO_TEST_CASE(create_app_dek)
{
- Wae::Test::create_app_dek(WAE_DOWNLOADED_NORMAL_APP);
+ Wae::Test::create_app_ce(WAE_DOWNLOADED_NORMAL_APP);
}
BOOST_AUTO_TEST_CASE(encrypt_decrypt_normal_app)
diff --git a/tests/test-common.cpp b/tests/test-common.cpp
index 3e9e331..3157ca6 100644
--- a/tests/test-common.cpp
+++ b/tests/test-common.cpp
@@ -38,15 +38,42 @@ std::string bytes_to_hex(const std::vector<unsigned char> &bytes)
return ss.str();
}
-std::vector<unsigned char> bytearr_to_vec(const unsigned char *bytes, size_t len)
+std::string bytes_to_hex(const raw_buffer_s *rb)
{
- if (bytes == nullptr || len == 0)
+ if (!is_buffer_valid(rb))
+ return std::string();
+
+ return bytes_to_hex(rb->buf, rb->size);
+}
+
+std::string bytes_to_hex(const unsigned char *ptr, size_t len)
+{
+ std::stringstream ss;
+ ss << std::hex;
+
+ for (size_t i = 0; i < len; ++i)
+ ss << std::setw(2) << std::setfill('0') << static_cast<int>(ptr[i]);
+
+ return ss.str();
+}
+
+std::vector<unsigned char> bytearr_to_vec(const raw_buffer_s *rb)
+{
+ if (!is_buffer_valid(rb))
+ return std::vector<unsigned char>();
+
+ return bytearr_to_vec(rb->buf, rb->size);
+}
+
+std::vector<unsigned char> bytearr_to_vec(const unsigned char *ptr, size_t len)
+{
+ if (ptr == nullptr || len == 0)
return std::vector<unsigned char>();
- std::vector<unsigned char> vec;
+ std::vector<unsigned char> vec(len);
for (size_t i = 0; i < len; ++i)
- vec.push_back(bytes[i]);
+ vec[i] = ptr[i];
return vec;
}
diff --git a/tests/test-common.h b/tests/test-common.h
index 2c4c54e..57bf83b 100644
--- a/tests/test-common.h
+++ b/tests/test-common.h
@@ -28,6 +28,8 @@
#include "colour_log_formatter.h"
+#include "types.h"
+
/* fixtures should be declared on outside of namespace */
struct TestConfig {
TestConfig()
@@ -45,7 +47,10 @@ namespace Wae {
namespace Test {
std::string bytes_to_hex(const std::vector<unsigned char> &bytes);
-std::vector<unsigned char> bytearr_to_vec(const unsigned char *bytes, size_t len);
+std::string bytes_to_hex(const unsigned char *ptr, size_t len);
+std::string bytes_to_hex(const raw_buffer_s *rb);
+std::vector<unsigned char> bytearr_to_vec(const unsigned char *ptr, size_t len);
+std::vector<unsigned char> bytearr_to_vec(const raw_buffer_s *);
} // namespace Test
} // namespace Wae
diff --git a/tests/test-helper.cpp b/tests/test-helper.cpp
index 144299a..f303b75 100644
--- a/tests/test-helper.cpp
+++ b/tests/test-helper.cpp
@@ -25,78 +25,64 @@
#include "key_handler.h"
#include "crypto_service.h"
+#include "types.h"
+#include "key_manager.h"
#include "test-common.h"
namespace Wae {
namespace Test {
-void add_get_remove_dek(wae_app_type_e app_type)
+void add_get_remove_ce(wae_app_type_e app_type)
{
const char *pkg_id = "TEST_PKG_ID";
- std::vector<unsigned char> dek(32, 0);
-
- BOOST_REQUIRE(_get_random(dek.size(), dek.data()) == WAE_ERROR_NONE);
-
- remove_app_dek(pkg_id, app_type);
-
- int tmp = _add_dek_to_key_manager(pkg_id, app_type, dek.data(), dek.size());
- BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NONE,
- "Failed to _add_dek_to_key_manager. ec: " << tmp);
-
- unsigned char *_stored_dek = nullptr;
- size_t _stored_dek_len = 0;
- tmp = get_app_dek(pkg_id, app_type, &_stored_dek, &_stored_dek_len);
- BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NONE,
- "Failed to get_app_dek. ec: " << tmp);
-
- auto stored_dek = Wae::Test::bytearr_to_vec(_stored_dek, _stored_dek_len);
- free(_stored_dek);
-
- BOOST_REQUIRE_MESSAGE(stored_dek == dek, "stored dek and dek isn't matched!");
-
- tmp = remove_app_dek(pkg_id, app_type);
- BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NONE, "Failed to remove_app_dek. ec: " << tmp);
-
- _stored_dek = nullptr;
- tmp = get_app_dek(pkg_id, app_type, &_stored_dek, &_stored_dek_len);
- if (_stored_dek)
- free(_stored_dek);
-
- BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NO_KEY,
- "dek removed but it's remained still. ec: " << tmp);
+ const crypto_element_s *ce = nullptr;
+ int tmp = create_app_ce(pkg_id, app_type, &ce);
+ BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NONE, "Failed to create_app_ce. ec: " << tmp);
+
+ const crypto_element_s *stored_ce = nullptr;
+ tmp = get_app_ce(pkg_id, app_type, true, &stored_ce);
+ BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NONE, "Failed to get_app_ce. ec: " << tmp);
+
+ BOOST_REQUIRE_MESSAGE(ce == stored_ce,
+ "ce(" << ce << ") and cached ce(" << stored_ce << ") pointer addr is different!");
+
+ tmp = remove_app_ce(pkg_id, app_type);
+ BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NONE, "Failed to remove_app_ce. ec: " << tmp);
+
+ if (app_type == WAE_DOWNLOADED_GLOBAL_APP) {
+ tmp = get_app_ce(pkg_id, app_type, true, &stored_ce);
+ BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NONE && stored_ce->is_migrated_app,
+ "when getting app ce which is there isn't, it should be migrated case! "
+ "ret("<< tmp << ") and is_migrated_app(" << stored_ce->is_migrated_app << ")");
+ } else {
+ tmp = get_app_ce(pkg_id, app_type, false, &stored_ce);
+ BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NO_KEY,
+ "removed app ce is still remaining. ret(" << tmp << ")");
+ }
}
-void create_app_dek(wae_app_type_e app_type)
+void create_app_ce(wae_app_type_e app_type)
{
const char *pkg_id = "TEST_PKG_ID";
- remove_app_dek(pkg_id, app_type);
-
- unsigned char *_dek = nullptr;
- size_t _dek_len = 0;
+ remove_app_ce(pkg_id, app_type);
- int tmp = create_app_dek(pkg_id, app_type, &_dek, &_dek_len);
- BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NONE,
- "Failed to create_app_dek. ec: " << tmp);
+ const crypto_element_s *ce = nullptr;
- auto dek = Wae::Test::bytearr_to_vec(_dek, _dek_len);
- free(_dek);
+ int tmp = create_app_ce(pkg_id, app_type, &ce);
+ BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NONE, "Failed to create_app_ce. ec: " << tmp);
- unsigned char *_stored_dek = nullptr;
- size_t _stored_dek_len = 0;
- tmp = get_app_dek(pkg_id, app_type, &_stored_dek, &_stored_dek_len);
- BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NONE, "Failed to get_app_dek. ec: " << tmp);
- auto stored_dek = bytearr_to_vec(_stored_dek, _stored_dek_len);
- free(_stored_dek);
+ const crypto_element_s *stored_ce = nullptr;
+ tmp = get_app_ce(pkg_id, app_type, false, &stored_ce);
+ BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NONE, "Failed to get_app_ce. ec: " << tmp);
- BOOST_REQUIRE_MESSAGE(stored_dek == dek,
- "stored dek and dek isn't matched! "
- "stored_dek(" << Wae::Test::bytes_to_hex(stored_dek) << ") "
- "dek(" << Wae::Test::bytes_to_hex(dek) << ")");
+ BOOST_REQUIRE_MESSAGE(ce == stored_ce,
+ "ce(" << ce << ") and cached ce(" << stored_ce << ") pointer addr is different!");
- remove_app_dek(pkg_id, app_type);
+ tmp = remove_app_ce(pkg_id, app_type);
+ BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NONE, "Failed to remove_app_ce. ec: " << tmp);
}
void encrypt_decrypt_web_app(wae_app_type_e app_type)
@@ -125,7 +111,7 @@ void encrypt_decrypt_web_app(wae_app_type_e app_type)
wae_remove_app_dek(pkg_id, app_type);
if (app_type == WAE_PRELOADED_APP)
- _clear_app_deks_loaded();
+ clear_app_deks_loaded_from_key_manager();
std::vector<unsigned char> plaintext = {
'a', 'b', 'c', 'a', 'b', 'c', 'x', 'y',
@@ -150,7 +136,7 @@ void encrypt_decrypt_web_app(wae_app_type_e app_type)
auto encrypted = bytearr_to_vec(_encrypted, _enc_len);
free(_encrypted);
- _remove_app_dek_from_cache(pkg_id);
+ _remove_app_ce_from_cache(pkg_id);
if (app_type == WAE_PRELOADED_APP)
load_preloaded_app_deks(true);
@@ -166,8 +152,8 @@ void encrypt_decrypt_web_app(wae_app_type_e app_type)
BOOST_REQUIRE_MESSAGE(plaintext == decrypted,
"plaintext and decrypted isn't matched! "
- "plaintext(" << Wae::Test::bytes_to_hex(plaintext) << ") "
- "decrypted(" << Wae::Test::bytes_to_hex(decrypted) << ")");
+ "plaintext(" << bytes_to_hex(plaintext) << ") "
+ "decrypted(" << bytes_to_hex(decrypted) << ")");
tmp = wae_remove_app_dek(pkg_id, app_type);
BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NONE,
diff --git a/tests/test-helper.h b/tests/test-helper.h
index 132ceae..c0c77f6 100644
--- a/tests/test-helper.h
+++ b/tests/test-helper.h
@@ -25,8 +25,8 @@
namespace Wae {
namespace Test {
-void add_get_remove_dek(wae_app_type_e app_type);
-void create_app_dek(wae_app_type_e app_type);
+void add_get_remove_ce(wae_app_type_e app_type);
+void create_app_ce(wae_app_type_e app_type);
void encrypt_decrypt_web_app(wae_app_type_e app_type);
} // namespace Test