Fix C code naming rules & minor fixes
[platform/core/security/libwebappenc.git] / tests / test-helper.cpp
1 /*
2  *  Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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
15  *
16  *
17  * @file        test-helper.cpp
18  * @author      Kyungwook Tak (k.tak@samsung.com)
19  * @version     1.0
20  */
21 #include "test-helper.h"
22
23 #include <cstring>
24 #include <vector>
25
26 #include "key_handler.h"
27 #include "crypto_service.h"
28
29 #include "test-common.h"
30
31 namespace Wae {
32 namespace Test {
33
34 void add_get_remove_dek(wae_app_type_e app_type)
35 {
36         const char *pkg_id = "TEST_PKG_ID";
37
38         std::vector<unsigned char> dek(32, 0);
39
40         BOOST_REQUIRE(_get_random(dek.size(), dek.data()) == WAE_ERROR_NONE);
41
42         remove_app_dek(pkg_id, app_type);
43
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);
47
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);
53
54         auto stored_dek = Wae::Test::bytearr_to_vec(_stored_dek, _stored_dek_len);
55         free(_stored_dek);
56
57         BOOST_REQUIRE_MESSAGE(stored_dek == dek, "stored dek and dek isn't matched!");
58
59         tmp = remove_app_dek(pkg_id, app_type);
60         BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NONE, "Failed to remove_app_dek. ec: " << tmp);
61
62         _stored_dek = nullptr;
63         tmp = get_app_dek(pkg_id, app_type, &_stored_dek, &_stored_dek_len);
64         if (_stored_dek)
65                 free(_stored_dek);
66
67         BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NO_KEY,
68                         "dek removed but it's remained still. ec: " << tmp);
69 }
70
71 void create_app_dek(wae_app_type_e app_type)
72 {
73         const char *pkg_id = "TEST_PKG_ID";
74
75         remove_app_dek(pkg_id, app_type);
76
77         unsigned char *_dek = nullptr;
78         size_t _dek_len = 0;
79
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);
83
84         auto dek = Wae::Test::bytearr_to_vec(_dek, _dek_len);
85         free(_dek);
86
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);
92         free(_stored_dek);
93
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) << ")");
98
99         remove_app_dek(pkg_id, app_type);
100 }
101
102 void encrypt_decrypt_web_app(wae_app_type_e app_type)
103 {
104         const char *pkg_id1 = "testpkg_for_normal";
105         const char *pkg_id2 = "testpkg_for_global";
106         const char *pkg_id3 = "testpkg_for_preloaded";
107
108         const char *pkg_id = nullptr;
109         switch (app_type) {
110         case WAE_DOWNLOADED_NORMAL_APP:
111                 pkg_id = pkg_id1;
112                 break;
113
114         case WAE_DOWNLOADED_GLOBAL_APP:
115                 pkg_id = pkg_id2;
116                 break;
117
118         case WAE_PRELOADED_APP:
119         default:
120                 pkg_id = pkg_id3;
121                 break;
122         }
123
124         // remove old test data
125         wae_remove_app_dek(pkg_id, app_type);
126
127         if (app_type == WAE_PRELOADED_APP)
128                 _clear_app_deks_loaded();
129
130         std::vector<unsigned char> plaintext = {
131                 'a', 'b', 'c', 'a', 'b', 'c', 'x', 'y',
132                 'o', 'q', '2', 'e', 'v', '0', '1', 'x'
133         };
134
135         // test for downloaded web application
136         unsigned char *_encrypted = nullptr;
137         size_t _enc_len = 0;
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);
142         free(_encrypted);
143
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);
149
150         auto encrypted = bytearr_to_vec(_encrypted, _enc_len);
151         free(_encrypted);
152
153         _remove_app_dek_from_cache(pkg_id);
154
155         if (app_type == WAE_PRELOADED_APP)
156                 load_preloaded_app_deks(true);
157
158         unsigned char *_decrypted = nullptr;
159         size_t _dec_len = 0;
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);
164
165         auto decrypted = bytearr_to_vec(_decrypted, _dec_len);
166
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) << ")");
171
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);
175 }
176
177 } // namespace Test
178 } // namespace Wae