-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTestKmip.java
More file actions
185 lines (161 loc) · 8.74 KB
/
TestKmip.java
File metadata and controls
185 lines (161 loc) · 8.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package com.cosmian;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Arrays;
import java.util.Optional;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import com.cosmian.rest.kmip.data_structures.KeyBlock;
import com.cosmian.rest.kmip.data_structures.KeyMaterial;
import com.cosmian.rest.kmip.data_structures.KeyValue;
import com.cosmian.rest.kmip.data_structures.TransparentSymmetricKey;
import com.cosmian.rest.kmip.json.KmipStruct;
import com.cosmian.rest.kmip.objects.SymmetricKey;
import com.cosmian.rest.kmip.operations.GetResponse;
import com.cosmian.rest.kmip.operations.Import;
import com.cosmian.rest.kmip.operations.ImportResponse;
import com.cosmian.rest.kmip.operations.TestStruct;
import com.cosmian.rest.kmip.types.Attributes;
import com.cosmian.rest.kmip.types.CryptographicAlgorithm;
import com.cosmian.rest.kmip.types.KeyFormatType;
import com.cosmian.rest.kmip.types.KeyWrapType;
import com.cosmian.rest.kmip.types.Link;
import com.cosmian.rest.kmip.types.LinkType;
import com.cosmian.rest.kmip.types.LinkedObjectIdentifier;
import com.cosmian.rest.kmip.types.ObjectType;
import com.cosmian.rest.kmip.types.UniqueIdentifier;
import com.cosmian.utils.Resources;
import com.fasterxml.classmate.MemberResolver;
import com.fasterxml.classmate.ResolvedType;
import com.fasterxml.classmate.ResolvedTypeWithMembers;
import com.fasterxml.classmate.TypeResolver;
import com.fasterxml.classmate.members.ResolvedField;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class TestKmip {
@BeforeAll
public static void before_all() {
TestUtils.initLogging();
}
private SymmetricKey symmetricKey() {
KeyValue kv =
new KeyValue(new KeyMaterial(new TransparentSymmetricKey("bytes".getBytes())), Optional.empty());
SymmetricKey symmetricKey = new SymmetricKey(new KeyBlock(KeyFormatType.TransparentSymmetricKey,
Optional.empty(), kv, CryptographicAlgorithm.AES, 256, Optional.empty()
));
return symmetricKey;
}
@Test
public void test_serialization_deserialization() throws Exception {
String unique_identifier = "unique_identifier";
ObjectType object_type = ObjectType.Symmetric_Key;
Optional<Boolean> replace_existing = Optional.of(Boolean.TRUE);
Optional<KeyWrapType> key_wrap_type = Optional.of(KeyWrapType.As_Registered);
Attributes attributes = new Attributes(object_type, Optional.of(CryptographicAlgorithm.AES));
SymmetricKey symmetricKey = symmetricKey();
Import import_request =
new Import(unique_identifier, object_type, replace_existing, key_wrap_type, attributes, symmetricKey);
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(import_request);
System.out.println(json);
Import direct_deserialized = mapper.readValue(json, Import.class);
assertEquals(unique_identifier, direct_deserialized.getUniqueIdentifier());
assertEquals(object_type, direct_deserialized.getObjectType());
assertEquals(replace_existing, direct_deserialized.getReplaceExisting());
assertEquals(key_wrap_type, direct_deserialized.getKeyWrapType());
assertEquals(symmetricKey, (SymmetricKey) direct_deserialized.getObject());
KmipStruct indirect_deserialized = mapper.readValue(json, KmipStruct.class);
assertEquals(Import.class, indirect_deserialized.getClass());
Import deserialized = (Import) indirect_deserialized;
assertEquals(unique_identifier, deserialized.getUniqueIdentifier());
assertEquals(object_type, deserialized.getObjectType());
assertEquals(replace_existing, deserialized.getReplaceExisting());
assertEquals(key_wrap_type, deserialized.getKeyWrapType());
assertEquals(symmetricKey, (SymmetricKey) deserialized.getObject());
}
@Test
public void test_required() throws Exception {
String unique_identifier = "unique_identifier";
ObjectType object_type = ObjectType.Symmetric_Key;
Optional<Boolean> replace_existing = Optional.of(Boolean.TRUE);
Optional<KeyWrapType> key_wrap_type = Optional.of(KeyWrapType.As_Registered);
Attributes attributes = new Attributes(object_type, Optional.of(CryptographicAlgorithm.AES));
Import import_request =
new Import(unique_identifier, object_type, replace_existing, key_wrap_type, attributes, null);
ObjectMapper mapper = new ObjectMapper();
try {
mapper.writeValueAsString(import_request);
throw new RuntimeException("should have thrown on NULL Object which is required");
} catch (JsonMappingException e) {
// fine
}
}
@Test
public void test_not_required() throws Exception {
String unique_identifier = "unique_identifier";
ObjectType object_type = ObjectType.Symmetric_Key;
Optional<Boolean> replace_existing = Optional.empty();
Optional<KeyWrapType> key_wrap_type = Optional.empty();
Attributes attributes = new Attributes(object_type, Optional.of(CryptographicAlgorithm.AES));
Import import_request =
new Import(unique_identifier, object_type, replace_existing, key_wrap_type, attributes, symmetricKey());
// ReplaceExisting KeyWrapType is not required
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(import_request);
System.out.println(json);
Import direct_deserialized = mapper.readValue(json, Import.class);
assertTrue(!direct_deserialized.getKeyWrapType().isPresent());
}
@Test
public void test_class_mate() throws Exception {
TypeResolver type_resolver = new TypeResolver();
ResolvedType resolved_test_class = type_resolver.resolve(TestStruct.class);
MemberResolver member_resolver = new MemberResolver(type_resolver);
ResolvedTypeWithMembers resolved_test_class_with_members =
member_resolver.resolve(resolved_test_class, null, null);
for (ResolvedField f : resolved_test_class_with_members.getMemberFields()) {
if (f.getName().equals("opt_string")) {
Class<?> clazz = f.getType().getTypeParameters().get(0).getErasedType();
assertEquals(String.class, clazz);
}
}
}
@Test
public void test_choice_optional_array() throws Exception {
LinkedObjectIdentifier loi = new LinkedObjectIdentifier(UniqueIdentifier.ID_Placeholder);
assertEquals(UniqueIdentifier.ID_Placeholder, loi.get());
Link link_1 = new Link(LinkType.Public_Key_Link, new LinkedObjectIdentifier(UniqueIdentifier.ID_Placeholder));
Link link_2 = new Link(LinkType.Certificate_Link, new LinkedObjectIdentifier("cert"));
TestStruct test_struct = new TestStruct(Optional.of("blah"), new Link[] {link_1, link_2});
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(test_struct);
System.out.println(json);
TestStruct deserialized = mapper.readValue(json, TestStruct.class);
assertEquals(test_struct.getUnique_identifier(), deserialized.getUnique_identifier());
assertTrue(Arrays.equals(test_struct.getLink(), deserialized.getLink()));
}
@Test
public void test_import_response_from_rust() throws Exception {
String json =
"{\"tag\":\"ImportResponse\",\"type\":\"Structure\",\"value\":[{\"tag\":\"UniqueIdentifier\",\"type\":\"TextString\",\"value\":\"blah\"}]}";
ObjectMapper mapper = new ObjectMapper();
ImportResponse ir = mapper.readValue(json, ImportResponse.class);
assertEquals("blah", ir.getUniqueIdentifier());
}
@Test
public void deserialize_rust_symmetric_key_response() throws Exception {
String response_json = Resources.load_resource("kmip/rust_get_response_symmetric_key.json");
ObjectMapper mapper = new ObjectMapper();
GetResponse gr = mapper.readValue(response_json, GetResponse.class);
assertEquals(ObjectType.Symmetric_Key, gr.getObjectType());
SymmetricKey key = (SymmetricKey) gr.getObject();
Attributes attributes = key.attributes();
assertEquals(4, attributes.getCryptographicUsageMask().get());
KeyMaterial material = key.getKeyBlock().getKeyValue().getKeyMaterial();
assertTrue(material.get() instanceof TransparentSymmetricKey);
TransparentSymmetricKey tsk = (TransparentSymmetricKey) material.get();
assertNotNull(tsk.getKey());
assertTrue(tsk.getKey().length > 0);
}
}