Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
JniDeleteAttributeKmip.java
Go to the documentation of this file.
1 package com.p6r.kmipserverlib;
2 
3 import org.junit.*;
4 
5 import javax.security.auth.x500.X500Principal;
6 import java.io.ByteArrayInputStream;
7 import java.io.InputStream;
8 import java.nio.charset.Charset;
9 import java.security.cert.CertificateFactory;
10 import java.security.cert.X509Certificate;
11 
12 import static org.junit.Assert.assertEquals;
13 
23 public class JniDeleteAttributeKmip {
24 
25  @BeforeClass
26  public static void oneTimeSetUp() {
27  // NOOP
28  System.out.println("@BeforeClass - oneTimeSetUp");
29  }
30 
31  @AfterClass
32  public static void oneTimeTearDown() {
33  // NOOP
34  System.out.println("@AfterClass - oneTimeTearDown");
35  }
36 
37  @Before
38  public void setUp() {
39  // NOOP
40  System.out.println("@Before - setUp");
41  }
42 
43  @After
44  public void tearDown() {
45  // NOOP
46  System.out.println("@After - tearDown");
47  }
48 
54  @Test
56  System.out.println("@Test - JNICall-DeleteAttributeKMIP");
57 
58  P6KMIPServerLib sl = new P6KMIPServerLib();
59 
60  // -> KMIP 1.3 TTLV message with 1 batch item generated by P6R's Secure KMIP Client (SKC)
61  byte[] testMessage = {
62  0x42, 0x00, 0x78, 0x01, 0x00, 0x00, 0x00, (byte) 0xc0, 0x42, 0x00, 0x77, 0x01, 0x00, 0x00, 0x00, 0x58, 0x42, 0x00, 0x69, 0x01, 0x00, 0x00, 0x00, 0x20, 0x42, 0x00, 0x6a, 0x02, 0x00, 0x00, 0x00, 0x04,
63  0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x6b, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x50, 0x02, 0x00, 0x00, 0x00, 0x04,
64  0x00, 0x00, 0x27, 0x10, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, (byte) 0x92, 0x09, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x5a, 0x29, (byte) 0xcf, (byte) 0xe7, 0x42, 0x00, 0x0d, 0x02, 0x00, 0x00, 0x00, 0x04,
65  0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x0f, 0x01, 0x00, 0x00, 0x00, 0x58, 0x42, 0x00, 0x5c, 0x05, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x00,
66  0x42, 0x00, 0x79, 0x01, 0x00, 0x00, 0x00, 0x40, 0x42, 0x00, (byte) 0x94, 0x07, 0x00, 0x00, 0x00, 0x14, 0x41, 0x57, 0x41, 0x78, 0x77, 0x36, 0x47, 0x6b, 0x6f, 0x45, 0x67, 0x45, 0x36, 0x79, 0x65, 0x6b,
67  0x35, 0x61, 0x4c, 0x6a, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x0a, 0x07, 0x00, 0x00, 0x00, 0x13, 0x43, 0x6f, 0x6e, 0x74, 0x61, 0x63, 0x74, 0x20, 0x49, 0x6e, 0x66, 0x6f, 0x72, 0x6d, 0x61, 0x74,
68  0x69, 0x6f, 0x6e, 0x00, 0x00, 0x00, 0x00, 0x00
69  };
70 
71  // -> test the different forms of the KMIP 2.0 Delete Attribute operation
72  String testMessage20_1 = "<RequestMessage>\n" +
73  "<RequestHeader>\n" +
74  " <ProtocolVersion>\n" +
75  " <ProtocolVersionMajor type=\"Integer\" value=\"2\"/>\n" +
76  " <ProtocolVersionMinor type=\"Integer\" value=\"0\"/>\n" +
77  " </ProtocolVersion>\n" +
78  " <BatchCount type=\"Integer\" value=\"1\"/>\n" +
79  "</RequestHeader>\n" +
80  "<BatchItem>\n" +
81  " <Operation type=\"Enumeration\" value=\"DeleteAttribute\"/>\n" +
82  " <RequestPayload>\n" +
83  " <UniqueIdentifier type=\"TextString\" value=\"e82a80f0-fe20-4cbb-a1b6-00e3a81a7e9f\"/>\n" +
84  " <CurrentAttribute>\n" +
85  " <Name>\n" +
86  " <NameValue type=\"TextString\" value=\"Fake-Modify-test-23\"/>\n" +
87  " <NameType type=\"Enumeration\" value=\"UninterpretedTextString\"/>\n" +
88  " </Name>\n" +
89  " </CurrentAttribute>\n" +
90  " </RequestPayload>\n" +
91  "</BatchItem>\n" +
92  "</RequestMessage>";
93 
94  String testMessage20_2 = "<RequestMessage>\n" +
95  "<RequestHeader>\n" +
96  " <ProtocolVersion>\n" +
97  " <ProtocolVersionMajor type=\"Integer\" value=\"2\"/>\n" +
98  " <ProtocolVersionMinor type=\"Integer\" value=\"0\"/>\n" +
99  " </ProtocolVersion>\n" +
100  " <BatchCount type=\"Integer\" value=\"1\"/>\n" +
101  "</RequestHeader>\n" +
102  "<BatchItem>\n" +
103  " <Operation type=\"Enumeration\" value=\"DeleteAttribute\"/>\n" +
104  " <RequestPayload>\n" +
105  " <UniqueIdentifier type=\"TextString\" value=\"e82a80f0-fe20-4cbb-a1b6-00e3a81a7e9f\"/>\n" +
106  " <AttributeReference type=\"Enumeration\" value=\"AlternativeName\"/>\n" +
107  " </RequestPayload>\n" +
108  "</BatchItem>\n" +
109  "</RequestMessage>";
110 
111  String testMessage20_3 = "<RequestMessage>\n" +
112  "<RequestHeader>\n" +
113  " <ProtocolVersion>\n" +
114  " <ProtocolVersionMajor type=\"Integer\" value=\"2\"/>\n" +
115  " <ProtocolVersionMinor type=\"Integer\" value=\"0\"/>\n" +
116  " </ProtocolVersion>\n" +
117  " <BatchCount type=\"Integer\" value=\"1\"/>\n" +
118  "</RequestHeader>\n" +
119  "<BatchItem>\n" +
120  " <Operation type=\"Enumeration\" value=\"DeleteAttribute\"/>\n" +
121  " <RequestPayload>\n" +
122  " <UniqueIdentifier type=\"TextString\" value=\"e82a80f0-fe20-4cbb-a1b6-00e3a81a7e9f\"/>\n" +
123  " <AttributeReference><VendorIdentification type=\"TextString\" value=\"P6R\"/><AttributeName type=\"TextString\" value=\"SKC-client\" /></AttributeReference>\n" +
124  " </RequestPayload>\n" +
125  "</BatchItem>\n" +
126  "</RequestMessage>";
127 
128 
129  try {
130  sl.initializeLibrary(P6KMIPServerLib.FLAGS_NONE);
131 
132  String libVersion = sl.getLibraryVersion();
133  System.out.println(libVersion);
134 
135  // [A] Test delete attribute KMIP 1.x version
136  // -> server read incoming KMIP request message from a socket and loaded those bytes (e.g., TTLV, XML, JSON) into the parser)
137  // -> the type of message: TTLC, XML, JSON can be determine by the mime type passed in the HTTP request, or lack of one if just using SSL connection
138  sl.setMessageBuffer(testMessage, KMIPConstants.FORMAT_TTLV);
139 
140  // -> now we can pull parts of the request message apart, this can be done over and over again if desired
141  RequestHeader rh = sl.getRequestHeader();
142  assertEquals("1.0", rh.getProtocolVersion());
143 
144  // -> parsed message is maintained in parser until another call to setMessageBuffer() of freeLibrary() is called
145  // -> register certificate request with out any attributes sent with the certificate
146  for (int i = 0; i < rh.getBatchCount(); i++) {
147 
148  BatchItem bi = sl.getBatchItem(i + 1);
149  if (bi instanceof DeleteAttributeBatchItem) {
150  DeleteAttributeBatchItem rb = (DeleteAttributeBatchItem) bi;
151  // -> there is no batch id required if there is only one batch item in a message
152  assertEquals(null, rb.getUniqueBatchId());
153 
154  String uniqueId = rb.getUniqueId();
155  assertEquals("AWAxw6GkoEgE6yek5aLj", uniqueId);
156 
157  String attribName = rb.getAttributeName();
158  assertEquals("Contact Information", attribName);
159 
160  int index = rb.getIndex();
161  assertEquals(0, index);
162  }
163  }
164 
165  // [B] Test KMIP 2.0 version of delete attribute operation, test various versions of the operation
166  // -> current attribute only
167  sl.setMessageBuffer(testMessage20_1.getBytes(Charset.forName("UTF-8")), KMIPConstants.FORMAT_MSGXML);
168  rh = sl.getRequestHeader();
169  assertEquals("2.0", rh.getProtocolVersion());
170 
171  for (int i = 0; i < rh.getBatchCount(); i++) {
172 
173  BatchItem bi = sl.getBatchItem(i + 1);
174  if (bi instanceof DeleteAttribute2BatchItem) {
175  DeleteAttribute2BatchItem rb = (DeleteAttribute2BatchItem) bi;
176 
177  String uniqueId = rb.getUniqueId();
178  assertEquals("e82a80f0-fe20-4cbb-a1b6-00e3a81a7e9f", uniqueId);
179 
180  String currentAttribute = rb.getCurrentAttribute();
181  assertEquals("Name: Fake-Modify-test-23 - text_string", currentAttribute);
182 
183  String attributeReference = rb.getAttributeReference();
184  assertEquals(null, attributeReference);
185  }
186  }
187 
188  // -> attribute reference as a text string
189  sl.setMessageBuffer(testMessage20_2.getBytes(Charset.forName("UTF-8")), KMIPConstants.FORMAT_MSGXML);
190  rh = sl.getRequestHeader();
191  assertEquals("2.0", rh.getProtocolVersion());
192 
193  for (int i = 0; i < rh.getBatchCount(); i++) {
194 
195  BatchItem bi = sl.getBatchItem(i + 1);
196  if (bi instanceof DeleteAttribute2BatchItem) {
197  DeleteAttribute2BatchItem rb = (DeleteAttribute2BatchItem) bi;
198 
199  String uniqueId = rb.getUniqueId();
200  assertEquals("e82a80f0-fe20-4cbb-a1b6-00e3a81a7e9f", uniqueId);
201 
202  String currentAttribute = rb.getCurrentAttribute();
203  assertEquals(null, currentAttribute);
204 
205  String attributeReference = rb.getAttributeReference();
206  assertEquals("Alternative Name", attributeReference);
207  }
208  }
209 
210  // -> attribute reference as a structure
211  sl.setMessageBuffer(testMessage20_3.getBytes(Charset.forName("UTF-8")), KMIPConstants.FORMAT_MSGXML);
212  rh = sl.getRequestHeader();
213  assertEquals("2.0", rh.getProtocolVersion());
214 
215  for (int i = 0; i < rh.getBatchCount(); i++) {
216 
217  BatchItem bi = sl.getBatchItem(i + 1);
218  if (bi instanceof DeleteAttribute2BatchItem) {
219  DeleteAttribute2BatchItem rb = (DeleteAttribute2BatchItem) bi;
220 
221  String uniqueId = rb.getUniqueId();
222  assertEquals("e82a80f0-fe20-4cbb-a1b6-00e3a81a7e9f", uniqueId);
223 
224  String currentAttribute = rb.getCurrentAttribute();
225  assertEquals(null, currentAttribute);
226 
227  String attributeReference = rb.getAttributeReference();
228  assertEquals("P6R-SKC-client", attributeReference);
229  }
230  }
231 
232  sl.freeLibrary();
233 
234  } catch (Exception e) {
235  // -> we shoud not get here
236  System.out.println(e.toString());
237  assertEquals(0, 1);
238  }
239  }
240 }
void JNICall_DeleteAttributeKMIP()
Test: Verify parser can parse an incoming delete attribute request for both KMIP versions 1...
A JUNIT test demonstrating how to parse an incoming KMIP request from a client.