1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.crypto.store.wiki.internal

File X509CertificateWikiStoreTest.java

 

Code metrics

2
192
21
1
507
376
23
0.12
9.14
21
1.1

Classes

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