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

File X509KeyWikiStoreTest.java

 

Code metrics

0
184
15
1
450
331
15
0.08
12.27
15
1

Classes

Class Line # Actions
X509KeyWikiStoreTest 84 184 0% 15 0
1.0100%
 

Contributing tests

This file is covered by 14 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.math.BigInteger;
23    import java.util.Collections;
24   
25    import javax.inject.Provider;
26   
27    import org.junit.Before;
28    import org.junit.Rule;
29    import org.junit.Test;
30    import org.xwiki.crypto.AsymmetricKeyFactory;
31    import org.xwiki.crypto.BinaryStringEncoder;
32    import org.xwiki.crypto.params.cipher.asymmetric.PrivateKeyParameters;
33    import org.xwiki.crypto.password.PrivateKeyPasswordBasedEncryptor;
34    import org.xwiki.crypto.pkix.CertificateFactory;
35    import org.xwiki.crypto.pkix.params.CertifiedKeyPair;
36    import org.xwiki.crypto.pkix.params.CertifiedPublicKey;
37    import org.xwiki.crypto.pkix.params.x509certificate.DistinguishedName;
38    import org.xwiki.crypto.pkix.params.x509certificate.X509CertifiedPublicKey;
39    import org.xwiki.crypto.pkix.params.x509certificate.extension.X509Extensions;
40    import org.xwiki.crypto.store.KeyStore;
41    import org.xwiki.crypto.store.StoreReference;
42    import org.xwiki.crypto.store.WikiStoreReference;
43    import org.xwiki.model.EntityType;
44    import org.xwiki.model.internal.reference.DefaultSymbolScheme;
45    import org.xwiki.model.reference.DocumentReference;
46    import org.xwiki.model.reference.EntityReference;
47    import org.xwiki.model.reference.EntityReferenceProvider;
48    import org.xwiki.model.reference.LocalDocumentReference;
49    import org.xwiki.model.reference.WikiReference;
50    import org.xwiki.query.Query;
51    import org.xwiki.query.QueryManager;
52    import org.xwiki.test.annotation.ComponentList;
53    import org.xwiki.test.mockito.MockitoComponentMockingRule;
54   
55    import com.xpn.xwiki.XWiki;
56    import com.xpn.xwiki.XWikiContext;
57    import com.xpn.xwiki.doc.XWikiDocument;
58    import com.xpn.xwiki.internal.model.reference.CurrentReferenceDocumentReferenceResolver;
59    import com.xpn.xwiki.internal.model.reference.CurrentReferenceEntityReferenceResolver;
60    import com.xpn.xwiki.internal.model.reference.CurrentStringEntityReferenceResolver;
61    import com.xpn.xwiki.objects.BaseObject;
62   
63    import static org.hamcrest.CoreMatchers.equalTo;
64    import static org.hamcrest.CoreMatchers.notNullValue;
65    import static org.hamcrest.CoreMatchers.nullValue;
66    import static org.junit.Assert.assertThat;
67    import static org.mockito.ArgumentMatchers.any;
68    import static org.mockito.Mockito.mock;
69    import static org.mockito.Mockito.verify;
70    import static org.mockito.Mockito.when;
71   
72    /**
73    * Unit tests for {@link X509KeyWikiStore}.
74    *
75    * @version $Id: 20c48ca5139565f14a297d4a16aac8d5dcc40de7 $
76    * @since 6.0
77    */
78    @ComponentList({
79    CurrentReferenceDocumentReferenceResolver.class,
80    CurrentReferenceEntityReferenceResolver.class,
81    CurrentStringEntityReferenceResolver.class,
82    DefaultSymbolScheme.class
83    })
 
