View Javadoc
1   /*
2    * This file is part of dependency-check-core.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   * Copyright (c) 2012 Jeremy Long. All Rights Reserved.
17   */
18  package org.owasp.dependencycheck.utils;
19  
20  import java.io.File;
21  import java.io.IOException;
22  import java.security.NoSuchAlgorithmException;
23  
24  import org.junit.Assert;
25  import org.junit.Rule;
26  import org.junit.Test;
27  import org.junit.rules.ExpectedException;
28  import org.owasp.dependencycheck.utils.Checksum;
29  
30  /**
31   *
32   * @author Jeremy Long
33   */
34  public class ChecksumTest {
35  
36      @Rule
37      public ExpectedException expectedException = ExpectedException.none();
38  
39      /**
40       * Test of getChecksum method, of class Checksum.
41       *
42       * @throws Exception thrown when an exception occurs.
43       */
44      @Test
45      public void testGetChecksum() throws Exception {
46          String algorithm = "MD5";
47          File file = new File(this.getClass().getClassLoader().getResource("checkSumTest.file").toURI().getPath());
48          byte[] expResult = {-16, -111, 92, 95, 70, -72, -49, -94, -125, -27, -83, 103, -96, -101, 55, -109};
49          byte[] result = Checksum.getChecksum(algorithm, file);
50          boolean arraysAreEqual = true;
51          if (expResult.length == result.length) {
52              for (int i = 0; arraysAreEqual && i < result.length; i++) {
53                  arraysAreEqual = result[i] == expResult[i];
54              }
55          } else {
56              Assert.fail("Checksum results do not match expected results.");
57          }
58          Assert.assertTrue(arraysAreEqual);
59      }
60  
61      /**
62       * Test of getChecksum method, of class Checksum. This checks that an exception is thrown when an invalid path is specified.
63       *
64       * @throws Exception is thrown when an exception occurs.
65       */
66      @Test
67      public void testGetChecksum_FileNotFound() throws Exception {
68          String algorithm = "MD5";
69          File file = new File("not a valid file");
70  
71          expectedException.expect(IOException.class);
72          Checksum.getChecksum(algorithm, file);
73      }
74  
75      /**
76       * Test of getChecksum method, of class Checksum. This checks that an exception is thrown when an invalid algorithm is
77       * specified.
78       *
79       * @throws Exception is thrown when an exception occurs.
80       */
81      @Test
82      public void testGetChecksum_NoSuchAlgorithm() throws Exception {
83          String algorithm = "some unknown algorithm";
84          File file = new File(this.getClass().getClassLoader().getResource("checkSumTest.file").getPath());
85  
86          expectedException.expect(NoSuchAlgorithmException.class);
87          Checksum.getChecksum(algorithm, file);
88      }
89  
90      /**
91       * Test of getMD5Checksum method, of class Checksum.
92       *
93       * @throws Exception is thrown when an exception occurs.
94       */
95      @Test
96      public void testGetMD5Checksum() throws Exception {
97          File file = new File(this.getClass().getClassLoader().getResource("checkSumTest.file").toURI().getPath());
98          //String expResult = "F0915C5F46B8CFA283E5AD67A09B3793";
99          String expResult = "f0915c5f46b8cfa283e5ad67a09b3793";
100         String result = Checksum.getMD5Checksum(file);
101         Assert.assertEquals(expResult, result);
102     }
103 
104     /**
105      * Test of getSHA1Checksum method, of class Checksum.
106      *
107      * @throws Exception is thrown when an exception occurs.
108      */
109     @Test
110     public void testGetSHA1Checksum() throws Exception {
111         File file = new File(this.getClass().getClassLoader().getResource("checkSumTest.file").toURI().getPath());
112         //String expResult = "B8A9FF28B21BCB1D0B50E24A5243D8B51766851A";
113         String expResult = "b8a9ff28b21bcb1d0b50e24a5243d8b51766851a";
114         String result = Checksum.getSHA1Checksum(file);
115         Assert.assertEquals(expResult, result);
116     }
117 
118     /**
119      * Test of getHex method, of class Checksum.
120      */
121     @Test
122     public void testGetHex() {
123         byte[] raw = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
124         //String expResult = "000102030405060708090A0B0C0D0E0F10";
125         String expResult = "000102030405060708090a0b0c0d0e0f10";
126         String result = Checksum.getHex(raw);
127         Assert.assertEquals(expResult, result);
128     }
129 }