/*
Copyright (c) 2013, Colorado State University
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

This software is provided by the copyright holders and contributors "as is" and
any express or implied warranties, including, but not limited to, the implied
warranties of merchantability and fitness for a particular purpose are
disclaimed. In no event shall the copyright holder or contributors be liable for
any direct, indirect, incidental, special, exemplary, or consequential damages
(including, but not limited to, procurement of substitute goods or services;
loss of use, data, or profits; or business interruption) however caused and on
any theory of liability, whether in contract, strict liability, or tort
(including negligence or otherwise) arising in any way out of the use of this
software, even if advised of the possibility of such damage.
*/

package galileo.test.net;

import java.io.IOException;

import java.util.Arrays;
import java.util.Random;

import galileo.event.EventType;
import galileo.event.GalileoEvent;
import galileo.serialization.SerializationException;
import galileo.serialization.SerializationInputStream;
import galileo.serialization.SerializationOutputStream;
import galileo.util.Checksum;

/**
 *
 * @author malensek
 */
public class HashTestEvent implements GalileoEvent {

    private byte[] data;
    private byte[] hash;

    private Random random = new Random();
    private Checksum check = new Checksum();

    public HashTestEvent(int size) {
        data = new byte[size];
        random.nextBytes(data);
        hash = check.hash(data);
    }

    public boolean verify() {
        if (Arrays.equals(hash, check.hash(data))) {
            return true;
        } else {
            //debug
            return false;
        }
    }

    @Override
    public EventType getType() {
        return EventType.DEBUG;
    }

    @Deserialize
    public HashTestEvent(SerializationInputStream in)
    throws IOException, SerializationException {
        hash = in.readField();
        data = in.readField();
    }

    @Override
    public void serialize(SerializationOutputStream out)
    throws IOException {
        out.writeField(hash);
        out.writeField(data);
    }
}

@@ -1368,16 +1368,45 @@
 ption;%0A%0A
+import java.math.BigInteger;%0A
 import j
@@ -1714,16 +1714,193 @@
 m;%0A%0A/**%0A
+ * An event for hash tests that includes a variable sized payload with its SHA1%0A * checksum to ensure the event was not corrupted while being transferred%0A * across the network.%0A
  *%0A * @a
@@ -2392,51 +2392,444 @@
     
-//debug%0A            return false;%0A        %7D
+BigInteger b1 = new BigInteger(1, hash);%0A            BigInteger b2 = new BigInteger(1, check.hash(data));%0A            System.out.println(b1.toString(16) + %22 =/= %22 + b2.toString(16));%0A            return false;%0A        %7D%0A    %7D%0A%0A    /**%0A     * Corrupt a HashTest event on purpose.  Sneaky sneaky...%0A     */%0A    public void corrupt() %7B%0A        int r = random.nextInt(data.length);%0A        byte b = data%5Br%5D;%0A        data%5Br%5D = (byte) (b + 1);
 %0A   

Add corrupt() method to manually corrupt events