Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
JniAttributesKmip.java
package com.p6r.kmip;
import org.junit.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
public class JniAttributesKmip {
private static final String _hostName = "kmiptest01.p6r.com";
@BeforeClass
public static void oneTimeSetUp() {
// NOOP
System.out.println("@BeforeClass - oneTimeSetUp");
}
@AfterClass
public static void oneTimeTearDown() {
// NOOP
System.out.println("@AfterClass - oneTimeTearDown");
}
@Before
public void setUp() {
// NOOP
System.out.println("@Before - setUp");
}
@After
public void tearDown() {
// NOOP
System.out.println("@After - tearDown");
}
@Test
public void JNICall_attributesKMIP() {
System.out.println("@Test - JNICall-attributesKMIP");
String uid1 = null;
String uid2 = null;
String[] attribute = null;
P6KMIPClient kc = new P6KMIPClient();
try {
kc.initializeLibrary(P6KMIPClient.FLAGS_NONE);
kc.open(_hostName, null);
// -> create a symmetric key and use it to test out attributes
uid1 = kc.createSymmetricKey(KMIPConstants.ALG_AES, 128, (KMIPConstants.USAGE_MASK_ENCRYPT | KMIPConstants.USAGE_MASK_DECRYPT));
System.out.println("UID of new AES key: " + uid1);
// -> test what happens when we pass in a null value for a string parameter
try {
kc.destroy(uid2);
} catch (Exception e) {
System.out.println(e.toString());
}
attribute = kc.getAttribute(uid1, "Original Creation Date");
System.out.println("Original Creation Date of key: " + attribute[0]);
kc.addTextAttribute(uid1, "x-ID", "Test-Attributes-1");
attribute = kc.getAttribute(uid1, "x-ID");
assertEquals(attribute[0], "Test-Attributes-1");
kc.modifyTextAttribute(uid1, "x-ID", "Modified-Test-Attributes-1", 0);
attribute = kc.getAttribute(uid1, "x-ID");
assertEquals(attribute[0], "Modified-Test-Attributes-1");
// -> attribute is gone so we should get back an empty array
kc.deleteAttribute(uid1, "x-ID", 0);
attribute = kc.getAttribute(uid1, "x-ID");
assertEquals(0, attribute.length);
// -> try time attribute, add one week to the current time
kc.addTimeAttribute(uid1, "Protect Stop Date", System.currentTimeMillis() + 604800000); // -> one week in the future
kc.modifyTimeAttribute(uid1, "Protect Stop Date", System.currentTimeMillis() + 259200000); // -> 3 days in the future
attribute = kc.getAttribute(uid1, "Protect Stop Date");
System.out.println("Protect Stop Date of key: " + attribute[0]);
// -> if server supports multi-value attributes then the second call with be at index 1
kc.addTextAttribute(uid1, "x-ID-5", "Test-Attributes-5");
kc.addTextAttribute(uid1, "x-ID-5", "Test-Attributes-999");
attribute = kc.getAttribute(uid1, "x-ID-5");
assertEquals(attribute[0], "Test-Attributes-5");
assertEquals(attribute[1], "Test-Attributes-999 (index:1)");
// -> modify the 2nd instance
kc.modifyTextAttribute(uid1, "x-ID-5", "Modified-Test-Attributes-999", 1);
attribute = kc.getAttribute(uid1, "x-ID-5");
assertEquals(attribute[0], "Test-Attributes-5");
assertEquals(attribute[1], "Modified-Test-Attributes-999 (index:1)");
// -> delete the 2nd instance
kc.deleteAttribute(uid1, "x-ID-5", 1);
attribute = kc.getAttribute(uid1, "x-ID-5");
assertEquals(attribute[0], "Test-Attributes-5");
assertEquals(1, attribute.length);
// A bunch of error tests
// -> can't modify an attribute that does not exist
try {
kc.modifyTextAttribute(uid1, "Contact Information", "Mark Joseph", 0);
} catch (Exception e) {
System.out.println(e.toString());
}
// -> can't delete an attribute that does not exist
try {
kc.deleteAttribute(uid1, "Contact Information", 0);
} catch (Exception e) {
System.out.println(e.toString());
}
// -> cannot add an unsupported attribute
try {
kc.addTextAttribute(uid1, "Cryptographic Algorithm", "AES");
} catch (Exception e) {
System.out.println(e.toString());
}
kc.destroy(uid1);
kc.close();
kc.freeLibrary();
} catch (Exception e) {
// -> we shoud not get here
System.out.println(e.toString());
assertEquals(0, 1);
}
}
}