1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.crypto.store.wiki.internal

File X509CertificateWikiStoreTest.java

 

Code metrics

2
192
21
1
508
377
23
0.12
9.14
21
1.1

Classes

Class Line # Actions
X509CertificateWikiStoreTest 92 192 0% 23 1
0.995348899.5%
 

Contributing tests

This file is covered by 16 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.crypto.store.wiki.internal;
21   
22    import java.lang.reflect.Field;
23    import java.math.BigInteger;
24    import java.util.Arrays;
25    import java.util.Collections;
26   
27    import javax.inject.Provider;
28   
29    import org.junit.Before;
30    import org.junit.Rule;
31    import org.junit.Test;
32    import org.xwiki.component.util.ReflectionUtils;
33    import org.xwiki.crypto.BinaryStringEncoder;
34    import org.xwiki.crypto.pkix.CertificateFactory;
35    import org.xwiki.crypto.pkix.params.CertifiedPublicKey;
36    import org.xwiki.crypto.pkix.params.x509certificate.DistinguishedName;
37    import org.xwiki.crypto.pkix.params.x509certificate.X509CertifiedPublicKey;
38    import org.xwiki.crypto.pkix.params.x509certificate.extension.X509Extensions;
39    import org.xwiki.crypto.store.CertificateStore;
40    import org.xwiki.crypto.store.StoreReference;
41    import org.xwiki.crypto.store.WikiStoreReference;
42    import org.xwiki.crypto.store.wiki.internal.query.AbstractX509IssuerAndSerialQuery;
43    import org.xwiki.crypto.store.wiki.internal.query.AbstractX509KeyIdentifierQuery;
44    import org.xwiki.crypto.store.wiki.internal.query.AbstractX509StoreQuery;
45    import org.xwiki.crypto.store.wiki.internal.query.AbstractX509SubjectQuery;
46    import org.xwiki.model.EntityType;
47    import org.xwiki.model.internal.reference.DefaultEntityReferenceProvider;
48    import org.xwiki.model.internal.reference.DefaultSymbolScheme;
49    import org.xwiki.model.internal.reference.LocalStringEntityReferenceSerializer;
50    import org.xwiki.model.reference.DocumentReference;
51    import org.xwiki.model.reference.EntityReference;
52    import org.xwiki.model.reference.EntityReferenceProvider;
53    import org.xwiki.model.reference.LocalDocumentReference;
54    import org.xwiki.model.reference.WikiReference;
55    import org.xwiki.query.Query;
56    import org.xwiki.query.QueryManager;
57    import org.xwiki.test.annotation.ComponentList;
58    import org.xwiki.test.mockito.MockitoComponentMockingRule;
59   
60    import com.xpn.xwiki.XWiki;
61    import com.xpn.xwiki.XWikiContext;
62    import com.xpn.xwiki.doc.XWikiDocument;
63    import com.xpn.xwiki.internal.model.reference.CurrentReferenceDocumentReferenceResolver;
64    import com.xpn.xwiki.internal.model.reference.CurrentReferenceEntityReferenceResolver;
65    import com.xpn.xwiki.internal.model.reference.CurrentStringEntityReferenceResolver;
66    import com.xpn.xwiki.objects.BaseObject;
67   
68    import static org.hamcrest.CoreMatchers.equalTo;
69    import static org.hamcrest.Matchers.contains;
70    import static org.junit.Assert.assertThat;
71    import static org.mockito.ArgumentMatchers.any;
72    import static org.mockito.ArgumentMatchers.eq;
73    import static org.mockito.Mockito.mock;
74    import static org.mockito.Mockito.never;
75    import static org.mockito.Mockito.times;
76    import static org.mockito.Mockito.verify;
77    import static org.mockito.Mockito.when;
78   
79    /**
80    * Unit tests for {@link X509CertificateWikiStore}.
81    *
82    * @version $Id: 2314ee8e0b9e8984c1802ecc7a34451c2091dfb6 $
83    * @since 6.0
84    */
85    @ComponentList({
86    CurrentReferenceDocumentReferenceResolver.class,
87    CurrentReferenceEntityReferenceResolver.class,
88    CurrentStringEntityReferenceResolver.class,
89    LocalStringEntityReferenceSerializer.class,
90    DefaultSymbolScheme.class
91    })
 
