Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
JniWrappedRegisterKmip.java
Go to the documentation of this file.
1 package com.p6r.kmipserverlib;
2 
3 import org.junit.*;
4 
5 import static org.junit.Assert.assertEquals;
6 
16 public class JniWrappedRegisterKmip {
17 
18  @BeforeClass
19  public static void oneTimeSetUp() {
20  // NOOP
21  System.out.println("@BeforeClass - oneTimeSetUp");
22  }
23 
24  @AfterClass
25  public static void oneTimeTearDown() {
26  // NOOP
27  System.out.println("@AfterClass - oneTimeTearDown");
28  }
29 
30  @Before
31  public void setUp() {
32  // NOOP
33  System.out.println("@Before - setUp");
34  }
35 
36  @After
37  public void tearDown() {
38  // NOOP
39  System.out.println("@After - tearDown");
40  }
41 
47  @Test
49  System.out.println("@Test - JNICall-WrappedRegisterKMIP");
50 
51  // -> this parser is multi-thread safe by using JNI monitor locking
52  // -> use one parser object per server thread is recommended
53  P6KMIPServerLib sl = new P6KMIPServerLib();
54 
55  // -> KMIP 1.3 TTLV message with 1 batch item generated by P6R's Secure KMIP Client (SKC)
56  byte[] testMessage = {
57  0x42, 0x00, 0x78, 0x01, 0x00, 0x00, 0x02, 0x50, 0x42, 0x00, 0x77, 0x01, 0x00, 0x00, 0x00, 0x48, 0x42, 0x00, 0x69, 0x01, 0x00, 0x00, 0x00, 0x20, 0x42, 0x00, 0x6a, 0x02, 0x00, 0x00, 0x00, 0x04,
58  0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x6b, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, (byte)0x92, 0x09, 0x00, 0x00, 0x00, 0x08,
59  0x00, 0x00, 0x00, 0x00, 0x59, (byte)0xee, 0x0d, (byte)0xe8, 0x42, 0x00, 0x0d, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x0f, 0x01, 0x00, 0x00, 0x01, (byte)0xf8,
60  0x42, 0x00, 0x5c, 0x05, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, (byte)0x93, 0x08, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
61  0x42, 0x00, 0x79, 0x01, 0x00, 0x00, 0x01, (byte)0xd0, 0x42, 0x00, 0x57, 0x05, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, (byte)0x91, 0x01, 0x00, 0x00, 0x00, 0x38,
62  0x42, 0x00, 0x08, 0x01, 0x00, 0x00, 0x00, 0x30, 0x42, 0x00, 0x0a, 0x07, 0x00, 0x00, 0x00, 0x04, 0x78, 0x2d, 0x49, 0x44, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x0b, 0x07, 0x00, 0x00, 0x00, 0x11,
63  0x50, 0x36, 0x52, 0x2d, 0x4d, 0x45, 0x4d, 0x4f, 0x52, 0x59, 0x2d, 0x54, 0x45, 0x53, 0x54, 0x2d, 0x37, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, (byte)0x8f, 0x01, 0x00, 0x00, 0x01, 0x78,
64  0x42, 0x00, 0x40, 0x01, 0x00, 0x00, 0x01, 0x70, 0x42, 0x00, 0x42, 0x05, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x45, 0x01, 0x00, 0x00, 0x00, 0x30,
65  0x42, 0x00, 0x43, 0x08, 0x00, 0x00, 0x00, 0x28, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
66  0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x42, 0x00, 0x28, 0x05, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00,
67  0x42, 0x00, 0x2a, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x46, 0x01, 0x00, 0x00, 0x01, 0x00, 0x42, 0x00, (byte)0x9e, 0x05, 0x00, 0x00, 0x00, 0x04,
68  0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x36, 0x01, 0x00, 0x00, 0x00, 0x78, 0x42, 0x00, (byte)0x94, 0x07, 0x00, 0x00, 0x00, 0x11, 0x31, 0x31, 0x31, 0x2d, 0x32, 0x32, 0x32, 0x2d,
69  0x64, 0x68, 0x64, 0x68, 0x64, 0x2d, 0x64, 0x64, 0x35, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x2b, 0x01, 0x00, 0x00, 0x00, 0x50, 0x42, 0x00, 0x11, 0x05, 0x00, 0x00, 0x00, 0x04,
70  0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x5f, 0x05, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, (byte)0x83, 0x05, 0x00, 0x00, 0x00, 0x04,
71  0x00, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, (byte)0xc5, 0x06, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, (byte)0xcd, 0x02, 0x00, 0x00, 0x00, 0x04,
72  0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x4e, 0x01, 0x00, 0x00, 0x00, 0x38, 0x42, 0x00, (byte)0x94, 0x07, 0x00, 0x00, 0x00, 0x07, 0x34, 0x35, 0x2d, 0x33, 0x33, 0x34, 0x34, 0x00,
73  0x42, 0x00, 0x2b, 0x01, 0x00, 0x00, 0x00, 0x20, 0x42, 0x00, 0x38, 0x05, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, (byte)0xae, 0x05, 0x00, 0x00, 0x00, 0x04,
74  0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x4d, 0x08, 0x00, 0x00, 0x00, 0x08, (byte)0xab, (byte)0xbc, 0x34, (byte)0xff, (byte)0xf3, (byte)0xd3, 0x5a, 0x0c, 0x42, 0x00, 0x3d, 0x08, 0x00, 0x00, 0x00, 0x04,
75  0x11, 0x22, 0x33, 0x44, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, (byte)0xa3, 0x05, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00
76  };
77 
78  try {
79  sl.initializeLibrary(P6KMIPServerLib.FLAGS_NONE);
80 
81  String libVersion = sl.getLibraryVersion();
82  System.out.println(libVersion);
83 
84  // -> server read incoming KMIP request message from a socket and loaded those bytes (e.g., TTLV, XML, JSON) into the parser)
85  // -> the type of message: TTLC, XML, JSON can be determine by the mime type passed in the HTTP request, or lack of one if just using SSL connection
86  sl.setMessageBuffer(testMessage, KMIPConstants.FORMAT_TTLV);
87 
88  // -> now we can pull parts of the request message apart, this can be done over and over again if desired
89  RequestHeader rh = sl.getRequestHeader();
90  assertEquals("1.3", rh.getProtocolVersion());
91 
92  // -> parsed message is maintained in parser until another call to setMessageBuffer() of freeLibrary() is called
93  for (int i = 0; i < rh.getBatchCount(); i++) {
94 
95  BatchItem bi = sl.getBatchItem(i + 1);
96  if (bi instanceof RegisterWrappedKeyBatchItem) {
97  RegisterWrappedKeyBatchItem rb = (RegisterWrappedKeyBatchItem) bi;
98  assertEquals(1, rb.getUniqueBatchId()[0]);
99  assertEquals(KMIPConstants.OBJECT_SYMMETRICKEY, rb.getObjectType());
100  assertEquals(KMIPConstants.ALG_AES, rb.getAlgorithm());
101  assertEquals(KMIPConstants.KEYFORMAT_TRANSYMKEY, rb.getKeyFromatType());
102  assertEquals(256, rb.getKeyLength());
103 
104  byte[] keyBytes = rb.getWrappedBytes();
105  assertEquals(40, keyBytes.length);
106  for (int j = 0; j < keyBytes.length; j++) {
107  if (j < 14)
108  assertEquals((byte) j, keyBytes[j]);
109  else assertEquals((byte)j+1, keyBytes[j]);
110  }
111 
112  // TBD get encrypt params
113 
114  // TBD get sign params
115 
116 
117  String[] templateAttributes = rb.getTemplateAttributes();
118  assertEquals(1, templateAttributes.length);
119  assertEquals("x-ID: P6R-MEMORY-TEST-7", templateAttributes[0]);
120 
121  String[] templateNames = rb.getTemplateNames();
122  if (null != templateNames) assertEquals(0,1);
123  }
124  }
125  sl.freeLibrary();
126 
127  } catch (Exception e) {
128  // -> we shoud not get here
129  System.out.println(e.toString());
130  assertEquals(0, 1);
131  }
132  }
133 }
void JNICall_WrappedRegisterKMIP()
Test: Verify parser can break message into multiple batch items.
A JUNIT test demonstrating how to parse an incoming KMIP request from a client.