2 * Copyright (c) 2012 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.
18 * @file test_cases.cpp
19 * @author Dongsun Lee (ds73.lee@samsung.com)
21 * @brief libwebappenc test cases
32 #include <dpl/test/test_runner.h>
33 #include <dpl/test/test_runner_multiprocess.h>
34 #include <dpl/log/log.h>
35 #include "tests_common.h"
36 #include <web_app_enc.h>
38 #define TEST_PKGID_1 "testpkg_for_downloaded"
39 #define TEST_PKGID_2 "testpkg_for_preloaded"
40 #define TEST_PLAINTEXT "adbdfdfdfdfdererfdfdfererfdrerfdrer"
41 #define PRELOADED_WAPP_FILE1 "/usr/share/wae/test/PRELOADED_WAPP_FILE1.enc"
43 #define DOWNLOADED_ENC_FILE "/tmp/downloaded_enc_file"
44 #define PRELOADED_ENC_FILE "/tmp/preloaded_enc_file"
48 const uid_t DEFAULT_UID = 5001;
50 int _read_from_file(const char* path, unsigned char** data, size_t* len)
52 int ret = WAE_ERROR_NONE;
55 unsigned char* file_contents = NULL;
65 fseek(f, 0, SEEK_END); // move to the end of a file
67 fseek(f, 0, SEEK_SET); // move to the start of a file
69 file_contents = (unsigned char*) malloc(file_len);
70 if(file_contents == NULL) {
71 ret = WAE_ERROR_MEMORY;
74 memset(file_contents, 0x00, file_len);
76 while( (ch = fgetc(f)) != EOF) {
77 file_contents[i++]=(char)ch;
80 *data = file_contents;
86 if(ret != WAE_ERROR_NONE && file_contents != NULL)
92 int _write_to_file(const char* path, const unsigned char* data, size_t len)
94 int ret = WAE_ERROR_NONE;
101 ret = WAE_ERROR_FILE;
105 write_len = fwrite(data, 1, len, f);
106 if(write_len != (int) len) {
107 ret = WAE_ERROR_FILE;
117 } // namespace anonymous
120 RUNNER_TEST_GROUP_INIT(libwebappenc)
122 RUNNER_TEST(T01_init) {
123 wae_remove_app_dek(DEFAULT_UID, TEST_PKGID_1);
124 wae_remove_global_app_dek(TEST_PKGID_2, true);
127 RUNNER_CHILD_TEST(T02_downloaded_web_app_enc){
128 int ret = WAE_ERROR_NONE;
129 const char* pkgId = TEST_PKGID_1;
130 const char* plaintext = TEST_PLAINTEXT;
131 size_t plaintextLen = strlen(plaintext);
132 unsigned char* encrypted = NULL;
135 ret = wae_encrypt_web_application(DEFAULT_UID, pkgId,
136 (const unsigned char*)plaintext, plaintextLen,
137 &encrypted, &encLen);
138 RUNNER_ASSERT_MSG(ret == WAE_ERROR_NONE, "FAIL: wae_encrypt_web_application. ret=" << ret);
140 ret = _write_to_file(DOWNLOADED_ENC_FILE, encrypted, encLen);
141 RUNNER_ASSERT_MSG(ret == WAE_ERROR_NONE, "FAIL: _write_to_file. file=" << DOWNLOADED_ENC_FILE);
144 RUNNER_CHILD_TEST(T03_downloaded_web_app_dec){
145 int ret = WAE_ERROR_NONE;
146 const char* pkgId = TEST_PKGID_1;
147 const char* plaintext = TEST_PLAINTEXT;
148 size_t plaintextLen = strlen(plaintext);
149 unsigned char* encrypted = NULL;
151 unsigned char* decrypted = NULL;
154 ret = _read_from_file(DOWNLOADED_ENC_FILE, &encrypted, &encLen);
155 RUNNER_ASSERT_MSG(ret == WAE_ERROR_NONE, "FAIL: _read_from_file. ret=" << ret);
157 ret = wae_decrypt_web_application(DEFAULT_UID, pkgId, encrypted, encLen, &decrypted, &decLen);
158 RUNNER_ASSERT_MSG(ret == WAE_ERROR_NONE, "FAIL: wae_decrypt_web_application. ret=" << ret);
160 RUNNER_ASSERT_MSG(plaintextLen == decLen,
161 "FAIL: plaintext_len("<<plaintextLen<<") != decrypted_len(" <<decLen<<")");
162 RUNNER_ASSERT_MSG(strncmp(plaintext, (char *)decrypted, decLen) == 0,
163 "FAIL: plaintext("<<plaintext <<") != decrypted("<<(char *)decrypted <<")");
167 RUNNER_CHILD_TEST(T04_preloaded_web_app_enc){
168 int ret = WAE_ERROR_NONE;
169 const char* pkgId = TEST_PKGID_2;
170 const char* plaintext = TEST_PLAINTEXT;
171 size_t plaintextLen = strlen(plaintext);
172 unsigned char* encrypted = NULL;
175 ret = wae_encrypt_global_web_application(pkgId, true,
176 (const unsigned char*)plaintext, plaintextLen,
177 &encrypted, &encLen);
178 RUNNER_ASSERT_MSG(ret == WAE_ERROR_NONE, "FAIL: wae_encrypt_global_web_application. ret=" << ret);
180 ret = _write_to_file(PRELOADED_ENC_FILE, encrypted, encLen);
181 RUNNER_ASSERT_MSG(ret == WAE_ERROR_NONE, "FAIL: _write_to_file. file=" << DOWNLOADED_ENC_FILE);
184 RUNNER_TEST(T05_reload_app_deks) {
185 int ret = system("wae_initializer --reload");
186 RUNNER_ASSERT_MSG(ret != -1, "FAIL: load_preloaded_app_deks. ret=" << ret);
189 RUNNER_CHILD_TEST(T06_preloaded_web_app_dec){
190 int ret = WAE_ERROR_NONE;
191 const char* pkgId = TEST_PKGID_2;
192 const char* plaintext = TEST_PLAINTEXT;
193 size_t plaintextLen = strlen(plaintext);
194 unsigned char* encrypted = NULL;
196 unsigned char* decrypted = NULL;
199 ret = _read_from_file(PRELOADED_ENC_FILE, &encrypted, &encLen);
200 RUNNER_ASSERT_MSG(ret == WAE_ERROR_NONE, "FAIL: _read_from_file. ret=" << ret);
202 ret = wae_decrypt_global_web_application(pkgId, true, encrypted, encLen, &decrypted, &decLen);
203 RUNNER_ASSERT_MSG(ret == WAE_ERROR_NONE, "FAIL: wae_decrypt_global_web_application. ret=" << ret);
205 RUNNER_ASSERT_MSG(plaintextLen == decLen,
206 "FAIL: plaintext_len("<<plaintextLen<<") != decrypted_len(" <<decLen<<")");
207 RUNNER_ASSERT_MSG(strncmp(plaintext, (char *)decrypted, decLen) == 0,
208 "FAIL: plaintext("<<plaintext <<") != decrypted("<<(char *)decrypted <<")");
212 RUNNER_CHILD_TEST(T07_remove_app_dek) {
213 int ret = WAE_ERROR_NONE;
215 ret = wae_remove_app_dek(DEFAULT_UID, TEST_PKGID_1);
216 RUNNER_ASSERT_MSG(ret == WAE_ERROR_NONE, "FAIL: wae_remove_app_dek. ret=" << ret);
218 ret = wae_remove_global_app_dek(TEST_PKGID_2, true);
219 RUNNER_ASSERT_MSG(ret == WAE_ERROR_NONE, "FAIL: wae_remove_global_app_dek. ret=" << ret);