View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.enforcer.rules.checksum;
20  
21  import java.io.File;
22  import java.io.IOException;
23  
24  import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
25  import org.codehaus.plexus.util.FileUtils;
26  import org.junit.jupiter.api.Test;
27  import org.junit.jupiter.api.io.TempDir;
28  
29  import static org.junit.jupiter.api.Assertions.assertThrows;
30  import static org.junit.jupiter.api.Assertions.assertTrue;
31  
32  /**
33   * Test the "RequireFileChecksum" rule
34   *
35   * @author Lyubomyr Shaydariv
36   */
37  class TestRequireFileChecksum {
38  
39      private final RequireFileChecksum rule = new RequireFileChecksum();
40  
41      @TempDir
42      public File temporaryFolder;
43  
44      @Test
45      void testFileChecksumMd5() throws IOException, EnforcerRuleException {
46          File f = File.createTempFile("junit", null, temporaryFolder);
47          FileUtils.fileWrite(f, "message");
48  
49          rule.setFile(f);
50          rule.setChecksum("78e731027d8fd50ed642340b7c9a63b3");
51          rule.setType("md5");
52  
53          rule.execute();
54      }
55  
56      @Test
57      void testFileChecksumMd5UpperCase() throws IOException, EnforcerRuleException {
58          File f = File.createTempFile("junit", null, temporaryFolder);
59          FileUtils.fileWrite(f, "message");
60  
61          rule.setFile(f);
62          rule.setChecksum("78E731027D8FD50ED642340B7C9A63B3");
63          rule.setType("md5");
64  
65          rule.execute();
66      }
67  
68      @Test
69      void testFileChecksumMd5GivenFileDoesNotExistFailure() {
70          File f = new File("nonExistent");
71          Throwable exception = assertThrows(EnforcerRuleException.class, () -> {
72              rule.setFile(f);
73              rule.setChecksum("78e731027d8fd50ed642340b7c9a63b3");
74              rule.setType("md5");
75  
76              rule.execute();
77          });
78          assertTrue(exception.getMessage().contains("File does not exist: " + f.getAbsolutePath()));
79      }
80  
81      @Test
82      void testFileChecksumMd5GivenFileDoesNotExistFailureWithMessage() {
83          File f = new File("nonExistent");
84          String configuredMessage = "testMessageFileDoesNotExist";
85          Throwable exception = assertThrows(EnforcerRuleException.class, () -> {
86              rule.setFile(f);
87              rule.setChecksum("78e731027d8fd50ed642340b7c9a63b3");
88              rule.setType("md5");
89              rule.setNonexistentFileMessage(configuredMessage);
90  
91              rule.execute();
92          });
93          assertTrue(exception.getMessage().contains(configuredMessage));
94      }
95  
96      @Test
97      void testFileChecksumMd5GivenFileIsNotReadableFailure() throws IOException {
98          File t = File.createTempFile("junit", null, temporaryFolder);
99          File f = new File(t.getAbsolutePath()) {
100             private static final long serialVersionUID = 6987790643999338089L;
101 
102             @Override
103             public boolean canRead() {
104                 return false;
105             }
106         };
107         Throwable exception = assertThrows(EnforcerRuleException.class, () -> {
108             rule.setFile(f);
109             rule.setChecksum("78e731027d8fd50ed642340b7c9a63b3");
110             rule.setType("md5");
111 
112             rule.execute();
113         });
114         assertTrue(exception.getMessage().contains("Cannot read file: " + f.getAbsolutePath()));
115     }
116 
117     @Test
118     void testFileChecksumMd5GivenFileIsADirectoryFailure() {
119         File f = temporaryFolder;
120         Throwable exception = assertThrows(EnforcerRuleException.class, () -> {
121             rule.setFile(f);
122             rule.setChecksum("78e731027d8fd50ed642340b7c9a63b3");
123             rule.setType("md5");
124 
125             rule.execute();
126         });
127         assertTrue(
128                 exception.getMessage().contains("Cannot calculate the checksum of directory: " + f.getAbsolutePath()));
129     }
130 
131     @Test
132     void testFileChecksumMd5NoFileSpecifiedFailure() {
133         Throwable exception = assertThrows(EnforcerRuleException.class, () -> {
134             rule.setChecksum("78e731027d8fd50ed642340b7c9a63b3");
135             rule.setType("md5");
136 
137             rule.execute();
138         });
139         assertTrue(exception.getMessage().contains("Input file unspecified"));
140     }
141 
142     @Test
143     void testFileChecksumMd5NoChecksumSpecifiedFailure() {
144         Throwable exception = assertThrows(EnforcerRuleException.class, () -> {
145             File f = File.createTempFile("junit", null, temporaryFolder);
146 
147             rule.setFile(f);
148             rule.setType("md5");
149 
150             rule.execute();
151         });
152         assertTrue(exception.getMessage().contains("Checksum unspecified"));
153     }
154 
155     @Test
156     void testFileChecksumMd5NoTypeSpecifiedFailure() {
157         Throwable exception = assertThrows(EnforcerRuleException.class, () -> {
158             File f = File.createTempFile("junit", null, temporaryFolder);
159 
160             rule.setFile(f);
161             rule.setChecksum("78e731027d8fd50ed642340b7c9a63b3");
162 
163             rule.execute();
164         });
165         assertTrue(exception.getMessage().contains("Hash type unspecified"));
166     }
167 
168     @Test
169     void testFileChecksumMd5ChecksumMismatchFailure() throws IOException {
170         File f = File.createTempFile("junit", null, temporaryFolder);
171         Throwable exception = assertThrows(EnforcerRuleException.class, () -> {
172             FileUtils.fileWrite(f, "message");
173 
174             rule.setFile(f);
175             rule.setChecksum("ffeeddccbbaa99887766554433221100");
176             rule.setType("md5");
177 
178             rule.execute();
179         });
180         assertTrue(exception
181                 .getMessage()
182                 .contains("md5 hash of " + f.getAbsolutePath()
183                         + " was 78e731027d8fd50ed642340b7c9a63b3 but expected ffeeddccbbaa99887766554433221100"));
184     }
185 
186     @Test
187     void testFileChecksumMd5ChecksumMismatchFailureWithMessage() {
188         String configuredMessage = "testMessage";
189         Throwable exception = assertThrows(EnforcerRuleException.class, () -> {
190             File f = File.createTempFile("junit", null, temporaryFolder);
191             FileUtils.fileWrite(f, "message");
192 
193             rule.setFile(f);
194             rule.setChecksum("ffeeddccbbaa99887766554433221100");
195             rule.setType("md5");
196             rule.setMessage(configuredMessage);
197 
198             rule.execute();
199         });
200         assertTrue(exception.getMessage().contains(configuredMessage));
201     }
202 
203     @Test
204     void testFileChecksumSha1() throws IOException, EnforcerRuleException {
205         File f = File.createTempFile("junit", null, temporaryFolder);
206         FileUtils.fileWrite(f, "message");
207 
208         rule.setFile(f);
209         rule.setChecksum("6f9b9af3cd6e8b8a73c2cdced37fe9f59226e27d");
210         rule.setType("sha1");
211 
212         rule.execute();
213     }
214 
215     @Test
216     void testFileChecksumSha256() throws IOException, EnforcerRuleException {
217         File f = File.createTempFile("junit", null, temporaryFolder);
218         FileUtils.fileWrite(f, "message");
219 
220         rule.setFile(f);
221         rule.setChecksum("ab530a13e45914982b79f9b7e3fba994cfd1f3fb22f71cea1afbf02b460c6d1d");
222         rule.setType("sha256");
223 
224         rule.execute();
225     }
226 
227     @Test
228     void testFileChecksumSha384() throws IOException, EnforcerRuleException {
229         File f = File.createTempFile("junit", null, temporaryFolder);
230         FileUtils.fileWrite(f, "message");
231 
232         rule.setFile(f);
233         rule.setChecksum(
234                 "353eb7516a27ef92e96d1a319712d84b902eaa828819e53a8b09af7028103a9978ba8feb6161e33c3619c5da4c4666a5");
235         rule.setType("sha384");
236 
237         rule.execute();
238     }
239 
240     @Test
241     void testFileChecksumSha512() throws IOException, EnforcerRuleException {
242         File f = File.createTempFile("junit", null, temporaryFolder);
243         FileUtils.fileWrite(f, "message");
244 
245         rule.setFile(f);
246         rule.setChecksum(
247                 "f8daf57a3347cc4d6b9d575b31fe6077e2cb487f60a96233c08cb479dbf31538cc915ec6d48bdbaa96ddc1a16db4f4f96f37276cfcb3510b8246241770d5952c");
248         rule.setType("sha512");
249 
250         rule.execute();
251     }
252 }