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.eclipse.aether.internal.impl.checksum;
20  
21  import java.util.Collections;
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import org.eclipse.aether.DefaultRepositorySystemSession;
26  import org.eclipse.aether.artifact.Artifact;
27  import org.eclipse.aether.artifact.DefaultArtifact;
28  import org.eclipse.aether.impl.RepositorySystemLifecycle;
29  import org.eclipse.aether.internal.impl.DefaultRepositorySystemLifecycle;
30  import org.eclipse.aether.internal.test.util.TestUtils;
31  import org.eclipse.aether.spi.checksums.TrustedChecksumsSource;
32  import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactory;
33  import org.junit.jupiter.api.BeforeEach;
34  import org.junit.jupiter.api.Test;
35  
36  import static org.junit.jupiter.api.Assertions.*;
37  import static org.junit.jupiter.api.Assumptions.*;
38  
39  public abstract class FileTrustedChecksumsSourceTestSupport {
40      private static final Artifact ARTIFACT_WITHOUT_CHECKSUM = new DefaultArtifact("test:test:1.0");
41  
42      private static final Artifact ARTIFACT_WITH_CHECKSUM = new DefaultArtifact("test:test:2.0");
43  
44      private static final String ARTIFACT_TRUSTED_CHECKSUM = "trustedChecksum";
45  
46      private DefaultRepositorySystemSession session;
47  
48      private ChecksumAlgorithmFactory checksumAlgorithmFactory;
49  
50      private RepositorySystemLifecycle repositorySystemLifecycle;
51  
52      private FileTrustedChecksumsSourceSupport subject;
53  
54      private boolean checksumWritten;
55  
56      @BeforeEach
57      void before() throws Exception {
58          session = TestUtils.newSession();
59          // populate local repository
60          checksumAlgorithmFactory = new Sha1ChecksumAlgorithmFactory();
61          repositorySystemLifecycle = new DefaultRepositorySystemLifecycle();
62          subject = prepareSubject(repositorySystemLifecycle);
63          checksumWritten = false;
64  
65          DefaultRepositorySystemSession prepareSession = new DefaultRepositorySystemSession(session);
66          enableSource(prepareSession);
67          TrustedChecksumsSource.Writer writer = subject.getTrustedArtifactChecksumsWriter(prepareSession);
68          if (writer != null) {
69              HashMap<String, String> checksums = new HashMap<>();
70              checksums.put(checksumAlgorithmFactory.getName(), ARTIFACT_TRUSTED_CHECKSUM);
71              writer.addTrustedArtifactChecksums(
72                      ARTIFACT_WITH_CHECKSUM,
73                      prepareSession.getLocalRepository(),
74                      Collections.singletonList(checksumAlgorithmFactory),
75                      checksums);
76              checksumWritten = true;
77          }
78      }
79  
80      protected abstract FileTrustedChecksumsSourceSupport prepareSubject(RepositorySystemLifecycle lifecycle);
81  
82      protected abstract void enableSource(DefaultRepositorySystemSession session);
83  
84      @Test
85      void notEnabled() {
86          assertNull(subject.getTrustedArtifactChecksums(
87                  session,
88                  ARTIFACT_WITH_CHECKSUM,
89                  session.getLocalRepository(),
90                  Collections.singletonList(checksumAlgorithmFactory)));
91      }
92  
93      @Test
94      void noProvidedArtifactChecksum() {
95          enableSource(session);
96          Map<String, String> providedChecksums = subject.getTrustedArtifactChecksums(
97                  session,
98                  ARTIFACT_WITHOUT_CHECKSUM,
99                  session.getLocalRepository(),
100                 Collections.singletonList(checksumAlgorithmFactory));
101         assertNotNull(providedChecksums);
102         assertTrue(providedChecksums.isEmpty());
103     }
104 
105     @Test
106     void haveProvidedArtifactChecksum() {
107         assumeTrue(checksumWritten);
108         enableSource(session);
109         Map<String, String> providedChecksums = subject.getTrustedArtifactChecksums(
110                 session,
111                 ARTIFACT_WITH_CHECKSUM,
112                 session.getLocalRepository(),
113                 Collections.singletonList(checksumAlgorithmFactory));
114         assertNotNull(providedChecksums);
115         assertFalse(providedChecksums.isEmpty());
116         assertEquals(1, providedChecksums.size());
117         assertEquals(providedChecksums.get(checksumAlgorithmFactory.getName()), ARTIFACT_TRUSTED_CHECKSUM);
118     }
119 }