2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
17 * @file test-helper.cpp
18 * @author Kyungwook Tak (k.tak@samsung.com)
21 #include "test-helper.h"
26 #include "key_handler.h"
27 #include "crypto_service.h"
29 #include "test-common.h"
34 void add_get_remove_dek(wae_app_type_e app_type)
36 const char *pkg_id = "TEST_PKG_ID";
38 std::vector<unsigned char> dek(32, 0);
40 BOOST_REQUIRE(_get_random(dek.size(), dek.data()) == WAE_ERROR_NONE);
42 remove_app_dek(pkg_id, app_type);
44 int tmp = _add_dek_to_key_manager(pkg_id, app_type, dek.data(), dek.size());
45 BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NONE,
46 "Failed to _add_dek_to_key_manager. ec: " << tmp);
48 unsigned char *_stored_dek = nullptr;
49 size_t _stored_dek_len = 0;
50 tmp = get_app_dek(pkg_id, app_type, &_stored_dek, &_stored_dek_len);
51 BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NONE,
52 "Failed to get_app_dek. ec: " << tmp);
54 auto stored_dek = Wae::Test::bytearr_to_vec(_stored_dek, _stored_dek_len);
57 BOOST_REQUIRE_MESSAGE(stored_dek == dek, "stored dek and dek isn't matched!");
59 tmp = remove_app_dek(pkg_id, app_type);
60 BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NONE, "Failed to remove_app_dek. ec: " << tmp);
62 _stored_dek = nullptr;
63 tmp = get_app_dek(pkg_id, app_type, &_stored_dek, &_stored_dek_len);
67 BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NO_KEY,
68 "dek removed but it's remained still. ec: " << tmp);
71 void create_app_dek(wae_app_type_e app_type)
73 const char *pkg_id = "TEST_PKG_ID";
75 remove_app_dek(pkg_id, app_type);
77 unsigned char *_dek = nullptr;
80 int tmp = create_app_dek(pkg_id, app_type, &_dek, &_dek_len);
81 BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NONE,
82 "Failed to create_app_dek. ec: " << tmp);
84 auto dek = Wae::Test::bytearr_to_vec(_dek, _dek_len);
87 unsigned char *_stored_dek = nullptr;
88 size_t _stored_dek_len = 0;
89 tmp = get_app_dek(pkg_id, app_type, &_stored_dek, &_stored_dek_len);
90 BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NONE, "Failed to get_app_dek. ec: " << tmp);
91 auto stored_dek = bytearr_to_vec(_stored_dek, _stored_dek_len);
94 BOOST_REQUIRE_MESSAGE(stored_dek == dek,
95 "stored dek and dek isn't matched! "
96 "stored_dek(" << Wae::Test::bytes_to_hex(stored_dek) << ") "
97 "dek(" << Wae::Test::bytes_to_hex(dek) << ")");
99 remove_app_dek(pkg_id, app_type);
102 void encrypt_decrypt_web_app(wae_app_type_e app_type)
104 const char *pkg_id1 = "testpkg_for_normal";
105 const char *pkg_id2 = "testpkg_for_global";
106 const char *pkg_id3 = "testpkg_for_preloaded";
108 const char *pkg_id = nullptr;
110 case WAE_DOWNLOADED_NORMAL_APP:
114 case WAE_DOWNLOADED_GLOBAL_APP:
118 case WAE_PRELOADED_APP:
124 // remove old test data
125 wae_remove_app_dek(pkg_id, app_type);
127 if (app_type == WAE_PRELOADED_APP)
128 _clear_app_deks_loaded();
130 std::vector<unsigned char> plaintext = {
131 'a', 'b', 'c', 'a', 'b', 'c', 'x', 'y',
132 'o', 'q', '2', 'e', 'v', '0', '1', 'x'
135 // test for downloaded web application
136 unsigned char *_encrypted = nullptr;
138 int tmp = wae_encrypt_web_application(pkg_id, app_type, plaintext.data(),
139 plaintext.size(), &_encrypted, &_enc_len);
140 BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NONE,
141 "Failed to wae_encrypt_web_application. ec: " << tmp);
144 // encrypt test twice
145 tmp = wae_encrypt_web_application(pkg_id, app_type, plaintext.data(),
146 plaintext.size(), &_encrypted, &_enc_len);
147 BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NONE,
148 "Failed to wae_encrypt_web_application second time. ec: " << tmp);
150 auto encrypted = bytearr_to_vec(_encrypted, _enc_len);
153 _remove_app_dek_from_cache(pkg_id);
155 if (app_type == WAE_PRELOADED_APP)
156 load_preloaded_app_deks(true);
158 unsigned char *_decrypted = nullptr;
160 tmp = wae_decrypt_web_application(pkg_id, app_type, encrypted.data(),
161 encrypted.size(), &_decrypted, &_dec_len);
162 BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NONE,
163 "Failed to wae_decrypt_web_application. ec: " << tmp);
165 auto decrypted = bytearr_to_vec(_decrypted, _dec_len);
167 BOOST_REQUIRE_MESSAGE(plaintext == decrypted,
168 "plaintext and decrypted isn't matched! "
169 "plaintext(" << Wae::Test::bytes_to_hex(plaintext) << ") "
170 "decrypted(" << Wae::Test::bytes_to_hex(decrypted) << ")");
172 tmp = wae_remove_app_dek(pkg_id, app_type);
173 BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NONE,
174 "Failed to wae_remove_app_dek. ec: " << tmp);