1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.rendering.macro.internal

File SignableMacroTest.java

 

Code metrics

0
22
11
2
168
121
11
0.5
2
5.5
1

Classes

Class Line # Actions
SignableMacroTest 64 19 0% 8 10
0.629629663%
SignableMacroTest.TestSignable 105 3 0% 3 4
0.3333333433.3%
 

Contributing tests

This file is covered by 2 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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    * Test implementation of signable macro based on {@link org.xwiki.rendering.macro.AbstractSignableMacro}
60    *
61    * @version $Id: 4fdc5eecd1eedf622b9e2d76d086e8cc70686b29 $
62    * @since 6.1M2
63    */
 
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() {
 
71  0 toggle @Override
72    public Collection<CMSSignerVerifiedInformation> getSignatures()
73    {
74  0 return null;
75    }
76   
 
77  0 toggle @Override
78    public Collection<CertifiedPublicKey> getCertificates()
79    {
80  0 return null;
81    }
82   
 
83  0 toggle @Override
84    public String getContentType()
85    {
86  0 return null;
87    }
88   
 
89  0 toggle @Override
90    public byte[] getContent()
91    {
92  0 return new byte[0];
93    }
94   
 
95  0 toggle @Override
96    public boolean isVerified()
97    {
98  0 return false;
99    }
100    };
101   
102    @Component(staticRegistration = false)
103    @Named("testmacro")
104    @Singleton
 
105    public static class TestSignable extends AbstractNoParameterSignableMacro
106    {
 
107  2 toggle public TestSignable()
108    {
109  2 super("testmacro");
110    }
111   
 
112  0 toggle @Override
113    public boolean supportsInlineMode()
114    {
115  0 return false;
116    }
117   
 
118  0 toggle @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   
 
133  2 toggle @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   
 
156  1 toggle @Test
157    public void testMacroSigning() throws Exception
158    {
159  1 macro.sign(BLOCK, PARAMETERS);
160  1 verify(store).store(BLOCK_REFERENCE, SIGNATURE);
161    }
162   
 
163  1 toggle @Test
164    public void testMacroVerifying() throws Exception
165    {
166  1 assertThat(macro.verify(BLOCK, null), equalTo(VERIFIED));
167    }
168    }