Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
JniRNGKmip.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 JniRNGKmip {
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_RNGKMIP() {
50  System.out.println("@Test - JNICall-RNGKMIP");
51 
52  byte[] expectedData = { 0x33, 0x3C, 0x06, 0x58, 0x77, 0x06, 0x22, 0x50, (byte)0x99, (byte)0xA6, 0x74, 0x38, (byte)0xF2, 0x63, (byte)0xF8, (byte)0xF9, 0x32, (byte)0xF6, 0x4B, (byte)0x86, 0x0C, 0x3A, 0x7D, (byte)0xBB, 0x21, (byte)0xBC, 0x2B, (byte)0xD5, 0x66, (byte)0x85, (byte)0xD8, (byte)0xBC };
53 
54  // -> this parser is multi-thread safe by using JNI monitor locking
55  // -> use one parser object per server thread is recommended
56  P6KMIPServerLib sl = new P6KMIPServerLib();
57 
58  // -> KMIP 1.3 JSON message with 1 batch item generated by P6R's Secure KMIP Client (SKC)
59  String testMessage1 = "{\"tag\":\"RequestMessage\", \"value\":[{\"tag\":\"RequestHeader\", \"value\":[{\"tag\":\"ProtocolVersion\", \"value\":[{\"tag\":\"ProtocolVersionMajor\", \"type\":\"Integer\", \"value\":1}, {\"tag\":\"ProtocolVersionMinor\", \"type\":\"Integer\", \"value\":3}]}, {\"tag\":\"BatchCount\", \"type\":\"Integer\", \"value\":1}]}, {\"tag\":\"BatchItem\", \"value\":[{\"tag\":\"Operation\", \"type\":\"Enumeration\", \"value\":\"RNGRetrieve\"}, {\"tag\":\"RequestPayload\", \"value\":[{\"tag\":\"DataLength\", \"type\":\"Integer\", \"value\":32}]}]}]}";
60 
61  String testMessage2 = "{\"tag\":\"RequestMessage\", \"value\":[{\"tag\":\"RequestHeader\", \"value\":[{\"tag\":\"ProtocolVersion\", \"value\":[{\"tag\":\"ProtocolVersionMajor\", \"type\":\"Integer\", \"value\":1}, {\"tag\":\"ProtocolVersionMinor\", \"type\":\"Integer\", \"value\":3}]}, {\"tag\":\"BatchCount\", \"type\":\"Integer\", \"value\":1}]}, {\"tag\":\"BatchItem\", \"value\":[{\"tag\":\"Operation\", \"type\":\"Enumeration\", \"value\":\"RNGSeed\"}, {\"tag\":\"RequestPayload\", \"value\":[{\"tag\":\"Data\", \"type\":\"ByteString\", \"value\":\"333C06587706225099A67438F263F8F932F64B860C3A7DBB21BC2BD56685D8BC\"}]}]}]}";
62 
63  try {
64  sl.initializeLibrary(P6KMIPServerLib.FLAGS_NONE);
65 
66  String libVersion = sl.getLibraryVersion();
67  System.out.println(libVersion);
68 
69  // [A] Here we test RNG Retrieve request
70  // -> server read incoming KMIP request message from a socket and loaded those bytes (e.g., TTLV, XML, JSON) into the parser)
71  // -> 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
72  sl.setMessageBuffer(testMessage1.getBytes(Charset.forName("UTF-8")), KMIPConstants.FORMAT_MSGJSON);
73 
74  RequestHeader rh = sl.getRequestHeader();
75  assertEquals("1.3", rh.getProtocolVersion());
76 
77  // -> parsed message is maintained in parser until another call to setMessageBuffer() of freeLibrary() is called
78  for (int i = 0; i < rh.getBatchCount(); i++) {
79 
80  BatchItem bi = sl.getBatchItem(i + 1);
81  if (bi instanceof RNGRetrieveBatchItem) {
82  RNGRetrieveBatchItem ck = (RNGRetrieveBatchItem) bi;
83 
84  int length = ck.getDataLength();
85  assertEquals(32, length);
86  }
87  }
88 
89  // [B] Here we test a RNG Seed request
90  sl.setMessageBuffer(testMessage2.getBytes(Charset.forName("UTF-8")), KMIPConstants.FORMAT_MSGJSON);
91 
92  rh = sl.getRequestHeader();
93  assertEquals("1.3", rh.getProtocolVersion());
94 
95  // -> parsed message is maintained in parser until another call to setMessageBuffer() of freeLibrary() is called
96  for (int i = 0; i < rh.getBatchCount(); i++) {
97 
98  BatchItem bi = sl.getBatchItem(i + 1);
99  if (bi instanceof RNGSeedBatchItem) {
100  RNGSeedBatchItem ck = (RNGSeedBatchItem) bi;
101 
102  byte[] dataToSeed = ck.getData();
103  assertEquals(32, dataToSeed.length);
104  for( int j=0; j < dataToSeed.length; j++) {
105  assertEquals( expectedData[j], dataToSeed[j]);
106  }
107  }
108  }
109  sl.freeLibrary();
110 
111  } catch (Exception e) {
112  // -> we shoud not get here
113  System.out.println(e.toString());
114  assertEquals(0, 1);
115  }
116  }
117 }
118 
void JNICall_RNGKMIP()
Test: Verify parser can handle an JSON formated Sign operation.
Definition: JniRNGKmip.java:49
A JUNIT test demonstrating how to parse an incoming KMIP request from a client.
Definition: JniRNGKmip.java:17