Add test manifest for exec labeling
[platform/core/security/libwebappenc.git] / tests / wae_tests.c
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        wae_tests.c
18  * @author      Dongsun Lee (ds73.lee@samsung.com)
19  * @version     1.0
20  * @brief       internal test cases for libwebappenc.
21  */
22
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <unistd.h>
27
28 #include "web_app_enc.h"
29 #include "key_handler.h"
30 #include "crypto_service.h"
31
32 #include <tzplatform_config.h>
33
34 static int tc_seq = 0;
35 static int tc_succ = 0;
36 static int tc_fail = 0;
37
38 #define FPRINTF(format, args...) fprintf(stdout, format, ##args)
39
40 static int RUNTC(int (*tc_method)(), const char *tc_name)
41 {
42         int ret = WAE_ERROR_NONE;
43         FPRINTF("[%02d:%s]started...\n", tc_seq, tc_name);
44         ret = tc_method();
45
46         if (ret == WAE_ERROR_NONE) {
47                 FPRINTF("[%02d:%s]ended. SUCCESS\n\n", tc_seq, tc_name);
48                 tc_succ++;
49         } else {
50                 FPRINTF("[%02d:%s]ended. FAIL. error=%d\n\n", tc_seq, tc_name, ret);
51                 tc_fail++;
52         }
53
54         tc_seq++;
55         return ret;
56 }
57
58 static void PRINT_TC_SUMMARY()
59 {
60         FPRINTF("\n");
61         FPRINTF("===============================================\n");
62         FPRINTF(" TOTAL = %d, SUCCESS = %d, FAIL = %d\n", tc_seq, tc_succ, tc_fail);
63         FPRINTF("===============================================\n");
64 }
65
66 void _print_binary_to_hex(const char *msg, unsigned char *bin, size_t len)
67 {
68         size_t i = 0;
69         FPRINTF("%s", msg);
70
71         for (i = 0; i < len; i++) {
72                 FPRINTF("%02x", bin[i]);
73         }
74
75         FPRINTF("\n");
76 }
77
78 int _compare_binary(const unsigned char *b1, size_t b1Len, const unsigned char *b2, size_t b2Len)
79 {
80         size_t i = 0;
81
82         if (b1Len != b2Len)
83                 return b1Len - b2Len;
84
85         for (i = 0; i < b1Len; i++) {
86                 if (b1[i] != b2[i])
87                         return b1[i] - b2[i];
88         }
89
90         return 0;
91 }
92
93 //=================================================================================
94 // tests for crypto_service.h
95 //=================================================================================
96 int wae_tc_encrypt_decrypt_app_dek()
97 {
98         int ret = WAE_ERROR_NONE;
99         unsigned char dek[32];
100         unsigned char *encryptedDek = NULL;
101         size_t encryptedDekLen = 0;
102         unsigned char *decryptedDek = NULL;
103         size_t decryptedDekLen = 0;
104
105         const char *priKey =
106                 "-----BEGIN RSA PRIVATE KEY-----\n"
107                 "MIIEpgIBAAKCAQEA0kWtjpRO7Zh2KX2naVE/BDJdrfwK9xexfNA0MkY2VJ4J2AKM\n"
108                 "YTj1D1jntceryupCEHOvP3rum+WsFvPXduz9+VKnSsSqj4jcTUubtpDUGA5G79Iq\n"
109                 "LEPFuSBaqI8Uwkzd08pE+s30oaJDnNazMhSq8JkqBPoCCwtUs73ruE9VbtsBO/kT\n"
110                 "lASIAfe8nXqcJLcDQgWYhizjJw0Pi6d74oCwS2OTvQDNvsXfFnA0ZJEEYw/rZLir\n"
111                 "j7OHoOjz+Sh5N+1uA3Up6SPPEbHuP6L12YxqHdy7gnJXodLhvE/cR4SN9VW7+qmC\n"
112                 "MBjmLkBejGrEX3STS9sLI7MZHu9Y26dwuYb4+wIDAQABAoIBAQCwxqV/vc2RUGDe\n"
113                 "xuXM0+IvrAw37jJlw4SS0xNexMp+XxMViCbuwYy851h96azS/himbiuCKd6aL/96\n"
114                 "mGunbtyiFEvSvv5Jh5z2Wr9BQAcfZjla+4w7BIsg9UNifE/OfgLsQBu34xhsHtfK\n"
115                 "7nFehCOl/I5n+qtnD5KZPe0DWacQdwY4vEAj6YyXdb2bBg+MiwE9KVxGEIUDbklh\n"
116                 "Is70JXczjLZCS+lIpOKh0/lbZmBZePoUbVTtS+GvtPTpQC/aTHRkwGoEtuPEWpbL\n"
117                 "0Q1d6zO+vDJVLJlb5FF2haghs8IlqAxkkPjeUTNye+WktRrDQxmPu/blbxQrygfq\n"
118                 "Au5tBnsxAoGBAOiVtcpg32puo3Yq2Y78oboe9PuHaQP0d3DhwP3/7J0BeNslpjW7\n"
119                 "E1LWsVsCanxTE8XPUdFfAWgMk7lQqESN0wawGmSmWk+eQPZdjHanBaC8vh7aKjo6\n"
120                 "q9FdT1DKjrRi23QyDco3f3E7hvM93IAAhw1ikNu8DT19JAxtdeMh5WAZAoGBAOdw\n"
121                 "6neEvIFXh3RWEv2/GKVhVR8mxDqxmuFdXpOF+YWsK0Tg4uC8jm9kUGnwXgT2Mjke\n"
122                 "oAwYAFcRbHQQGsxy/vkV16kv4aurTE2hMpjeXCAakwV0Pi2w1f9WnDokjgORkOmc\n"
123                 "+QK9I8egdFPMVDfQjhLslhSUY0Eb4qcJ6q9WxfQzAoGBANSsAFybk+7oWAO3TtQW\n"
124                 "YXOk1vIgcYAyS/0mEKixGZS/QdlxZbf/5b17nxTO8rvX416fIftG2ixgQ7vR6us0\n"
125                 "m9+jq56ZFj9zP4eHJudf9h9yNo5TgwVXnMCGh/4iGbcMJgrrsfxUHu5VNiK5UCSj\n"
126                 "VtqAZGDoZVryUMIkXQVhezIRAoGBAN7QUIqcGbcUA24257Wu4hVlrUN+WPCAyDEr\n"
127                 "aL/x/ZV5eXaoYwQlw6LuGpTDOmDgfN2M5FyARuOL/LOIRaSLGXnIU4WoeUSCd8VM\n"
128                 "6Z9Og7bMnrpjfPEUDBH02hcH1kkNPUwLOZgva2Dm0tdSIcpSWFVTu/E4Io4uQHi8\n"
129                 "DVqc2ZsNAoGBAJT76ezXNSSv8hnrKqTpwgTicpqhRZ3eFQjyl4HRL26AJMKv++x8\n"
130                 "4/IsVIwxaHzpbN3nnCjmAHV4gX9YpxVnvYcZflC9WZeDkwNMLmPYb3Zg27EzSMfQ\n"
131                 "8yrfWJZo3qobipcHf1yohAt4fHk9kUKtPHEwp0xKe//rfhswLb3VCzvQ\n"
132                 "-----END RSA PRIVATE KEY-----";
133         const char *pubKey =
134                 "-----BEGIN PUBLIC KEY-----\n"
135                 "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA0kWtjpRO7Zh2KX2naVE/\n"
136                 "BDJdrfwK9xexfNA0MkY2VJ4J2AKMYTj1D1jntceryupCEHOvP3rum+WsFvPXduz9\n"
137                 "+VKnSsSqj4jcTUubtpDUGA5G79IqLEPFuSBaqI8Uwkzd08pE+s30oaJDnNazMhSq\n"
138                 "8JkqBPoCCwtUs73ruE9VbtsBO/kTlASIAfe8nXqcJLcDQgWYhizjJw0Pi6d74oCw\n"
139                 "S2OTvQDNvsXfFnA0ZJEEYw/rZLirj7OHoOjz+Sh5N+1uA3Up6SPPEbHuP6L12Yxq\n"
140                 "Hdy7gnJXodLhvE/cR4SN9VW7+qmCMBjmLkBejGrEX3STS9sLI7MZHu9Y26dwuYb4\n"
141                 "+wIDAQAB\n"
142                 "-----END PUBLIC KEY-----";
143
144         ret = encrypt_app_dek((const unsigned char *) pubKey, strlen(pubKey),
145                                                   dek, sizeof(dek), &encryptedDek, &encryptedDekLen);
146
147         if (ret != WAE_ERROR_NONE) {
148                 FPRINTF("...FAIL: encrypt_app_dek. ret=%d\n", ret);
149                 ret = WAE_ERROR_CRYPTO;
150                 goto error;
151         }
152
153         ret = decrypt_app_dek((const unsigned char *) priKey, strlen(priKey), NULL,
154                                                   encryptedDek, encryptedDekLen, &decryptedDek, &decryptedDekLen);
155
156         if (ret != WAE_ERROR_NONE) {
157                 FPRINTF("...FAIL: decrypt_app_dek. ret=%d\n", ret);
158                 ret = WAE_ERROR_CRYPTO;
159                 goto error;
160         }
161
162         _print_binary_to_hex("...ORIG DEK= ", dek, sizeof(dek));
163         _print_binary_to_hex("...ENC  DEK= ", encryptedDek, encryptedDekLen);
164         _print_binary_to_hex("...DEC  DEK= ", decryptedDek, decryptedDekLen);
165
166         if (_compare_binary(dek, sizeof(dek), decryptedDek, decryptedDekLen) != 0) {
167                 FPRINTF("...FAIL: ORIG DEK != decrypted DEK\n");
168                 ret = WAE_ERROR_CRYPTO;
169                 goto error;
170         }
171
172         ret = WAE_ERROR_NONE;
173
174 error:
175         if (encryptedDek != NULL)
176                 free(encryptedDek);
177
178         if (decryptedDek != NULL)
179                 free(decryptedDek);
180
181         return ret;
182 }
183
184 int wae_tc_encrypt_decrypt_aes_cbc()
185 {
186         int ret = WAE_ERROR_NONE;
187
188         unsigned char dek[32] = {0, };
189         size_t keyLen = 32;
190         const char *plaintext = "adbdfdfdfdfdererfdfdfererfdrerfdrer";
191         size_t plaintextLen = strlen(plaintext);
192         unsigned char *encrypted = NULL;
193         size_t encLen = 0;
194         unsigned char *decrypted = NULL;
195         size_t decLen = 0;
196         char decrypted_str[1024] = {0, };
197
198         ret = _get_random(keyLen, dek);
199
200         ret = encrypt_aes_cbc(dek, keyLen, (const unsigned char *)plaintext, plaintextLen, &encrypted, &encLen);
201
202         if (ret != WAE_ERROR_NONE) {
203                 FPRINTF("...FAIL: encrypt_aes_cbc. ret=%d\n", ret);
204                 ret = WAE_ERROR_CRYPTO;
205                 goto error;
206         }
207
208         ret = decrypt_aes_cbc(dek, keyLen, encrypted, encLen, &decrypted, &decLen);
209
210         if (ret != WAE_ERROR_NONE) {
211                 FPRINTF("...FAIL: decrypt_aes_cbc. ret=%d\n", ret);
212                 ret = WAE_ERROR_CRYPTO;
213                 goto error;
214         }
215
216         if (plaintextLen != decLen) {
217                 FPRINTF("...FAIL: plaintextLen(%d) != decLen(%d)\n", (int) plaintextLen, (int) decLen);
218                 ret = WAE_ERROR_CRYPTO;
219                 goto error;
220         }
221
222         memcpy(decrypted_str, decrypted, decLen);
223         FPRINTF("...plaintext = %s\n", plaintext);
224         FPRINTF("...decrypted = %s\n", decrypted_str);
225
226         if (strcmp(plaintext, decrypted_str) != 0) {
227                 FPRINTF("...FAIL: plaintext(%s) != decrypted(%s)\n", plaintext, decrypted_str);
228                 ret = WAE_ERROR_CRYPTO;
229                 goto error;
230         }
231
232 error:
233         if (encrypted != NULL)
234                 free(encrypted);
235
236         if (decrypted != NULL)
237                 free(decrypted);
238
239         return ret;
240 }
241
242 //=================================================================================
243 // tests for key_handler.h
244 //=================================================================================
245 int wae_tc_cache()
246 {
247         int ret = WAE_ERROR_NONE;
248
249         const char *pkg1 = "pkg1";
250         const char *pkg2 = "pkg2";
251         const char *pkg3 = "pkg3";
252         const char *pkgDummy = "dummy";
253
254         unsigned char dek1[32] = {1, };
255         unsigned char dek2[32] = {2, };
256         unsigned char dek3[32] = {3, };
257         unsigned char *retDek = NULL;
258
259         _initialize_cache();
260
261         _add_app_dek_to_cache(pkg1, dek1);
262         _add_app_dek_to_cache(pkg2, dek2);
263         _add_app_dek_to_cache(pkg3, dek3);
264
265         retDek = NULL;
266         retDek = _get_app_dek_from_cache(pkg1);
267
268         if (retDek == NULL || _compare_binary(dek1, 32, retDek, 32) != 0) {
269                 FPRINTF("failed in cache. Diffent DEK1\n");
270                 ret = WAE_ERROR_UNKNOWN;
271                 goto error;
272         }
273
274         _print_binary_to_hex("...DEK1         : ", dek1, 32);
275         _print_binary_to_hex("...Returen DEK1 : ", retDek, 32);
276
277         retDek = NULL;
278         retDek = _get_app_dek_from_cache(pkg2);
279
280         if (retDek == NULL || _compare_binary(dek2, 32, retDek, 32) != 0) {
281                 FPRINTF("failed in cache. Diffent DEK2\n");
282                 ret = WAE_ERROR_UNKNOWN;
283                 goto error;
284         }
285
286         _print_binary_to_hex("...DEK2         : ", dek2, 32);
287         _print_binary_to_hex("...Returen DEK1 : ", retDek, 32);
288
289         retDek = NULL;
290         retDek = _get_app_dek_from_cache(pkg3);
291
292         if (retDek == NULL || _compare_binary(dek3, 32, retDek, 32) != 0) {
293                 FPRINTF("failed in cache. Diffent DEK3\n");
294                 ret = WAE_ERROR_UNKNOWN;
295                 goto error;
296         }
297
298         _print_binary_to_hex("...DEK3         : ", dek3, 32);
299         _print_binary_to_hex("...Returen DEK3 : ", retDek, 32);
300
301         retDek = NULL;
302         retDek = _get_app_dek_from_cache(pkgDummy);
303
304         if (retDek != NULL) {
305                 FPRINTF("failed in cache. Wrong DEK_DUMMY1 returned\n");
306                 _print_binary_to_hex("retured wrong DEK : ", retDek, 32);
307                 ret = WAE_ERROR_UNKNOWN;
308                 goto error;
309         }
310
311         _remove_app_dek_from_cache(pkg3);
312         retDek = NULL;
313         retDek = _get_app_dek_from_cache(pkg3);
314
315         if (retDek != NULL) {
316                 FPRINTF("fail to remove app dek from cache\n");
317                 ret = WAE_ERROR_UNKNOWN;
318                 goto error;
319         }
320
321         _initialize_cache();
322
323         _add_app_dek_to_cache(pkg1, dek1);
324
325         retDek = NULL;
326         retDek = _get_app_dek_from_cache(pkg2);
327
328         if (retDek != NULL) {
329                 FPRINTF("failed in cache. Wrong DEK_DUMMY2 returned\n");
330                 _print_binary_to_hex("retured wrong DEK : ", retDek, 32);
331                 ret = WAE_ERROR_UNKNOWN;
332                 goto error;
333         }
334
335         ret = WAE_ERROR_NONE;
336
337 error:
338         return ret;
339 }
340
341 int wae_tc_get_random()
342 {
343         int ret = WAE_ERROR_NONE;
344
345         size_t rand_len = 32;
346         unsigned char random[32] = {0, };
347
348         ret = _get_random(rand_len, random);
349
350         _print_binary_to_hex("...RANDOM = ", random, sizeof(random));
351
352         return ret;
353 }
354
355 int wae_tc_get_alias()
356 {
357         int ret = WAE_ERROR_NONE;
358
359         const char *pkgId = "TEST_PKG_ID";
360         char alias[256] = {0, };
361
362         _get_alias(pkgId, WAE_DOWNLOADED_NORMAL_APP, true, alias, sizeof(alias));
363         FPRINTF("...pkgid=%s, alias for normal for save. app=%s\n", pkgId, alias);
364
365         _get_alias(pkgId, WAE_DOWNLOADED_NORMAL_APP, false, alias, sizeof(alias));
366         FPRINTF("...pkgid=%s, alias for normal for get. app=%s\n", pkgId, alias);
367
368         _get_alias(pkgId, WAE_DOWNLOADED_GLOBAL_APP, true, alias, sizeof(alias));
369         FPRINTF("...pkgid=%s, alias for global app=%s\n", pkgId, alias);
370
371         _get_alias(pkgId, WAE_PRELOADED_APP, true, alias, sizeof(alias));
372         FPRINTF("...pkgid=%s, alias for preloaded app=%s\n", pkgId, alias);
373
374         return ret;
375 }
376
377 int _wae_tc_add_get_remove_dek(wae_app_type_e appType)
378 {
379         int ret = WAE_ERROR_NONE;
380
381         const char *pkgId = "TEST_PKG_ID";
382
383         size_t dekLen = 32;
384         unsigned char dek[32] = {0, };
385         size_t storedDekLen = 0;
386         unsigned char *storedDek = NULL;
387
388         ret = _get_random(dekLen, dek);
389
390         remove_app_dek(pkgId, appType);
391
392         ret = _add_dek_to_key_manager(pkgId, appType, dek, dekLen);
393
394         if (ret != WAE_ERROR_NONE) {
395                 FPRINTF("...FAIL: _add_dek_to_key_manager. ret=%d\n", ret);
396                 goto error;
397         }
398
399         ret = get_app_dek(pkgId, appType, &storedDek, &storedDekLen);
400
401         if (ret != WAE_ERROR_NONE) {
402                 FPRINTF("...FAIL: get_app_dek. ret=%d\n", ret);
403                 goto error;
404         }
405
406         if (_compare_binary(dek, dekLen, storedDek, storedDekLen) != 0) {
407                 ret = WAE_ERROR_KEY_MANAGER;
408                 FPRINTF("...FAIL: DEK != STORED_DEK.\n");
409                 goto error;
410         }
411
412         ret = remove_app_dek(pkgId, appType);
413
414         if (ret != WAE_ERROR_NONE) {
415                 FPRINTF("...FAIL: remove_app_dek. ret=%d\n", ret);
416                 goto error;
417         }
418
419         ret = get_app_dek(pkgId, appType, &storedDek, &storedDekLen);
420
421         if (ret == WAE_ERROR_NONE) {
422                 ret = WAE_ERROR_UNKNOWN;
423                 FPRINTF("...FAIL: APP DEK still exists in key_manager.\n");
424                 goto error;
425         }
426
427         ret = WAE_ERROR_NONE;
428
429 error:
430         if (storedDek != NULL)
431                 free(storedDek);
432
433         return ret;
434 }
435
436 int wae_tc_add_get_remove_dek_for_normal_app()
437 {
438         return _wae_tc_add_get_remove_dek(WAE_DOWNLOADED_NORMAL_APP);
439 }
440
441 int wae_tc_add_get_remove_dek_for_global_app()
442 {
443         return _wae_tc_add_get_remove_dek(WAE_DOWNLOADED_GLOBAL_APP);
444 }
445
446 int wae_tc_add_get_remove_dek_for_preloaded_app()
447 {
448         return _wae_tc_add_get_remove_dek(WAE_PRELOADED_APP);
449 }
450
451 int wae_tc_get_preloaded_app_dek_file_path()
452 {
453         int ret = WAE_ERROR_NONE;
454
455         const char *pkgId = "test_pkg";
456         const char *expectedPath = tzplatform_mkpath4(TZ_SYS_SHARE,
457                                                            "wae", "app_dek", "WAE_APP_DEK_test_pkg.adek");
458         char path[256];
459
460         FPRINTF("...expected path : %s\n", expectedPath);
461         ret = _get_preloaded_app_dek_file_path(pkgId, sizeof(path), path);
462         FPRINTF("...returned path : %s\n", path);
463
464         if (ret != WAE_ERROR_NONE || strncmp(expectedPath, path, strlen(expectedPath)) != 0) {
465                 ret = WAE_ERROR_UNKNOWN;
466                 goto error;
467         }
468
469 error:
470         return ret;
471 }
472
473 int wae_tc_extract_pkg_id_from_file_name()
474 {
475         int ret = WAE_ERROR_NONE;
476         const char *fileName = "WAE_APP_DEK_test_pkg.adek";
477         const char *expectedPkgId = "test_pkg";
478         char pkgId[100];
479
480         ret = _extract_pkg_id_from_file_name(fileName, pkgId);
481         FPRINTF("...expected pkgId: %s\n", expectedPkgId);
482         FPRINTF("...returned pkgId: %s\n", pkgId);
483
484         if (ret != WAE_ERROR_NONE || strncmp(expectedPkgId, pkgId, strlen(expectedPkgId)) != 0) {
485                 ret = WAE_ERROR_UNKNOWN;
486                 goto error;
487         }
488
489 error:
490         return ret;
491
492 }
493
494 int wae_tc_read_write_encrypted_app_dek()
495 {
496         int ret = WAE_ERROR_NONE;
497         const char *pkgId = "write_test_pkg";
498         unsigned char dek[256];
499         unsigned char *readDek = NULL;
500         size_t readDekLen = 0;
501
502         ret = _write_encrypted_app_dek_to_file(pkgId, dek, sizeof(dek));
503
504         if (ret != WAE_ERROR_NONE) {
505                 FPRINTF("Fail to _write_encrypted_app_dek_to_file. pkgId=%s\n", pkgId);
506                 goto error;
507         }
508
509         ret = _read_encrypted_app_dek_from_file(pkgId, &readDek, &readDekLen);
510
511         if (ret != WAE_ERROR_NONE) {
512                 FPRINTF("Fail to _read_encrypted_app_dek_from_file. pkgId=%s\n", pkgId);
513                 goto error;
514         }
515
516         _print_binary_to_hex("...ORIG DEK= ", dek, sizeof(dek));
517         _print_binary_to_hex("...READ DEK= ", readDek, readDekLen);
518
519         if (_compare_binary(dek, sizeof(dek), readDek, readDekLen) != 0) {
520                 ret = WAE_ERROR_UNKNOWN;
521                 FPRINTF("...FAIL: DEK != read_DEK.\n");
522                 goto error;
523         }
524
525 error:
526         if (readDek != NULL)
527                 free(readDek);
528
529         return ret;
530 }
531
532
533 int _wae_tc_create_app_dek(wae_app_type_e appType)
534 {
535         int ret = WAE_ERROR_NONE;
536
537         const char *pkgId = "TEST_PKG_ID";
538         unsigned char *dek = NULL;
539         size_t dekLen = 0;
540
541         size_t storedDekLen = 0;
542         unsigned char *storedDek = NULL;
543
544         remove_app_dek(pkgId, appType);
545
546         ret = create_app_dek(pkgId, appType, &dek, &dekLen);
547
548         if (ret != WAE_ERROR_NONE) {
549                 FPRINTF("...FAIL: create_app_dek. ret=%d\n", ret);
550                 goto error;
551         }
552
553         ret = get_app_dek(pkgId, appType, &storedDek, &storedDekLen);
554
555         if (ret != WAE_ERROR_NONE) {
556                 ret = WAE_ERROR_KEY_MANAGER;
557                 FPRINTF("...FAIL: get_app_dek. ret=%d\n", ret);
558                 goto error;
559         }
560
561         _print_binary_to_hex("...CREATED  DEK = ", dek, dekLen);
562         _print_binary_to_hex("...STORED   DEK = ", storedDek, storedDekLen);
563
564         if (_compare_binary(dek, dekLen, storedDek, storedDekLen) != 0) {
565                 ret = WAE_ERROR_FILE;
566                 FPRINTF("...FAIL: DEK != STORED_DEK.\n");
567                 goto error;
568         }
569
570         remove_app_dek(pkgId, appType);
571
572         ret = WAE_ERROR_NONE;
573
574 error:
575         if (dek != NULL)
576                 free(dek);
577
578         if (storedDek != NULL)
579                 free(storedDek);
580
581         return ret;
582 }
583
584 int wae_tc_create_app_dek_for_normal_app()
585 {
586         return _wae_tc_create_app_dek(WAE_DOWNLOADED_NORMAL_APP);
587 }
588
589 int wae_tc_create_app_dek_for_global_app()
590 {
591         return _wae_tc_create_app_dek(WAE_DOWNLOADED_GLOBAL_APP);
592 }
593
594 int wae_tc_create_app_dek_for_preloaded_app()
595 {
596         return _wae_tc_create_app_dek(WAE_PRELOADED_APP);
597 }
598
599 int wae_tc_get_create_preloaded_app_dek()
600 {
601         int ret = WAE_ERROR_NONE;
602
603         const char *pkgId = "TEST_PKG_ID_FOR_CREATE";
604         unsigned char *dek = NULL;
605         unsigned char *readDek = NULL;
606         size_t readDekLen = 0;
607         size_t dekLen = 0;
608
609         ret = get_preloaded_app_dek(pkgId, &readDek, &readDekLen);
610
611         if (ret != WAE_ERROR_NO_KEY) {
612                 FPRINTF("...FAIL: There should be no APP DEK.  get_preloaded_app_dek. ret=%d\n", ret);
613                 ret = WAE_ERROR_FILE;
614                 goto error;
615         }
616
617         ret = create_preloaded_app_dek(pkgId, &dek, &dekLen);
618
619         if (ret != WAE_ERROR_NONE) {
620                 FPRINTF("...FAIL: create_preloaded_app_dek. ret=%d\n", ret);
621                 goto error;
622         }
623
624         ret = get_preloaded_app_dek(pkgId, &readDek, &readDekLen);
625
626         if (ret != WAE_ERROR_NONE) {
627                 FPRINTF("...FAIL: get_preloaded_app_dek. ret=%d\n", ret);
628                 goto error;
629         }
630
631         _print_binary_to_hex("...CREATED DEK = ", dek, dekLen);
632         _print_binary_to_hex("...READ    DEK = ", readDek, readDekLen);
633
634         if (_compare_binary(dek, dekLen, readDek, readDekLen) != 0) {
635                 ret = WAE_ERROR_FILE;
636                 FPRINTF("...FAIL: DEK != READ_DEK.\n");
637                 goto error;
638         }
639
640         ret = WAE_ERROR_NONE;
641
642 error:
643         if (dek != NULL)
644                 free(dek);
645
646         if (readDek != NULL)
647                 free(readDek);
648
649         return ret;
650 }
651
652 int wae_tc_load_preloaded_app_deks()
653 {
654         int ret = WAE_ERROR_NONE;
655
656         const char *pkgId1 = "TEST_PKGID_1";
657         unsigned char *dek1 = NULL;
658         size_t dekLen1 = 0;
659         unsigned char *readDek1 = NULL;
660         size_t readDekLen1 = 0;
661         char path1[MAX_PATH_LEN] = {0, };
662
663         const char *pkgId2 = "TEST_PKGID_2";
664         unsigned char *dek2 = NULL;
665         size_t dekLen2 = 0;
666         unsigned char *readDek2 = NULL;
667         size_t readDekLen2 = 0;
668         char path2[MAX_PATH_LEN] = {0, };
669
670         _get_preloaded_app_dek_file_path(pkgId1, sizeof(path1), path1);
671         _get_preloaded_app_dek_file_path(pkgId2, sizeof(path2), path2);
672
673         // remove old test data
674         remove_app_dek(pkgId1, WAE_PRELOADED_APP);
675         remove_app_dek(pkgId2, WAE_PRELOADED_APP);
676         unlink(path1);
677         unlink(path2);
678
679         // create 2 dek for preloaded app
680         ret = create_preloaded_app_dek(pkgId1, &dek1, &dekLen1);
681
682         if (ret != WAE_ERROR_NONE) {
683                 FPRINTF("...FAIL: create_preloaded_app_dek. ret=%d\n", ret);
684                 goto error;
685         }
686
687         ret = create_preloaded_app_dek(pkgId2, &dek2, &dekLen2);
688
689         if (ret != WAE_ERROR_NONE) {
690                 FPRINTF("...FAIL: create_preloaded_app_dek. ret=%d\n", ret);
691                 goto error;
692         }
693
694         // load_preloaded_app_deks
695         ret = load_preloaded_app_deks(true);
696
697         if (ret != WAE_ERROR_NONE) {
698                 FPRINTF("...FAIL: load_preloaded_app_deks. ret=%d\n", ret);
699                 goto error;
700         }
701
702         // get_app_dek
703         ret = get_app_dek(pkgId1, WAE_PRELOADED_APP, &readDek1, &readDekLen1);
704
705         if (ret != WAE_ERROR_NONE) {
706                 FPRINTF("...FAIL: get_app_dek. ret=%d\n", ret);
707                 goto error;
708         }
709
710         ret = get_app_dek(pkgId2, WAE_PRELOADED_APP, &readDek2, &readDekLen2);
711
712         if (ret != WAE_ERROR_NONE) {
713                 FPRINTF("...FAIL: get_app_dek. ret=%d\n", ret);
714                 goto error;
715         }
716
717         _print_binary_to_hex("...CREATED DEK1 = ", dek1, dekLen1);
718         _print_binary_to_hex("...READ    DEK1 = ", readDek1, readDekLen1);
719
720         if (_compare_binary(dek1, dekLen1, readDek1, readDekLen1) != 0) {
721                 ret = WAE_ERROR_FILE;
722                 FPRINTF("...FAIL: DEK1 != READ_DEK1.\n");
723                 goto error;
724         }
725
726         _print_binary_to_hex("...CREATED DEK2 = ", dek2, dekLen2);
727         _print_binary_to_hex("...READ    DEK2 = ", readDek2, readDekLen2);
728
729         if (_compare_binary(dek2, dekLen2, readDek2, readDekLen2) != 0) {
730                 ret = WAE_ERROR_FILE;
731                 FPRINTF("...FAIL: DEK2 != READ_DEK2.\n");
732                 goto error;
733         }
734
735         // remove_app_dek
736         remove_app_dek(pkgId1, WAE_PRELOADED_APP);
737         remove_app_dek(pkgId2, WAE_PRELOADED_APP);
738
739         ret = WAE_ERROR_NONE;
740
741 error:
742         if (dek1 != NULL)
743                 free(dek1);
744
745         if (readDek1 != NULL)
746                 free(readDek1);
747
748         if (dek2 != NULL)
749                 free(dek2);
750
751         if (readDek2 != NULL)
752                 free(readDek2);
753
754         return ret;
755 }
756
757 int _wae_tc_encrypt_decrypt_web_app(wae_app_type_e appType)
758 {
759         int ret = WAE_ERROR_NONE;
760
761         const char *pkgId1 = "testpkg_for_normal";
762         const char *pkgId2 = "testpkg_for_global";
763         const char *pkgId3 = "testpkg_for_preloaded";
764         const char *pkgId = NULL;
765         const char *plaintext = "adbdfdfdfdfdererfdfdfererfdrerfdrer";
766         size_t plaintextLen = strlen(plaintext);
767         unsigned char *encrypted = NULL;
768         size_t encLen = 0;
769         unsigned char *decrypted = NULL;
770         size_t decLen = 0;
771         char decrypted_str[1024] = {0, };
772
773         switch (appType) {
774         case WAE_DOWNLOADED_NORMAL_APP:
775                 pkgId = pkgId1;
776                 break;
777
778         case WAE_DOWNLOADED_GLOBAL_APP:
779                 pkgId = pkgId2;
780                 break;
781
782         case WAE_PRELOADED_APP:
783                 pkgId = pkgId3;
784                 break;
785         }
786
787         // remove old test data
788         ret = wae_remove_app_dek(pkgId, appType);
789
790         if (appType == WAE_PRELOADED_APP) {
791                 _clear_app_deks_loaded();
792         }
793
794         // test for downloaded web application
795         ret = wae_encrypt_web_application(pkgId, appType,
796                                                                           (const unsigned char *)plaintext, plaintextLen,
797                                                                           &encrypted, &encLen);
798
799         if (ret != WAE_ERROR_NONE) {
800                 FPRINTF("...FAIL: wae_encrypt_web_application. ret=%d\n", ret);
801                 goto error;
802         }
803
804         // encrypt test twice
805         ret = wae_encrypt_web_application(pkgId, appType,
806                                                                           (const unsigned char *)plaintext, plaintextLen,
807                                                                           &encrypted, &encLen);
808
809         if (ret != WAE_ERROR_NONE) {
810                 FPRINTF("...FAIL: wae_encrypt_web_application. ret=%d\n", ret);
811                 goto error;
812         }
813
814         _remove_app_dek_from_cache(pkgId);
815
816         if (appType == WAE_PRELOADED_APP) {
817                 load_preloaded_app_deks(true);
818         }
819
820         ret = wae_decrypt_web_application(pkgId, appType, encrypted, encLen, &decrypted, &decLen);
821
822         if (ret != WAE_ERROR_NONE) {
823                 FPRINTF("...FAIL: wae_decrypt_web_application. ret=%d\n", ret);
824                 goto error;
825         }
826
827         if (plaintextLen != decLen) {
828                 FPRINTF("...FAIL: plaintextLen(%d) != decLen(%d)\n", (int) plaintextLen, (int) decLen);
829                 ret = WAE_ERROR_CRYPTO;
830                 goto error;
831         }
832
833         memcpy(decrypted_str, decrypted, decLen);
834         FPRINTF("...plaintext(downloaded) = %s\n", plaintext);
835         FPRINTF("...decrypted(downloaded) = %s\n", decrypted_str);
836
837         if (strcmp(plaintext, decrypted_str) != 0) {
838                 FPRINTF("...FAIL: plaintext(%s) != decrypted(%s)\n", plaintext, decrypted_str);
839                 ret = WAE_ERROR_CRYPTO;
840                 goto error;
841         }
842
843         ret = wae_remove_app_dek(pkgId, appType);
844
845         if (ret != WAE_ERROR_NONE) {
846                 FPRINTF("...FAIL: wae_remove_app_dek. ret=%d\n", ret);
847                 goto error;
848         }
849
850 error:
851         if (encrypted != NULL)
852                 free(encrypted);
853
854         if (decrypted != NULL)
855                 free(decrypted);
856
857         return ret;
858 }
859
860 int wae_tc_encrypt_decrypt_normal_app()
861 {
862         return _wae_tc_encrypt_decrypt_web_app(WAE_DOWNLOADED_NORMAL_APP);
863 }
864
865 int wae_tc_encrypt_decrypt_global_app()
866 {
867         return _wae_tc_encrypt_decrypt_web_app(WAE_DOWNLOADED_GLOBAL_APP);
868 }
869
870 int wae_tc_encrypt_decrypt_preloaded_app()
871 {
872         return _wae_tc_encrypt_decrypt_web_app(WAE_PRELOADED_APP);
873 }
874
875
876 int run_test_cases(char *test_mode)
877 {
878         if (strcmp(test_mode, "system") == 0) {
879                 RUNTC(wae_tc_encrypt_decrypt_app_dek, "wae_tc_encrypt_decrypt_app_dek");
880                 RUNTC(wae_tc_encrypt_decrypt_aes_cbc, "wae_tc_encrypt_decrypt_aes_cbc");
881                 RUNTC(wae_tc_cache, "wae_tc_cache");
882
883                 RUNTC(wae_tc_get_random, "wae_tc_get_random");
884                 RUNTC(wae_tc_get_alias, "wae_tc_get_alias");
885
886                 RUNTC(wae_tc_add_get_remove_dek_for_global_app, "wae_tc_add_get_remove_dek_for_global_app");
887                 RUNTC(wae_tc_add_get_remove_dek_for_preloaded_app, "wae_tc_add_get_remove_dek_for_preloaded_app");
888
889                 RUNTC(wae_tc_get_preloaded_app_dek_file_path, "wae_tc_get_preloaded_app_dek_file_path");
890                 RUNTC(wae_tc_extract_pkg_id_from_file_name, "wae_tc_extract_pkg_id_from_file_name");
891                 RUNTC(wae_tc_read_write_encrypted_app_dek, "wae_tc_read_write_encrypted_app_dek");
892
893                 RUNTC(wae_tc_create_app_dek_for_global_app, "wae_tc_create_app_dek_for_global_app");
894                 RUNTC(wae_tc_create_app_dek_for_preloaded_app, "wae_tc_create_app_dek_for_preloaded_app");
895
896                 RUNTC(wae_tc_get_create_preloaded_app_dek, "wae_tc_get_create_preloaded_app_dek");
897                 RUNTC(wae_tc_load_preloaded_app_deks, "wae_tc_load_preloaded_app_deks");
898
899                 RUNTC(wae_tc_encrypt_decrypt_global_app, "wae_tc_encrypt_decrypt_global_app");
900                 RUNTC(wae_tc_encrypt_decrypt_preloaded_app, "wae_tc_encrypt_decrypt_preloaded_app");
901         } else {
902                 RUNTC(wae_tc_add_get_remove_dek_for_normal_app, "wae_tc_add_get_remove_dek_for_normal_app");
903                 RUNTC(wae_tc_create_app_dek_for_normal_app, "wae_tc_create_app_dek_for_normal_app");
904                 RUNTC(wae_tc_encrypt_decrypt_normal_app, "wae_tc_encrypt_decrypt_normal_app");
905         }
906
907         PRINT_TC_SUMMARY();
908         return 0;
909 }
910
911 int main(int argc, char *argv[])
912 {
913         int ret = 0;
914
915         if (argc != 2 || (strcmp(argv[1], "system") != 0 && strcmp(argv[1], "user"))) {
916                 FPRINTF("invalid command formant.  command format : %s system|user\n", argv[0]);
917                 exit(1);
918         }
919
920         ret = run_test_cases(argv[1]);
921
922         return ret;
923 }