Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
JniCreateKeyPairKmip.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 static org.junit.Assert.assertEquals;
7 
17 public class JniCreateKeyPairKmip {
18 
19  @BeforeClass
20  public static void oneTimeSetUp() {
21  // NOOP
22  System.out.println("@BeforeClass - oneTimeSetUp");
23  }
24 
25  @AfterClass
26  public static void oneTimeTearDown() {
27  // NOOP
28  System.out.println("@AfterClass - oneTimeTearDown");
29  }
30 
31  @Before
32  public void setUp() {
33  // NOOP
34  System.out.println("@Before - setUp");
35  }
36 
37  @After
38  public void tearDown() {
39  // NOOP
40  System.out.println("@After - tearDown");
41  }
42 
48  @Test
49  public void JNICall_CreateKeyPairKMIP() {
50  System.out.println("@Test - JNICall-CreateKeyPairKMIP");
51 
52  // -> this parser is multi-thread safe by using JNI monitor locking
53  // -> use one parser object per server thread is recommended
54  P6KMIPServerLib sl = new P6KMIPServerLib();
55 
56  // -> KMIP 1.3 XML message with 1 batch item generated by P6R's Secure KMIP Client (SKC)
57  String testMessage =
58  "<RequestMessage><RequestHeader>" +
59  "<ProtocolVersion> " +
60  "<ProtocolVersionMajor type=\"Integer\" value=\"1\"/> " +
61  "<ProtocolVersionMinor type=\"Integer\" value=\"3\"/> " +
62  "</ProtocolVersion> " +
63  "<BatchCount type=\"Integer\" value=\"1\"/> " +
64  "</RequestHeader> " +
65  "<BatchItem> " +
66  "<Operation type=\"Enumeration\" value=\"CreateKeyPair\"/> " +
67  "<RequestPayload> " +
68  "<CommonTemplateAttribute> " +
69  "<Attribute> " +
70  "<AttributeName type=\"TextString\" value=\"Cryptographic Algorithm\"/> " +
71  "<AttributeValue type=\"Enumeration\" value=\"RSA\"/> " +
72  "</Attribute> " +
73  "<Attribute> " +
74  "<AttributeName type=\"TextString\" value=\"Cryptographic Length\"/> " +
75  "<AttributeValue type=\"Integer\" value=\"1024\"/> " +
76  "</Attribute> " +
77  "</CommonTemplateAttribute> " +
78  "<PrivateKeyTemplateAttribute> " +
79  "<Attribute> " +
80  "<AttributeName type=\"TextString\" value=\"Name\"/> " +
81  "<AttributeValue> " +
82  "<NameValue type=\"TextString\" value=\"TC-81-13-privatekey1\"/> " +
83  "<NameType type=\"Enumeration\" value=\"UninterpretedTextString\"/> " +
84  "</AttributeValue> " +
85  "</Attribute> " +
86  "<Attribute> " +
87  "<AttributeName type=\"TextString\" value=\"Cryptographic Usage Mask\"/> " +
88  "<AttributeValue type=\"Integer\" value=\"0x00000001\"/> " +
89  "</Attribute> " +
90  "</PrivateKeyTemplateAttribute> " +
91  "<PublicKeyTemplateAttribute> " +
92  "<Attribute> " +
93  "<AttributeName type=\"TextString\" value=\"Name\"/> " +
94  "<AttributeValue> " +
95  "<NameValue type=\"TextString\" value=\"TC-81-13-publickey1\"/> " +
96  "<NameType type=\"Enumeration\" value=\"UninterpretedTextString\"/> " +
97  "</AttributeValue> " +
98  "</Attribute> " +
99  "<Attribute> " +
100  "<AttributeName type=\"TextString\" value=\"Cryptographic Usage Mask\"/> " +
101  "<AttributeValue type=\"Integer\" value=\"0x00000002\"/> " +
102  "</Attribute> " +
103  "</PublicKeyTemplateAttribute> " +
104  "</RequestPayload> " +
105  "</BatchItem></RequestMessage>";
106 
107  try {
108  sl.initializeLibrary(P6KMIPServerLib.FLAGS_NONE);
109 
110  String libVersion = sl.getLibraryVersion();
111  System.out.println(libVersion);
112 
113  // -> server read incoming KMIP request message from a socket and loaded those bytes (e.g., TTLV, XML, JSON) into the parser)
114  // -> 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
115  sl.setMessageBuffer(testMessage.getBytes(Charset.forName("UTF-8")), KMIPConstants.FORMAT_MSGXML);
116 
117  // -> now we can pull parts of the request message apart, this can be done over and over again if desired
118  RequestHeader rh = sl.getRequestHeader();
119  assertEquals("1.3", rh.getProtocolVersion());
120 
121  // -> parsed message is maintained in parser until another call to setMessageBuffer() of freeLibrary() is called
122  for (int i = 0; i < rh.getBatchCount(); i++) {
123 
124  BatchItem bi = sl.getBatchItem(i + 1);
125  if (bi instanceof CreateKeyPairBatchItem) {
126  CreateKeyPairBatchItem ck = (CreateKeyPairBatchItem) bi;
127 
128  // -> batch id is not required if only one batch item is present
129  byte[] batchId = ck.getUniqueBatchId();
130  assertEquals(null, batchId);
131 
132  // test that attributes are as expected from message above
133  String[] commonAttribs = ck.getCommonAttributes();
134  assertEquals(2, commonAttribs.length);
135  assertEquals("Cryptographic Algorithm: 4", commonAttribs[0]);
136  assertEquals("Cryptographic Length: 1024", commonAttribs[1]);
137 
138  String[] commonNames = ck.getCommonNames();
139  assertEquals(null, commonNames);
140 
141  String[] privateAttribs = ck.getPrivateAttributes();
142  assertEquals(2, privateAttribs.length);
143  assertEquals("Name: TC-81-13-privatekey1 - text_string", privateAttribs[0]);
144  assertEquals("Cryptographic Usage Mask: 1", privateAttribs[1]);
145 
146  String[] publicAttribs = ck.getPublicAttributes();
147  assertEquals(2, publicAttribs.length);
148  assertEquals("Name: TC-81-13-publickey1 - text_string", publicAttribs[0]);
149  assertEquals("Cryptographic Usage Mask: 2", publicAttribs[1]);
150  }
151  }
152  sl.freeLibrary();
153 
154  } catch (Exception e) {
155  // -> we shoud not get here
156  System.out.println(e.toString());
157  assertEquals(0, 1);
158  }
159  }
160 }
A JUNIT test demonstrating how to parse an incoming KMIP request from a client.
void JNICall_CreateKeyPairKMIP()
Test: Verify parser can handle an XML formated Create Key Pair operation.