/* ======================================================================
   Parts Copyright 2006 University of Leeds, Oxford University, University of the Highlands and Islands.

   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.

====================================================================== */

package org.bodington.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

import junit.framework.TestCase;

public class VirusScannerTest extends TestCase
{
    
    //ClamAV Test
    static String exploit1 = "2443456c6961636d615472455354755363696b67736e24465245452d544553542d5349474e415455524524454545454524";
    
    static String safe1 = "This should not be a virus";
    
    /*
     * Test method for 'org.bodington.util.VirusScanner.isOk(File)'
     */
    public void testIsOk() throws Exception
    {

        VirusScanner scanner = VirusScanner.getInstance();
        File file = File.createTempFile("test", "virus");
        file.deleteOnExit();
        
        OutputStream output = new FileOutputStream( file );
        output.write(convertToBytes(exploit1));
        output.close();
        
        assertFalse(scanner.scan(file).isOk());
        
        output = new FileOutputStream( file );
        output.write(safe1.getBytes());
        output.close();
        
        assertTrue(scanner.scan(file).isOk());
        
    }
    
    /**
     * Used to convert HEX signatures into byte arrays for writing to a file.
     */ 
    private static byte[] convertToBytes(String input)
    {
        byte[] output = new byte[input.length()/2];
        
        for (int pos = 0; pos < input.length()-1; pos += 2)
        {
            String chunk = input.substring(pos, pos +2);
            output[pos/2] = (byte)(Integer.parseInt(chunk, 16));
        }
        
        return output;
    }

}
