Return-Path: X-Original-To: apmail-pdfbox-commits-archive@www.apache.org Delivered-To: apmail-pdfbox-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 5F97217C88 for ; Thu, 5 Feb 2015 19:27:22 +0000 (UTC) Received: (qmail 99529 invoked by uid 500); 5 Feb 2015 19:27:22 -0000 Delivered-To: apmail-pdfbox-commits-archive@pdfbox.apache.org Received: (qmail 99504 invoked by uid 500); 5 Feb 2015 19:27:22 -0000 Mailing-List: contact commits-help@pdfbox.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@pdfbox.apache.org Delivered-To: mailing list commits@pdfbox.apache.org Received: (qmail 99494 invoked by uid 99); 5 Feb 2015 19:27:22 -0000 Received: from eris.apache.org (HELO hades.apache.org) (140.211.11.105) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 05 Feb 2015 19:27:22 +0000 Received: from hades.apache.org (localhost [127.0.0.1]) by hades.apache.org (ASF Mail Server at hades.apache.org) with ESMTP id 1650CAC006D; Thu, 5 Feb 2015 19:27:22 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1657668 - /pdfbox/trunk/fontbox/src/test/java/org/apache/fontbox/cff/Type1FontUtilTest.java Date: Thu, 05 Feb 2015 19:27:22 -0000 To: commits@pdfbox.apache.org From: tilman@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20150205192722.1650CAC006D@hades.apache.org> Author: tilman Date: Thu Feb 5 19:27:21 2015 New Revision: 1657668 URL: http://svn.apache.org/r1657668 Log: PDFBOX-1978: deterministic and non-deterministic tests Modified: pdfbox/trunk/fontbox/src/test/java/org/apache/fontbox/cff/Type1FontUtilTest.java Modified: pdfbox/trunk/fontbox/src/test/java/org/apache/fontbox/cff/Type1FontUtilTest.java URL: http://svn.apache.org/viewvc/pdfbox/trunk/fontbox/src/test/java/org/apache/fontbox/cff/Type1FontUtilTest.java?rev=1657668&r1=1657667&r2=1657668&view=diff ============================================================================== --- pdfbox/trunk/fontbox/src/test/java/org/apache/fontbox/cff/Type1FontUtilTest.java (original) +++ pdfbox/trunk/fontbox/src/test/java/org/apache/fontbox/cff/Type1FontUtilTest.java Thu Feb 5 19:27:21 2015 @@ -16,74 +16,100 @@ */ package org.apache.fontbox.cff; -import static org.junit.Assert.assertArrayEquals; - import java.util.Random; -import org.junit.Test; +import junit.framework.TestCase; +import static org.junit.Assert.assertArrayEquals; +import org.junit.internal.ArrayComparisonFailure; /** * This class includes some tests for the Type1FontUtil class. - * + * * @author Villu Ruusmann * @version $Revision$ */ -public class Type1FontUtilTest +public class Type1FontUtilTest extends TestCase { + static final long DEFAULTSEED = 12345; + static final long LOOPS = 1000; /** * Tests the hex encoding/decoding. */ - @Test - public void hexEncoding() + public void testHexEncoding() { - byte[] bytes = randomBytes(128); + long seed = DEFAULTSEED; + tryHexEncoding(seed); + for (int i = 0; i < LOOPS; ++i) + { + tryHexEncoding(System.currentTimeMillis()); + } + } + + private void tryHexEncoding(long seed) throws ArrayComparisonFailure + { + byte[] bytes = createRandomByteArray(128, seed); String encodedBytes = Type1FontUtil.hexEncode(bytes); byte[] decodedBytes = Type1FontUtil.hexDecode(encodedBytes); - - assertArrayEquals(bytes, decodedBytes); + + assertArrayEquals("Seed: " + seed, bytes, decodedBytes); } /** * Tests the eexec encryption/decryption. */ - @Test - public void eexecEncryption() + public void testEexecEncryption() { - byte[] bytes = randomBytes(128); + long seed = DEFAULTSEED; + tryEexecEncryption(seed); + for (int i = 0; i < LOOPS; ++i) + { + tryEexecEncryption(System.currentTimeMillis()); + } + } + + private void tryEexecEncryption(long seed) throws ArrayComparisonFailure + { + byte[] bytes = createRandomByteArray(128, seed); byte[] encryptedBytes = Type1FontUtil.eexecEncrypt(bytes); byte[] decryptedBytes = Type1FontUtil.eexecDecrypt(encryptedBytes); - assertArrayEquals(bytes, decryptedBytes); + assertArrayEquals("Seed: " + seed, bytes, decryptedBytes); } /** * Tests the charstring encryption/decryption. */ - @Test - public void charstringEncryption() + public void testCharstringEncryption() + { + long seed = DEFAULTSEED; + tryCharstringEncryption(seed); + for (int i = 0; i < LOOPS; ++i) + { + tryCharstringEncryption(System.currentTimeMillis()); + } + } + + private void tryCharstringEncryption(long seed) throws ArrayComparisonFailure { - byte[] bytes = randomBytes(128); + byte[] bytes = createRandomByteArray(128, seed); byte[] encryptedBytes = Type1FontUtil.charstringEncrypt(bytes, 4); - byte[] decryptedBytes = Type1FontUtil.charstringDecrypt(encryptedBytes, - 4); + byte[] decryptedBytes = Type1FontUtil.charstringDecrypt(encryptedBytes, 4); - assertArrayEquals(bytes, decryptedBytes); + assertArrayEquals("Seed: " + seed, bytes, decryptedBytes); } - private static byte[] randomBytes(int length) + private static byte[] createRandomByteArray(int arrayLength, long seed) { - byte[] bytes = new byte[length]; + byte[] bytes = new byte[arrayLength]; + Random ramdom = new Random(seed); - for (int i = 0; i < length; i++) + for (int i = 0; i < arrayLength; i++) { - bytes[i] = (byte) RANDOM.nextInt(256); + bytes[i] = (byte) ramdom.nextInt(256); } - return bytes; } - - private static final Random RANDOM = new Random(); -} \ No newline at end of file +}