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.shared.jarsigner;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.util.Map;
24  import java.util.jar.Attributes;
25  import java.util.jar.JarFile;
26  import java.util.jar.Manifest;
27  
28  import org.junit.jupiter.api.Test;
29  
30  import static org.junit.jupiter.api.Assertions.assertEquals;
31  import static org.junit.jupiter.api.Assertions.assertFalse;
32  import static org.junit.jupiter.api.Assertions.assertTrue;
33  
34  /**
35   * Created on 11/8/13.
36   *
37   * @author Tony Chemit
38   * @since 1.1
39   */
40  class JarSignerUtilTest extends AbstractJarSignerTest {
41  
42      @Test
43      void testUnsignArchive() throws Exception {
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          assertEquals(originalCleanManifest, 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://issues.apache.org/jira/browse/MSHARED-314
64          Manifest manifest = readManifest(target);
65  
66          Manifest cleanManifest = JarSignerUtil.buildUnsignedManifest(manifest);
67          assertFalse(containsDigest(cleanManifest));
68  
69          assertEquals(manifest, cleanManifest);
70          assertEquals(manifest, originalCleanManifest);
71      }
72  
73      private Manifest readManifest(File file) throws IOException {
74          JarFile jarFile = new JarFile(file);
75  
76          Manifest manifest = jarFile.getManifest();
77  
78          jarFile.close();
79  
80          return manifest;
81      }
82  
83      private boolean containsDigest(Manifest manifest) {
84          for (Map.Entry<String, Attributes> entry : manifest.getEntries().entrySet()) {
85              Attributes attr = entry.getValue();
86  
87              for (Map.Entry<Object, Object> objectEntry : attr.entrySet()) {
88                  String attributeKey = String.valueOf(objectEntry.getKey());
89                  if (attributeKey.endsWith("-Digest")) {
90                      return true;
91                  }
92              }
93          }
94          return false;
95      }
96  }