Merge branch 'tizen' into 'ckm'
[platform/core/test/security-tests.git] / src / libwebappenc-tests / test_cases.cpp
1 /*
2  * Copyright (c) 2012 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 /*
18  * @file        test_cases.cpp
19  * @author      Dongsun Lee (ds73.lee@samsung.com)
20  * @version     1.0
21  * @brief       libwebappenc test cases 
22  */
23
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <string>
29 #include <sstream>
30 #include <fcntl.h>
31 #include <unistd.h>
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>
37
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"
42
43 #define DOWNLOADED_ENC_FILE "/tmp/downloaded_enc_file"
44 #define PRELOADED_ENC_FILE "/tmp/preloaded_enc_file"
45
46 namespace {
47
48 const uid_t DEFAULT_UID = 5001;
49
50 int _read_from_file(const char* path, unsigned char** data, size_t* len)
51 {
52     int ret = WAE_ERROR_NONE;
53     FILE* f = NULL;
54     int file_len = -1;
55     unsigned char* file_contents = NULL;
56     int ch = 0;
57     int i = 0;
58
59     f = fopen(path, "r");
60     if( f == NULL) {
61         ret = WAE_ERROR_FILE;
62         goto error;
63     }
64
65     fseek(f, 0, SEEK_END); // move to the end of a file
66     file_len = ftell(f);
67     fseek(f, 0, SEEK_SET); // move to the start of a file
68
69     file_contents = (unsigned char*) malloc(file_len);
70     if(file_contents == NULL) {
71          ret = WAE_ERROR_MEMORY;
72          goto error;
73     }
74     memset(file_contents, 0x00, file_len);
75
76     while( (ch = fgetc(f)) != EOF) {
77         file_contents[i++]=(char)ch;
78     }
79
80     *data = file_contents;
81     *len = file_len;
82
83 error:
84     if(f != NULL)
85         fclose(f);
86     if(ret != WAE_ERROR_NONE && file_contents != NULL)
87         free(file_contents);
88
89     return ret;
90 }
91
92 int _write_to_file(const char* path, const unsigned char* data, size_t len)
93 {
94     int ret = WAE_ERROR_NONE;
95
96     FILE* f = NULL;
97     int write_len = -1;
98
99     f = fopen(path, "w");
100     if( f == NULL) {
101         ret = WAE_ERROR_FILE;
102         goto error;
103     }
104
105     write_len = fwrite(data, 1, len, f);
106     if(write_len != (int) len) {
107         ret = WAE_ERROR_FILE;
108         goto error;
109     }
110 error:
111     if(f != NULL)
112         fclose(f);
113
114     return ret;
115 }
116
117 } // namespace anonymous
118
119
120 RUNNER_TEST_GROUP_INIT(libwebappenc)
121
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);
125 }
126
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;
133     size_t encLen = 0;
134
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);
139
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);
142 }
143
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;
150     size_t encLen = 0;
151     unsigned char* decrypted = NULL;
152     size_t decLen = 0;
153
154     ret = _read_from_file(DOWNLOADED_ENC_FILE, &encrypted, &encLen);
155     RUNNER_ASSERT_MSG(ret == WAE_ERROR_NONE, "FAIL: _read_from_file. ret=" << ret);
156
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);
159
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 <<")");
164 }
165
166
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;
173     size_t encLen = 0;
174
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);
179
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);
182 }
183
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);
187 }
188
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;
195     size_t encLen = 0;
196     unsigned char* decrypted = NULL;
197     size_t decLen = 0;
198
199     ret = _read_from_file(PRELOADED_ENC_FILE, &encrypted, &encLen);
200     RUNNER_ASSERT_MSG(ret == WAE_ERROR_NONE, "FAIL: _read_from_file. ret=" << ret);
201
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);
204
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 <<")");
209 }
210
211
212 RUNNER_CHILD_TEST(T07_remove_app_dek) {
213     int ret = WAE_ERROR_NONE;
214
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);
217
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);
220 }