View Javadoc

    1   /*
    2    * This file is part of dependency-check-core.
    3    *
    4    * Dependency-check-core is free software: you can redistribute it and/or modify it
    5    * under the terms of the GNU General Public License as published by the Free
    6    * Software Foundation, either version 3 of the License, or (at your option) any
    7    * later version.
    8    *
    9    * Dependency-check-core is distributed in the hope that it will be useful, but
    10   * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11   * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
    12   * details.
    13   *
    14   * You should have received a copy of the GNU General Public License along with
    15   * dependency-check-core. If not, see http://www.gnu.org/licenses/.
    16   *
    17   * Copyright (c) 2012 Jeremy Long. All Rights Reserved.
    18   */
    19  package org.owasp.dependencycheck.utils;
    20
    21  import org.owasp.dependencycheck.utils.Checksum;
    22  import java.io.File;
    23  import java.io.IOException;
    24  import java.security.NoSuchAlgorithmException;
    25  import org.junit.After;
    26  import org.junit.AfterClass;
    27  import org.junit.Assert;
    28  import org.junit.Before;
    29  import org.junit.BeforeClass;
    30  import org.junit.Test;
    31
    32  /**
    33   *
    34   * @author Jeremy Long (jeremy.long@owasp.org)
    35   */
    36  public class ChecksumTest {
    37
    38      @BeforeClass
    39      public static void setUpClass() throws Exception {
    40      }
    41
    42      @AfterClass
    43      public static void tearDownClass() throws Exception {
    44      }
    45
    46      @Before
    47      public void setUp() throws Exception {
    48      }
    49
    50      @After
    51      public void tearDown() throws Exception {
    52      }
    53
    54      /**
    55       * Test of getChecksum method, of class Checksum.
    56       *
    57       * @throws Exception thrown when an exception occurs.
    58       */
    59      @Test
    60      public void testGetChecksum() throws Exception {
    61          String algorithm = "MD5";
    62          File file = new File(this.getClass().getClassLoader().getResource("checkSumTest.file").getPath());
    63          byte[] expResult = {-16, -111, 92, 95, 70, -72, -49, -94, -125, -27, -83, 103, -96, -101, 55, -109};
    64          byte[] result = Checksum.getChecksum(algorithm, file);
    65          boolean arraysAreEqual = true;
    66          if (expResult.length == result.length) {
    67              for (int i = 0; arraysAreEqual && i < result.length; i++) {
    68                  arraysAreEqual = result[i] == expResult[i];
    69              }
    70          } else {
    71              Assert.fail("Checksum results do not match expected results.");
    72          }
    73          Assert.assertTrue(arraysAreEqual);
    74      }
    75
    76      /**
    77       * Test of getChecksum method, of class Checksum. This checks that an
    78       * exception is thrown when an invalid path is specified.
    79       *
    80       * @throws Exception is thrown when an exception occurs.
    81       */
    82      @Test
    83      public void testGetChecksum_FileNotFound() throws Exception {
    84          String algorithm = "MD5";
    85          File file = new File("not a valid file");
    86          boolean exceptionThrown = false;
    87          try {
    88              byte[] result = Checksum.getChecksum(algorithm, file);
    89          } catch (IOException ex) {
    90              exceptionThrown = true;
    91          }
    92          Assert.assertTrue(exceptionThrown);
    93      }
    94
    95      /**
    96       * Test of getChecksum method, of class Checksum. This checks that an
    97       * exception is thrown when an invalid algorithm is specified.
    98       *
    99       * @throws Exception is thrown when an exception occurs.
    100      */
    101     @Test
    102     public void testGetChecksum_NoSuchAlgorithm() throws Exception {
    103         String algorithm = "some unknown algorithm";
    104         File file = new File(this.getClass().getClassLoader().getResource("checkSumTest.file").getPath());
    105         boolean exceptionThrown = false;
    106         try {
    107             byte[] result = Checksum.getChecksum(algorithm, file);
    108         } catch (NoSuchAlgorithmException ex) {
    109             exceptionThrown = true;
    110         }
    111         Assert.assertTrue(exceptionThrown);
    112     }
    113
    114     /**
    115      * Test of getMD5Checksum method, of class Checksum.
    116      *
    117      * @throws Exception is thrown when an exception occurs.
    118      */
    119     @Test
    120     public void testGetMD5Checksum() throws Exception {
    121         File file = new File(this.getClass().getClassLoader().getResource("checkSumTest.file").getPath());
    122         String expResult = "F0915C5F46B8CFA283E5AD67A09B3793";
    123         String result = Checksum.getMD5Checksum(file);
    124         Assert.assertEquals(expResult, result);
    125     }
    126
    127     /**
    128      * Test of getSHA1Checksum method, of class Checksum.
    129      *
    130      * @throws Exception is thrown when an exception occurs.
    131      */
    132     @Test
    133     public void testGetSHA1Checksum() throws Exception {
    134         File file = new File(this.getClass().getClassLoader().getResource("checkSumTest.file").getPath());
    135         String expResult = "B8A9FF28B21BCB1D0B50E24A5243D8B51766851A";
    136         String result = Checksum.getSHA1Checksum(file);
    137         Assert.assertEquals(expResult, result);
    138     }
    139
    140     /**
    141      * Test of getHex method, of class Checksum.
    142      */
    143     @Test
    144     public void testGetHex() {
    145         byte[] raw = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
    146         String expResult = "000102030405060708090A0B0C0D0E0F10";
    147         String result = Checksum.getHex(raw);
    148         Assert.assertEquals(expResult, result);
    149     }
    150 }