92    public class X509CertificateWikiStoreTest
93    {
94    private static final byte[] CERTIFICATE = "certificate".getBytes();
95    private static final String ENCODED_CERTIFICATE = "encoded_certificate";
96    private static final byte[] SUBJECT_KEYID = "subjectKeyId".getBytes();
97    private static final String ENCODED_SUBJECTKEYID = "encoded_subjectKeyId";
98    private static final String SUBJECT = "CN=Subject";
99    private static final String ISSUER = "CN=Issuer";
100    private static final BigInteger SERIAL = new BigInteger("1234567890");
101   
102    private static final String WIKI = "wiki";
103    private static final String SPACE = "space";
104    private static final String DOCUMENT = "document";
105    private static final String FULLNAME = SPACE + '.' + DOCUMENT;
106   
107    private static final WikiReference WIKI_REFERENCE = new WikiReference(WIKI);
108    private static final EntityReference SPACE_REFERENCE = new EntityReference(SPACE, EntityType.WIKI);
109    private static final EntityReference DOCUMENT_REFERENCE = new EntityReference(DOCUMENT, EntityType.DOCUMENT);
110   
111    private static final LocalDocumentReference DOC_STORE_ENTREF = new LocalDocumentReference("space", DOCUMENT);
112    private static final EntityReference SPACE_STORE_ENTREF = new EntityReference(SPACE, EntityType.SPACE);
113   
114    private static final StoreReference DOC_STORE_REF = new WikiStoreReference(DOC_STORE_ENTREF);
115    private static final StoreReference SPACE_STORE_REF = new WikiStoreReference(SPACE_STORE_ENTREF);
116   
117    protected static final String BIND_KEYID = getFieldValue(AbstractX509KeyIdentifierQuery.class, "KEYID");
118    protected static final String BIND_ISSUER = getFieldValue(AbstractX509IssuerAndSerialQuery.class, "ISSUER");
119    protected static final String BIND_SERIAL = getFieldValue(AbstractX509IssuerAndSerialQuery.class, "SERIAL");
120    protected static final String BIND_SUBJECT = getFieldValue(AbstractX509SubjectQuery.class, "SUBJECT");
121   
122    private static final String BIND_STORE = getFieldValue(AbstractX509StoreQuery.class, "STORE");
123   
124    @Rule
125    public MockitoComponentMockingRule<CertificateStore> mocker =
126    new MockitoComponentMockingRule<CertificateStore>(X509CertificateWikiStore.class);
127   
128    private XWikiContext xcontext;
129    private XWiki xwiki;
130   
131    private Query query;
132   
133    private CertificateStore store;
134   
 
135  5 toggle private static String getFieldValue(Class<?> clazz, String fieldName)
136    {
137  5 try {
138  5 Field field = ReflectionUtils.getField(clazz, fieldName);
139  5 boolean isAccessible = field.isAccessible();
140  5 try {
141  5 field.setAccessible(true);
142  5 return (String) field.get(field);
143    } finally {
144  5 field.setAccessible(isAccessible);
145    }
146    } catch (Exception e) {
147  0 throw new RuntimeException(e);
148    }
149    }
150   
 
151  16 toggle @Before
152    public void setUp() throws Exception
153    {
154  16 this.mocker.registerMockComponent(EntityReferenceProvider.class, "default");
155   
156  16 EntityReferenceProvider valueProvider = this.mocker.registerMockComponent(EntityReferenceProvider.class, "current");
157  16 when(valueProvider.getDefaultReference(EntityType.WIKI)).thenReturn(WIKI_REFERENCE);
158  16 when(valueProvider.getDefaultReference(EntityType.SPACE)).thenReturn(SPACE_REFERENCE);
159  16 when(valueProvider.getDefaultReference(EntityType.DOCUMENT)).thenReturn(DOCUMENT_REFERENCE);
160   
161  16 Provider<XWikiContext> xcontextProvider = mocker.registerMockComponent(XWikiContext.TYPE_PROVIDER);
162  16 xcontext = mock(XWikiContext.class);
163  16 when(xcontextProvider.get()).thenReturn(xcontext);
164  16 xwiki = mock(com.xpn.xwiki.XWiki.class);
165  16 when(xcontext.getWiki()).thenReturn(xwiki);
166   
167  16 BinaryStringEncoder encoder = mocker.getInstance(BinaryStringEncoder.class, "Base64");
168  16 when(encoder.encode(CERTIFICATE, 64)).thenReturn(ENCODED_CERTIFICATE);
169  16 when(encoder.decode(ENCODED_CERTIFICATE)).thenReturn(CERTIFICATE);
170  16 when(encoder.encode(SUBJECT_KEYID)).thenReturn(ENCODED_SUBJECTKEYID);
171  16 when(encoder.decode(ENCODED_SUBJECTKEYID)).thenReturn(SUBJECT_KEYID);
172   
173  16 QueryManager queryManager = mocker.getInstance(QueryManager.class);
174  16 query = mock(Query.class);
175  16 when(query.bindValue(any(String.class), any())).thenReturn(query);
176  16 when(query.setWiki(WIKI)).thenReturn(query);
177  16 when(queryManager.createQuery(any(String.class), any(String.class))).thenReturn(query);
178   
179  16 store = mocker.getComponentUnderTest();
180    }
181   
 
182  20 toggle private CertifiedPublicKey getMockedCertificate(boolean hasKeyId) throws Exception
183    {
184  20 X509CertifiedPublicKey certificate = mock(X509CertifiedPublicKey.class);
185   
186  20 when(certificate.getSerialNumber()).thenReturn(SERIAL);
187  20 when(certificate.getIssuer()).thenReturn(new DistinguishedName(ISSUER));
188  20 when(certificate.getSubject()).thenReturn(new DistinguishedName(SUBJECT));
189  20 when(certificate.getEncoded()).thenReturn(CERTIFICATE);
190   
191  20 if (hasKeyId) {
192  12 X509Extensions extensions = mock(X509Extensions.class);
193  12 when(certificate.getExtensions()).thenReturn(extensions);
194  12 when(extensions.getSubjectKeyIdentifier()).thenReturn(SUBJECT_KEYID);
195  12 when(certificate.getSubjectKeyIdentifier()).thenReturn(SUBJECT_KEYID);
196    }
197   
198  20 return certificate;
199    }
200   
 
201  1 toggle @Test
202    public void testStoringNewCertificateWithKeyIdToDocument() throws Exception
203    {
204  1 XWikiDocument storeDoc = mock(XWikiDocument.class);
205  1 when(xwiki.getDocument(new DocumentReference(WIKI, SPACE, DOCUMENT), xcontext)).thenReturn(storeDoc);
206   
207  1 BaseObject certObj = mock(BaseObject.class);
208  1 when(storeDoc.newXObject(X509CertificateWikiStore.CERTIFICATECLASS, xcontext)).thenReturn(certObj);
209   
210  1 store.store(DOC_STORE_REF, getMockedCertificate(true));
211   
212  1 verify(certObj).setStringValue(X509CertificateWikiStore.CERTIFICATECLASS_PROP_KEYID, ENCODED_SUBJECTKEYID);
213  1 verify(certObj).setStringValue(X509CertificateWikiStore.CERTIFICATECLASS_PROP_ISSUER, ISSUER);
214  1 verify(certObj).setStringValue(X509CertificateWikiStore.CERTIFICATECLASS_PROP_SERIAL, SERIAL.toString());
215  1 verify(certObj).setStringValue(X509CertificateWikiStore.CERTIFICATECLASS_PROP_SUBJECT, SUBJECT);
216  1 verify(certObj).setLargeStringValue(X509CertificateWikiStore.CERTIFICATECLASS_PROP_CERTIFICATE, ENCODED_CERTIFICATE);
217   
218  1 verify(xwiki).saveDocument(storeDoc, xcontext);
219    }
220   
 
221  1 toggle @Test
222    public void testStoringNewCertificateWithKeyIdToSpace() throws Exception
223    {
224  1 XWikiDocument storeDoc = mock(XWikiDocument.class);
225  1 when(xwiki.getDocument(new DocumentReference(WIKI, SPACE, ENCODED_SUBJECTKEYID), xcontext)).thenReturn(storeDoc);
226   
227  1 BaseObject certObj = mock(BaseObject.class);
228  1 when(storeDoc.newXObject(X509CertificateWikiStore.CERTIFICATECLASS, xcontext)).thenReturn(certObj);
229   
230  1 store.store(SPACE_STORE_REF, getMockedCertificate(true));
231   
232  1 verify(certObj).setStringValue(X509CertificateWikiStore.CERTIFICATECLASS_PROP_KEYID, ENCODED_SUBJECTKEYID);
233  1 verify(certObj).setStringValue(X509CertificateWikiStore.CERTIFICATECLASS_PROP_ISSUER, ISSUER);
234  1 verify(certObj).setStringValue(X509CertificateWikiStore.CERTIFICATECLASS_PROP_SERIAL, SERIAL.toString());
235  1 verify(certObj).setStringValue(X509CertificateWikiStore.CERTIFICATECLASS_PROP_SUBJECT, SUBJECT);
236  1 verify(certObj).setLargeStringValue(X509CertificateWikiStore.CERTIFICATECLASS_PROP_CERTIFICATE, ENCODED_CERTIFICATE);
237   
238  1 verify(xwiki).saveDocument(storeDoc, xcontext);
239    }
240   
 
241  1 toggle @Test
242    public void testStoringNewCertificateWithoutKeyIdToDocument() throws Exception
243    {
244  1 XWikiDocument storeDoc = mock(XWikiDocument.class);
245  1 when(xwiki.getDocument(new DocumentReference(WIKI, SPACE, DOCUMENT), xcontext)).thenReturn(storeDoc);
246   
247  1 BaseObject certObj = mock(BaseObject.class);
248  1 when(storeDoc.newXObject(X509CertificateWikiStore.CERTIFICATECLASS, xcontext)).thenReturn(certObj);
249   
250  1 store.store(DOC_STORE_REF, getMockedCertificate(false));
251   
252  1 verify(certObj, never()).setStringValue(X509CertificateWikiStore.CERTIFICATECLASS_PROP_KEYID, ENCODED_SUBJECTKEYID);
253  1 verify(certObj).setStringValue(X509CertificateWikiStore.CERTIFICATECLASS_PROP_ISSUER, ISSUER);
254  1 verify(certObj).setStringValue(X509CertificateWikiStore.CERTIFICATECLASS_PROP_SERIAL, SERIAL.toString());
255  1 verify(certObj).setStringValue(X509CertificateWikiStore.CERTIFICATECLASS_PROP_SUBJECT, SUBJECT);
256  1 verify(certObj).setLargeStringValue(X509CertificateWikiStore.CERTIFICATECLASS_PROP_CERTIFICATE, ENCODED_CERTIFICATE);
257   
258  1 verify(xwiki).saveDocument(storeDoc, xcontext);
259    }
260   
 
261  1 toggle @Test
262    public void testStoringNewCertificateWithoutKeyIdToSpace() throws Exception
263    {
264  1 XWikiDocument storeDoc = mock(XWikiDocument.class);
265  1 when(xwiki.getDocument(new DocumentReference(WIKI, SPACE, SERIAL.toString() + ", " + ISSUER), xcontext)).thenReturn(storeDoc);
266   
267  1 BaseObject certObj = mock(BaseObject.class);
268  1 when(storeDoc.newXObject(X509CertificateWikiStore.CERTIFICATECLASS, xcontext)).thenReturn(certObj);
269   
270  1 store.store(SPACE_STORE_REF, getMockedCertificate(false));
271   
272  1 verify(certObj, never()).setStringValue(X509CertificateWikiStore.CERTIFICATECLASS_PROP_KEYID, ENCODED_SUBJECTKEYID);
273  1 verify(certObj).setStringValue(X509CertificateWikiStore.CERTIFICATECLASS_PROP_ISSUER, ISSUER);
274  1 verify(certObj).setStringValue(X509CertificateWikiStore.CERTIFICATECLASS_PROP_SERIAL, SERIAL.toString());
275  1 verify(certObj).setStringValue(X509CertificateWikiStore.CERTIFICATECLASS_PROP_SUBJECT, SUBJECT);
276  1 verify(certObj).setLargeStringValue(X509CertificateWikiStore.CERTIFICATECLASS_PROP_CERTIFICATE, ENCODED_CERTIFICATE);
277   
278  1 verify(xwiki).saveDocument(storeDoc, xcontext);
279    }
280   
 
281  1 toggle @Test
282    public void testUpdatingCertificateWithKeyIdToDocument() throws Exception
283    {
284  1 XWikiDocument storeDoc = mock(XWikiDocument.class);
285  1 when(xwiki.getDocument(new DocumentReference(WIKI, SPACE, DOCUMENT), xcontext)).thenReturn(storeDoc);
286   
287  1 BaseObject certObj = mock(BaseObject.class);
288  1 when(storeDoc.getXObject(X509CertificateWikiStore.CERTIFICATECLASS, 123)).thenReturn(certObj);
289   
290  1 when(query.<Object[]>execute()).thenReturn(Collections.singletonList(new Object[]{"space.document", 123}));
291   
292  1 store.store(DOC_STORE_REF, getMockedCertificate(true));
293   
294  1 verify(query).bindValue(BIND_KEYID, ENCODED_SUBJECTKEYID);
295  1 verify(query).bindValue(BIND_STORE, FULLNAME);
296   
297  1 verify(certObj, never()).setStringValue(eq(X509CertificateWikiStore.CERTIFICATECLASS_PROP_KEYID), any(String.class));
298  1 verify(certObj, never()).setStringValue(eq(X509CertificateWikiStore.CERTIFICATECLASS_PROP_ISSUER), any(String.class));
299  1 verify(certObj, never()).setStringValue(eq(X509CertificateWikiStore.CERTIFICATECLASS_PROP_SERIAL), any(String.class));
300  1 verify(certObj, never()).setStringValue(eq(X509CertificateWikiStore.CERTIFICATECLASS_PROP_SUBJECT), any(String.class));
301  1 verify(certObj).setLargeStringValue(X509CertificateWikiStore.CERTIFICATECLASS_PROP_CERTIFICATE, ENCODED_CERTIFICATE);
302   
303  1 verify(xwiki).saveDocument(storeDoc, xcontext);
304    }
305   
 
306  1 toggle @Test
307    public void testUpdatingCertificateWithKeyIdToSpace() throws Exception
308    {
309  1 XWikiDocument storeDoc = mock(XWikiDocument.class);
310  1 when(xwiki.getDocument(new DocumentReference(WIKI, SPACE, ENCODED_SUBJECTKEYID), xcontext)).thenReturn(storeDoc);
311   
312  1 BaseObject certObj = mock(BaseObject.class);
313  1 when(storeDoc.getXObject(X509CertificateWikiStore.CERTIFICATECLASS, 0)).thenReturn(certObj);
314   
315  1 when(query.<Object[]>execute()).thenReturn(Collections.singletonList(new Object[]{"space." + ENCODED_SUBJECTKEYID, 0}));
316   
317  1 store.store(SPACE_STORE_REF, getMockedCertificate(true));
318   
319  1 verify(query).bindValue(BIND_KEYID, ENCODED_SUBJECTKEYID);
320  1 verify(query).bindValue(BIND_STORE, SPACE);
321   
322  1 verify(certObj, never()).setStringValue(eq(X509CertificateWikiStore.CERTIFICATECLASS_PROP_KEYID), any(String.class));
323  1 verify(certObj, never()).setStringValue(eq(X509CertificateWikiStore.CERTIFICATECLASS_PROP_ISSUER), any(String.class));
324  1 verify(certObj, never()).setStringValue(eq(X509CertificateWikiStore.CERTIFICATECLASS_PROP_SERIAL), any(String.class));
325  1 verify(certObj, never()).setStringValue(eq(X509CertificateWikiStore.CERTIFICATECLASS_PROP_SUBJECT), any(String.class));
326  1 verify(certObj).setLargeStringValue(X509CertificateWikiStore.CERTIFICATECLASS_PROP_CERTIFICATE, ENCODED_CERTIFICATE);
327   
328  1 verify(xwiki).saveDocument(storeDoc, xcontext);
329    }
330   
 
331  1 toggle @Test
332    public void testUpdatingCertificateWithoutKeyIdToDocument() throws Exception
333    {
334  1 XWikiDocument storeDoc = mock(XWikiDocument.class);
335  1 when(xwiki.getDocument(new DocumentReference(WIKI, SPACE, DOCUMENT), xcontext)).thenReturn(storeDoc);
336   
337  1 BaseObject certObj = mock(BaseObject.class);
338  1 when(storeDoc.getXObject(X509CertificateWikiStore.CERTIFICATECLASS, 123)).thenReturn(certObj);
339   
340  1 when(query.<Object[]>execute()).thenReturn(Collections.singletonList(new Object[]{"space.document", 123}));
341   
342  1 store.store(DOC_STORE_REF, getMockedCertificate(false));
343   
344  1 verify(query).bindValue(BIND_ISSUER, ISSUER);
345  1 verify(query).bindValue(BIND_SERIAL, SERIAL.toString());
346  1 verify(query).bindValue(BIND_STORE, FULLNAME);
347   
348  1 verify(certObj, never()).setStringValue(eq(X509CertificateWikiStore.CERTIFICATECLASS_PROP_KEYID), any(String.class));
349  1 verify(certObj, never()).setStringValue(eq(X509CertificateWikiStore.CERTIFICATECLASS_PROP_ISSUER), any(String.class));
350  1 verify(certObj, never()).setStringValue(eq(X509CertificateWikiStore.CERTIFICATECLASS_PROP_SERIAL), any(String.class));
351  1 verify(certObj, never()).setStringValue(eq(X509CertificateWikiStore.CERTIFICATECLASS_PROP_SUBJECT), any(String.class));
352  1 verify(certObj).setLargeStringValue(X509CertificateWikiStore.CERTIFICATECLASS_PROP_CERTIFICATE, ENCODED_CERTIFICATE);
353   
354  1 verify(xwiki).saveDocument(storeDoc, xcontext);
355    }
356   
 
357  1 toggle @Test
358    public void testUpdatingCertificateWithoutKeyIdToSpace() throws Exception
359    {
360  1 XWikiDocument storeDoc = mock(XWikiDocument.class);
361  1 when(xwiki.getDocument(new DocumentReference(WIKI, SPACE, SERIAL.toString() + ", " + ISSUER), xcontext)).thenReturn(storeDoc);
362   
363  1 BaseObject certObj = mock(BaseObject.class);
364  1 when(storeDoc.getXObject(X509CertificateWikiStore.CERTIFICATECLASS, 0)).thenReturn(certObj);
365   
366  1 when(query.<Object[]>execute()).thenReturn(
367    Collections.singletonList(new Object[]{"space." + SERIAL.toString() + ", " + ISSUER, 0}));
368   
369  1 store.store(SPACE_STORE_REF, getMockedCertificate(false));
370   
371  1 verify(query).bindValue(BIND_ISSUER, ISSUER);
372  1 verify(query).bindValue(BIND_SERIAL, SERIAL.toString());
373  1 verify(query).bindValue(BIND_STORE, SPACE);
374   
375  1 verify(certObj, never()).setStringValue(eq(X509CertificateWikiStore.CERTIFICATECLASS_PROP_KEYID), any(String.class));
376  1 verify(certObj, never()).setStringValue(eq(X509CertificateWikiStore.CERTIFICATECLASS_PROP_ISSUER), any(String.class));
377  1 verify(certObj, never()).setStringValue(eq(X509CertificateWikiStore.CERTIFICATECLASS_PROP_SERIAL), any(String.class));
378  1 verify(certObj, never()).setStringValue(eq(X509CertificateWikiStore.CERTIFICATECLASS_PROP_SUBJECT), any(String.class));
379  1 verify(certObj).setLargeStringValue(X509CertificateWikiStore.CERTIFICATECLASS_PROP_CERTIFICATE, ENCODED_CERTIFICATE);
380   
381  1 verify(xwiki).saveDocument(storeDoc, xcontext);
382    }
383   
 
384  4 toggle private CertifiedPublicKey mockSingleCertQuery() throws Exception
385    {
386  4 CertifiedPublicKey certificate = getMockedCertificate(true);
387  4 CertificateFactory factory = mocker.getInstance(CertificateFactory.class, "X509");
388  4 when(factory.decode(CERTIFICATE)).thenReturn(certificate);
389   
390  4 when(query.<String>execute()).thenReturn(Collections.singletonList(ENCODED_CERTIFICATE));
391  4 return certificate;
392    }
393   
 
394  1 toggle @Test
395    public void testRetrievingCertificateUsingKeyIdFromDocument() throws Exception
396    {
397  1 CertifiedPublicKey certificate = mockSingleCertQuery();
398   
399  1 assertThat(this.store.getCertificateProvider(DOC_STORE_REF).getCertificate(SUBJECT_KEYID),
400    equalTo(certificate));
401   
402  1 verify(this.query).bindValue(BIND_KEYID, ENCODED_SUBJECTKEYID);
403  1 verify(this.query, times(3)).bindValue(BIND_STORE, FULLNAME);
404    }
405   
 
406  1 toggle @Test
407    public void testRetrievingCertificateUsingKeyIdFromSpace() throws Exception
408    {
409  1 CertifiedPublicKey certificate = mockSingleCertQuery();
410   
411  1 assertThat(this.store.getCertificateProvider(SPACE_STORE_REF).getCertificate(SUBJECT_KEYID),
412    equalTo(certificate));
413   
414  1 verify(this.query).bindValue(BIND_KEYID, ENCODED_SUBJECTKEYID);
415  1 verify(this.query, times(3)).bindValue(BIND_STORE, SPACE);
416    }
417   
 
418  1 toggle @Test
419    public void testRetrievingCertificateUsingIssueAndSerialFromDocument() throws Exception
420    {
421  1 CertifiedPublicKey certificate = mockSingleCertQuery();
422   
423  1 assertThat(store.getCertificateProvider(DOC_STORE_REF).getCertificate(new DistinguishedName(ISSUER), SERIAL),
424    equalTo(certificate));
425   
426  1 verify(query).bindValue(BIND_ISSUER, ISSUER);
427  1 verify(query).bindValue(BIND_SERIAL, SERIAL.toString());
428  1 verify(query, times(3)).bindValue(BIND_STORE, FULLNAME);
429    }
430   
 
431  1 toggle @Test
432    public void testRetrievingCertificateUsingIssueAndSerialFromSpace() throws Exception
433    {
434  1 CertifiedPublicKey certificate = mockSingleCertQuery();
435   
436  1 assertThat(store.getCertificateProvider(SPACE_STORE_REF).getCertificate(new DistinguishedName(ISSUER), SERIAL),
437    equalTo(certificate));
438   
439  1 verify(query).bindValue(BIND_ISSUER, ISSUER);
440  1 verify(query).bindValue(BIND_SERIAL, SERIAL.toString());
441  1 verify(query, times(3)).bindValue(BIND_STORE, SPACE);
442    }
443   
 
444  4 toggle private CertifiedPublicKey[] mockMultiCertsQuery() throws Exception
445    {
446  4 CertifiedPublicKey[] certs = new CertifiedPublicKey[2];
447  4 byte[] cert2 = "certificate2".getBytes();
448  4 String encodedCert2 = "encoded_certificate2";
449  4 certs[0] = getMockedCertificate(true);
450  4 certs[1] = getMockedCertificate(false);
451  4 CertificateFactory factory = this.mocker.getInstance(CertificateFactory.class, "X509");
452  4 when(factory.decode(CERTIFICATE)).thenReturn(certs[0]);
453  4 when(factory.decode(cert2)).thenReturn(certs[1]);
454   
455  4 BinaryStringEncoder encoder = this.mocker.getInstance(BinaryStringEncoder.class, "Base64");
456  4 when(encoder.encode(cert2, 64)).thenReturn(encodedCert2);
457  4 when(encoder.decode(encodedCert2)).thenReturn(cert2);
458   
459  4 when(this.query.<String>execute()).thenReturn(Arrays.asList(ENCODED_CERTIFICATE, encodedCert2));
460   
461  4 return certs;
462    }
463   
 
464  1 toggle @Test
465    public void testRetrievingCertificatesUsingSubjectFromDocument() throws Exception
466    {
467  1 CertifiedPublicKey[] certs = mockMultiCertsQuery();
468   
469  1 assertThat(this.store.getCertificateProvider(DOC_STORE_REF).getCertificate(new DistinguishedName(SUBJECT)),
470    contains(certs));
471   
472  1 verify(this.query).bindValue(BIND_SUBJECT, SUBJECT);
473  1 verify(this.query, times(3)).bindValue(BIND_STORE, FULLNAME);
474    }
475   
 
476  1 toggle @Test
477    public void testRetrievingCertificatesUsingSubjectFromSpace() throws Exception
478    {
479  1 CertifiedPublicKey[] certs = mockMultiCertsQuery();
480   
481  1 assertThat(this.store.getCertificateProvider(SPACE_STORE_REF).getCertificate(new DistinguishedName(SUBJECT)),
482    contains(certs));
483   
484  1 verify(this.query).bindValue(BIND_SUBJECT, SUBJECT);
485  1 verify(this.query, times(3)).bindValue(BIND_STORE, SPACE);
486    }
487   
 
488  1 toggle @Test
489    public void testRetrievingAllCertificatesFromDocument() throws Exception
490    {
491  1 CertifiedPublicKey[] certs = mockMultiCertsQuery();
492   
493  1 assertThat(this.store.getAllCertificates(DOC_STORE_REF), contains(certs));
494   
495  1 verify(this.query).bindValue(BIND_STORE, FULLNAME);
496    }
497   
 
498  1 toggle @Test
499    public void testRetrievingAllCertificatesFromSpace() throws Exception
500    {
501  1 CertifiedPublicKey[] certs = mockMultiCertsQuery();
502   
503  1 assertThat(this.store.getAllCertificates(SPACE_STORE_REF), contains(certs));
504   
505  1 verify(this.query).bindValue(BIND_STORE, SPACE);
506    }
507   
508    }