Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
JniCreateSecretDataKmip.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 
7 import static org.junit.Assert.assertEquals;
8 
19 
20  @BeforeClass
21  public static void oneTimeSetUp() {
22  // NOOP
23  System.out.println("@BeforeClass - oneTimeSetUp");
24  }
25 
26  @AfterClass
27  public static void oneTimeTearDown() {
28  // NOOP
29  System.out.println("@AfterClass - oneTimeTearDown");
30  }
31 
32  @Before
33  public void setUp() {
34  // NOOP
35  System.out.println("@Before - setUp");
36  }
37 
38  @After
39  public void tearDown() {
40  // NOOP
41  System.out.println("@After - tearDown");
42  }
43 
49  @Test
51  System.out.println("@Test - JNICall-CreateSecretDataKMIP");
52 
53  // -> this parser is multi-thread safe by using JNI monitor locking
54  // -> use one parser object per server thread is recommended
55  P6KMIPServerLib sl = new P6KMIPServerLib();
56 
57  // -> KMIP 1.3 XML message with 1 batch item generated by P6R's Secure KMIP Client (SKC)
58  String testMessage =
59  "<RequestMessage>\n" +
60  "<RequestHeader>\n" +
61  " <ProtocolVersion>\n" +
62  " <ProtocolVersionMajor type=\"Integer\" value=\"1\"/>\n" +
63  " <ProtocolVersionMinor type=\"Integer\" value=\"4\"/>\n" +
64  " </ProtocolVersion>\n" +
65  " <BatchOrderOption type=\"Boolean\" value=\"true\"/>\n" +
66  " <BatchCount type=\"Integer\" value=\"1\"/>\n" +
67  "</RequestHeader>\n" +
68  "<BatchItem>\n" +
69  " <Operation type=\"Enumeration\" value=\"Create\"/>\n" +
70  " <UniqueBatchItemID type=\"ByteString\" value=\"01\"/>\n" +
71  " <RequestPayload>\n" +
72  " <ObjectType type=\"Enumeration\" value=\"SecretData\"/>\n" +
73  " <TemplateAttribute>\n" +
74  " <Attribute>\n" +
75  " <AttributeName type=\"TextString\" value=\"Cryptographic Length\"/>\n" +
76  " <AttributeValue type=\"Integer\" value=\"80\"/>\n" +
77  " </Attribute>\n" +
78  " </TemplateAttribute>\n" +
79  " </RequestPayload>\n" +
80  "</BatchItem>\n" +
81  "</RequestMessage>";
82 
83  try {
84  sl.initializeLibrary(P6KMIPServerLib.FLAGS_NONE);
85 
86  String libVersion = sl.getLibraryVersion();
87  System.out.println(libVersion);
88 
89  // -> server read incoming KMIP request message from a socket and loaded those bytes (e.g., TTLV, XML, JSON) into the parser)
90  // -> the type of message: TTLV, XML, JSON can be determine by the mime type passed in the HTTP request, or lack of one if just using SSL connection
91  sl.setMessageBuffer(testMessage.getBytes(Charset.forName("UTF-8")), KMIPConstants.FORMAT_MSGXML);
92 
93  // -> now we can pull parts of the request message apart, this can be done over and over again if desired
94  RequestHeader rh = sl.getRequestHeader();
95  assertEquals("1.4", rh.getProtocolVersion());
96 
97  // -> parsed message is maintained in parser until another call to setMessageBuffer() of freeLibrary() is called
98  for (int i = 0; i < rh.getBatchCount(); i++) {
99 
100  BatchItem bi = sl.getBatchItem(i + 1);
101  if (bi instanceof CreateBatchItem) {
102  CreateBatchItem ck = (CreateBatchItem) bi;
103 
104  // -> batch id is not required if only one batch item is present
105  byte[] batchId = ck.getUniqueBatchId();
106  assertEquals(1, batchId.length);
107  assertEquals(0x01, batchId[0]);
108 
109  int objectType = ck.getObjectType();
110  assertEquals(KMIPConstants.OBJECT_SECRETDATA, objectType);
111 
112  // test that attributes are as expected from message above
113  String[] attributes = ck.getAttributes();
114  assertEquals(1, attributes.length);
115  assertEquals("Cryptographic Length: 80", attributes[0]);
116  }
117  }
118  sl.freeLibrary();
119 
120  } catch (Exception e) {
121  // -> we shoud not get here
122  System.out.println(e.toString());
123  assertEquals(0, 1);
124  }
125  }
126 }
A JUNIT test demonstrating how to parse an incoming KMIP request from a client.
void JNICall_CreateSecretDataKMIP()
Test: Verify parser can handle a Create operation for Secret Data Object.