/*
 * 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) {
    
    System.out.print("Start test ...");

    try {

      System.loadLibrary("TreeTank");

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

      final Random r = new Random();
      final byte[] b = new byte[32767];
      r.nextBytes(b);

      final short length = 16574;
      buffer.put(b);

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

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

      if (decryptLength != length) {
        throw new Exception("Error: Length after decryption is wrong: "
            + decryptLength);
      }

      buffer.rewind();
      for (int i = 0; i < 32767; i++) {
        if (buffer.get() != b[i]) {
          throw new Exception("Error: Byte does not match at " + i);
        }
      }
      
      System.out.println(" SUCCESS.");

    } catch (Exception e) {
      e.printStackTrace();
    }

  }

}

@@ -1105,53 +1105,8 @@
 s) %7B
-%0A    %0A    System.out.print(%22Start test ...%22);
 %0A%0A  
@@ -1329,17 +1329,31 @@
  byte%5B%5D 
-b
+referenceBuffer
  = new b
@@ -1386,71 +1386,854 @@
 tes(
-b);%0A%0A      final short length = 16574;%0A      buffer.put(b);%0A%0A  
+referenceBuffer);%0A      buffer.put(referenceBuffer);%0A%0A      test(crypto, (short) 7, buffer, referenceBuffer);%0A      test(crypto, (short) 32, buffer, referenceBuffer);%0A      test(crypto, (short) 188, buffer, referenceBuffer);%0A      test(crypto, (short) 1200, buffer, referenceBuffer);%0A      test(crypto, (short) 4932, buffer, referenceBuffer);%0A      test(crypto, (short) 8452, buffer, referenceBuffer);%0A      test(crypto, (short) 10000, buffer, referenceBuffer);%0A%0A    %7D catch (Exception e) %7B%0A      System.out.println(%22FAILURE: %22 + e.getMessage());%0A    %7D%0A%0A  %7D%0A%0A  private static final void test(%0A      final ICrypto crypto,%0A      final short length,%0A      final ByteBuffer buffer,%0A      final byte%5B%5D referenceBuffer) throws Exception %7B%0A%0A    System.out.print(%22Test page length %22 + length + %22: %22);%0A%0A    final long start = System.currentTimeMillis();%0A%0A
     
@@ -2284,27 +2284,24 @@
 h, buffer);%0A
-%0A  
     final sh
@@ -2356,24 +2356,73 @@
 buffer);%0A%0A  
+  final long stop = System.currentTimeMillis();%0A%0A
     if (decr
@@ -2440,26 +2440,24 @@
 = length) %7B%0A
-  
       throw 
@@ -2524,18 +2524,16 @@
         
-  
 + decryp
@@ -2546,23 +2546,19 @@
 h);%0A    
- 
- 
 %7D%0A%0A
-  
     buff
@@ -2574,18 +2574,16 @@
 ();%0A    
- 
- 
 for (int
@@ -2594,21 +2594,29 @@
  0; i %3C 
-32767
+decryptLength
 ; i++) %7B
@@ -2612,26 +2612,24 @@
 gth; i++) %7B%0A
-  
       if (bu
@@ -2646,18 +2646,30 @@
  != 
-b
+referenceBuffer
 %5Bi%5D) %7B%0A
-  
     
@@ -2741,127 +2741,130 @@
     
-  
 %7D%0A    
- 
- 
 %7D
+%0A
 %0A    
+buffer.rewind();%0A
   
-%0A
   
-    System.out.println(%22 SUCCESS.%22);%0A%0A    %7D catch (Exception e) %7B%0A      e.printStackTrace();%0A    %7D
+buffer.put(referenceBuffer);%0A%0A    System.out.println((stop - start) + %22%5Bms%5D: SUCCESS.%22);
 %0A%0A  

Optimised test for several page lengths.