/*
 * Copyright (c) 2008, Marc Kramis (Ph.D. Thesis), University of Konstanz
 * 
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 * 
 * $Id$
 */

package org.treetank.service;

import java.nio.ByteBuffer;
import java.util.Random;

import org.treetank.pagelayer.CryptoNativeImpl;
import org.treetank.pagelayer.ICrypto;

public class TestTreeTankService {

  /**
   * @param args
   */
  public static void main(String[] args) {

    try {

      System.loadLibrary("TreeTank");

      final ByteBuffer buffer = ByteBuffer.allocateDirect(32767);
      final ICrypto crypto = new CryptoNativeImpl();

      final Random r = new Random();
      final byte[] referenceBuffer = new byte[32767];
      r.nextBytes(referenceBuffer);
      buffer.put(referenceBuffer);

      test(crypto, (short) 30, buffer, referenceBuffer);
      test(crypto, (short) 32, buffer, referenceBuffer);
      test(crypto, (short) 188, buffer, referenceBuffer);
      test(crypto, (short) 1200, buffer, referenceBuffer);
      test(crypto, (short) 4932, buffer, referenceBuffer);
      test(crypto, (short) 8452, buffer, referenceBuffer);
      test(crypto, (short) 9000, buffer, referenceBuffer);

    } catch (Exception e) {
      System.out.println(": FAILURE: " + e.getMessage());
    }

  }

  private static final void test(
      final ICrypto crypto,
      final short length,
      final ByteBuffer buffer,
      final byte[] referenceBuffer) throws Exception {

    System.out.print("Test page length: " + length);

    final long start = System.currentTimeMillis();

    final short cryptLength = crypto.crypt(length, buffer);
    final short decryptLength = crypto.decrypt(cryptLength, buffer);

    final long stop = System.currentTimeMillis();

    System.out.print("..." + cryptLength + "..." + decryptLength);

    if (decryptLength != length) {
      throw new Exception("Bad result length: " + decryptLength);
    }

    buffer.rewind();
    buffer.position(24);
    for (int i = 24; i < decryptLength; i++) {
      if (buffer.get() != referenceBuffer[i]) {
        throw new Exception("Error: Byte does not match at " + i);
      }
    }

    buffer.rewind();
    buffer.put(referenceBuffer);

    System.out.println(": " + (stop - start) + "[ms]: SUCCESS.");

  }

}

@@ -906,16 +906,62 @@
 andom;%0A%0A
+import org.treetank.pagelayer.CryptoJavaImpl;%0A
 import o
@@ -1209,65 +1209,257 @@
     
-final ByteBuffer buffer = ByteBuffer.allocateDirect(32767
+System.out.println(%22--- Test Java Crypto: ----------------------------------%22);%0A      final ICrypto javaCrypto = new CryptoJavaImpl();%0A      testCrypto(javaCrypto);%0A%0A      System.out.println(%22--- Test Native Crypto: --------------------------------%22
 );%0A 
@@ -1473,25 +1473,31 @@
 nal ICrypto 
-c
+nativeC
 rypto = new 
@@ -1516,19 +1516,292 @@
 Impl();%0A
-%0A
   
+    testCrypto(nativeCrypto);%0A%0A    %7D catch (Exception e) %7B%0A      System.out.println(%22: FAILURE: %22 + e.getMessage());%0A    %7D%0A%0A  %7D%0A%0A  private static final void testCrypto(final ICrypto crypto) throws Exception %7B%0A    final ByteBuffer buffer = ByteBuffer.allocateDirect(32767);%0A%0A
     fina
@@ -1827,26 +1827,24 @@
 ndom();%0A    
- 
- 
 final byte%5B%5D
@@ -1879,18 +1879,16 @@
 32767%5D;%0A
-  
     r.ne
@@ -1917,18 +1917,16 @@
 r);%0A    
-  
 buffer.p
@@ -1947,30 +1947,40 @@
 ffer);%0A%0A    
- 
- 
 test
+CryptDecrypt
 (crypto, (sh
@@ -2010,38 +2010,48 @@
 nceBuffer);%0A    
-  
 test
+CryptDecrypt
 (crypto, (short)
@@ -2077,38 +2077,48 @@
 nceBuffer);%0A    
-  
 test
+CryptDecrypt
 (crypto, (short)
@@ -2145,38 +2145,48 @@
 nceBuffer);%0A    
-  
 test
+CryptDecrypt
 (crypto, (short)
@@ -2214,38 +2214,48 @@
 nceBuffer);%0A    
-  
 test
+CryptDecrypt
 (crypto, (short)
@@ -2283,38 +2283,48 @@
 nceBuffer);%0A    
-  
 test
+CryptDecrypt
 (crypto, (short)
@@ -2360,22 +2360,32 @@
 r);%0A    
-  
 test
+CryptDecrypt
 (crypto,
@@ -2429,102 +2429,8 @@
 r);%0A
-%0A    %7D catch (Exception e) %7B%0A      System.out.println(%22: FAILURE: %22 + e.getMessage());%0A    %7D%0A%0A
   %7D%0A
@@ -2462,16 +2462,28 @@
 oid test
+CryptDecrypt
 (%0A      

Test now outputs both the java and native crypto results.