Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
JniStreamCryptoKmip.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 
18 public class JniStreamCryptoKmip {
19 
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_StreamCryptoKMIP() {
52  System.out.println("@Test - JNICall-StreamCryptoKMIP");
53 
54  byte[] expectedData = { 0x13, 0x14, 0x15, 0x16 };
55  byte[] expectedCorrelation = { 0x34, 0x35, 0x37, 0x35, 0x31, 0x35, 0x38, 0x36, 0x2D, 0x66, 0x33, 0x30, 0x63, 0x2D, 0x34, 0x61, 0x39, 0x33, 0x2D, 0x61, 0x34, 0x39, 0x66, 0x2D, 0x34, 0x62, 0x34, 0x37, 0x32, 0x66, 0x33, 0x66, 0x62, 0x61, 0x31, 0x32 };
56 
57  // -> this parser is multi-thread safe by using JNI monitor locking
58  // -> use one parser object per server thread is recommended
59  P6KMIPServerLib sl = new P6KMIPServerLib();
60 
61  // -> KMIP 1.2 XML message with 1 batch item generated by P6R's Secure KMIP Client (SKC)
62  String testMessage = "<RequestMessage>\n" +
63  "<RequestHeader>\n" +
64  "<ProtocolVersion>\n" +
65  "<ProtocolVersionMajor type=\"Integer\" value=\"1\"/>\n" +
66  "<ProtocolVersionMinor type=\"Integer\" value=\"4\"/>\n" +
67  "</ProtocolVersion>\n" +
68  "<BatchCount type=\"Integer\" value=\"1\"/>\n" +
69  "</RequestHeader>\n" +
70  "<BatchItem>\n" +
71  "<Operation type=\"Enumeration\" value=\"Encrypt\"/>\n" +
72  "<RequestPayload>\n" +
73  "<UniqueIdentifier type=\"TextString\" value=\"f7ca0624-4d72-4118-883d-660a9dc05f6b\"/>\n" +
74  "<Data type=\"ByteString\" value=\"13141516\"/>\n" +
75  "<CorrelationValue type=\"ByteString\" value=\"34353735313538362D663330632D346139332D613439662D346234373266336662613132\"/>\n" +
76  "<FinalIndicator type=\"Boolean\" value=\"true\"/>\n" +
77  "</RequestPayload>\n" +
78  "</BatchItem>\n" +
79  "</RequestMessage>";
80  try {
81  sl.initializeLibrary(P6KMIPServerLib.FLAGS_NONE);
82 
83  String libVersion = sl.getLibraryVersion();
84  System.out.println(libVersion);
85 
86  // -> server read incoming KMIP request message from a socket and loaded those bytes (e.g., TTLV, XML, JSON) into the parser)
87  // -> 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
88  sl.setMessageBuffer(testMessage.getBytes(Charset.forName("UTF-8")), KMIPConstants.FORMAT_MSGXML);
89 
90  // -> here we test non-streaming encryption request
91  RequestHeader rh = sl.getRequestHeader();
92  assertEquals("1.4", rh.getProtocolVersion());
93 
94  // -> parsed message is maintained in parser until another call to setMessageBuffer() of freeLibrary() is called
95  for (int i = 0; i < rh.getBatchCount(); i++) {
96 
97  BatchItem bi = sl.getBatchItem(i + 1);
98  if (bi instanceof EncryptBatchItem) {
99  EncryptBatchItem ck = (EncryptBatchItem) bi;
100 
101  String uniqueId = ck.getUniqueId();
102  assertEquals("f7ca0624-4d72-4118-883d-660a9dc05f6b", uniqueId);
103 
104  CryptoStreamHandle sh = ck.getStreamHandle();
105  byte[] correlation = sh.getCorrelationValue();
106  assertEquals(36, correlation.length);
107  for( int j=0; j < correlation.length; j++) {
108  assertEquals( expectedCorrelation[j], correlation[j]);
109  }
110 
111  boolean initIndicator = sh.getInitIndicator();
112  assertEquals(false, initIndicator);
113 
114  boolean finalIndicator = sh.getFinalIndicator();
115  assertEquals(true, finalIndicator);
116 
117  CryptograhicParameters params = ck.getParams();
118  assertEquals(null, params);
119 
120  byte[] dataToEncrypt = ck.getData();
121  assertEquals(4, dataToEncrypt.length);
122  for( int j=0; j < dataToEncrypt.length; j++) {
123  assertEquals( expectedData[j], dataToEncrypt[j]);
124  }
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 }
A JUNIT test demonstrating how to parse an incoming KMIP request from a client.
void JNICall_StreamCryptoKMIP()
Test: Verify parser can handle an JSON formated Encrypt operation.