--- /dev/null
+/*
+ * Copyright (c) 2012 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 test_cases.cpp
+ * @author Dongsun Lee (ds73.lee@samsung.com)
+ * @version 1.0
+ * @brief libwebappenc test cases
+ */
+
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <string>
+#include <sstream>
+#include <fcntl.h>
+#include <unistd.h>
+#include <dpl/test/test_runner.h>
+#include <dpl/test/test_runner_multiprocess.h>
+#include <dpl/log/log.h>
+#include "tests_common.h"
+#include <web_app_enc.h>
+
+#define TEST_PKGID_1 "testpkg_for_downloaded"
+#define TEST_PKGID_2 "testpkg_for_preloaded"
+#define TEST_PLAINTEXT "adbdfdfdfdfdererfdfdfererfdrerfdrer"
+#define PRELOADED_WAPP_FILE1 "/usr/share/wae/test/PRELOADED_WAPP_FILE1.enc"
+
+#define DOWNLOADED_ENC_FILE "/tmp/downloaded_enc_file"
+#define PRELOADED_ENC_FILE "/tmp/preloaded_enc_file"
+
+int _read_from_file(const char* path, unsigned char** data, size_t* len)
+{
+ int ret = WAE_ERROR_NONE;
+ FILE* f = NULL;
+ int file_len = -1;
+ unsigned char* file_contents = NULL;
+ int ch = 0;
+ int i = 0;
+
+ f = fopen(path, "r");
+ if( f == NULL) {
+ ret = WAE_ERROR_FILE;
+ goto error;
+ }
+
+ fseek(f, 0, SEEK_END); // move to the end of a file
+ file_len = ftell(f);
+ fseek(f, 0, SEEK_SET); // move to the start of a file
+
+ file_contents = (unsigned char*) malloc(file_len);
+ if(file_contents == NULL) {
+ ret = WAE_ERROR_MEMORY;
+ goto error;
+ }
+ memset(file_contents, 0x00, file_len);
+
+ while( (ch = fgetc(f)) != EOF) {
+ file_contents[i++]=(char)ch;
+ }
+
+ *data = file_contents;
+ *len = file_len;
+
+error:
+ if(f != NULL)
+ fclose(f);
+ if(ret != WAE_ERROR_NONE && file_contents != NULL)
+ free(file_contents);
+
+ return ret;
+}
+
+int _write_to_file(const char* path, const unsigned char* data, size_t len)
+{
+ int ret = WAE_ERROR_NONE;
+
+ FILE* f = NULL;
+ int write_len = -1;
+
+ f = fopen(path, "w");
+ if( f == NULL) {
+ ret = WAE_ERROR_FILE;
+ goto error;
+ }
+
+ write_len = fwrite(data, 1, len, f);
+ if(write_len != (int) len) {
+ ret = WAE_ERROR_FILE;
+ goto error;
+ }
+error:
+ if(f != NULL)
+ fclose(f);
+
+ return ret;
+}
+
+
+RUNNER_TEST_GROUP_INIT(libwebappenc)
+
+RUNNER_TEST(T01_init) {
+ wae_remove_app_dek(TEST_PKGID_1);
+ wae_remove_app_dek(TEST_PKGID_2);
+}
+
+RUNNER_CHILD_TEST(T02_downloaded_web_app_enc){
+ int ret = WAE_ERROR_NONE;
+ const char* pkgId = TEST_PKGID_1;
+ const char* plaintext = TEST_PLAINTEXT;
+ size_t plaintextLen = strlen(plaintext);
+ unsigned char* encrypted = NULL;
+ size_t encLen = 0;
+
+ int isPreloaded = 0; // Downloaded
+
+ ret = wae_encrypt_web_application(pkgId, isPreloaded,
+ (const unsigned char*)plaintext, plaintextLen,
+ &encrypted, &encLen);
+ RUNNER_ASSERT_MSG(ret == WAE_ERROR_NONE, "FAIL: wae_encrypt_web_application. ret=" << ret);
+
+ ret = _write_to_file(DOWNLOADED_ENC_FILE, encrypted, encLen);
+ RUNNER_ASSERT_MSG(ret == WAE_ERROR_NONE, "FAIL: _write_to_file. file=" << DOWNLOADED_ENC_FILE);
+}
+
+RUNNER_CHILD_TEST(T03_downloaded_web_app_dec){
+ int ret = WAE_ERROR_NONE;
+ const char* pkgId = TEST_PKGID_1;
+ const char* plaintext = TEST_PLAINTEXT;
+ size_t plaintextLen = strlen(plaintext);
+ unsigned char* encrypted = NULL;
+ size_t encLen = 0;
+ unsigned char* decrypted = NULL;
+ size_t decLen = 0;
+
+ int isPreloaded = 0; // Downloaded
+
+ ret = _read_from_file(DOWNLOADED_ENC_FILE, &encrypted, &encLen);
+ RUNNER_ASSERT_MSG(ret == WAE_ERROR_NONE, "FAIL: _read_from_file. ret=" << ret);
+
+ ret = wae_decrypt_web_application(pkgId, isPreloaded, encrypted, encLen, &decrypted, &decLen);
+ RUNNER_ASSERT_MSG(ret == WAE_ERROR_NONE, "FAIL: wae_decrypt_web_application. ret=" << ret);
+
+ RUNNER_ASSERT_MSG(plaintextLen == decLen,
+ "FAIL: plaintext_len("<<plaintextLen<<") != decrypted_len(" <<decLen<<")");
+ RUNNER_ASSERT_MSG(strncmp(plaintext, (char *)decrypted, decLen) == 0,
+ "FAIL: plaintext("<<plaintext <<") != decrypted("<<(char *)decrypted <<")");
+}
+
+
+RUNNER_CHILD_TEST(T04_preloaded_web_app_enc){
+ int ret = WAE_ERROR_NONE;
+ const char* pkgId = TEST_PKGID_2;
+ const char* plaintext = TEST_PLAINTEXT;
+ size_t plaintextLen = strlen(plaintext);
+ unsigned char* encrypted = NULL;
+ size_t encLen = 0;
+
+ int isPreloaded = 1; // Preloaded
+
+ ret = wae_encrypt_web_application(pkgId, isPreloaded,
+ (const unsigned char*)plaintext, plaintextLen,
+ &encrypted, &encLen);
+ RUNNER_ASSERT_MSG(ret == WAE_ERROR_NONE, "FAIL: wae_encrypt_web_application. ret=" << ret);
+
+ ret = _write_to_file(PRELOADED_ENC_FILE, encrypted, encLen);
+ RUNNER_ASSERT_MSG(ret == WAE_ERROR_NONE, "FAIL: _write_to_file. file=" << DOWNLOADED_ENC_FILE);
+}
+
+RUNNER_TEST(T05_reload_app_deks) {
+ int ret = system("wae_initializer --reload");
+ RUNNER_ASSERT_MSG(ret != -1, "FAIL: load_preloaded_app_deks. ret=" << ret);
+}
+
+RUNNER_CHILD_TEST(T06_preloaded_web_app_dec){
+ int ret = WAE_ERROR_NONE;
+ const char* pkgId = TEST_PKGID_2;
+ const char* plaintext = TEST_PLAINTEXT;
+ size_t plaintextLen = strlen(plaintext);
+ unsigned char* encrypted = NULL;
+ size_t encLen = 0;
+ unsigned char* decrypted = NULL;
+ size_t decLen = 0;
+
+ int isPreloaded = 1; // Preloaded
+
+ ret = _read_from_file(PRELOADED_ENC_FILE, &encrypted, &encLen);
+ RUNNER_ASSERT_MSG(ret == WAE_ERROR_NONE, "FAIL: _read_from_file. ret=" << ret);
+
+ ret = wae_decrypt_web_application(pkgId, isPreloaded, encrypted, encLen, &decrypted, &decLen);
+ RUNNER_ASSERT_MSG(ret == WAE_ERROR_NONE, "FAIL: wae_decrypt_web_application. ret=" << ret);
+
+ RUNNER_ASSERT_MSG(plaintextLen == decLen,
+ "FAIL: plaintext_len("<<plaintextLen<<") != decrypted_len(" <<decLen<<")");
+ RUNNER_ASSERT_MSG(strncmp(plaintext, (char *)decrypted, decLen) == 0,
+ "FAIL: plaintext("<<plaintext <<") != decrypted("<<(char *)decrypted <<")");
+}
+
+
+RUNNER_CHILD_TEST(T07_remove_app_dek) {
+ int ret = WAE_ERROR_NONE;
+
+ ret = wae_remove_app_dek(TEST_PKGID_1);
+ RUNNER_ASSERT_MSG(ret == WAE_ERROR_NONE, "FAIL: wae_remove_app_dek. ret=" << ret);
+
+ ret = wae_remove_app_dek(TEST_PKGID_2);
+ RUNNER_ASSERT_MSG(ret == WAE_ERROR_NONE, "FAIL: wae_remove_app_dek. ret=" << ret);
+}