View Javadoc
1   package org.apache.maven.shared.jarsigner;
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  import java.util.Map;
25  import java.util.jar.Attributes;
26  import java.util.jar.JarFile;
27  import java.util.jar.Manifest;
28  
29  /**
30   * Created on 11/8/13.
31   *
32   * @author Tony Chemit
33   * @since 1.1
34   */
35  public class JarSignerUtilTest
36      extends AbstractJarSignerTest
37  {
38  
39      // Fix MSHARED-277
40      public void testUnsignArchive()
41          throws Exception
42      {
43  
44          File target = prepareTestJar( "javax.persistence_2.0.5.v201212031355.jar" );
45  
46          assertTrue( JarSignerUtil.isArchiveSigned( target ) );
47  
48          // check that manifest contains some digest attributes
49          Manifest originalManifest = readManifest( target );
50          assertTrue( containsDigest( originalManifest ) );
51  
52          Manifest originalCleanManifest = JarSignerUtil.buildUnsignedManifest( originalManifest );
53          assertFalse( containsDigest( originalCleanManifest ) );
54  
55          assertTrue( originalCleanManifest.equals( JarSignerUtil.buildUnsignedManifest( originalCleanManifest ) ) );
56  
57          JarSignerUtil.unsignArchive( target );
58  
59          assertFalse( JarSignerUtil.isArchiveSigned( target ) );
60  
61          // check that manifest has no digest entry
62          // see https://jira.codehaus.org/browse/MSHARED-314
63          Manifest manifest = readManifest( target );
64  
65          Manifest cleanManifest = JarSignerUtil.buildUnsignedManifest( manifest );
66          assertFalse( containsDigest( cleanManifest ) );
67  
68          assertTrue( manifest.equals( cleanManifest ) );
69          assertTrue( manifest.equals( originalCleanManifest ) );
70  
71      }
72  
73      private Manifest readManifest( File file )
74          throws IOException
75      {
76          JarFile jarFile = new JarFile( file );
77  
78          Manifest manifest = jarFile.getManifest();
79  
80          jarFile.close();
81  
82          return manifest;
83      }
84  
85      private boolean containsDigest( Manifest manifest )
86      {
87          for ( Map.Entry<String, Attributes> entry : manifest.getEntries().entrySet() )
88          {
89              Attributes attr = entry.getValue();
90  
91              for ( Map.Entry<Object, Object> objectEntry : attr.entrySet() )
92              {
93                  String attributeKey = String.valueOf( objectEntry.getKey() );
94                  if ( attributeKey.endsWith( "-Digest" ) )
95                  {
96                      return true;
97                  }
98              }
99          }
100         return false;
101     }
102 }