Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
JniRegisterCertRequestKmip.java
Go to the documentation of this file.
1 package com.p6r.kmipserverlib;
2 
3 import org.junit.*;
4 
5 import java.math.BigInteger;
6 import java.nio.charset.Charset;
7 import java.text.DateFormat;
8 import java.text.SimpleDateFormat;
9 import java.util.Date;
10 
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotEquals;
13 
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-RegisterCertRequestKMIP");
57 
58  byte[] expectedBytes = { 0x30, (byte)0x82, 0x02, 0x29, 0x30, (byte)0x82, 0x01, (byte)0x92, (byte)0xA0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x08, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x30 };
59 
60  // -> this parser is multi-thread safe by using JNI monitor locking
61  // -> use one parser object per server thread is recommended
62  P6KMIPServerLib sl = new P6KMIPServerLib();
63 
64  // -> KMIP 2.0 XML message with 1 batch item generated by P6R's Secure KMIP Client (SKC)
65  String testMessage =
66  "<RequestMessage>\n" +
67  " <RequestHeader>\n" +
68  " <ProtocolVersion>\n" +
69  " <ProtocolVersionMajor type=\"Integer\" value=\"2\"/>\n" +
70  " <ProtocolVersionMinor type=\"Integer\" value=\"0\"/>\n" +
71  " </ProtocolVersion>\n" +
72  " <BatchCount type=\"Integer\" value=\"1\"/>\n" +
73  " </RequestHeader>\n" +
74  " <BatchItem>\n" +
75  " <Operation type=\"Enumeration\" value=\"Register\"/>\n" +
76  " <RequestPayload>\n" +
77  " <ObjectType type=\"Enumeration\" value=\"CertificateRequest\"/>\n" +
78  " <Attributes>\n" +
79  " <CryptographicUsageMask type=\"Integer\" value=\"0x00000400\"/>\n" +
80  " </Attributes>\n" +
81  " <CertificateRequest>\n" +
82  " <CertificateRequestType type=\"Enumeration\" value=\"PKCS_10\"/>\n" +
83  " <CertificateRequestValue type=\"ByteString\" value=\"3082022930820192A0030201020208313233343536373830\"/>\n" +
84  " </CertificateRequest>\n" +
85  " </RequestPayload>\n" +
86  "</BatchItem>\n" +
87  "</RequestMessage>";
88 
89  try {
90  sl.initializeLibrary(P6KMIPServerLib.FLAGS_NONE);
91 
92  String libVersion = sl.getLibraryVersion();
93  System.out.println(libVersion);
94 
95  // -> server read incoming KMIP request message from a socket and loaded those bytes (e.g., TTLV, XML, JSON) into the parser)
96  // -> 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
97  sl.setMessageBuffer(testMessage.getBytes(Charset.forName("UTF-8")), KMIPConstants.FORMAT_MSGXML);
98 
99  // -> now we can pull parts of the request message apart, this can be done over and over again if desired
100  RequestHeader rh = sl.getRequestHeader();
101  assertEquals("2.0", rh.getProtocolVersion());
102 
103  // -> parsed message is maintained in parser until another call to setMessageBuffer() of freeLibrary() is called
104  for (int i = 0; i < rh.getBatchCount(); i++) {
105 
106  BatchItem bi = sl.getBatchItem(i + 1);
107  if (bi instanceof RegisterCertificateRequestObjectBatchItem) {
108  RegisterCertificateRequestObjectBatchItem cr = (RegisterCertificateRequestObjectBatchItem) bi;
109 
110  byte[] batchId = cr.getUniqueBatchId();
111  assertEquals(null, batchId);
112 
113  int certRequestType = cr.getCcertificateRequestType();
114  assertEquals(KMIPConstants.CERTREQUEST_PKCS10, certRequestType);
115 
116  byte[] certRequestValue = cr.getCertificateRequestValue();
117  assertEquals(24, certRequestValue.length);
118  for( int j=0; j < certRequestValue.length; j++ ) {
119  assertEquals(expectedBytes[j], certRequestValue[j]);
120  }
121 
122  String[] attributes = cr.getAttributes();
123  assertEquals(1, attributes.length);
124  assertEquals("Cryptographic Usage Mask: 400", attributes[0]);
125  }
126  }
127  sl.freeLibrary();
128 
129  } catch (Exception e) {
130  // -> we shoud not get here
131  System.out.println(e.toString());
132  assertEquals(0, 1);
133  }
134  }
135 
136 }
void JNICall_RegisterCertRequestKMIP()
Test: Verify parser can handle a Register Certificate Request as added to KMIP 2.0.
A JUNIT test demonstrating how to parse an incoming KMIP request from a client.