Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
JniGetAttributesKmip.java
Go to the documentation of this file.
1 package com.p6r.kmipserverlib;
2 
3 import org.junit.*;
4 
5 import java.nio.charset.Charset;
6 import java.util.Date;
7 
8 import static org.junit.Assert.assertEquals;
9 
19 public class JniGetAttributesKmip {
20 
21  @BeforeClass
22  public static void oneTimeSetUp() {
23  // NOOP
24  System.out.println("@BeforeClass - oneTimeSetUp");
25  }
26 
27  @AfterClass
28  public static void oneTimeTearDown() {
29  // NOOP
30  System.out.println("@AfterClass - oneTimeTearDown");
31  }
32 
33  @Before
34  public void setUp() {
35  // NOOP
36  System.out.println("@Before - setUp");
37  }
38 
39  @After
40  public void tearDown() {
41  // NOOP
42  System.out.println("@After - tearDown");
43  }
44 
50  @Test
51  public void JNICall_GetAttributesKMIP() {
52  System.out.println("@Test - JNICall-GetAttributesKMIP");
53 
54  // -> this parser is multi-thread safe by using JNI monitor locking
55  // -> use one parser object per server thread is recommended
56  P6KMIPServerLib sl = new P6KMIPServerLib();
57 
58  // -> KMIP 1.4 but also works in 1.3 XML message with 1 batch item generated by P6R's Secure KMIP Client (SKC)
59  String testMessage1 = "<RequestMessage><RequestHeader><ProtocolVersion><ProtocolVersionMajor type=\"Integer\" value=\"1\"/><ProtocolVersionMinor type=\"Integer\" value=\"4\"/></ProtocolVersion><BatchCount type=\"Integer\" value=\"1\"/></RequestHeader><BatchItem><Operation type=\"Enumeration\" value=\"GetAttributes\"/><RequestPayload><UniqueIdentifier type=\"TextString\" value=\"941e4340-c60b-4aea-99e9-3a10de99ccc8\"/><AttributeName type=\"TextString\" value=\"State\"/><AttributeName type=\"TextString\" value=\"Cryptographic Usage Mask\"/><AttributeName type=\"TextString\" value=\"Unique Identifier\"/><AttributeName type=\"TextString\" value=\"Object Type\"/><AttributeName type=\"TextString\" value=\"Cryptographic Algorithm\"/><AttributeName type=\"TextString\" value=\"Cryptographic Length\"/><AttributeName type=\"TextString\" value=\"Digest\"/><AttributeName type=\"TextString\" value=\"Initial Date\"/><AttributeName type=\"TextString\" value=\"Last Change Date\"/><AttributeName type=\"TextString\" value=\"Original Creation Date\"/><AttributeName type=\"TextString\" value=\"Random Number Generator\"/></RequestPayload></BatchItem></RequestMessage>";
60  String testMessage2 = "<RequestMessage><RequestHeader><ProtocolVersion><ProtocolVersionMajor type=\"Integer\" value=\"1\"/><ProtocolVersionMinor type=\"Integer\" value=\"4\"/></ProtocolVersion><BatchCount type=\"Integer\" value=\"1\"/></RequestHeader><BatchItem><Operation type=\"Enumeration\" value=\"Activate\"/><RequestPayload><UniqueIdentifier type=\"TextString\" value=\"941e4340-c60b-4aea-99e9-3a10de99ccc8\"/></RequestPayload></BatchItem></RequestMessage>";
61  String testMessage3 = "<RequestMessage><RequestHeader><ProtocolVersion><ProtocolVersionMajor type=\"Integer\" value=\"1\"/><ProtocolVersionMinor type=\"Integer\" value=\"4\"/></ProtocolVersion><BatchCount type=\"Integer\" value=\"1\"/></RequestHeader><BatchItem><Operation type=\"Enumeration\" value=\"Revoke\"/><RequestPayload><UniqueIdentifier type=\"TextString\" value=\"941e4340-c60b-4aea-99e9-3a10de99ccc8\"/><RevocationReason><RevocationReasonCode type=\"Enumeration\" value=\"KeyCompromise\"/></RevocationReason><CompromiseOccurrenceDate type=\"DateTime\" value=\"2018-03-19T14:28:07Z\"/></RequestPayload></BatchItem></RequestMessage>";
62  String testMessage4 = "<RequestMessage><RequestHeader><ProtocolVersion><ProtocolVersionMajor type=\"Integer\" value=\"1\"/><ProtocolVersionMinor type=\"Integer\" value=\"4\"/></ProtocolVersion><BatchCount type=\"Integer\" value=\"1\"/></RequestHeader><BatchItem><Operation type=\"Enumeration\" value=\"Destroy\"/><RequestPayload><UniqueIdentifier type=\"TextString\" value=\"941e4340-c60b-4aea-99e9-3a10de99ccc8\"/></RequestPayload></BatchItem></RequestMessage>";
63  String testMessage5 = "<RequestMessage><RequestHeader><ProtocolVersion><ProtocolVersionMajor type=\"Integer\" value=\"2\"/><ProtocolVersionMinor type=\"Integer\" value=\"0\"/></ProtocolVersion><BatchCount type=\"Integer\" value=\"1\"/></RequestHeader><BatchItem><Operation type=\"Enumeration\" value=\"GetAttributes\"/><RequestPayload><UniqueIdentifier type=\"TextString\" value=\"941e4340-c60b-4aea-99e9-3a10de99ccc8\"/>\n" +
64  "<AttributeReference type=\"Enumeration\" value=\"State\"/><AttributeReference type=\"Enumeration\" value=\"CryptographicUsageMask\"/>\n" +
65  "<AttributeReference type=\"Enumeration\" value=\"UniqueIdentifier\"/><AttributeReference type=\"Enumeration\" value=\"ObjectType\"/>\n" +
66  "<AttributeReference type=\"Enumeration\" value=\"CryptographicAlgorithm\"/><AttributeReference type=\"Enumeration\" value=\"CryptographicLength\"/>\n" +
67  "<AttributeReference type=\"Enumeration\" value=\"Digest\"/><AttributeReference type=\"Enumeration\" value=\"InitialDate\"/>\n" +
68  "<AttributeReference><VendorIdentification type=\"TextString\" value=\"P6R\"/><AttributeName type=\"TextString\" value=\"SKC-client\" /></AttributeReference>\n" +
69  "<AttributeReference type=\"Enumeration\" value=\"LastChangeDate\"/><AttributeReference type=\"Enumeration\" value=\"OriginalCreationDate\"/>\n" +
70  "<AttributeReference type=\"Enumeration\" value=\"RandomNumberGenerator\"/>" +
71  "</RequestPayload></BatchItem></RequestMessage>";
72 
73  try {
74  sl.initializeLibrary(P6KMIPServerLib.FLAGS_NONE);
75 
76  String libVersion = sl.getLibraryVersion();
77  System.out.println(libVersion);
78 
79  // [A] Here we test Get Attributes request
80  sl.setMessageBuffer(testMessage1.getBytes(Charset.forName("UTF-8")), KMIPConstants.FORMAT_MSGXML);
81 
82  RequestHeader rh = sl.getRequestHeader();
83  assertEquals("1.4", rh.getProtocolVersion());
84 
85  // -> parsed message is maintained in parser until another call to setMessageBuffer() of freeLibrary() is called
86  for (int i = 0; i < rh.getBatchCount(); i++) {
87 
88  BatchItem bi = sl.getBatchItem(i + 1);
89  if (bi instanceof GetAttributesBatchItem) {
90  GetAttributesBatchItem ck = (GetAttributesBatchItem) bi;
91 
92  String uniqueId = ck.getUniqueId();
93  assertEquals("941e4340-c60b-4aea-99e9-3a10de99ccc8", uniqueId);
94 
95  String[] attributes = ck.getAttributes();
96  assertEquals(11, attributes.length);
97  assertEquals("State", attributes[0]);
98  assertEquals("Cryptographic Usage Mask", attributes[1]);
99  assertEquals("Unique Identifier", attributes[2]);
100  assertEquals("Object Type", attributes[3]);
101  assertEquals("Cryptographic Algorithm", attributes[4]);
102  assertEquals("Cryptographic Length", attributes[5]);
103  assertEquals("Digest", attributes[6]);
104  assertEquals("Initial Date", attributes[7]);
105  assertEquals("Last Change Date", attributes[8]);
106  assertEquals("Original Creation Date", attributes[9]);
107  assertEquals("Random Number Generator", attributes[10]);
108 
109  MessageExtension me = ck.getExtension();
110  assertEquals(null, me);
111  }
112  }
113 
114  sl.setMessageBuffer(testMessage2.getBytes(Charset.forName("UTF-8")), KMIPConstants.FORMAT_MSGXML);
115  for (int i = 0; i < rh.getBatchCount(); i++) {
116 
117  BatchItem bi = sl.getBatchItem(i + 1);
118  if (bi instanceof ActivateBatchItem) {
119  ActivateBatchItem ck = (ActivateBatchItem) bi;
120 
121  String uniqueId = ck.getUniqueId();
122  assertEquals("941e4340-c60b-4aea-99e9-3a10de99ccc8", uniqueId);
123 
124  MessageExtension me = ck.getExtension();
125  assertEquals(null, me);
126  }
127  }
128 
129  sl.setMessageBuffer(testMessage3.getBytes(Charset.forName("UTF-8")), KMIPConstants.FORMAT_MSGXML);
130  for (int i = 0; i < rh.getBatchCount(); i++) {
131 
132  BatchItem bi = sl.getBatchItem(i + 1);
133  if (bi instanceof RevokeBatchItem) {
134  RevokeBatchItem ck = (RevokeBatchItem) bi;
135 
136  String uniqueId = ck.getUniqueId();
137  assertEquals("941e4340-c60b-4aea-99e9-3a10de99ccc8", uniqueId);
138 
139  int reasonCode = ck.getReasonCode();
140  assertEquals(KMIPConstants.REVOCATION_KEYCOMPROMISE, reasonCode);
141 
142  String message = ck.getMessage();
143  assertEquals(null, message);
144 
145  Date compromisedOn = ck.getCompromiseOccurrence();
146  System.out.println("Revoked date:" + compromisedOn);
147 
148  MessageExtension me = ck.getExtension();
149  assertEquals(null, me);
150  }
151  }
152 
153  sl.setMessageBuffer(testMessage4.getBytes(Charset.forName("UTF-8")), KMIPConstants.FORMAT_MSGXML);
154  for (int i = 0; i < rh.getBatchCount(); i++) {
155 
156  BatchItem bi = sl.getBatchItem(i + 1);
157  if (bi instanceof DestroyBatchItem) {
158  DestroyBatchItem ck = (DestroyBatchItem) bi;
159 
160  String uniqueId = ck.getUniqueId();
161  assertEquals("941e4340-c60b-4aea-99e9-3a10de99ccc8", uniqueId);
162 
163  MessageExtension me = ck.getExtension();
164  assertEquals(null, me);
165  }
166  }
167 
168  // -> parse the KMIP 2.0 version of the Get Attributes operation
169  sl.setMessageBuffer(testMessage5.getBytes(Charset.forName("UTF-8")), KMIPConstants.FORMAT_MSGXML);
170  rh = sl.getRequestHeader();
171  assertEquals("2.0", rh.getProtocolVersion());
172 
173  for (int i = 0; i < rh.getBatchCount(); i++) {
174 
175  BatchItem bi = sl.getBatchItem(i + 1);
176  if (bi instanceof GetAttributesBatchItem) {
177  GetAttributesBatchItem ck = (GetAttributesBatchItem) bi;
178 
179  String uniqueId = ck.getUniqueId();
180  assertEquals("941e4340-c60b-4aea-99e9-3a10de99ccc8", uniqueId);
181 
182  String[] attributes = ck.getAttributes();
183  assertEquals(12, attributes.length);
184  assertEquals("State", attributes[0]);
185  assertEquals("Cryptographic Usage Mask", attributes[1]);
186  assertEquals("Unique Identifier", attributes[2]);
187  assertEquals("Object Type", attributes[3]);
188  assertEquals("Cryptographic Algorithm", attributes[4]);
189  assertEquals("Cryptographic Length", attributes[5]);
190  assertEquals("Digest", attributes[6]);
191  assertEquals("Initial Date", attributes[7]);
192  assertEquals("P6R-SKC-client", attributes[8]);
193  assertEquals("Last Change Date", attributes[9]);
194  assertEquals("Original Creation Date", attributes[10]);
195  assertEquals("Random Number Generator", attributes[11]);
196 
197  MessageExtension me = ck.getExtension();
198  assertEquals(null, me);
199  }
200  }
201 
202  sl.freeLibrary();
203 
204  } catch (Exception e) {
205  // -> we shoud not get here
206  System.out.println(e.toString());
207  assertEquals(0, 1);
208  }
209  }
210 }
void JNICall_GetAttributesKMIP()
Test: Verify parser can handle an XML formated Get Attributes operation.
A JUNIT test demonstrating how to parse an incoming KMIP request from a client.