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