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