summaryrefslogtreecommitdiff
path: root/tests/wae_tests.c
blob: 2710f989e944452c82c756411883ccb830009c7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
/*
 *  Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd All Rights Reserved
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License
 *
 *
 * @file        wae_tests.c
 * @author      Dongsun Lee (ds73.lee@samsung.com)
 * @version     1.0
 * @brief       internal test cases for libwebappenc.
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include "web_app_enc.h"
#include "key_handler.h"
#include "crypto_service.h"

#include <tzplatform_config.h>

static int tc_seq = 0;
static int tc_succ = 0;
static int tc_fail = 0;

#define FPRINTF(format, args...) fprintf(stdout, format, ##args)

static int RUNTC(int (*tc_method)(), const char* tc_name)
{
    int ret = WAE_ERROR_NONE;
    FPRINTF("[%02d:%s]started...\n", tc_seq, tc_name);
    ret = tc_method();
    if(ret == WAE_ERROR_NONE) {
        FPRINTF("[%02d:%s]ended. SUCCESS\n\n", tc_seq, tc_name);
        tc_succ++;
    } else {
        FPRINTF("[%02d:%s]ended. FAIL. error=%d\n\n", tc_seq, tc_name, ret);
        tc_fail++;
    }
    tc_seq++;
    return ret;
}

static void PRINT_TC_SUMMARY()
{
    FPRINTF("\n");
    FPRINTF("===============================================\n");
    FPRINTF(" TOTAL = %d, SUCCESS = %d, FAIL = %d\n", tc_seq, tc_succ, tc_fail);
    FPRINTF("===============================================\n");
}

void _print_binary_to_hex(const char* msg, unsigned char* bin, size_t len)
{
    size_t i = 0;
    FPRINTF("%s", msg);
    for(i=0; i<len; i++) {
        FPRINTF("%02x", bin[i]);
    }
    FPRINTF("\n");
}

int _compare_binary(const unsigned char* b1, size_t b1Len, const unsigned char* b2, size_t b2Len)
{
    size_t i = 0;
    if(b1Len != b2Len)
        return b1Len - b2Len;
    for(i=0; i<b1Len; i++) {
        if(b1[i] != b2[i])
            return b1[i] - b2[i];
    }
    return 0;
}


//=================================================================================
// tests for crypto_service.h
//=================================================================================
int wae_tc_encrypt_decrypt_app_dek()
{
    int ret = WAE_ERROR_NONE;
    unsigned char dek[32];
    unsigned char* encryptedDek = NULL;
    size_t encryptedDekLen = 0;
    unsigned char* decryptedDek = NULL;
    size_t decryptedDekLen = 0;

    const char* priKey =
            "-----BEGIN RSA PRIVATE KEY-----\n"
            "MIIEpgIBAAKCAQEA0kWtjpRO7Zh2KX2naVE/BDJdrfwK9xexfNA0MkY2VJ4J2AKM\n"
            "YTj1D1jntceryupCEHOvP3rum+WsFvPXduz9+VKnSsSqj4jcTUubtpDUGA5G79Iq\n"
            "LEPFuSBaqI8Uwkzd08pE+s30oaJDnNazMhSq8JkqBPoCCwtUs73ruE9VbtsBO/kT\n"
            "lASIAfe8nXqcJLcDQgWYhizjJw0Pi6d74oCwS2OTvQDNvsXfFnA0ZJEEYw/rZLir\n"
            "j7OHoOjz+Sh5N+1uA3Up6SPPEbHuP6L12YxqHdy7gnJXodLhvE/cR4SN9VW7+qmC\n"
            "MBjmLkBejGrEX3STS9sLI7MZHu9Y26dwuYb4+wIDAQABAoIBAQCwxqV/vc2RUGDe\n"
            "xuXM0+IvrAw37jJlw4SS0xNexMp+XxMViCbuwYy851h96azS/himbiuCKd6aL/96\n"
            "mGunbtyiFEvSvv5Jh5z2Wr9BQAcfZjla+4w7BIsg9UNifE/OfgLsQBu34xhsHtfK\n"
            "7nFehCOl/I5n+qtnD5KZPe0DWacQdwY4vEAj6YyXdb2bBg+MiwE9KVxGEIUDbklh\n"
            "Is70JXczjLZCS+lIpOKh0/lbZmBZePoUbVTtS+GvtPTpQC/aTHRkwGoEtuPEWpbL\n"
            "0Q1d6zO+vDJVLJlb5FF2haghs8IlqAxkkPjeUTNye+WktRrDQxmPu/blbxQrygfq\n"
            "Au5tBnsxAoGBAOiVtcpg32puo3Yq2Y78oboe9PuHaQP0d3DhwP3/7J0BeNslpjW7\n"
            "E1LWsVsCanxTE8XPUdFfAWgMk7lQqESN0wawGmSmWk+eQPZdjHanBaC8vh7aKjo6\n"
            "q9FdT1DKjrRi23QyDco3f3E7hvM93IAAhw1ikNu8DT19JAxtdeMh5WAZAoGBAOdw\n"
            "6neEvIFXh3RWEv2/GKVhVR8mxDqxmuFdXpOF+YWsK0Tg4uC8jm9kUGnwXgT2Mjke\n"
            "oAwYAFcRbHQQGsxy/vkV16kv4aurTE2hMpjeXCAakwV0Pi2w1f9WnDokjgORkOmc\n"
            "+QK9I8egdFPMVDfQjhLslhSUY0Eb4qcJ6q9WxfQzAoGBANSsAFybk+7oWAO3TtQW\n"
            "YXOk1vIgcYAyS/0mEKixGZS/QdlxZbf/5b17nxTO8rvX416fIftG2ixgQ7vR6us0\n"
            "m9+jq56ZFj9zP4eHJudf9h9yNo5TgwVXnMCGh/4iGbcMJgrrsfxUHu5VNiK5UCSj\n"
            "VtqAZGDoZVryUMIkXQVhezIRAoGBAN7QUIqcGbcUA24257Wu4hVlrUN+WPCAyDEr\n"
            "aL/x/ZV5eXaoYwQlw6LuGpTDOmDgfN2M5FyARuOL/LOIRaSLGXnIU4WoeUSCd8VM\n"
            "6Z9Og7bMnrpjfPEUDBH02hcH1kkNPUwLOZgva2Dm0tdSIcpSWFVTu/E4Io4uQHi8\n"
            "DVqc2ZsNAoGBAJT76ezXNSSv8hnrKqTpwgTicpqhRZ3eFQjyl4HRL26AJMKv++x8\n"
            "4/IsVIwxaHzpbN3nnCjmAHV4gX9YpxVnvYcZflC9WZeDkwNMLmPYb3Zg27EzSMfQ\n"
            "8yrfWJZo3qobipcHf1yohAt4fHk9kUKtPHEwp0xKe//rfhswLb3VCzvQ\n"
            "-----END RSA PRIVATE KEY-----";
    const char* pubKey =
            "-----BEGIN PUBLIC KEY-----\n"
            "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA0kWtjpRO7Zh2KX2naVE/\n"
            "BDJdrfwK9xexfNA0MkY2VJ4J2AKMYTj1D1jntceryupCEHOvP3rum+WsFvPXduz9\n"
            "+VKnSsSqj4jcTUubtpDUGA5G79IqLEPFuSBaqI8Uwkzd08pE+s30oaJDnNazMhSq\n"
            "8JkqBPoCCwtUs73ruE9VbtsBO/kTlASIAfe8nXqcJLcDQgWYhizjJw0Pi6d74oCw\n"
            "S2OTvQDNvsXfFnA0ZJEEYw/rZLirj7OHoOjz+Sh5N+1uA3Up6SPPEbHuP6L12Yxq\n"
            "Hdy7gnJXodLhvE/cR4SN9VW7+qmCMBjmLkBejGrEX3STS9sLI7MZHu9Y26dwuYb4\n"
            "+wIDAQAB\n"
            "-----END PUBLIC KEY-----";

    ret = encrypt_app_dek((const unsigned char*) pubKey, strlen(pubKey),
                          dek, sizeof(dek), &encryptedDek, &encryptedDekLen);
    if(ret != WAE_ERROR_NONE){
        FPRINTF("...FAIL: encrypt_app_dek. ret=%d\n", ret);
        ret = WAE_ERROR_CRYPTO;
        goto error;
    }

    ret = decrypt_app_dek((const unsigned char*) priKey, strlen(priKey), NULL,
                          encryptedDek, encryptedDekLen, &decryptedDek, &decryptedDekLen);
    if(ret != WAE_ERROR_NONE){
        FPRINTF("...FAIL: decrypt_app_dek. ret=%d\n", ret);
        ret = WAE_ERROR_CRYPTO;
        goto error;
    }

    _print_binary_to_hex("...ORIG DEK= ", dek, sizeof(dek));
    _print_binary_to_hex("...ENC  DEK= ", encryptedDek, encryptedDekLen);
    _print_binary_to_hex("...DEC  DEK= ", decryptedDek, decryptedDekLen);

    if(_compare_binary(dek, sizeof(dek), decryptedDek, decryptedDekLen) != 0) {
        FPRINTF("...FAIL: ORIG DEK != decrypted DEK\n");
        ret = WAE_ERROR_CRYPTO;
        goto error;
    }

    ret = WAE_ERROR_NONE;

error:
    if(encryptedDek != NULL)
        free(encryptedDek);
    if(decryptedDek != NULL)
        free(decryptedDek);

    return ret;
}

int wae_tc_encrypt_decrypt_aes_cbc()
{
    int ret = WAE_ERROR_NONE;

    unsigned char dek[32] = {0, };
    size_t keyLen = 32;
    const char* plaintext= "adbdfdfdfdfdererfdfdfererfdrerfdrer";
    size_t plaintextLen = strlen(plaintext);
    unsigned char* encrypted = NULL;
    size_t encLen = 0;
    unsigned char* decrypted = NULL;
    size_t decLen = 0;
    char decrypted_str[1024] = {0, };

    ret = _get_random(keyLen, dek);

    ret = encrypt_aes_cbc(dek, keyLen, (const unsigned char*)plaintext, plaintextLen, &encrypted, &encLen);
    if(ret != WAE_ERROR_NONE){
        FPRINTF("...FAIL: encrypt_aes_cbc. ret=%d\n", ret);
        ret = WAE_ERROR_CRYPTO;
        goto error;
    }

    ret = decrypt_aes_cbc(dek, keyLen, encrypted, encLen, &decrypted, &decLen);
    if(ret != WAE_ERROR_NONE){
        FPRINTF("...FAIL: decrypt_aes_cbc. ret=%d\n", ret);
        ret = WAE_ERROR_CRYPTO;
        goto error;
    }

    if(plaintextLen != decLen) {
        FPRINTF("...FAIL: plaintextLen(%d) != decLen(%d)\n", (int) plaintextLen, (int) decLen);
        ret = WAE_ERROR_CRYPTO;
        goto error;
    }

    memcpy(decrypted_str, decrypted, decLen);
    FPRINTF("...plaintext = %s\n", plaintext);
    FPRINTF("...decrypted = %s\n", decrypted_str);
    if(strcmp(plaintext, decrypted_str) != 0) {
        FPRINTF("...FAIL: plaintext(%s) != decrypted(%s)\n", plaintext, decrypted_str);
        ret = WAE_ERROR_CRYPTO;
        goto error;
    }

error:
    if(encrypted != NULL)
        free(encrypted);
    if(decrypted != NULL)
        free(decrypted);

    return ret;
}

//=================================================================================
// tests for key_handler.h
//=================================================================================
int wae_tc_cache()
{
    int ret = WAE_ERROR_NONE;

    const char* pkg1 = "pkg1";
    const char* pkg2 = "pkg2";
    const char* pkg3 = "pkg3";
    const char* pkgDummy = "dummy";

    unsigned char dek1[32] = {1, };
    unsigned char dek2[32] = {2, };
    unsigned char dek3[32] = {3, };
    unsigned char* retDek = NULL;

    _initialize_cache();

    _add_app_dek_to_cache(pkg1, dek1);
    _add_app_dek_to_cache(pkg2, dek2);
    _add_app_dek_to_cache(pkg3, dek3);

    retDek = NULL;
    retDek = _get_app_dek_from_cache(pkg1);
    if(retDek == NULL || _compare_binary(dek1, 32, retDek, 32) != 0) {
        FPRINTF("failed in cache. Diffent DEK1\n");
        ret = WAE_ERROR_UNKNOWN;
        goto error;
    }
    _print_binary_to_hex("...DEK1         : ", dek1, 32);
    _print_binary_to_hex("...Returen DEK1 : ", retDek, 32);

    retDek = NULL;
    retDek = _get_app_dek_from_cache(pkg2);
    if(retDek == NULL || _compare_binary(dek2, 32, retDek, 32) != 0) {
        FPRINTF("failed in cache. Diffent DEK2\n");
        ret = WAE_ERROR_UNKNOWN;
        goto error;
    }
    _print_binary_to_hex("...DEK2         : ", dek2, 32);
    _print_binary_to_hex("...Returen DEK1 : ", retDek, 32);

    retDek = NULL;
    retDek = _get_app_dek_from_cache(pkg3);
    if(retDek == NULL || _compare_binary(dek3, 32, retDek, 32) != 0) {
        FPRINTF("failed in cache. Diffent DEK3\n");
        ret = WAE_ERROR_UNKNOWN;
        goto error;
    }
    _print_binary_to_hex("...DEK3         : ", dek3, 32);
    _print_binary_to_hex("...Returen DEK3 : ", retDek, 32);

    retDek = NULL;
    retDek = _get_app_dek_from_cache(pkgDummy);
    if(retDek != NULL) {
        FPRINTF("failed in cache. Wrong DEK_DUMMY1 returned\n");
        _print_binary_to_hex("retured wrong DEK : ", retDek, 32);
        ret = WAE_ERROR_UNKNOWN;
        goto error;
    }

    _remove_app_dek_from_cache(pkg3);
    retDek = NULL;
    retDek = _get_app_dek_from_cache(pkg3);
    if(retDek != NULL) {
        FPRINTF("fail to remove app dek from cache\n");
        ret = WAE_ERROR_UNKNOWN;
        goto error;
    }

    _initialize_cache();

    _add_app_dek_to_cache(pkg1, dek1);

    retDek = NULL;
    retDek = _get_app_dek_from_cache(pkg2);
    if(retDek != NULL) {
        FPRINTF("failed in cache. Wrong DEK_DUMMY2 returned\n");
        _print_binary_to_hex("retured wrong DEK : ", retDek, 32);
        ret = WAE_ERROR_UNKNOWN;
        goto error;
    }

    ret = WAE_ERROR_NONE;
error:
    return ret;
}

int wae_tc_get_random()
{
    int ret = WAE_ERROR_NONE;

    size_t rand_len = 32;
    unsigned char random[32] = {0, };

    ret = _get_random(rand_len, random);

    _print_binary_to_hex("...RANDOM = ", random, sizeof(random));

    return ret;
}

int wae_tc_get_alias()
{
    int ret = WAE_ERROR_NONE;

    const char* pkgId = "TEST_PKG_ID";
    char alias[256] = {0, };

    _get_alias(pkgId, WAE_DOWNLOADED_NORMAL_APP, alias, sizeof(alias));
    FPRINTF("...pkgid=%s, alias for normal app=%s\n", pkgId, alias);

    _get_alias(pkgId, WAE_DOWNLOADED_GLOBAL_APP, alias, sizeof(alias));
    FPRINTF("...pkgid=%s, alias for global app=%s\n", pkgId, alias);

    _get_alias(pkgId, WAE_PRELOADED_APP, alias, sizeof(alias));
    FPRINTF("...pkgid=%s, alias for preloaded app=%s\n", pkgId, alias);

    return ret;
}

int _wae_tc_add_get_remove_dek(wae_app_type_e appType)
{
    int ret = WAE_ERROR_NONE;

    const char* pkgId = "TEST_PKG_ID";

    size_t dekLen= 32;
    unsigned char dek[32] = {0, };
    size_t storedDekLen = 0;
    unsigned char* storedDek = NULL;

    ret = _get_random(dekLen, dek);

    remove_app_dek(pkgId, appType);

    ret = _add_dek_to_key_manager(pkgId, appType, dek, dekLen);
    if(ret != WAE_ERROR_NONE) {
        FPRINTF("...FAIL: _add_dek_to_key_manager. ret=%d\n", ret);
        goto error;
    }

    ret = get_app_dek(pkgId, appType, &storedDek, &storedDekLen);
    if(ret != WAE_ERROR_NONE) {
        FPRINTF("...FAIL: get_app_dek. ret=%d\n", ret);
        goto error;
    }

    if(_compare_binary(dek, dekLen, storedDek, storedDekLen) != 0 ) {
        ret = WAE_ERROR_KEY_MANAGER;
        FPRINTF("...FAIL: DEK != STORED_DEK.\n");
        goto error;
    }

    ret = remove_app_dek(pkgId, appType);
    if(ret != WAE_ERROR_NONE) {
        FPRINTF("...FAIL: remove_app_dek. ret=%d\n", ret);
        goto error;
    }

    ret = get_app_dek(pkgId, appType, &storedDek, &storedDekLen);
    if(ret == WAE_ERROR_NONE) {
        ret = WAE_ERROR_UNKNOWN;
        FPRINTF("...FAIL: APP DEK still exists in key_manager.\n");
        goto error;
    }

    ret = WAE_ERROR_NONE;
error:
    if(storedDek != NULL)
        free(storedDek);

    return ret;
}

int wae_tc_add_get_remove_dek_for_normal_app()
{
    return _wae_tc_add_get_remove_dek(WAE_DOWNLOADED_NORMAL_APP);
}

int wae_tc_add_get_remove_dek_for_global_app()
{
    return _wae_tc_add_get_remove_dek(WAE_DOWNLOADED_GLOBAL_APP);
}

int wae_tc_add_get_remove_dek_for_preloaded_app()
{
    return _wae_tc_add_get_remove_dek(WAE_PRELOADED_APP);
}

int wae_tc_get_preloaded_app_dek_file_path()
{
    int ret = WAE_ERROR_NONE;

    const char *pkgId = "test_pkg";
    const char *expectedPath = tzplatform_mkpath4(TZ_SYS_SHARE,
                                    "wae", "app_dek", "WAE_APP_DEK_test_pkg.adek");
    char path[256];

    FPRINTF("...expected path : %s\n", expectedPath);
    ret = _get_preloaded_app_dek_file_path(pkgId, path);
    FPRINTF("...returned path : %s\n", path);

    if(ret != WAE_ERROR_NONE || strncmp(expectedPath, path, strlen(expectedPath)) != 0) {
        ret = WAE_ERROR_UNKNOWN;
        goto error;
    }
error:
    return ret;
}

int wae_tc_extract_pkg_id_from_file_name()
{
    int ret = WAE_ERROR_NONE;
    const char* fileName = "WAE_APP_DEK_test_pkg.adek";
    const char* expectedPkgId = "test_pkg";
    char pkgId[100];

    ret = _extract_pkg_id_from_file_name(fileName, pkgId);
    FPRINTF("...expected pkgId: %s\n", expectedPkgId);
    FPRINTF("...returned pkgId: %s\n", pkgId);

    if(ret != WAE_ERROR_NONE || strncmp(expectedPkgId, pkgId, strlen(expectedPkgId)) != 0) {
        ret = WAE_ERROR_UNKNOWN;
        goto error;
    }
error:
    return ret;

}

int wae_tc_read_write_encrypted_app_dek()
{
    int ret = WAE_ERROR_NONE;
    const char* pkgId = "write_test_pkg";
    unsigned char dek[256];
    unsigned char* readDek = NULL;
    size_t readDekLen = 0;

    ret = _write_encrypted_app_dek_to_file(pkgId, dek, sizeof(dek));
    if(ret != WAE_ERROR_NONE) {
        FPRINTF("Fail to _write_encrypted_app_dek_to_file. pkgId=%s\n", pkgId);
        goto error;
    }

    ret = _read_encrypted_app_dek_from_file(pkgId, &readDek, &readDekLen);
    if(ret != WAE_ERROR_NONE) {
        FPRINTF("Fail to _read_encrypted_app_dek_from_file. pkgId=%s\n", pkgId);
        goto error;
    }

    _print_binary_to_hex("...ORIG DEK= ", dek, sizeof(dek));
    _print_binary_to_hex("...READ DEK= ", readDek, readDekLen);
    if(_compare_binary(dek, sizeof(dek), readDek, readDekLen) != 0 ) {
        ret = WAE_ERROR_UNKNOWN;
        FPRINTF("...FAIL: DEK != read_DEK.\n");
        goto error;
    }

error:
    if(readDek != NULL)
        free(readDek);
    return ret;
}


int _wae_tc_create_app_dek(wae_app_type_e appType)
{
    int ret = WAE_ERROR_NONE;

    const char* pkgId = "TEST_PKG_ID";
    unsigned char* dek = NULL;
    size_t dekLen = 0;

    size_t storedDekLen = 0;
    unsigned char* storedDek = NULL;

    remove_app_dek(pkgId, appType);

    ret = create_app_dek(pkgId, appType, &dek, &dekLen);
    if(ret != WAE_ERROR_NONE) {
        FPRINTF("...FAIL: create_app_dek. ret=%d\n", ret);
        goto error;
    }

    ret = get_app_dek(pkgId, appType, &storedDek, &storedDekLen);
    if(ret != WAE_ERROR_NONE) {
        ret = WAE_ERROR_KEY_MANAGER;
        FPRINTF("...FAIL: get_app_dek. ret=%d\n", ret);
        goto error;
    }

    _print_binary_to_hex("...CREATED  DEK = ", dek, dekLen);
    _print_binary_to_hex("...STORED   DEK = ", storedDek, storedDekLen);
    if(_compare_binary(dek, dekLen, storedDek, storedDekLen) != 0 ) {
        ret = WAE_ERROR_FILE;
        FPRINTF("...FAIL: DEK != STORED_DEK.\n");
        goto error;
    }

    remove_app_dek(pkgId, appType);

    ret = WAE_ERROR_NONE;
error:
    if(dek != NULL)
        free(dek);
    if(storedDek != NULL)
        free(storedDek);
    return ret;
}

int wae_tc_create_app_dek_for_normal_app()
{
    return _wae_tc_create_app_dek(WAE_DOWNLOADED_NORMAL_APP);
}

int wae_tc_create_app_dek_for_global_app()
{
    return _wae_tc_create_app_dek(WAE_DOWNLOADED_GLOBAL_APP);
}

int wae_tc_create_app_dek_for_preloaded_app()
{
    return _wae_tc_create_app_dek(WAE_PRELOADED_APP);
}

int wae_tc_get_create_preloaded_app_dek()
{
    int ret = WAE_ERROR_NONE;

    const char* pkgId = "TEST_PKG_ID_FOR_CREATE";
    unsigned char *dek = NULL;
    unsigned char *readDek = NULL;
    size_t readDekLen = 0;
    size_t dekLen = 0;

    ret = get_preloaded_app_dek(pkgId, &readDek, &readDekLen);
    if(ret != WAE_ERROR_NO_KEY) {
        FPRINTF("...FAIL: There should be no APP DEK.  get_preloaded_app_dek. ret=%d\n", ret);
        ret = WAE_ERROR_FILE;
        goto error;
    }

    ret = create_preloaded_app_dek(pkgId, &dek, &dekLen);
    if(ret != WAE_ERROR_NONE) {
        FPRINTF("...FAIL: create_preloaded_app_dek. ret=%d\n", ret);
        goto error;
    }

    ret = get_preloaded_app_dek(pkgId, &readDek, &readDekLen);
    if(ret != WAE_ERROR_NONE) {
        FPRINTF("...FAIL: get_preloaded_app_dek. ret=%d\n", ret);
        goto error;
    }

    _print_binary_to_hex("...CREATED DEK = ", dek, dekLen);
    _print_binary_to_hex("...READ    DEK = ", readDek, readDekLen);

    if(_compare_binary(dek, dekLen, readDek, readDekLen) != 0 ) {
        ret = WAE_ERROR_FILE;
        FPRINTF("...FAIL: DEK != READ_DEK.\n");
        goto error;
    }

    ret = WAE_ERROR_NONE;
error:
    if(dek != NULL)
        free(dek);
    if(readDek != NULL)
        free(readDek);
    return ret;
}

int wae_tc_load_preloaded_app_deks()
{
    int ret = WAE_ERROR_NONE;

    const char* pkgId1 = "TEST_PKGID_1";
    unsigned char* dek1 = NULL;
    size_t dekLen1 = 0;
    unsigned char* readDek1 = NULL;
    size_t readDekLen1 = 0;
    char path1[MAX_PATH_LEN] = {0, };
    FILE *f1 = NULL;

    const char* pkgId2 = "TEST_PKGID_2";
    unsigned char* dek2 = NULL;
    size_t dekLen2 = 0;
    unsigned char* readDek2 = NULL;
    size_t readDekLen2 = 0;
    char path2[MAX_PATH_LEN] = {0, };
    FILE *f2 = NULL;

    _get_preloaded_app_dek_file_path(pkgId1, path1);
    _get_preloaded_app_dek_file_path(pkgId2, path2);

    // remove old test data
    remove_app_dek(pkgId1, WAE_PRELOADED_APP);
    remove_app_dek(pkgId2, WAE_PRELOADED_APP);
    unlink(path1);
    unlink(path2);

    // create 2 dek for preloaded app
    ret = create_preloaded_app_dek(pkgId1, &dek1, &dekLen1);
    if(ret != WAE_ERROR_NONE) {
        FPRINTF("...FAIL: create_preloaded_app_dek. ret=%d\n", ret);
        goto error;
    }

    ret = create_preloaded_app_dek(pkgId2, &dek2, &dekLen2);
    if(ret != WAE_ERROR_NONE) {
        FPRINTF("...FAIL: create_preloaded_app_dek. ret=%d\n", ret);
        goto error;
    }

    // load_preloaded_app_deks
    ret = load_preloaded_app_deks(WAE_TRUE);
    if(ret != WAE_ERROR_NONE) {
        FPRINTF("...FAIL: load_preloaded_app_deks. ret=%d\n", ret);
        goto error;
    }

    // get_app_dek
    ret = get_app_dek(pkgId1, WAE_PRELOADED_APP, &readDek1, &readDekLen1);
    if(ret != WAE_ERROR_NONE) {
        FPRINTF("...FAIL: get_app_dek. ret=%d\n", ret);
        goto error;
    }

    ret = get_app_dek(pkgId2, WAE_PRELOADED_APP, &readDek2, &readDekLen2);
    if(ret != WAE_ERROR_NONE) {
        FPRINTF("...FAIL: get_app_dek. ret=%d\n", ret);
        goto error;
    }

    _print_binary_to_hex("...CREATED DEK1 = ", dek1, dekLen1);
    _print_binary_to_hex("...READ    DEK1 = ", readDek1, readDekLen1);
    if(_compare_binary(dek1, dekLen1, readDek1, readDekLen1) != 0 ) {
        ret = WAE_ERROR_FILE;
        FPRINTF("...FAIL: DEK1 != READ_DEK1.\n");
        goto error;
    }

    _print_binary_to_hex("...CREATED DEK2 = ", dek2, dekLen2);
    _print_binary_to_hex("...READ    DEK2 = ", readDek2, readDekLen2);
    if(_compare_binary(dek2, dekLen2, readDek2, readDekLen2) != 0 ) {
        ret = WAE_ERROR_FILE;
        FPRINTF("...FAIL: DEK2 != READ_DEK2.\n");
        goto error;
    }

    // remove_app_dek
    remove_app_dek(pkgId1, WAE_PRELOADED_APP);
    remove_app_dek(pkgId2, WAE_PRELOADED_APP);

    ret = WAE_ERROR_NONE;
error:
    if(dek1 != NULL)
        free(dek1);
    if(readDek1 != NULL)
        free(readDek1);
    if(f1 != NULL)
        fclose(f1);
    if(dek2 != NULL)
        free(dek2);
    if(readDek2 != NULL)
        free(readDek2);
    if(f2 != NULL)
        fclose(f2);

    return ret;
}

int _wae_tc_encrypt_decrypt_web_app(wae_app_type_e appType)
{
    int ret = WAE_ERROR_NONE;

    const char* pkgId1 = "testpkg_for_normal";
    const char* pkgId2 = "testpkg_for_global";
    const char* pkgId3 = "testpkg_for_preloaded";
    const char* pkgId = NULL;
    const char* plaintext= "adbdfdfdfdfdererfdfdfererfdrerfdrer";
    size_t plaintextLen = strlen(plaintext);
    unsigned char* encrypted = NULL;
    size_t encLen = 0;
    unsigned char* decrypted = NULL;
    size_t decLen = 0;
    char decrypted_str[1024] = {0, };

    switch(appType) {
        case WAE_DOWNLOADED_NORMAL_APP:
            pkgId = pkgId1;
            break;
        case WAE_DOWNLOADED_GLOBAL_APP:
            pkgId = pkgId2;
            break;
        case WAE_PRELOADED_APP:
            pkgId = pkgId3;
            break;
    }

    // remove old test data
    ret = wae_remove_app_dek(pkgId, appType);
    if(appType == WAE_PRELOADED_APP) {
        _clear_app_deks_loaded();
    }

    // test for downloaded web application
    ret = wae_encrypt_web_application(pkgId, appType,
                                      (const unsigned char*)plaintext, plaintextLen,
                                      &encrypted, &encLen);
    if(ret != WAE_ERROR_NONE){
        FPRINTF("...FAIL: wae_encrypt_web_application. ret=%d\n", ret);
        goto error;
    }

    // encrypt test twice
    ret = wae_encrypt_web_application(pkgId, appType,
                                      (const unsigned char*)plaintext, plaintextLen,
                                      &encrypted, &encLen);
    if(ret != WAE_ERROR_NONE){
        FPRINTF("...FAIL: wae_encrypt_web_application. ret=%d\n", ret);
        goto error;
    }

    _remove_app_dek_from_cache(pkgId);

    if(appType == WAE_PRELOADED_APP) {
        load_preloaded_app_deks(WAE_TRUE);
    }

    ret = wae_decrypt_web_application(pkgId, appType, encrypted, encLen, &decrypted, &decLen);
    if(ret != WAE_ERROR_NONE){
        FPRINTF("...FAIL: wae_decrypt_web_application. ret=%d\n", ret);
        goto error;
    }

    if(plaintextLen != decLen) {
        FPRINTF("...FAIL: plaintextLen(%d) != decLen(%d)\n", (int) plaintextLen, (int) decLen);
        ret = WAE_ERROR_CRYPTO;
        goto error;
    }

    memcpy(decrypted_str, decrypted, decLen);
    FPRINTF("...plaintext(downloaded) = %s\n", plaintext);
    FPRINTF("...decrypted(downloaded) = %s\n", decrypted_str);
    if(strcmp(plaintext, decrypted_str) != 0) {
        FPRINTF("...FAIL: plaintext(%s) != decrypted(%s)\n", plaintext, decrypted_str);
        ret = WAE_ERROR_CRYPTO;
        goto error;
    }

    ret = wae_remove_app_dek(pkgId, appType);
    if(ret != WAE_ERROR_NONE){
        FPRINTF("...FAIL: wae_remove_app_dek. ret=%d\n", ret);
        goto error;
    }

error:
    if(encrypted != NULL)
        free(encrypted);
    if(decrypted != NULL)
        free(decrypted);

    return ret;
}

int wae_tc_encrypt_decrypt_normal_app()
{
    return _wae_tc_encrypt_decrypt_web_app(WAE_DOWNLOADED_NORMAL_APP);
}

int wae_tc_encrypt_decrypt_global_app()
{
    return _wae_tc_encrypt_decrypt_web_app(WAE_DOWNLOADED_GLOBAL_APP);
}

int wae_tc_encrypt_decrypt_preloaded_app()
{
    return _wae_tc_encrypt_decrypt_web_app(WAE_PRELOADED_APP);
}


int run_test_cases(char* test_mode)
{
    if(strcmp(test_mode, "system") == 0) {
        RUNTC(wae_tc_encrypt_decrypt_app_dek, "wae_tc_encrypt_decrypt_app_dek");
        RUNTC(wae_tc_encrypt_decrypt_aes_cbc, "wae_tc_encrypt_decrypt_aes_cbc");
        RUNTC(wae_tc_cache, "wae_tc_cache");

        RUNTC(wae_tc_get_random, "wae_tc_get_random");
        RUNTC(wae_tc_get_alias, "wae_tc_get_alias");

        RUNTC(wae_tc_add_get_remove_dek_for_global_app, "wae_tc_add_get_remove_dek_for_global_app");
        RUNTC(wae_tc_add_get_remove_dek_for_preloaded_app, "wae_tc_add_get_remove_dek_for_preloaded_app");

        RUNTC(wae_tc_get_preloaded_app_dek_file_path, "wae_tc_get_preloaded_app_dek_file_path");
        RUNTC(wae_tc_extract_pkg_id_from_file_name, "wae_tc_extract_pkg_id_from_file_name");
        RUNTC(wae_tc_read_write_encrypted_app_dek, "wae_tc_read_write_encrypted_app_dek");

        RUNTC(wae_tc_create_app_dek_for_global_app, "wae_tc_create_app_dek_for_global_app");
        RUNTC(wae_tc_create_app_dek_for_preloaded_app, "wae_tc_create_app_dek_for_preloaded_app");

        RUNTC(wae_tc_get_create_preloaded_app_dek, "wae_tc_get_create_preloaded_app_dek");
        RUNTC(wae_tc_load_preloaded_app_deks, "wae_tc_load_preloaded_app_deks");

        RUNTC(wae_tc_encrypt_decrypt_global_app, "wae_tc_encrypt_decrypt_global_app");
        RUNTC(wae_tc_encrypt_decrypt_preloaded_app, "wae_tc_encrypt_decrypt_preloaded_app");
    }else {
        RUNTC(wae_tc_add_get_remove_dek_for_normal_app, "wae_tc_add_get_remove_dek_for_normal_app");
        RUNTC(wae_tc_create_app_dek_for_normal_app, "wae_tc_create_app_dek_for_normal_app");
        RUNTC(wae_tc_encrypt_decrypt_normal_app, "wae_tc_encrypt_decrypt_normal_app");
    }

    PRINT_TC_SUMMARY();
    return 0;
}

int main(int argc, char* argv[])
{
    int ret = 0;

    if(argc != 2 || (strcmp(argv[1],"system") != 0 && strcmp(argv[1],"user")) ) {
        FPRINTF("invalid command formant.  command format : %s system|user\n", argv[0]);
        exit(1);
    }

    ret = run_test_cases(argv[1]);

    return ret;
}