| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package org.xwiki.rendering.signature.internal; |
| 21 |
|
|
| 22 |
|
import java.io.IOException; |
| 23 |
|
import java.io.OutputStream; |
| 24 |
|
import java.util.Collections; |
| 25 |
|
|
| 26 |
|
import org.junit.Before; |
| 27 |
|
import org.junit.Rule; |
| 28 |
|
import org.junit.Test; |
| 29 |
|
import org.junit.rules.ExpectedException; |
| 30 |
|
import org.mockito.invocation.InvocationOnMock; |
| 31 |
|
import org.mockito.stubbing.Answer; |
| 32 |
|
import org.xwiki.crypto.pkix.CertificateProvider; |
| 33 |
|
import org.xwiki.crypto.pkix.ChainingCertificateProvider; |
| 34 |
|
import org.xwiki.crypto.signer.CMSSignedDataVerifier; |
| 35 |
|
import org.xwiki.crypto.signer.param.CMSSignedDataVerified; |
| 36 |
|
import org.xwiki.rendering.block.Block; |
| 37 |
|
import org.xwiki.rendering.block.MacroBlock; |
| 38 |
|
import org.xwiki.rendering.block.MacroMarkerBlock; |
| 39 |
|
import org.xwiki.rendering.block.WordBlock; |
| 40 |
|
import org.xwiki.rendering.signature.BlockSignatureVerifier; |
| 41 |
|
import org.xwiki.test.mockito.MockitoComponentMockingRule; |
| 42 |
|
|
| 43 |
|
import static org.hamcrest.core.IsEqual.equalTo; |
| 44 |
|
import static org.junit.Assert.assertThat; |
| 45 |
|
import static org.mockito.AdditionalMatchers.or; |
| 46 |
|
import static org.mockito.ArgumentMatchers.any; |
| 47 |
|
import static org.mockito.ArgumentMatchers.eq; |
| 48 |
|
import static org.mockito.Mockito.doAnswer; |
| 49 |
|
import static org.mockito.Mockito.mock; |
| 50 |
|
import static org.mockito.Mockito.when; |
| 51 |
|
|
| 52 |
|
|
| 53 |
|
@link |
| 54 |
|
|
| 55 |
|
@version |
| 56 |
|
@since |
| 57 |
|
|
| |
|
| 55% |
Uncovered Elements: 9 (20) |
Complexity: 5 |
Complexity Density: 0.33 |
|
| 58 |
|
public class MacroBlockSignatureVerifierTest |
| 59 |
|
{ |
| 60 |
|
@Rule |
| 61 |
|
public final MockitoComponentMockingRule<BlockSignatureVerifier> mocker = |
| 62 |
|
new MockitoComponentMockingRule<BlockSignatureVerifier>(MacroBlockSignatureVerifier.class); |
| 63 |
|
|
| 64 |
|
@Rule |
| 65 |
|
public ExpectedException thrown = ExpectedException.none(); |
| 66 |
|
|
| 67 |
|
private static final Block MACRO_BLOCK = new MacroBlock("macro", Collections.<String, String>emptyMap(), "content", false); |
| 68 |
|
private static final Block MARKER_BLOCK = new MacroMarkerBlock("macro", Collections.<String, String>emptyMap(), "content", Collections.<Block>emptyList(), false); |
| 69 |
|
private static final byte[] DUMPED_BLOCK = "macro\0\0content\04:wiki5:space6:source\0".getBytes(); |
| 70 |
|
private static final CertificateProvider CERT_PROVIDER = new ChainingCertificateProvider(); |
| 71 |
|
private static final byte[] BLOCK_SIGNATURE = "Signature".getBytes(); |
| 72 |
|
private static final CMSSignedDataVerified VERIFIED = mock(CMSSignedDataVerified.class); |
| 73 |
|
|
| 74 |
|
private BlockDumper dumper; |
| 75 |
|
private CMSSignedDataVerifier cmsVerifier; |
| 76 |
|
private BlockSignatureVerifier verifier; |
| 77 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
| 78 |
3 |
@Before... |
| 79 |
|
public void setUp() throws Exception |
| 80 |
|
{ |
| 81 |
3 |
verifier = mocker.getComponentUnderTest(); |
| 82 |
3 |
dumper = mocker.getInstance(BlockDumper.class, "macro"); |
| 83 |
3 |
cmsVerifier = mocker.getInstance(CMSSignedDataVerifier.class); |
| 84 |
|
|
| 85 |
3 |
doAnswer(new Answer() { |
| |
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
| 86 |
0 |
public Object answer(InvocationOnMock invocation) throws IOException {... |
| 87 |
0 |
Object[] args = invocation.getArguments(); |
| 88 |
0 |
OutputStream out = (OutputStream) args[0]; |
| 89 |
0 |
out.write(DUMPED_BLOCK); |
| 90 |
0 |
return null; |
| 91 |
|
} |
| 92 |
|
}).when(dumper).dump(any(OutputStream.class), or(eq(MACRO_BLOCK), eq(MARKER_BLOCK))); |
| 93 |
3 |
when(dumper.dump(or(eq(MACRO_BLOCK), eq(MARKER_BLOCK)))).thenReturn(DUMPED_BLOCK); |
| 94 |
|
|
| 95 |
3 |
when(cmsVerifier.verify(BLOCK_SIGNATURE, DUMPED_BLOCK, CERT_PROVIDER)).thenReturn(VERIFIED); |
| 96 |
|
} |
| 97 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
| 98 |
1 |
@Test... |
| 99 |
|
public void testMacroBlockSignatureVerification() throws Exception |
| 100 |
|
{ |
| 101 |
1 |
assertThat(verifier.verify(BLOCK_SIGNATURE, MACRO_BLOCK, CERT_PROVIDER), equalTo(VERIFIED)); |
| 102 |
|
} |
| 103 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
| 104 |
1 |
@Test... |
| 105 |
|
public void testMacroMarkerBlockSignatureVerification() throws Exception |
| 106 |
|
{ |
| 107 |
1 |
assertThat(verifier.verify(BLOCK_SIGNATURE, MARKER_BLOCK, CERT_PROVIDER), equalTo(VERIFIED)); |
| 108 |
|
} |
| 109 |
|
|
| |
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
3FAIL
|
|
| 110 |
0 |
@Test... |
| 111 |
|
public void testIncompatibleBlockVerification() throws Exception |
| 112 |
|
{ |
| 113 |
0 |
thrown.expect(IllegalArgumentException.class); |
| 114 |
0 |
thrown.expectMessage("Unsupported block [org.xwiki.rendering.block.WordBlock]."); |
| 115 |
|
|
| 116 |
0 |
assertThat(verifier.verify(BLOCK_SIGNATURE, new WordBlock("macro"), CERT_PROVIDER), equalTo(VERIFIED)); |
| 117 |
|
} |
| 118 |
|
} |