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

File X509KeyWikiStoreTest.java

 

Code metrics

0
185
15
1
453
333
15
0.08
12.33
15
1

Classes

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