Add data structures
[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 #include "types.h"
29 #include "key_manager.h"
30
31 #include "test-common.h"
32
33 namespace Wae {
34 namespace Test {
35
36 void add_get_remove_ce(wae_app_type_e app_type)
37 {
38         const char *pkg_id = "TEST_PKG_ID";
39
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);
43
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);
47
48         BOOST_REQUIRE_MESSAGE(ce == stored_ce,
49                 "ce(" << ce << ") and cached ce(" << stored_ce << ") pointer addr is different!");
50
51         tmp = remove_app_ce(pkg_id, app_type);
52         BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NONE, "Failed to remove_app_ce. ec: " << tmp);
53
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 << ")");
59         } else {
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 << ")");
63         }
64 }
65
66 void create_app_ce(wae_app_type_e app_type)
67 {
68         const char *pkg_id = "TEST_PKG_ID";
69
70         remove_app_ce(pkg_id, app_type);
71
72         const crypto_element_s *ce = nullptr;
73
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);
76
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);
80
81         BOOST_REQUIRE_MESSAGE(ce == stored_ce,
82                 "ce(" << ce << ") and cached ce(" << stored_ce << ") pointer addr is different!");
83
84         tmp = remove_app_ce(pkg_id, app_type);
85         BOOST_REQUIRE_MESSAGE(tmp == WAE_ERROR_NONE, "Failed to remove_app_ce. ec: " << tmp);
86 }
87
88 void encrypt_decrypt_web_app(wae_app_type_e app_type)
89 {
90         const char *pkg_id1 = "testpkg_for_normal";
91         const char *pkg_id2 = "testpkg_for_global";
92         const char *pkg_id3 = "testpkg_for_preloaded";
93
94         const char *pkg_id = nullptr;
95         switch (app_type) {
96         case WAE_DOWNLOADED_NORMAL_APP:
97                 pkg_id = pkg_id1;
98                 break;
99
100         case WAE_DOWNLOADED_GLOBAL_APP:
101                 pkg_id = pkg_id2;
102                 break;
103
104         case WAE_PRELOADED_APP:
105         default:
106                 pkg_id = pkg_id3;
107                 break;
108         }
109
110         // remove old test data
111         wae_remove_app_dek(pkg_id, app_type);
112
113         if (app_type == WAE_PRELOADED_APP)
114                 clear_app_deks_loaded_from_key_manager();
115
116         std::vector<unsigned char> plaintext = {
117                 'a', 'b', 'c', 'a', 'b', 'c', 'x', 'y',
118                 'o', 'q', '2', 'e', 'v', '0', '1', 'x'
119         };
120
121         // test for downloaded web application
122         unsigned char *_encrypted = nullptr;
123         size_t _enc_len = 0;
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);
128         free(_encrypted);
129
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);
135
136         auto encrypted = bytearr_to_vec(_encrypted, _enc_len);
137         free(_encrypted);
138
139         _remove_app_ce_from_cache(pkg_id);
140
141         if (app_type == WAE_PRELOADED_APP)
142                 load_preloaded_app_deks(true);
143
144         unsigned char *_decrypted = nullptr;
145         size_t _dec_len = 0;
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);
150
151         auto decrypted = bytearr_to_vec(_decrypted, _dec_len);
152
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) << ")");
157
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);
161 }
162
163 } // namespace Test
164 } // namespace Wae