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 "key_manager.h"
31 #include "test-common.h"
36 void add_get_remove_ce(wae_app_type_e app_type)
38 const char *pkg_id = "TEST_PKG_ID";
40 const crypto_element_s *ce = nullptr;
41 int tmp = create_app_ce(pkg_id, app_type, &ce);
42 BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NONE, "Failed to create_app_ce. ec: " << tmp);
44 const crypto_element_s *stored_ce = nullptr;
45 tmp = get_app_ce(pkg_id, app_type, true, &stored_ce);
46 BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NONE, "Failed to get_app_ce. ec: " << tmp);
48 BOOST_REQUIRE_MESSAGE(ce == stored_ce,
49 "ce(" << ce << ") and cached ce(" << stored_ce << ") pointer addr is different!");
51 tmp = remove_app_ce(pkg_id, app_type);
52 BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NONE, "Failed to remove_app_ce. ec: " << tmp);
54 if (app_type == WAE_DOWNLOADED_GLOBAL_APP) {
55 tmp = get_app_ce(pkg_id, app_type, true, &stored_ce);
56 BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NONE && stored_ce->is_migrated_app,
57 "when getting app ce which is there isn't, it should be migrated case! "
58 "ret("<< tmp << ") and is_migrated_app(" << stored_ce->is_migrated_app << ")");
60 tmp = get_app_ce(pkg_id, app_type, false, &stored_ce);
61 BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NO_KEY,
62 "removed app ce is still remaining. ret(" << tmp << ")");
66 void create_app_ce(wae_app_type_e app_type)
68 const char *pkg_id = "TEST_PKG_ID";
70 remove_app_ce(pkg_id, app_type);
72 const crypto_element_s *ce = nullptr;
74 int tmp = create_app_ce(pkg_id, app_type, &ce);
75 BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NONE, "Failed to create_app_ce. ec: " << tmp);
77 const crypto_element_s *stored_ce = nullptr;
78 tmp = get_app_ce(pkg_id, app_type, false, &stored_ce);
79 BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NONE, "Failed to get_app_ce. ec: " << tmp);
81 BOOST_REQUIRE_MESSAGE(ce == stored_ce,
82 "ce(" << ce << ") and cached ce(" << stored_ce << ") pointer addr is different!");
84 tmp = remove_app_ce(pkg_id, app_type);
85 BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NONE, "Failed to remove_app_ce. ec: " << tmp);
88 void encrypt_decrypt_web_app(wae_app_type_e app_type)
90 const char *pkg_id1 = "testpkg_for_normal";
91 const char *pkg_id2 = "testpkg_for_global";
92 const char *pkg_id3 = "testpkg_for_preloaded";
94 const char *pkg_id = nullptr;
96 case WAE_DOWNLOADED_NORMAL_APP:
100 case WAE_DOWNLOADED_GLOBAL_APP:
104 case WAE_PRELOADED_APP:
110 // remove old test data
111 wae_remove_app_dek(pkg_id, app_type);
113 if (app_type == WAE_PRELOADED_APP)
114 clear_app_deks_loaded_from_key_manager();
116 std::vector<unsigned char> plaintext = {
117 'a', 'b', 'c', 'a', 'b', 'c', 'x', 'y',
118 'o', 'q', '2', 'e', 'v', '0', '1', 'x'
121 // test for downloaded web application
122 unsigned char *_encrypted = nullptr;
124 int tmp = wae_encrypt_web_application(pkg_id, app_type, plaintext.data(),
125 plaintext.size(), &_encrypted, &_enc_len);
126 BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NONE,
127 "Failed to wae_encrypt_web_application. ec: " << tmp);
130 // encrypt test twice
131 tmp = wae_encrypt_web_application(pkg_id, app_type, plaintext.data(),
132 plaintext.size(), &_encrypted, &_enc_len);
133 BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NONE,
134 "Failed to wae_encrypt_web_application second time. ec: " << tmp);
136 auto encrypted = bytearr_to_vec(_encrypted, _enc_len);
139 _remove_app_ce_from_cache(pkg_id);
141 if (app_type == WAE_PRELOADED_APP)
142 load_preloaded_app_deks(true);
144 unsigned char *_decrypted = nullptr;
146 tmp = wae_decrypt_web_application(pkg_id, app_type, encrypted.data(),
147 encrypted.size(), &_decrypted, &_dec_len);
148 BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NONE,
149 "Failed to wae_decrypt_web_application. ec: " << tmp);
151 auto decrypted = bytearr_to_vec(_decrypted, _dec_len);
153 BOOST_REQUIRE_MESSAGE(plaintext == decrypted,
154 "plaintext and decrypted isn't matched! "
155 "plaintext(" << bytes_to_hex(plaintext) << ") "
156 "decrypted(" << bytes_to_hex(decrypted) << ")");
158 tmp = wae_remove_app_dek(pkg_id, app_type);
159 BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NONE,
160 "Failed to wae_remove_app_dek. ec: " << tmp);