Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
JniMACKmip.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 import static org.junit.Assert.assertNotEquals;
8 
18 public class JniMACKmip {
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
50  public void JNICall_MACKMIP() {
51  System.out.println("@Test - JNICall-MACKMIP");
52 
53  byte[] expectedData = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };
54 
55  byte[] MACExpected = { 0x19, 0x03, 0x61, 0x70, 0x3B, 0x02, (byte)0x88, (byte)0xCD, 0x3F, 0x14, (byte)0xD9, (byte)0x95, 0x57, 0x17, 0x48, (byte)0x80, (byte)0xA6, (byte)0x83, (byte)0xBD, (byte)0xC3, (byte)0x96, 0x28, (byte)0x9F, (byte)0x97, 0x76, 0x3D, 0x62, 0x4A, (byte)0xE6, (byte)0xFE, 0x45, 0x26 };
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.3 JSON message with 1 batch item generated by P6R's Secure KMIP Client (SKC)
62  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\":\"MAC\"}, {\"tag\":\"RequestPayload\", \"value\":[{\"tag\":\"UniqueIdentifier\", \"type\":\"TextString\", \"value\":\"bccbcbf8-fc60-4f3d-9a92-dd8ad7e8a697\"}, {\"tag\":\"Data\", \"type\":\"ByteString\", \"value\":\"01020304050607080910111213141516\"}]}]}]}";
63 
64  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\":\"MACVerify\"}, {\"tag\":\"RequestPayload\", \"value\":[{\"tag\":\"UniqueIdentifier\", \"type\":\"TextString\", \"value\":\"bccbcbf8-fc60-4f3d-9a92-dd8ad7e8a697\"}, {\"tag\":\"Data\", \"type\":\"ByteString\", \"value\":\"01020304050607080910111213141516\"}, {\"tag\":\"MACData\", \"type\":\"ByteString\", \"value\":\"190361703B0288CD3F14D99557174880A683BDC396289F97763D624AE6FE4526\"}]}]}]}";
65 
66 
67  try {
68  sl.initializeLibrary(P6KMIPServerLib.FLAGS_NONE);
69 
70  String libVersion = sl.getLibraryVersion();
71  System.out.println(libVersion);
72 
73  // [A] Here we test non-streaming MAC request
74  // -> server read incoming KMIP request message from a socket and loaded those bytes (e.g., TTLV, XML, JSON) into the parser)
75  // -> 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
76  sl.setMessageBuffer(testMessage1.getBytes(Charset.forName("UTF-8")), KMIPConstants.FORMAT_MSGJSON);
77 
78  RequestHeader rh = sl.getRequestHeader();
79  assertEquals("1.3", rh.getProtocolVersion());
80 
81  // -> parsed message is maintained in parser until another call to setMessageBuffer() of freeLibrary() is called
82  for (int i = 0; i < rh.getBatchCount(); i++) {
83 
84  BatchItem bi = sl.getBatchItem(i + 1);
85  if (bi instanceof MACBatchItem) {
86  MACBatchItem ck = (MACBatchItem) bi;
87 
88  String uniqueId = ck.getUniqueId();
89  assertEquals("bccbcbf8-fc60-4f3d-9a92-dd8ad7e8a697", uniqueId);
90 
91  // -> object is null since none in message
92  CryptograhicParameters params = ck.getParams();
93  assertEquals(null, params);
94 
95  byte[] dataToMAC = ck.getData();
96  assertEquals(16, dataToMAC.length);
97  for( int j=0; j < dataToMAC.length; j++) {
98  assertEquals( expectedData[j], dataToMAC[j]);
99  }
100  }
101  }
102 
103  // [B] Here we test a similar MAC verify request
104  sl.setMessageBuffer(testMessage2.getBytes(Charset.forName("UTF-8")), KMIPConstants.FORMAT_MSGJSON);
105 
106  rh = sl.getRequestHeader();
107  assertEquals("1.3", rh.getProtocolVersion());
108 
109  // -> parsed message is maintained in parser until another call to setMessageBuffer() of freeLibrary() is called
110  for (int i = 0; i < rh.getBatchCount(); i++) {
111 
112  BatchItem bi = sl.getBatchItem(i + 1);
113  if (bi instanceof MACVerifyBatchItem) {
114  MACVerifyBatchItem ck = (MACVerifyBatchItem) bi;
115 
116  String uniqueId = ck.getUniqueId();
117  assertEquals("bccbcbf8-fc60-4f3d-9a92-dd8ad7e8a697", uniqueId);
118 
119  CryptograhicParameters params = ck.getParams();
120  assertEquals(null, params);
121 
122  byte[] dataToMAC = ck.getData();
123  assertEquals(16, dataToMAC.length);
124  for( int j=0; j < dataToMAC.length; j++) {
125  assertEquals( expectedData[j], dataToMAC[j]);
126  }
127 
128  byte[] MAC = ck.getMACData();
129  assertEquals(32, MAC.length);
130  for( int k=0; k < MAC.length; k++) {
131  assertEquals( MACExpected[k], MAC[k]);
132  }
133  }
134  }
135  sl.freeLibrary();
136 
137  } catch (Exception e) {
138  // -> we shoud not get here
139  System.out.println(e.toString());
140  assertEquals(0, 1);
141  }
142  }
143 }
144 
A JUNIT test demonstrating how to parse an incoming KMIP request from a client.
Definition: JniMACKmip.java:18
void JNICall_MACKMIP()
Test: Verify parser can handle an JSON formated Sign operation.
Definition: JniMACKmip.java:50