84    public class X509KeyWikiStoreTest
85    {
86    private static final byte[] PASSWORD = "password".getBytes();
87    private static final byte[] PRIVATEKEY = "privatekey".getBytes();
88    private static final String ENCODED_PRIVATEKEY = "encoded_privatekey";
89    private static final byte[] ENCRYPTED_PRIVATEKEY = "encrypted_privatekey".getBytes();
90    private static final String ENCODED_ENCRYPTED_PRIVATEKEY = "encoded_encrypted_privatekey";
91    private static final byte[] CERTIFICATE = "certificate".getBytes();
92    private static final String ENCODED_CERTIFICATE = "encoded_certificate";
93    private static final byte[] SUBJECT_KEYID = "subjectKeyId".getBytes();
94    private static final String ENCODED_SUBJECTKEYID = "encoded_subjectKeyId";
95    private static final String SUBJECT = "CN=Subject";
96    private static final String ISSUER = "CN=Issuer";
97    private static final BigInteger SERIAL = new BigInteger("1234567890");
98   
99    private static final String WIKI = "wiki";
100    private static final String SPACE = "space";
101    private static final String DOCUMENT = "document";
102   
103    private static final WikiReference WIKI_REFERENCE = new WikiReference(WIKI);
104    private static final EntityReference SPACE_REFERENCE = new EntityReference(SPACE, EntityType.WIKI);
105    private static final EntityReference DOCUMENT_REFERENCE = new EntityReference(DOCUMENT, EntityType.DOCUMENT);
106   
107    private static final LocalDocumentReference DOC_STORE_ENTREF = new LocalDocumentReference("space", DOCUMENT);
108    private static final EntityReference SPACE_STORE_ENTREF = new EntityReference(SPACE, EntityType.SPACE);
109   
110    private static final StoreReference DOC_STORE_REF = new WikiStoreReference(DOC_STORE_ENTREF);
111    private static final StoreReference SPACE_STORE_REF = new WikiStoreReference(SPACE_STORE_ENTREF);
112   
113    @Rule
114    public MockitoComponentMockingRule<KeyStore> mocker = new MockitoComponentMockingRule<>(X509KeyWikiStore.class);
115   
116    private XWikiContext xcontext;
117    private XWiki xwiki;
118   
119    private Query query;
120   
121    private KeyStore store;
122   
123    PrivateKeyParameters privateKey;
124    X509CertifiedPublicKey certificate;
125    CertifiedKeyPair keyPair;
126   
 
127  14 toggle @Before
128    public void setUp() throws Exception
129    {
130  14 EntityReferenceProvider valueProvider = mock(EntityReferenceProvider.class);
131  14 when(valueProvider.getDefaultReference(EntityType.WIKI)).thenReturn(WIKI_REFERENCE);
132  14 when(valueProvider.getDefaultReference(EntityType.SPACE)).thenReturn(SPACE_REFERENCE);
133  14 when(valueProvider.getDefaultReference(EntityType.DOCUMENT)).thenReturn(DOCUMENT_REFERENCE);
134   
135  14 mocker.registerComponent(EntityReferenceProvider.class, "current", valueProvider);
136   
137  14 Provider<XWikiContext> xcontextProvider =
138    mocker.registerMockComponent(XWikiContext.TYPE_PROVIDER);
139  14 xcontext = mock(XWikiContext.class);
140  14 when(xcontextProvider.get()).thenReturn(xcontext);
141  14 xwiki = mock(com.xpn.xwiki.XWiki.class);
142  14 when(xcontext.getWiki()).thenReturn(xwiki);
143   
144  14 BinaryStringEncoder encoder = mocker.getInstance(BinaryStringEncoder.class, "Base64");
145  14 when(encoder.encode(PRIVATEKEY, 64)).thenReturn(ENCODED_PRIVATEKEY);
146  14 when(encoder.decode(ENCODED_PRIVATEKEY)).thenReturn(PRIVATEKEY);
147  14 when(encoder.encode(ENCRYPTED_PRIVATEKEY, 64)).thenReturn(ENCODED_ENCRYPTED_PRIVATEKEY);
148  14 when(encoder.decode(ENCODED_ENCRYPTED_PRIVATEKEY)).thenReturn(ENCRYPTED_PRIVATEKEY);
149  14 when(encoder.encode(CERTIFICATE, 64)).thenReturn(ENCODED_CERTIFICATE);
150  14 when(encoder.decode(ENCODED_CERTIFICATE)).thenReturn(CERTIFICATE);
151  14 when(encoder.encode(SUBJECT_KEYID)).thenReturn(ENCODED_SUBJECTKEYID);
152  14 when(encoder.decode(ENCODED_SUBJECTKEYID)).thenReturn(SUBJECT_KEYID);
153   
154  14 privateKey = mock(PrivateKeyParameters.class);
155  14 when(privateKey.getEncoded()).thenReturn(PRIVATEKEY);
156   
157  14 AsymmetricKeyFactory keyFactory = mocker.getInstance(AsymmetricKeyFactory.class);
158  14 when(keyFactory.fromPKCS8(PRIVATEKEY)).thenReturn(privateKey);
159   
160  14 PrivateKeyPasswordBasedEncryptor encryptor = mocker.getInstance(PrivateKeyPasswordBasedEncryptor.class);
161  14 when(encryptor.encrypt(PASSWORD, privateKey)).thenReturn(ENCRYPTED_PRIVATEKEY);
162  14 when(encryptor.decrypt(PASSWORD, ENCRYPTED_PRIVATEKEY)).thenReturn(privateKey);
163   
164  14 certificate = mock(X509CertifiedPublicKey.class);
165  14 when(certificate.getSerialNumber()).thenReturn(SERIAL);
166  14 when(certificate.getIssuer()).thenReturn(new DistinguishedName(ISSUER));
167  14 when(certificate.getSubject()).thenReturn(new DistinguishedName(SUBJECT));
168  14 when(certificate.getEncoded()).thenReturn(CERTIFICATE);
169   
170  14 CertificateFactory certificateFactory = mocker.getInstance(CertificateFactory.class, "X509");
171  14 when(certificateFactory.decode(CERTIFICATE)).thenReturn(certificate);
172   
173  14 X509Extensions extensions = mock(X509Extensions.class);
174  14 when(certificate.getExtensions()).thenReturn(extensions);
175  14 when(extensions.getSubjectKeyIdentifier()).thenReturn(SUBJECT_KEYID);
176  14 when(certificate.getSubjectKeyIdentifier()).thenReturn(SUBJECT_KEYID);
177   
178  14 keyPair = new CertifiedKeyPair(privateKey, certificate);
179   
180  14 QueryManager queryManager = mocker.getInstance(QueryManager.class);
181  14 query = mock(Query.class);
182  14 when(query.bindValue(any(String.class), any())).thenReturn(query);
183  14 when(query.setWiki(WIKI)).thenReturn(query);
184  14 when(queryManager.createQuery(any(String.class), any(String.class))).thenReturn(query);
185   
186  14 store = mocker.getComponentUnderTest();
187    }
188   
 
189  1 toggle @Test
190    public void storingPrivateKeyToEmptyDocument() throws Exception
191    {
192  1 XWikiDocument storeDoc = mock(XWikiDocument.class);
193  1 when(xwiki.getDocument(new DocumentReference(WIKI, SPACE, DOCUMENT), xcontext)).thenReturn(storeDoc);
194   
195  1 BaseObject certObj = mock(BaseObject.class);
196  1 when(storeDoc.newXObject(X509CertificateWikiStore.CERTIFICATECLASS, xcontext)).thenReturn(certObj);
197   
198  1 BaseObject pkObj = mock(BaseObject.class);
199  1 when(storeDoc.newXObject(X509KeyWikiStore.PRIVATEKEYCLASS, xcontext)).thenReturn(pkObj);
200   
201  1 store.store(DOC_STORE_REF, keyPair);
202   
203  1 verify(certObj).setStringValue(X509CertificateWikiStore.CERTIFICATECLASS_PROP_KEYID, ENCODED_SUBJECTKEYID);
204  1 verify(certObj).setStringValue(X509CertificateWikiStore.CERTIFICATECLASS_PROP_ISSUER, ISSUER);
205  1 verify(certObj).setStringValue(X509CertificateWikiStore.CERTIFICATECLASS_PROP_SERIAL, SERIAL.toString());
206  1 verify(certObj).setStringValue(X509CertificateWikiStore.CERTIFICATECLASS_PROP_SUBJECT, SUBJECT);
207  1 verify(certObj).setLargeStringValue(X509CertificateWikiStore.CERTIFICATECLASS_PROP_CERTIFICATE, ENCODED_CERTIFICATE);
208  1 verify(pkObj).setLargeStringValue(X509KeyWikiStore.PRIVATEKEYCLASS_PROP_KEY, ENCODED_PRIVATEKEY);
209   
210  1 verify(xwiki).saveDocument(storeDoc, xcontext);
211    }
212   
 
213  1 toggle @Test
214    public void storingPrivateKeyToCertificateDocument() throws Exception
215    {
216  1 XWikiDocument storeDoc = mock(XWikiDocument.class);
217  1 when(xwiki.getDocument(new DocumentReference(WIKI, SPACE, DOCUMENT), xcontext)).thenReturn(storeDoc);
218   
219  1 BaseObject certObj = mock(BaseObject.class);
220  1 when(storeDoc.getXObject(X509CertificateWikiStore.CERTIFICATECLASS, 0)).thenReturn(certObj);
221   
222  1 when(query.<Object[]>execute()).thenReturn(Collections.singletonList(new Object[]{"space.document", 0}));
223   
224  1 BaseObject pkObj = mock(BaseObject.class);
225  1 when(storeDoc.newXObject(X509KeyWikiStore.PRIVATEKEYCLASS, xcontext)).thenReturn(pkObj);
226   
227  1 store.store(DOC_STORE_REF, keyPair);
228   
229  1 verify(certObj).setLargeStringValue(X509CertificateWikiStore.CERTIFICATECLASS_PROP_CERTIFICATE, ENCODED_CERTIFICATE);
230  1 verify(pkObj).setLargeStringValue(X509KeyWikiStore.PRIVATEKEYCLASS_PROP_KEY, ENCODED_PRIVATEKEY);
231   
232  1 verify(xwiki).saveDocument(storeDoc, xcontext);
233    }
234   
 
235  1 toggle @Test
236    public void storingPrivateKeyToEmptySpace() throws Exception
237    {
238  1 XWikiDocument storeDoc = mock(XWikiDocument.class);
239  1 when(xwiki.getDocument(new DocumentReference(WIKI, SPACE, ENCODED_SUBJECTKEYID), xcontext)).thenReturn(storeDoc);
240   
241  1 BaseObject certObj = mock(BaseObject.class);
242  1 when(storeDoc.newXObject(X509CertificateWikiStore.CERTIFICATECLASS, xcontext)).thenReturn(certObj);
243   
244  1 BaseObject pkObj = mock(BaseObject.class);
245  1 when(storeDoc.newXObject(X509KeyWikiStore.PRIVATEKEYCLASS, xcontext)).thenReturn(pkObj);
246   
247  1 store.store(SPACE_STORE_REF, keyPair);
248   
249  1 verify(certObj).setStringValue(X509CertificateWikiStore.CERTIFICATECLASS_PROP_KEYID, ENCODED_SUBJECTKEYID);
250  1 verify(certObj).setStringValue(X509CertificateWikiStore.CERTIFICATECLASS_PROP_ISSUER, ISSUER);
251  1 verify(certObj).setStringValue(X509CertificateWikiStore.CERTIFICATECLASS_PROP_SERIAL, SERIAL.toString());
252  1 verify(certObj).setStringValue(X509CertificateWikiStore.CERTIFICATECLASS_PROP_SUBJECT, SUBJECT);
253  1 verify(certObj).setLargeStringValue(X509CertificateWikiStore.CERTIFICATECLASS_PROP_CERTIFICATE, ENCODED_CERTIFICATE);
254  1 verify(pkObj).setLargeStringValue(X509KeyWikiStore.PRIVATEKEYCLASS_PROP_KEY, ENCODED_PRIVATEKEY);
255   
256  1 verify(xwiki).saveDocument(storeDoc, xcontext);
257    }
258   
 
259  1 toggle @Test
260    public void storingPrivateKeyToCertificateSpace() throws Exception
261    {
262  1 XWikiDocument storeDoc = mock(XWikiDocument.class);
263  1 when(xwiki.getDocument(new DocumentReference(WIKI, SPACE, ENCODED_SUBJECTKEYID), xcontext)).thenReturn(storeDoc);
264   
265  1 BaseObject certObj = mock(BaseObject.class);
266  1 when(storeDoc.getXObject(X509CertificateWikiStore.CERTIFICATECLASS, 0)).thenReturn(certObj);
267   
268  1 when(query.<Object[]>execute()).thenReturn(Collections.singletonList(new Object[]{"space." + ENCODED_SUBJECTKEYID, 0}));
269   
270  1 BaseObject pkObj = mock(BaseObject.class);
271  1 when(storeDoc.newXObject(X509KeyWikiStore.PRIVATEKEYCLASS, xcontext)).thenReturn(pkObj);
272   
273  1 store.store(SPACE_STORE_REF, keyPair);
274   
275  1 verify(certObj).setLargeStringValue(X509CertificateWikiStore.CERTIFICATECLASS_PROP_CERTIFICATE, ENCODED_CERTIFICATE);
276  1 verify(pkObj).setLargeStringValue(X509KeyWikiStore.PRIVATEKEYCLASS_PROP_KEY, ENCODED_PRIVATEKEY);
277   
278  1 verify(xwiki).saveDocument(storeDoc, xcontext);
279    }
280   
 
281  1 toggle @Test
282    public void storingEncryptedPrivateKey() 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.newXObject(X509CertificateWikiStore.CERTIFICATECLASS, xcontext)).thenReturn(certObj);
289   
290  1 BaseObject pkObj = mock(BaseObject.class);
291  1 when(storeDoc.newXObject(X509KeyWikiStore.PRIVATEKEYCLASS, xcontext)).thenReturn(pkObj);
292   
293  1 store.store(DOC_STORE_REF, keyPair, PASSWORD);
294   
295  1 verify(certObj).setStringValue(X509CertificateWikiStore.CERTIFICATECLASS_PROP_KEYID, ENCODED_SUBJECTKEYID);
296  1 verify(certObj).setStringValue(X509CertificateWikiStore.CERTIFICATECLASS_PROP_ISSUER, ISSUER);
297  1 verify(certObj).setStringValue(X509CertificateWikiStore.CERTIFICATECLASS_PROP_SERIAL, SERIAL.toString());
298  1 verify(certObj).setStringValue(X509CertificateWikiStore.CERTIFICATECLASS_PROP_SUBJECT, SUBJECT);
299  1 verify(certObj).setLargeStringValue(X509CertificateWikiStore.CERTIFICATECLASS_PROP_CERTIFICATE, ENCODED_CERTIFICATE);
300  1 verify(pkObj).setLargeStringValue(X509KeyWikiStore.PRIVATEKEYCLASS_PROP_KEY, ENCODED_ENCRYPTED_PRIVATEKEY);
301   
302  1 verify(xwiki).saveDocument(storeDoc, xcontext);
303    }
304   
 
