1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.crypto.store.wiki.internal; |
21 |
|
|
22 |
|
import javax.inject.Provider; |
23 |
|
|
24 |
|
import org.junit.Before; |
25 |
|
import org.junit.Rule; |
26 |
|
import org.junit.Test; |
27 |
|
import org.xwiki.crypto.BinaryStringEncoder; |
28 |
|
import org.xwiki.crypto.store.SignatureStore; |
29 |
|
import org.xwiki.model.EntityType; |
30 |
|
import org.xwiki.model.reference.BlockReference; |
31 |
|
import org.xwiki.model.reference.DocumentReference; |
32 |
|
import org.xwiki.model.reference.EntityReference; |
33 |
|
import org.xwiki.model.reference.EntityReferenceProvider; |
34 |
|
import org.xwiki.model.reference.WikiReference; |
35 |
|
import org.xwiki.test.annotation.ComponentList; |
36 |
|
import org.xwiki.test.mockito.MockitoComponentMockingRule; |
37 |
|
|
38 |
|
import com.xpn.xwiki.XWiki; |
39 |
|
import com.xpn.xwiki.XWikiContext; |
40 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
41 |
|
import com.xpn.xwiki.internal.model.reference.CurrentReferenceDocumentReferenceResolver; |
42 |
|
import com.xpn.xwiki.internal.model.reference.CurrentReferenceEntityReferenceResolver; |
43 |
|
import com.xpn.xwiki.objects.BaseObject; |
44 |
|
|
45 |
|
import static org.hamcrest.CoreMatchers.equalTo; |
46 |
|
import static org.hamcrest.CoreMatchers.nullValue; |
47 |
|
import static org.junit.Assert.assertThat; |
48 |
|
import static org.mockito.ArgumentMatchers.any; |
49 |
|
import static org.mockito.ArgumentMatchers.eq; |
50 |
|
import static org.mockito.Mockito.mock; |
51 |
|
import static org.mockito.Mockito.never; |
52 |
|
import static org.mockito.Mockito.verify; |
53 |
|
import static org.mockito.Mockito.when; |
54 |
|
|
55 |
|
|
56 |
|
@link |
57 |
|
|
58 |
|
@version |
59 |
|
@since |
60 |
|
|
61 |
|
@ComponentList({ CurrentReferenceDocumentReferenceResolver.class, CurrentReferenceEntityReferenceResolver.class }) |
|
|
| 100% |
Uncovered Elements: 0 (45) |
Complexity: 5 |
Complexity Density: 0.12 |
|
62 |
|
public class DefaultSignatureStoreTest |
63 |
|
{ |
64 |
|
private static final byte[] SIGNATURE = "signature".getBytes(); |
65 |
|
|
66 |
|
private static final String ENCODED_SIGNATURE = "encoded_signature"; |
67 |
|
|
68 |
|
private static final WikiReference WIKI_REFERENCE = new WikiReference("wiki"); |
69 |
|
private static final EntityReference SPACE_REFERENCE = new EntityReference("space", EntityType.WIKI); |
70 |
|
private static final EntityReference DOCUMENT_REFERENCE = new EntityReference("documents", EntityType.DOCUMENT); |
71 |
|
|
72 |
|
@Rule |
73 |
|
public MockitoComponentMockingRule<SignatureStore> mocker = |
74 |
|
new MockitoComponentMockingRule<SignatureStore>(DefaultSignatureStore.class); |
75 |
|
|
76 |
|
private XWikiContext xcontext; |
77 |
|
|
78 |
|
private XWiki xwiki; |
79 |
|
|
80 |
|
private SignatureStore store; |
81 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (14) |
Complexity: 1 |
Complexity Density: 0.07 |
|
82 |
4 |
@Before... |
83 |
|
public void setUp() throws Exception |
84 |
|
{ |
85 |
4 |
EntityReferenceProvider valueProvider = mock(EntityReferenceProvider.class); |
86 |
4 |
when(valueProvider.getDefaultReference(EntityType.WIKI)).thenReturn(WIKI_REFERENCE); |
87 |
4 |
when(valueProvider.getDefaultReference(EntityType.SPACE)).thenReturn(SPACE_REFERENCE); |
88 |
4 |
when(valueProvider.getDefaultReference(EntityType.DOCUMENT)).thenReturn(DOCUMENT_REFERENCE); |
89 |
|
|
90 |
4 |
this.mocker.registerComponent(EntityReferenceProvider.class, "current", valueProvider); |
91 |
|
|
92 |
4 |
Provider<XWikiContext> xcontextProvider = |
93 |
|
this.mocker.registerMockComponent(XWikiContext.TYPE_PROVIDER); |
94 |
4 |
this.xcontext = mock(XWikiContext.class); |
95 |
4 |
when(xcontextProvider.get()).thenReturn(this.xcontext); |
96 |
4 |
this.xwiki = mock(com.xpn.xwiki.XWiki.class); |
97 |
4 |
when(this.xcontext.getWiki()).thenReturn(this.xwiki); |
98 |
|
|
99 |
4 |
BinaryStringEncoder encoder = this.mocker.getInstance(BinaryStringEncoder.class, "Base64"); |
100 |
4 |
when(encoder.encode(SIGNATURE, 64)).thenReturn(ENCODED_SIGNATURE); |
101 |
4 |
when(encoder.decode(ENCODED_SIGNATURE)).thenReturn(SIGNATURE); |
102 |
|
|
103 |
4 |
this.store = this.mocker.getComponentUnderTest(); |
104 |
|
} |
105 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
1PASS
|
|
106 |
1 |
@Test... |
107 |
|
public void testStoringNewSignature() throws Exception |
108 |
|
{ |
109 |
1 |
XWikiDocument sourceDocument = mock(XWikiDocument.class); |
110 |
1 |
when(this.xwiki.getDocument(new DocumentReference("wiki", "space", "document"), this.xcontext)) |
111 |
|
.thenReturn(sourceDocument); |
112 |
|
|
113 |
1 |
BaseObject signatureObject = mock(BaseObject.class); |
114 |
1 |
when(sourceDocument.newXObject(DefaultSignatureStore.SIGNATURECLASS, this.xcontext)) |
115 |
|
.thenReturn(signatureObject); |
116 |
|
|
117 |
1 |
this.store.store(new BlockReference("block", new DocumentReference("wiki", "space", "document")), SIGNATURE); |
118 |
|
|
119 |
1 |
verify(signatureObject).setStringValue(DefaultSignatureStore.SIGNATURECLASS_PROP_REFERENCE, "block"); |
120 |
1 |
verify(signatureObject).setLargeStringValue(DefaultSignatureStore.SIGNATURECLASS_PROP_SIGNATURE, |
121 |
|
ENCODED_SIGNATURE); |
122 |
|
|
123 |
1 |
verify(this.xwiki).saveDocument(sourceDocument, this.xcontext); |
124 |
|
} |
125 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
1PASS
|
|
126 |
1 |
@Test... |
127 |
|
public void testUpdatingSignature() throws Exception |
128 |
|
{ |
129 |
1 |
XWikiDocument sourceDocument = mock(XWikiDocument.class); |
130 |
1 |
when(this.xwiki.getDocument(new DocumentReference("wiki", "space", "document"), this.xcontext)) |
131 |
|
.thenReturn(sourceDocument); |
132 |
|
|
133 |
1 |
BaseObject signatureObject = mock(BaseObject.class); |
134 |
1 |
when(sourceDocument.getXObject( |
135 |
|
new DocumentReference(DefaultSignatureStore.SIGNATURECLASS, new WikiReference("wiki")), |
136 |
|
DefaultSignatureStore.SIGNATURECLASS_PROP_REFERENCE, |
137 |
|
"block")).thenReturn(signatureObject); |
138 |
|
|
139 |
1 |
this.store.store(new BlockReference("block", new DocumentReference("wiki", "space", "document")), SIGNATURE); |
140 |
|
|
141 |
1 |
verify(signatureObject, never()).setStringValue(eq(DefaultSignatureStore.SIGNATURECLASS_PROP_REFERENCE), |
142 |
|
any(String.class)); |
143 |
1 |
verify(signatureObject).setLargeStringValue(DefaultSignatureStore.SIGNATURECLASS_PROP_SIGNATURE, |
144 |
|
ENCODED_SIGNATURE); |
145 |
|
|
146 |
1 |
verify(this.xwiki).saveDocument(sourceDocument, this.xcontext); |
147 |
|
} |
148 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
149 |
1 |
@Test... |
150 |
|
public void testRetrievingExistingSignature() throws Exception |
151 |
|
{ |
152 |
1 |
XWikiDocument sourceDocument = mock(XWikiDocument.class); |
153 |
1 |
when(this.xwiki.getDocument(new DocumentReference("wiki", "space", "document"), this.xcontext)) |
154 |
|
.thenReturn(sourceDocument); |
155 |
|
|
156 |
1 |
BaseObject signatureObject = mock(BaseObject.class); |
157 |
1 |
when(sourceDocument.getXObject( |
158 |
|
new DocumentReference(DefaultSignatureStore.SIGNATURECLASS, new WikiReference("wiki")), |
159 |
|
DefaultSignatureStore.SIGNATURECLASS_PROP_REFERENCE, |
160 |
|
"block")).thenReturn(signatureObject); |
161 |
|
|
162 |
1 |
when(signatureObject.getLargeStringValue(DefaultSignatureStore.SIGNATURECLASS_PROP_SIGNATURE)).thenReturn( |
163 |
|
ENCODED_SIGNATURE); |
164 |
|
|
165 |
1 |
assertThat( |
166 |
|
this.store.retrieve(new BlockReference("block", new DocumentReference("wiki", "space", "document"))), |
167 |
|
equalTo(SIGNATURE)); |
168 |
|
} |
169 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
170 |
1 |
@Test... |
171 |
|
public void testRetrievingMissingSignature() throws Exception |
172 |
|
{ |
173 |
1 |
XWikiDocument sourceDocument = mock(XWikiDocument.class); |
174 |
1 |
when(this.xwiki.getDocument(new DocumentReference("wiki", "space", "document"), this.xcontext)) |
175 |
|
.thenReturn(sourceDocument); |
176 |
|
|
177 |
1 |
assertThat( |
178 |
|
this.store.retrieve(new BlockReference("block", new DocumentReference("wiki", "space", "document"))), |
179 |
|
nullValue()); |
180 |
1 |
verify(sourceDocument).getXObject( |
181 |
|
new DocumentReference(DefaultSignatureStore.SIGNATURECLASS, new WikiReference("wiki")), |
182 |
|
DefaultSignatureStore.SIGNATURECLASS_PROP_REFERENCE, |
183 |
|
"block"); |
184 |
|
} |
185 |
|
} |