| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package org.xwiki.rendering.macro.internal; |
| 21 |
|
|
| 22 |
|
import java.util.Collection; |
| 23 |
|
import java.util.Collections; |
| 24 |
|
import java.util.List; |
| 25 |
|
|
| 26 |
|
import javax.inject.Named; |
| 27 |
|
import javax.inject.Singleton; |
| 28 |
|
|
| 29 |
|
import org.junit.Before; |
| 30 |
|
import org.junit.Rule; |
| 31 |
|
import org.junit.Test; |
| 32 |
|
import org.xwiki.component.annotation.Component; |
| 33 |
|
import org.xwiki.component.util.DefaultParameterizedType; |
| 34 |
|
import org.xwiki.crypto.pkix.params.CertifiedPublicKey; |
| 35 |
|
import org.xwiki.crypto.signer.param.CMSSignedDataGeneratorParameters; |
| 36 |
|
import org.xwiki.crypto.signer.param.CMSSignedDataVerified; |
| 37 |
|
import org.xwiki.crypto.signer.param.CMSSignerVerifiedInformation; |
| 38 |
|
import org.xwiki.crypto.store.SignatureStore; |
| 39 |
|
import org.xwiki.model.reference.BlockReference; |
| 40 |
|
import org.xwiki.model.reference.BlockReferenceResolver; |
| 41 |
|
import org.xwiki.properties.BeanManager; |
| 42 |
|
import org.xwiki.properties.internal.DefaultBeanDescriptor; |
| 43 |
|
import org.xwiki.rendering.block.Block; |
| 44 |
|
import org.xwiki.rendering.block.MacroBlock; |
| 45 |
|
import org.xwiki.rendering.macro.AbstractNoParameterSignableMacro; |
| 46 |
|
import org.xwiki.rendering.macro.MacroExecutionException; |
| 47 |
|
import org.xwiki.rendering.macro.SignableMacro; |
| 48 |
|
import org.xwiki.rendering.signature.BlockSignatureGenerator; |
| 49 |
|
import org.xwiki.rendering.signature.BlockSignatureVerifier; |
| 50 |
|
import org.xwiki.rendering.transformation.MacroTransformationContext; |
| 51 |
|
import org.xwiki.test.mockito.MockitoComponentMockingRule; |
| 52 |
|
|
| 53 |
|
import static org.hamcrest.CoreMatchers.equalTo; |
| 54 |
|
import static org.junit.Assert.assertThat; |
| 55 |
|
import static org.mockito.Mockito.verify; |
| 56 |
|
import static org.mockito.Mockito.when; |
| 57 |
|
|
| 58 |
|
|
| 59 |
|
@link |
| 60 |
|
|
| 61 |
|
@version |
| 62 |
|
@since |
| 63 |
|
|
| |
|
| 63% |
Uncovered Elements: 10 (27) |
Complexity: 8 |
Complexity Density: 0.42 |
|
| 64 |
|
public class SignableMacroTest |
| 65 |
|
{ |
| 66 |
|
private static final BlockReference BLOCK_REFERENCE = new BlockReference("blockName"); |
| 67 |
|
private static final Block BLOCK = new MacroBlock("testMacro", Collections.<String, String>emptyMap(), true); |
| 68 |
|
private static final byte[] SIGNATURE = "Signature".getBytes(); |
| 69 |
|
private static final CMSSignedDataGeneratorParameters PARAMETERS = new CMSSignedDataGeneratorParameters(); |
| 70 |
|
private static final CMSSignedDataVerified VERIFIED = new CMSSignedDataVerified() { |
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 71 |
0 |
@Override... |
| 72 |
|
public Collection<CMSSignerVerifiedInformation> getSignatures() |
| 73 |
|
{ |
| 74 |
0 |
return null; |
| 75 |
|
} |
| 76 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 77 |
0 |
@Override... |
| 78 |
|
public Collection<CertifiedPublicKey> getCertificates() |
| 79 |
|
{ |
| 80 |
0 |
return null; |
| 81 |
|
} |
| 82 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 83 |
0 |
@Override... |
| 84 |
|
public String getContentType() |
| 85 |
|
{ |
| 86 |
0 |
return null; |
| 87 |
|
} |
| 88 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 89 |
0 |
@Override... |
| 90 |
|
public byte[] getContent() |
| 91 |
|
{ |
| 92 |
0 |
return new byte[0]; |
| 93 |
|
} |
| 94 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 95 |
0 |
@Override... |
| 96 |
|
public boolean isVerified() |
| 97 |
|
{ |
| 98 |
0 |
return false; |
| 99 |
|
} |
| 100 |
|
}; |
| 101 |
|
|
| 102 |
|
@Component(staticRegistration = false) |
| 103 |
|
@Named("testmacro") |
| 104 |
|
@Singleton |
| |
|
| 33.3% |
Uncovered Elements: 4 (6) |
Complexity: 3 |
Complexity Density: 1 |
|
| 105 |
|
public static class TestSignable extends AbstractNoParameterSignableMacro |
| 106 |
|
{ |
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 107 |
2 |
public TestSignable()... |
| 108 |
|
{ |
| 109 |
2 |
super("testmacro"); |
| 110 |
|
} |
| 111 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 112 |
0 |
@Override... |
| 113 |
|
public boolean supportsInlineMode() |
| 114 |
|
{ |
| 115 |
0 |
return false; |
| 116 |
|
} |
| 117 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 118 |
0 |
@Override... |
| 119 |
|
public List<Block> execute(Object o, String s, MacroTransformationContext macroTransformationContext) |
| 120 |
|
throws MacroExecutionException |
| 121 |
|
{ |
| 122 |
0 |
return null; |
| 123 |
|
} |
| 124 |
|
} |
| 125 |
|
|
| 126 |
|
@Rule |
| 127 |
|
public final MockitoComponentMockingRule<SignableMacro> mocker = |
| 128 |
|
new MockitoComponentMockingRule<SignableMacro>(TestSignable.class); |
| 129 |
|
|
| 130 |
|
private SignableMacro macro; |
| 131 |
|
private SignatureStore store; |
| 132 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
|
| 133 |
2 |
@Before... |
| 134 |
|
public void setUp() throws Exception |
| 135 |
|
{ |
| 136 |
2 |
BeanManager beanManager = mocker.getInstance(BeanManager.class); |
| 137 |
2 |
when(beanManager.getBeanDescriptor(Object.class)).thenReturn(new DefaultBeanDescriptor(Object.class)); |
| 138 |
|
|
| 139 |
2 |
store = mocker.registerMockComponent(SignatureStore.class); |
| 140 |
2 |
when(store.retrieve(BLOCK_REFERENCE)).thenReturn(SIGNATURE); |
| 141 |
|
|
| 142 |
2 |
BlockSignatureGenerator signer = mocker.registerMockComponent(BlockSignatureGenerator.class, "macro"); |
| 143 |
2 |
when(signer.generate(BLOCK, PARAMETERS)).thenReturn(SIGNATURE); |
| 144 |
|
|
| 145 |
2 |
BlockSignatureVerifier verifier = mocker.registerMockComponent(BlockSignatureVerifier.class, "macro"); |
| 146 |
2 |
when(verifier.verify(SIGNATURE, BLOCK, null)).thenReturn(VERIFIED); |
| 147 |
|
|
| 148 |
2 |
BlockReferenceResolver<Block> resolver = |
| 149 |
|
mocker.registerMockComponent(new DefaultParameterizedType(null, BlockReferenceResolver.class, Block.class), |
| 150 |
|
"currentsignedmacro"); |
| 151 |
2 |
when(resolver.resolve(BLOCK)).thenReturn(BLOCK_REFERENCE); |
| 152 |
|
|
| 153 |
2 |
macro = mocker.getComponentUnderTest(); |
| 154 |
|
} |
| 155 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
| 156 |
1 |
@Test... |
| 157 |
|
public void testMacroSigning() throws Exception |
| 158 |
|
{ |
| 159 |
1 |
macro.sign(BLOCK, PARAMETERS); |
| 160 |
1 |
verify(store).store(BLOCK_REFERENCE, SIGNATURE); |
| 161 |
|
} |
| 162 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
| 163 |
1 |
@Test... |
| 164 |
|
public void testMacroVerifying() throws Exception |
| 165 |
|
{ |
| 166 |
1 |
assertThat(macro.verify(BLOCK, null), equalTo(VERIFIED)); |
| 167 |
|
} |
| 168 |
|
} |