add test cases for libwebappenc 23/44523/2
authorDongsun Lee <ds73.lee@samsung.com>
Thu, 23 Jul 2015 03:24:23 +0000 (12:24 +0900)
committerDongsun Lee <ds73.lee@samsung.com>
Fri, 24 Jul 2015 01:10:53 +0000 (10:10 +0900)
Change-Id: I2e433293c2566ecbaefd237e8e60a6e2dcf67a6e
Signed-off-by: Dongsun Lee <ds73.lee@samsung.com>
packaging/security-tests.spec
src/CMakeLists.txt
src/libwebappenc-tests/CMakeLists.txt [new file with mode: 0644]
src/libwebappenc-tests/libwebappenc-tests.cpp [new file with mode: 0644]
src/libwebappenc-tests/test_cases.cpp [new file with mode: 0644]

index a3d222e..c58bcc8 100644 (file)
@@ -22,6 +22,7 @@ BuildRequires: pkgconfig(libpcrecpp)
 BuildRequires: pkgconfig(libxml-2.0)
 BuildRequires: pkgconfig(libiri)
 BuildRequires: pkgconfig(sqlite3)
+BuildRequires: pkgconfig(libwebappenc)
 BuildRequires: cynara-devel
 BuildRequires: pkgconfig(libtzplatform-config)
 BuildRequires: boost-devel
@@ -111,3 +112,4 @@ echo "security-tests postinst done ..."
 /usr/lib/security-tests/cynara-tests/plugins/multiple-policy/*
 /usr/lib/security-tests/cynara-tests/plugins/test-agent/*
 /usr/bin/security-tests-inner-test
+/usr/bin/libwebappenc-tests
index b3c75e6..dfc23dc 100644 (file)
@@ -89,3 +89,4 @@ ADD_SUBDIRECTORY(smack-dbus-tests)
 ADD_SUBDIRECTORY(security-server-tests)
 ADD_SUBDIRECTORY(security-manager-tests)
 ADD_SUBDIRECTORY(cynara-tests)
+ADD_SUBDIRECTORY(libwebappenc-tests)
diff --git a/src/libwebappenc-tests/CMakeLists.txt b/src/libwebappenc-tests/CMakeLists.txt
new file mode 100644 (file)
index 0000000..3f4b5ae
--- /dev/null
@@ -0,0 +1,65 @@
+# Copyright (c) 2012-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        CMakeLists.txt
+# @author      Dongsun Lee (ds73.lee@samsung.com)
+# @version     0.1
+# @brief
+#
+INCLUDE(FindPkgConfig)
+SET(TARGET_WAE_TEST "libwebappenc-tests")
+
+#dependencies
+PKG_CHECK_MODULES(TARGET_WAE_DEP
+    libwebappenc
+    REQUIRED
+    )
+
+#files to compile
+SET(TARGET_WAE_TEST_SOURCES
+    ${PROJECT_SOURCE_DIR}/src/libwebappenc-tests/libwebappenc-tests.cpp
+    ${PROJECT_SOURCE_DIR}/src/libwebappenc-tests/test_cases.cpp
+    )
+
+#header directories
+INCLUDE_DIRECTORIES(SYSTEM
+    ${TARGET_WAE_DEP_INCLUDE_DIRS}
+    )
+
+INCLUDE_DIRECTORIES(
+    ${PROJECT_SOURCE_DIR}/src/common/
+    )
+
+#output format
+ADD_EXECUTABLE(${TARGET_WAE_TEST} ${TARGET_WAE_TEST_SOURCES})
+
+#linker directories
+TARGET_LINK_LIBRARIES(${TARGET_WAE_TEST}
+    ${TARGET_WAE_DEP_LIBRARIES}
+    dpl-test-framework
+    tests-common
+    )
+
+#place for output file
+INSTALL(TARGETS ${TARGET_WAE_TEST}
+    DESTINATION /usr/bin
+    PERMISSIONS OWNER_READ
+                OWNER_WRITE
+                OWNER_EXECUTE
+                GROUP_READ
+                GROUP_EXECUTE
+                WORLD_READ
+                WORLD_EXECUTE
+    )
+
diff --git a/src/libwebappenc-tests/libwebappenc-tests.cpp b/src/libwebappenc-tests/libwebappenc-tests.cpp
new file mode 100644 (file)
index 0000000..83a8f25
--- /dev/null
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2014 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        libwebappenc-tests.cpp
+ * @author      Dongsun Lee (ds73.lee@samsung.com)
+ * @version     1.0
+ * @brief       libwebappenc test runer
+ */
+#include <dpl/test/test_runner.h>
+
+int main (int argc, char *argv[])
+{
+    int status = DPL::Test::TestRunnerSingleton::Instance().ExecTestRunner(argc, argv);
+    return status;
+}
+
diff --git a/src/libwebappenc-tests/test_cases.cpp b/src/libwebappenc-tests/test_cases.cpp
new file mode 100644 (file)
index 0000000..c6c26a2
--- /dev/null
@@ -0,0 +1,222 @@
+/*
+ * 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);
+}