Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
JniRegisterSplitKeyKmip.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 
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
51  System.out.println("@Test - JNICall-RegisterSplitKeyKMIP");
52 
53  byte[] expectedSplit = { 0x66, (byte)0xC4, 0x6A, 0x77, 0x54, (byte)0xF9, 0x4D, (byte)0xE4, 0x20, (byte)0xC7, (byte)0xB1, (byte)0xA7, (byte)0xFF, (byte)0xF5, (byte)0xEC, 0x56 };
54 
55  // -> this parser is multi-thread safe by using JNI monitor locking
56  // -> use one parser object per server thread is recommended
57  P6KMIPServerLib sl = new P6KMIPServerLib();
58 
59  // -> KMIP 1.4 XML message with 1 batch item generated by P6R's Secure KMIP Client (SKC)
60  String testMessage = "<RequestMessage><RequestHeader><ProtocolVersion><ProtocolVersionMajor type=\"Integer\" value=\"1\"/><ProtocolVersionMinor type=\"Integer\" value=\"4\"/></ProtocolVersion><BatchCount type=\"Integer\" value=\"1\"/></RequestHeader><BatchItem><Operation type=\"Enumeration\" value=\"Register\"/><RequestPayload><ObjectType type=\"Enumeration\" value=\"SplitKey\"/><TemplateAttribute><Attribute><AttributeName type=\"TextString\" value=\"x-ID\"/><AttributeValue type=\"TextString\" value=\"TC-SJ-3-14-split1\"/></Attribute></TemplateAttribute><SplitKey><SplitKeyParts type=\"Integer\" value=\"4\"/><KeyPartIdentifier type=\"Integer\" value=\"1\"/><SplitKeyThreshold type=\"Integer\" value=\"2\"/><SplitKeyMethod type=\"Enumeration\" value=\"PolynomialSharingGF2_8\"/><KeyBlock><KeyFormatType type=\"Enumeration\" value=\"Raw\"/><KeyValue><KeyMaterial type=\"ByteString\" value=\"66C46A7754F94DE420C7B1A7FFF5EC56\"/></KeyValue></KeyBlock></SplitKey></RequestPayload></BatchItem></RequestMessage>";
61 
62  try {
63  sl.initializeLibrary(P6KMIPServerLib.FLAGS_NONE);
64 
65  String libVersion = sl.getLibraryVersion();
66  System.out.println(libVersion);
67 
68  // -> server read incoming KMIP request message from a socket and loaded those bytes (e.g., TTLV, XML, JSON) into the parser)
69  // -> 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
70  sl.setMessageBuffer(testMessage.getBytes(Charset.forName("UTF-8")), KMIPConstants.FORMAT_MSGXML);
71 
72  // -> now we can pull parts of the request message apart, this can be done over and over again if desired
73  RequestHeader rh = sl.getRequestHeader();
74  assertEquals("1.4", rh.getProtocolVersion());
75 
76  // -> parsed message is maintained in parser until another call to setMessageBuffer() of freeLibrary() is called
77  for (int i = 0; i < rh.getBatchCount(); i++) {
78 
79  BatchItem bi = sl.getBatchItem(i + 1);
80  if (bi instanceof RegisterSplitKeyBatchItem) {
81  RegisterSplitKeyBatchItem ck = (RegisterSplitKeyBatchItem) bi;
82 
83  // -> batch id is not required if only one batch item is present
84  byte[] batchId = ck.getUniqueBatchId();
85  assertEquals(null, batchId);
86 
87  int totalParts = ck.getTotalParts();
88  assertEquals(4, totalParts);
89 
90  int partNumber = ck.getPartNumber();
91  assertEquals(1, partNumber);
92 
93  int threshold = ck.getThreshold();
94  assertEquals(2, threshold);
95 
96  int method = ck.getMethod();
97  assertEquals(KMIPConstants.SPLITKEY_GF2_8, method);
98 
99  MessageExtension me = ck.getExtension();
100  assertEquals(null, me);
101 
102  byte[] keyPart = ck.getKeyPart();
103  assertEquals(16, keyPart.length);
104  for( int j=0; j < 16; j++ ) {
105  assertEquals(expectedSplit[j], keyPart[j]);
106  }
107 
108  String[] attributes = ck.getTemplateAttributes();
109  assertEquals(1, attributes.length);
110  assertEquals("x-ID: TC-SJ-3-14-split1", attributes[0]);
111  }
112  }
113  sl.freeLibrary();
114 
115  } catch (Exception e) {
116  // -> we shoud not get here
117  System.out.println(e.toString());
118  assertEquals(0, 1);
119  }
120  }
121 }
void JNICall_RegisterSplitKeyKMIP()
Test: Verify parser can handle an XML formated Register Split Key operation.
A JUNIT test demonstrating how to parse an incoming KMIP request from a client.