--- /dev/null
+/*
+ * Common
+ *
+ * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ * Jihoon Song <jihoon80.song@samsung.com>
+ * Hyeongseok Heo <hyeongseok.heo@samsung.com>
+ * BonYong Lee <bonyong.lee@samsung.com>
+ *
+ * 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.
+ *
+ * Contributors:
+ * - S-Core Co., Ltd
+ *
+ */
+
+package org.tizen.common.sign;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.io.File;
+import java.io.IOException;
+
+import hashsign.HashingSigning;
+
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.tizen.common.util.FileUtil;
+import org.tizen.common.util.OSChecker;
+
+
+/**
+ * Test case for {@link HashingSigning}
+ *
+ * @author JIhoon Song {@literal<jihoon80.song@samsung.com>}
+ */
+public class HashingSigningTest {
+
+ protected final Logger logger = LoggerFactory.getLogger( getClass() );
+
+ public final String TEST_DIR = "test/test_files";
+ public final String NONEXISTFOLDER = "test/test_nonexist";
+ public final String TEST_SPECIAL_DIR;
+
+ public final String TEST_FILE = "test.txt";
+
+ public final String AUTHOR_SIGNATURE = "author-signature.xml";
+ public final String DISTRIBUTOR1_SIGNATURE = "signature1.xml";
+ public final String DISTRIBUTOR2_SIGNATURE = "signature2.xml";
+
+ public final String P12_KEY = "key/samsung.devmode.sdk.cert.p12";
+ public final String P12_PASSWORD = "1234";
+
+
+ // Constructor
+ public HashingSigningTest() {
+ // Not allowed special characters :
+ // -- Linux : \\ $ ` : #
+ // -- Windows : \\ * ? " < > | $ ` : #
+
+ if ( OSChecker.isWindows() ) {
+ TEST_SPECIAL_DIR = "test/~!@%^&()_+-=[]{};',.";
+ } else {
+ TEST_SPECIAL_DIR = "test/~!@%^&*()_+|-=[]{};'<>?,.";
+ }
+ }
+
+ protected void createTestFile(String appDirPath) throws IOException {
+ if ( appDirPath == null ) {
+ return;
+ }
+
+ // make directory
+ File appDir = new File( appDirPath );
+ if ( !appDir.exists() ) {
+ appDir.mkdirs();
+ }
+
+ // create test source
+ String srcTextFile = FileUtil.appendPath( appDirPath, TEST_FILE );
+ FileUtil.createTextFile( new File( srcTextFile ), TEST_FILE, null );
+ }
+
+ protected void removeTestFile(String appDirPath) {
+ if ( appDirPath == null ) {
+ return;
+ }
+
+ File appDir = new File( appDirPath );
+ if ( appDir.exists() ) {
+ FileUtil.recursiveDelete( appDir );
+ }
+ }
+
+ protected void safeRemoveFile(String path) {
+ if ( path == null ) {
+ return;
+ }
+
+ File file = new File( path );
+ if ( file.exists() ) {
+ file.delete();
+ }
+ }
+
+ /**
+ * Test {@link HashingSigning#AuthorSignature(String, String,String, String, String)}
+ *
+ * @throws Exception in case of failure in test
+ *
+ * @see HashingSigning#AuthorSignature(String, String,String, String, String)
+ */
+ @Test
+ public void test_signAuthor() throws IOException {
+ final Object[][] TEST_CASES = new Object[][] {
+ new Object[] { null, null, null, null, null, IllegalStateException.class, null }, // null app path test
+ new Object[] { NONEXISTFOLDER, null, null, null, null, null, null }, // non-exist app path test
+ new Object[] { TEST_DIR, null, null, null, null, IllegalStateException.class, null }, // null p12 path test
+ new Object[] { TEST_DIR, "aaa", null, null, null, null, null }, // wrong p12 path test : silence fail
+ new Object[] { TEST_DIR, P12_KEY, null, null, null, null, null }, // null password test : silence fail with NPE stack trace
+ new Object[] { TEST_DIR, P12_KEY, "wrong", null, null, IOException.class, null }, // wrong password
+ new Object[] { TEST_DIR, P12_KEY, P12_PASSWORD, null, null, null, AUTHOR_SIGNATURE },
+ new Object[] { TEST_SPECIAL_DIR, P12_KEY, P12_PASSWORD, null, null, null, AUTHOR_SIGNATURE },
+ new Object[] { "test/한글", P12_KEY, P12_PASSWORD, null, null, null, AUTHOR_SIGNATURE },
+ };
+
+ int test_sequence = 0;
+ for ( final Object[] TEST_CASE : TEST_CASES ) {
+ final String appDirPath = (String) TEST_CASE[0];
+ final String pkContentFilePath = (String) TEST_CASE[1];
+ final String pkContentFilePasswd = (String) TEST_CASE[2];
+ final String caCertPath = (String) TEST_CASE[3];
+ final String rootCertPath = (String) TEST_CASE[4];
+ final Object expectedException = TEST_CASE[5];
+ final String expectedFile = (String) TEST_CASE[6];
+
+ test_sequence++;
+
+ // create test directory & source
+ if ( !NONEXISTFOLDER.equals( appDirPath ) ) {
+ createTestFile( appDirPath );
+ }
+
+ // get signature file path
+ String expectedFilePath = null;
+ if ( expectedFile != null ) {
+ expectedFilePath = FileUtil.appendPath( appDirPath, expectedFile );
+ }
+
+ try {
+ logger.info( test_sequence + "th test start" );
+
+ // run test function
+ HashingSigning.AuthorSignature( appDirPath, pkContentFilePath, pkContentFilePasswd, caCertPath, rootCertPath );
+
+ // in case of an exception
+ if ( expectedException != null ) {
+ fail( test_sequence + "th HashingSigning.AuthorSignature() must be throw exception" );
+ }
+
+ // no exception
+ if ( expectedFile != null ) {
+ // check a created signature file
+ assertTrue( test_sequence + "th Expected file exist : " + expectedFilePath, FileUtil.isExist( expectedFilePath ) );
+ } else {
+ // not created signature file, but an exception is not occurred.
+ // will print exception stack traces in the console
+ }
+ } catch (Exception e) {
+ assertEquals( test_sequence + "th a kind of exceptions : ", expectedException, e.getClass() );
+ } finally {
+ // crear a created signature file
+ safeRemoveFile( expectedFile );
+
+ // clear test directory & source
+ removeTestFile( appDirPath );
+ }
+ }
+ }
+
+ /**
+ * Test {@link HashingSigning#DistributorSignature(String, String,String, String, String, int)}
+ *
+ * @throws Exception in case of failure in test
+ *
+ * @see HashingSigning#DistributorSignature(String, String,String, String, String, int)
+ */
+ @Test
+ public void test_signDistributor() throws IOException {
+ final Object[][] TEST_CASES = new Object[][] {
+ new Object[] { null, null, null, null, null, 1, IllegalStateException.class, null }, // null app path test
+ new Object[] { NONEXISTFOLDER, null, null, null, null, 1, null, null }, // non-exist app path test
+ new Object[] { TEST_DIR, null, null, null, null, 1, IllegalStateException.class, null }, // null p12 path test
+ new Object[] { TEST_DIR, "aaa", null, null, null, 1, null, null }, // wrong p12 path test : silence fail
+ new Object[] { TEST_DIR, P12_KEY, null, null, null, 1, null, null }, // null password test : silence fail with NPE stack trace
+ new Object[] { TEST_DIR, P12_KEY, "wrong", null, null, 1, IOException.class, null }, // wrong password
+ new Object[] { TEST_DIR, P12_KEY, P12_PASSWORD, null, null, 1, null, DISTRIBUTOR1_SIGNATURE },
+ new Object[] { TEST_SPECIAL_DIR, P12_KEY, P12_PASSWORD, null, null, 1, null, DISTRIBUTOR1_SIGNATURE },
+ new Object[] { "test/한글", P12_KEY, P12_PASSWORD, null, null, 1, null, DISTRIBUTOR1_SIGNATURE },
+ new Object[] { TEST_DIR, P12_KEY, P12_PASSWORD, null, null, 2, null, DISTRIBUTOR2_SIGNATURE },
+ new Object[] { TEST_SPECIAL_DIR, P12_KEY, P12_PASSWORD, null, null, 2, null, DISTRIBUTOR2_SIGNATURE },
+ new Object[] { "test/한글", P12_KEY, P12_PASSWORD, null, null, 2, null, DISTRIBUTOR2_SIGNATURE },
+ };
+
+ int test_sequence = 0;
+ for ( final Object[] TEST_CASE : TEST_CASES ) {
+ final String appDirPath = (String) TEST_CASE[0];
+ final String pkContentFilePath = (String) TEST_CASE[1];
+ final String pkContentFilePasswd = (String) TEST_CASE[2];
+ final String caCertPath = (String) TEST_CASE[3];
+ final String rootCertPath = (String) TEST_CASE[4];
+ final int distNumber = (Integer) TEST_CASE[5];
+ final Object expectedException = TEST_CASE[6];
+ final String expectedFile = (String) TEST_CASE[7];
+
+ test_sequence++;
+
+ // create test directory & source
+ if ( !NONEXISTFOLDER.equals( appDirPath ) ) {
+ createTestFile( appDirPath );
+ }
+
+ // get signature file path
+ String expectedFilePath = null;
+ if ( expectedFile != null ) {
+ expectedFilePath = FileUtil.appendPath( appDirPath, expectedFile );
+ }
+
+ try {
+ logger.info( test_sequence + "th test start" );
+
+ // run test function
+ HashingSigning.DistributorSignature(appDirPath, pkContentFilePath, pkContentFilePasswd, caCertPath, rootCertPath, distNumber );
+
+ // in case of an exception
+ if ( expectedException != null ) {
+ fail( test_sequence + "th HashingSigning.AuthorSignature() must be throw exception" );
+ }
+
+ // no exception
+ if ( expectedFile != null ) {
+ // check a created signature file
+ assertTrue( test_sequence + "th Expected file exist : " + expectedFilePath, FileUtil.isExist( expectedFilePath ) );
+ } else {
+ // not created signature file, but an exception is not occurred.
+ // will print exception stack traces in the console
+ }
+ } catch (Exception e) {
+ // test exception
+ assertEquals( test_sequence + "th a kind of exceptions : ", expectedException, e.getClass() );
+ } finally {
+ // crear a created signature file
+ safeRemoveFile( expectedFile );
+
+ // clear test directory & source
+ removeTestFile( appDirPath );
+ }
+ }
+ }
+}