305  1 toggle @Test
306    public void updatingPrivateKey() throws Exception
307    {
308  1 XWikiDocument storeDoc = mock(XWikiDocument.class);
309  1 when(xwiki.getDocument(new DocumentReference(WIKI, SPACE, DOCUMENT), 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.document", 0}));
315   
316  1 BaseObject pkObj = mock(BaseObject.class);
317  1 when(storeDoc.getXObject(X509KeyWikiStore.PRIVATEKEYCLASS)).thenReturn(pkObj);
318   
319  1 store.store(DOC_STORE_REF, keyPair);
320   
321  1 verify(certObj).setLargeStringValue(X509CertificateWikiStore.CERTIFICATECLASS_PROP_CERTIFICATE, ENCODED_CERTIFICATE);
322  1 verify(pkObj).setLargeStringValue(X509KeyWikiStore.PRIVATEKEYCLASS_PROP_KEY, ENCODED_PRIVATEKEY);
323   
324  1 verify(xwiki).saveDocument(storeDoc, xcontext);
325    }
326   
 
327  1 toggle @Test
328    public void retrievePrivateKeyFromDocument() throws Exception
329    {
330  1 XWikiDocument storeDoc = mock(XWikiDocument.class);
331  1 when(xwiki.getDocument(new DocumentReference(WIKI, SPACE, DOCUMENT), xcontext)).thenReturn(storeDoc);
332   
333  1 BaseObject certObj = mock(BaseObject.class);
334  1 when(storeDoc.getXObject(X509CertificateWikiStore.CERTIFICATECLASS)).thenReturn(certObj);
335  1 when(certObj.getLargeStringValue(X509CertificateWikiStore.CERTIFICATECLASS_PROP_CERTIFICATE)).thenReturn(ENCODED_CERTIFICATE);
336   
337  1 BaseObject pkObj = mock(BaseObject.class);
338  1 when(storeDoc.getXObject(X509KeyWikiStore.PRIVATEKEYCLASS)).thenReturn(pkObj);
339  1 when(pkObj.getLargeStringValue(X509KeyWikiStore.PRIVATEKEYCLASS_PROP_KEY)).thenReturn(ENCODED_PRIVATEKEY);
340   
341  1 CertifiedKeyPair keyPair = store.retrieve(DOC_STORE_REF);
342  1 assertThat(keyPair, notNullValue());
343  1 assertThat(keyPair.getPrivateKey(), equalTo(privateKey));
344  1 assertThat(keyPair.getCertificate(), equalTo((CertifiedPublicKey) certificate));
345    }
346   
 
347  1 toggle @Test
348    public void retrieveEncryptedPrivateKeyFromDocument() throws Exception
349    {
350  1 XWikiDocument storeDoc = mock(XWikiDocument.class);
351  1 when(xwiki.getDocument(new DocumentReference(WIKI, SPACE, DOCUMENT), xcontext)).thenReturn(storeDoc);
352   
353  1 BaseObject certObj = mock(BaseObject.class);
354  1 when(storeDoc.getXObject(X509CertificateWikiStore.CERTIFICATECLASS)).thenReturn(certObj);
355  1 when(certObj.getLargeStringValue(X509CertificateWikiStore.CERTIFICATECLASS_PROP_CERTIFICATE)).thenReturn(ENCODED_CERTIFICATE);
356   
357  1 BaseObject pkObj = mock(BaseObject.class);
358  1 when(storeDoc.getXObject(X509KeyWikiStore.PRIVATEKEYCLASS)).thenReturn(pkObj);
359  1 when(pkObj.getLargeStringValue(X509KeyWikiStore.PRIVATEKEYCLASS_PROP_KEY)).thenReturn(ENCODED_ENCRYPTED_PRIVATEKEY);
360   
361  1 CertifiedKeyPair keyPair = store.retrieve(DOC_STORE_REF, PASSWORD);
362  1 assertThat(keyPair, notNullValue());
363  1 assertThat(keyPair.getPrivateKey(), equalTo(privateKey));
364  1 assertThat(keyPair.getCertificate(), equalTo((CertifiedPublicKey) certificate));
365    }
366   
 
367  1 toggle @Test
368    public void retrievePrivateKeyFromSpace() throws Exception
369    {
370  1 XWikiDocument storeDoc = mock(XWikiDocument.class);
371  1 when(xwiki.getDocument(new DocumentReference(WIKI, SPACE, ENCODED_SUBJECTKEYID), xcontext)).thenReturn(storeDoc);
372   
373  1 BaseObject pkObj = mock(BaseObject.class);
374  1 when(storeDoc.getXObject(X509KeyWikiStore.PRIVATEKEYCLASS)).thenReturn(pkObj);
375  1 when(pkObj.getLargeStringValue(X509KeyWikiStore.PRIVATEKEYCLASS_PROP_KEY)).thenReturn(ENCODED_PRIVATEKEY);
376   
377  1 when(query.<Object[]>execute()).thenReturn(Collections.singletonList(new Object[]{"space." + ENCODED_SUBJECTKEYID, 0}));
378   
379  1 CertifiedKeyPair keyPair = store.retrieve(SPACE_STORE_REF, certificate);
380  1 assertThat(keyPair, notNullValue());
381  1 assertThat(keyPair.getPrivateKey(), equalTo(privateKey));
382  1 assertThat(keyPair.getCertificate(), equalTo((CertifiedPublicKey) certificate));
383    }
384   
 
385  1 toggle @Test
386    public void retrieveEncryptedPrivateKeyFromSpace() throws Exception
387    {
388  1 XWikiDocument storeDoc = mock(XWikiDocument.class);
389  1 when(xwiki.getDocument(new DocumentReference(WIKI, SPACE, ENCODED_SUBJECTKEYID), xcontext)).thenReturn(storeDoc);
390   
391  1 BaseObject pkObj = mock(BaseObject.class);
392  1 when(storeDoc.getXObject(X509KeyWikiStore.PRIVATEKEYCLASS)).thenReturn(pkObj);
393  1 when(pkObj.getLargeStringValue(X509KeyWikiStore.PRIVATEKEYCLASS_PROP_KEY)).thenReturn(ENCODED_ENCRYPTED_PRIVATEKEY);
394   
395  1 when(query.<Object[]>execute()).thenReturn(Collections.singletonList(new Object[]{"space." + ENCODED_SUBJECTKEYID, 0}));
396   
397  1 CertifiedKeyPair keyPair = store.retrieve(SPACE_STORE_REF, certificate, PASSWORD);
398  1 assertThat(keyPair, notNullValue());
399  1 assertThat(keyPair.getPrivateKey(), equalTo(privateKey));
400  1 assertThat(keyPair.getCertificate(), equalTo((CertifiedPublicKey) certificate));
401    }
402   
 
403  1 toggle @Test
404    public void retrieveMissingPrivateKeyFromDocument() throws Exception
405    {
406  1 XWikiDocument storeDoc = mock(XWikiDocument.class);
407  1 when(xwiki.getDocument(new DocumentReference(WIKI, SPACE, DOCUMENT), xcontext)).thenReturn(storeDoc);
408   
409  1 BaseObject certObj = mock(BaseObject.class);
410  1 when(storeDoc.getXObject(X509CertificateWikiStore.CERTIFICATECLASS)).thenReturn(certObj);
411  1 when(certObj.getLargeStringValue(X509CertificateWikiStore.CERTIFICATECLASS_PROP_CERTIFICATE)).thenReturn(ENCODED_CERTIFICATE);
412   
413  1 CertifiedKeyPair keyPair = store.retrieve(DOC_STORE_REF);
414  1 assertThat(keyPair, nullValue());
415    }
416   
 
417  1 toggle @Test
418    public void retrieveMissingCertificateFromDocument() throws Exception
419    {
420  1 XWikiDocument storeDoc = mock(XWikiDocument.class);
421  1 when(xwiki.getDocument(new DocumentReference(WIKI, SPACE, DOCUMENT), xcontext)).thenReturn(storeDoc);
422   
423  1 BaseObject pkObj = mock(BaseObject.class);
424  1 when(storeDoc.getXObject(X509KeyWikiStore.PRIVATEKEYCLASS)).thenReturn(pkObj);
425  1 when(pkObj.getLargeStringValue(X509KeyWikiStore.PRIVATEKEYCLASS_PROP_KEY)).thenReturn(ENCODED_ENCRYPTED_PRIVATEKEY);
426   
427  1 CertifiedKeyPair keyPair = store.retrieve(DOC_STORE_REF);
428  1 assertThat(keyPair, nullValue());
429    }
430   
 
431  1 toggle @Test
432    public void retrieveMissingPrivateKeyFromSpace() throws Exception
433    {
434  1 XWikiDocument storeDoc = mock(XWikiDocument.class);
435  1 when(xwiki.getDocument(new DocumentReference(WIKI, SPACE, ENCODED_SUBJECTKEYID), xcontext)).thenReturn(storeDoc);
436   
437  1 when(query.<Object[]>execute()).thenReturn(Collections.singletonList(new Object[]{"space." + ENCODED_SUBJECTKEYID, 0}));
438   
439  1 CertifiedKeyPair keyPair = store.retrieve(SPACE_STORE_REF, certificate);
440  1 assertThat(keyPair, nullValue());
441    }
442   
 
443  1 toggle @Test
444    public void retrieveMissingCertificateFromSpace() throws Exception
445    {
446  1 CertifiedKeyPair keyPair = store.retrieve(SPACE_STORE_REF, certificate);
447  1 assertThat(keyPair, nullValue());
448    }
449   
450    }