Skip to content

Commit

Permalink
Merge pull request #255 from s4u/multi-map
Browse files Browse the repository at this point in the history
test for load multiple keys map
  • Loading branch information
slawekjaranowski authored Mar 26, 2021
2 parents 1a683d4 + 3ec0561 commit cc29221
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/main/java/org/simplify4u/plugins/keysmap/KeysMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,10 @@ private static String stripComments(String line) {
private static ArtifactInfo createArtifactInfo(String strArtifact, String strKeys) {
return new ArtifactInfo(strArtifact.trim(), new KeyInfo(strKeys.trim()));
}

// for testing purpose

int size() {
return keysMapList.size();
}
}
5 changes: 3 additions & 2 deletions src/test/java/org/simplify4u/plugins/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static org.mockito.Mockito.when;

import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.util.encoders.Hex;
import org.mockito.Mockito;

/**
Expand All @@ -31,7 +32,7 @@ public abstract class TestUtils {

public static PGPPublicKey getPGPgpPublicKey(long keyID) {

BigInteger bigInteger = BigInteger.valueOf(0xffffffffL & keyID);
BigInteger bigInteger = BigInteger.valueOf(0xffff_ffffL & keyID);
BigInteger bigInteger2 = BigInteger.valueOf(keyID);

bigInteger = bigInteger.shiftLeft(64);
Expand All @@ -46,7 +47,7 @@ public static PGPPublicKey getPGPgpPublicKey(long keyID) {
bytes = Arrays.copyOfRange(bytes, 1, bytes.length);
}

PGPPublicKey pgpKey = mock(PGPPublicKey.class, Mockito.withSettings().lenient());
PGPPublicKey pgpKey = mock(PGPPublicKey.class, Mockito.withSettings().lenient().name(Hex.toHexString(bytes)));
when(pgpKey.getFingerprint()).thenReturn(bytes);
when(pgpKey.isMasterKey()).thenReturn(true);

Expand Down
134 changes: 134 additions & 0 deletions src/test/java/org/simplify4u/plugins/keysmap/KeysMapMultiTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
* Copyright 2021 Slawomir Jaranowski
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.simplify4u.plugins.keysmap;

import java.io.IOException;
import java.util.function.Function;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doAnswer;
import static org.simplify4u.plugins.TestArtifactBuilder.testArtifact;
import static org.simplify4u.plugins.TestUtils.getPGPgpPublicKey;

import org.apache.maven.artifact.Artifact;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.codehaus.plexus.resource.ResourceManager;
import org.codehaus.plexus.resource.loader.ResourceNotFoundException;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.testng.MockitoTestNGListener;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

@Listeners(MockitoTestNGListener.class)
public class KeysMapMultiTest {

private static final Function<String, Boolean> A_TRUE = v -> true;
@Mock
private ResourceManager resourceManager;

@InjectMocks
private KeysMap keysMap;

@BeforeMethod
public void setup() throws ResourceNotFoundException {
doAnswer(invocation -> getClass().getResourceAsStream(invocation.getArgument(0)))
.when(resourceManager).getResourceAsInputStream(anyString());
}

@Test
public void loadMultipleKeysMapShouldContainsAllItems() throws ResourceNotFoundException, IOException {

keysMap.load("/keysMapMulti1.list");
keysMap.load("/keysMapMulti2.list");

assertThat(keysMap.size()).isEqualTo(15);
}

@DataProvider
public static Object[][] artifactWithKeyToTest() {
return new Object[][]{
{testArtifact().build(),
getPGPgpPublicKey(0x1111_1111_1111_1111L)},

{testArtifact().build(),
getPGPgpPublicKey(0x1111_1111_2222_2222L)},

{testArtifact().build(),
getPGPgpPublicKey(0x1111_1111_3333_3333L)},

{testArtifact().artifactId("test1").build(),
getPGPgpPublicKey(0x1111_1111_4444_4444L)},

{testArtifact().build(),
getPGPgpPublicKey(0x2222_2222_1111_1111L)},

{testArtifact().build(),
getPGPgpPublicKey(0x2222_2222_2222_2222L)},

{testArtifact().build(),
getPGPgpPublicKey(0x2222_2222_3333_3333L)},

{testArtifact().groupId("test.group2").build(),
getPGPgpPublicKey(0x2222_2222_4444_4444L)},

{testArtifact().artifactId("bad-sig").build(),
getPGPgpPublicKey(0x2222_2222_5555_5555L)},

{testArtifact().groupId("test.group7").artifactId("test7").build(),
getPGPgpPublicKey(0x7777_7777_7777_7777L)}
};
}

@Test(dataProvider = "artifactWithKeyToTest")
public void keyShouldBeValid(Artifact artifact, PGPPublicKey key) throws ResourceNotFoundException, IOException {

keysMap.load("/keysMapMulti1.list");
keysMap.load("/keysMapMulti2.list");

assertThat(keysMap.isValidKey(artifact, key, null)).isTrue();
}

@DataProvider
public static Object[][] artifactWithNoSig() {
return new Object[][]{
{testArtifact().groupId("test.group-no-sig").artifactId("no-sig").build()},
{testArtifact().groupId("test.group-no-sig").artifactId("no-sig1").build()},
{testArtifact().groupId("test.group-no-sig").artifactId("no-sig2").build()}
};
}


@Test(dataProvider = "artifactWithNoSig")
public void noSigShouldBeFound(Artifact artifact) throws ResourceNotFoundException, IOException {
keysMap.load("/keysMapMulti1.list");
keysMap.load("/keysMapMulti2.list");

assertThat(keysMap.isNoSignature(artifact)).isTrue();
}

@Test
public void badSigShouldBeFound() throws ResourceNotFoundException, IOException {

keysMap.load("/keysMapMulti1.list");
keysMap.load("/keysMapMulti2.list");

assertThat(keysMap.isBrokenSignature(testArtifact().artifactId("bad-sig").build())).isTrue();
}
}
19 changes: 19 additions & 0 deletions src/test/resources/keysMapMulti1.list
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

# the same artifacts in both maps, different keys
test.group = 0x 1111 1111 1111 1111
test.group = 0x 1111 1111 2222 2222
test.group:test = 0x 1111 1111 3333 3333

# only in map 1
test.group:test1 = 0x 1111 1111 4444 4444

# the same item in both maps
test.group7:test7 = 0x 7777 7777 7777 7777

# the same item in both maps
test.group-no-sig:no-sig = noSig

# only in map 1
test.group-no-sig:no-sig1 = noSig

test.group:bad-sig = badSig
18 changes: 18 additions & 0 deletions src/test/resources/keysMapMulti2.list
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

# the same item in both maps, different keys
test.group = 0x 2222 2222 1111 1111, 0x 2222 2222 2222 2222
test.group:test = 0x 2222 2222 3333 3333

# only in map 2
test.group2 = 0x 2222 2222 4444 4444

# the same item in both maps
test.group7:test7 = 0x 7777 7777 7777 7777

# the same item in both maps
test.group-no-sig:no-sig = noSig

# only in map 2
test.group-no-sig:no-sig2 = noSig

test.group:bad-sig = 0x 2222 2222 5555 5555

0 comments on commit cc29221

Please sign in to comment.