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

File MacroBlockSignatureVerifierTest.java

 
testIncompatibleBlockVerification: Unsupported block [org.xwiki.rendering.block.WordBlock].
 

Code metrics

0
15
5
1
118
79
5
0.33
3
5
1

Classes

Class Line # Actions
MacroBlockSignatureVerifierTest 58 15 0% 5 9
0.5555%
 

Contributing tests

This file is covered by 3 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.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    * Unit test for {@link MacroBlockSignatureVerifier}.
54    *
55    * @version $Id: 39fd632c3816e5cbcdd0015c5969a325274a58cb $
56    * @since 6.1M2
57    */
 
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   
 
78  3 toggle @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() {
 
86  0 toggle 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   
 
98  1 toggle @Test
99    public void testMacroBlockSignatureVerification() throws Exception
100    {
101  1 assertThat(verifier.verify(BLOCK_SIGNATURE, MACRO_BLOCK, CERT_PROVIDER), equalTo(VERIFIED));
102    }
103   
 
104  1 toggle @Test
105    public void testMacroMarkerBlockSignatureVerification() throws Exception
106    {
107  1 assertThat(verifier.verify(BLOCK_SIGNATURE, MARKER_BLOCK, CERT_PROVIDER), equalTo(VERIFIED));
108    }
109   
 
110  0 toggle @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 Test failure here assertThat(verifier.verify(BLOCK_SIGNATURE, new WordBlock("macro"), CERT_PROVIDER), equalTo(VERIFIED));
117    }
118    }