void SessionObjectTests::testDoubleAttr() { ByteString value3 = "BDEBDBEDBBDBEBDEBE792759537328"; ByteString value3a = "466487346943785684957634"; SessionObject testObject(NULL, 1, 1); CPPUNIT_ASSERT(testObject.isValid()); bool value1 = true; unsigned long value2 = 0x87654321; OSAttribute attr1(value1); OSAttribute attr2(value2); OSAttribute attr3(value3); CPPUNIT_ASSERT(testObject.setAttribute(CKA_TOKEN, attr1)); CPPUNIT_ASSERT(testObject.setAttribute(CKA_PRIME_BITS, attr2)); CPPUNIT_ASSERT(testObject.setAttribute(CKA_VALUE_BITS, attr3)); CPPUNIT_ASSERT(testObject.isValid()); CPPUNIT_ASSERT(testObject.attributeExists(CKA_TOKEN)); CPPUNIT_ASSERT(testObject.attributeExists(CKA_PRIME_BITS)); CPPUNIT_ASSERT(testObject.attributeExists(CKA_VALUE_BITS)); CPPUNIT_ASSERT(!testObject.attributeExists(CKA_ID)); CPPUNIT_ASSERT(testObject.getAttribute(CKA_TOKEN)->isBooleanAttribute()); CPPUNIT_ASSERT(testObject.getAttribute(CKA_PRIME_BITS)->isUnsignedLongAttribute()); CPPUNIT_ASSERT(testObject.getAttribute(CKA_VALUE_BITS)->isByteStringAttribute()); CPPUNIT_ASSERT(testObject.getAttribute(CKA_TOKEN)->getBooleanValue() == true); CPPUNIT_ASSERT(testObject.getAttribute(CKA_PRIME_BITS)->getUnsignedLongValue() == 0x87654321); CPPUNIT_ASSERT(testObject.getAttribute(CKA_VALUE_BITS)->getByteStringValue() == value3); bool value1a = false; unsigned long value2a = 0x76767676; OSAttribute attr1a(value1a); OSAttribute attr2a(value2a); OSAttribute attr3a(value3a); // Change the attributes CPPUNIT_ASSERT(testObject.isValid()); CPPUNIT_ASSERT(testObject.setAttribute(CKA_TOKEN, attr1a)); CPPUNIT_ASSERT(testObject.setAttribute(CKA_PRIME_BITS, attr2a)); CPPUNIT_ASSERT(testObject.setAttribute(CKA_VALUE_BITS, attr3a)); // Check the attributes CPPUNIT_ASSERT(testObject.getAttribute(CKA_TOKEN)->isBooleanAttribute()); CPPUNIT_ASSERT(testObject.getAttribute(CKA_PRIME_BITS)->isUnsignedLongAttribute()); CPPUNIT_ASSERT(testObject.getAttribute(CKA_VALUE_BITS)->isByteStringAttribute()); CPPUNIT_ASSERT(testObject.getAttribute(CKA_TOKEN)->getBooleanValue() == value1a); CPPUNIT_ASSERT(testObject.getAttribute(CKA_PRIME_BITS)->getUnsignedLongValue() == value2a); CPPUNIT_ASSERT(testObject.getAttribute(CKA_VALUE_BITS)->getByteStringValue() == value3a); CPPUNIT_ASSERT(testObject.isValid()); }
void test_a_dbobject_with_an_object::should_use_transactions() { DBObject testObject(connection); CPPUNIT_ASSERT(testObject.find(1)); CPPUNIT_ASSERT(testObject.isValid()); bool value1 = true; unsigned long value2 = 0x87654321; unsigned long value3 = 0xBDEBDBED; ByteString value4 = "AAAAAAAAAAAAAAAFFFFFFFFFFFFFFF"; OSAttribute attr1(value1); OSAttribute attr2(value2); OSAttribute attr3(value3); OSAttribute attr4(value4); CPPUNIT_ASSERT(testObject.setAttribute(CKA_TOKEN, attr1)); CPPUNIT_ASSERT(testObject.setAttribute(CKA_PRIME_BITS, attr2)); CPPUNIT_ASSERT(testObject.setAttribute(CKA_VALUE_BITS, attr3)); CPPUNIT_ASSERT(testObject.setAttribute(CKA_ID, attr4)); // Create secondary instance for the same object. // This needs to have a different connection to the database to simulate // another process accessing the data. DBObject testObject2(connection2); CPPUNIT_ASSERT(testObject2.find(1)); CPPUNIT_ASSERT(testObject2.isValid()); // Check that it has the same attributes CPPUNIT_ASSERT(testObject2.getAttribute(CKA_TOKEN).isBooleanAttribute()); CPPUNIT_ASSERT(testObject2.getAttribute(CKA_PRIME_BITS).isUnsignedLongAttribute()); CPPUNIT_ASSERT(testObject2.getAttribute(CKA_VALUE_BITS).isUnsignedLongAttribute()); CPPUNIT_ASSERT(testObject2.getAttribute(CKA_ID).isByteStringAttribute()); // Check that the attributes have the same values as set on testObject. CPPUNIT_ASSERT(testObject2.getAttribute(CKA_TOKEN).getBooleanValue() == value1); CPPUNIT_ASSERT(testObject2.getAttribute(CKA_PRIME_BITS).getUnsignedLongValue() == value2); CPPUNIT_ASSERT(testObject2.getAttribute(CKA_VALUE_BITS).getUnsignedLongValue() == value3); CPPUNIT_ASSERT(testObject2.getAttribute(CKA_ID).getByteStringValue() == value4); // New values bool value1a = false; unsigned long value2a = 0x12345678; unsigned long value3a = 0xABABABAB; ByteString value4a = "EDEDEDEDEDEDEDEDEDEDEDEDEDEDED"; OSAttribute attr1a(value1a); OSAttribute attr2a(value2a); OSAttribute attr3a(value3a); OSAttribute attr4a(value4a); // Start transaction on object CPPUNIT_ASSERT(testObject.startTransaction(DBObject::ReadWrite)); // Change the attributes CPPUNIT_ASSERT(testObject.setAttribute(CKA_TOKEN, attr1a)); CPPUNIT_ASSERT(testObject.setAttribute(CKA_PRIME_BITS, attr2a)); CPPUNIT_ASSERT(testObject.setAttribute(CKA_VALUE_BITS, attr3a)); CPPUNIT_ASSERT(testObject.setAttribute(CKA_ID, attr4a)); // Verify that the attributes were set CPPUNIT_ASSERT(testObject.getAttribute(CKA_TOKEN).isBooleanAttribute()); CPPUNIT_ASSERT(testObject.getAttribute(CKA_PRIME_BITS).isUnsignedLongAttribute()); CPPUNIT_ASSERT(testObject.getAttribute(CKA_VALUE_BITS).isUnsignedLongAttribute()); CPPUNIT_ASSERT(testObject.getAttribute(CKA_ID).isByteStringAttribute()); CPPUNIT_ASSERT(testObject.getAttribute(CKA_TOKEN).getBooleanValue() == value1a); CPPUNIT_ASSERT(testObject.getAttribute(CKA_PRIME_BITS).getUnsignedLongValue() == value2a); CPPUNIT_ASSERT(testObject.getAttribute(CKA_VALUE_BITS).getUnsignedLongValue() == value3a); CPPUNIT_ASSERT(testObject.getAttribute(CKA_ID).getByteStringValue() == value4a); // Verify that they are unchanged on the other instance CPPUNIT_ASSERT(testObject2.getAttribute(CKA_TOKEN).isBooleanAttribute()); CPPUNIT_ASSERT(testObject2.getAttribute(CKA_PRIME_BITS).isUnsignedLongAttribute()); CPPUNIT_ASSERT(testObject2.getAttribute(CKA_VALUE_BITS).isUnsignedLongAttribute()); CPPUNIT_ASSERT(testObject2.getAttribute(CKA_ID).isByteStringAttribute()); CPPUNIT_ASSERT(testObject2.getAttribute(CKA_TOKEN).getBooleanValue() == value1); CPPUNIT_ASSERT(testObject2.getAttribute(CKA_PRIME_BITS).getUnsignedLongValue() == value2); CPPUNIT_ASSERT(testObject2.getAttribute(CKA_VALUE_BITS).getUnsignedLongValue() == value3); CPPUNIT_ASSERT(testObject2.getAttribute(CKA_ID).getByteStringValue() == value4); // Commit the transaction CPPUNIT_ASSERT(testObject.commitTransaction()); // Verify that non-modifiable attributes did not propagate but modifiable attributes // have now changed on the other instance CPPUNIT_ASSERT(testObject2.getAttribute(CKA_TOKEN).isBooleanAttribute()); CPPUNIT_ASSERT(testObject2.getAttribute(CKA_PRIME_BITS).isUnsignedLongAttribute()); CPPUNIT_ASSERT(testObject2.getAttribute(CKA_VALUE_BITS).isUnsignedLongAttribute()); CPPUNIT_ASSERT(testObject2.getAttribute(CKA_ID).isByteStringAttribute()); // NOTE: 3 attributes below cannot be modified after creation and therefore are not required to propagate. CPPUNIT_ASSERT(testObject2.getAttribute(CKA_TOKEN).getBooleanValue() != value1a); CPPUNIT_ASSERT(testObject2.getAttribute(CKA_PRIME_BITS).getUnsignedLongValue() != value2a); CPPUNIT_ASSERT(testObject2.getAttribute(CKA_VALUE_BITS).getUnsignedLongValue() != value3a); // CKA_ID attribute can be modified after creation and therefore should have propagated. CPPUNIT_ASSERT(testObject2.getAttribute(CKA_ID).getByteStringValue() == value4a); // Start transaction on object CPPUNIT_ASSERT(testObject.startTransaction(DBObject::ReadWrite)); // Change the attributes CPPUNIT_ASSERT(testObject.setAttribute(CKA_TOKEN, attr1)); CPPUNIT_ASSERT(testObject.setAttribute(CKA_PRIME_BITS, attr2)); CPPUNIT_ASSERT(testObject.setAttribute(CKA_VALUE_BITS, attr3)); CPPUNIT_ASSERT(testObject.setAttribute(CKA_ID, attr4)); // Verify that the attributes were set CPPUNIT_ASSERT(testObject.getAttribute(CKA_TOKEN).isBooleanAttribute()); CPPUNIT_ASSERT(testObject.getAttribute(CKA_PRIME_BITS).isUnsignedLongAttribute()); CPPUNIT_ASSERT(testObject.getAttribute(CKA_VALUE_BITS).isUnsignedLongAttribute()); CPPUNIT_ASSERT(testObject.getAttribute(CKA_ID).isByteStringAttribute()); CPPUNIT_ASSERT(testObject.getAttribute(CKA_TOKEN).getBooleanValue() == value1); CPPUNIT_ASSERT(testObject.getAttribute(CKA_PRIME_BITS).getUnsignedLongValue() == value2); CPPUNIT_ASSERT(testObject.getAttribute(CKA_VALUE_BITS).getUnsignedLongValue() == value3); CPPUNIT_ASSERT(testObject.getAttribute(CKA_ID).getByteStringValue() == value4); // Create a fresh third instance for the same object to force the data to be retrieved from the database. DBObject testObject3(connection2); CPPUNIT_ASSERT(testObject3.find(1)); CPPUNIT_ASSERT(testObject3.isValid()); // Verify that they are unchanged on the other instance, while the transaction is still in progress. CPPUNIT_ASSERT(testObject3.getAttribute(CKA_TOKEN).isBooleanAttribute()); CPPUNIT_ASSERT(testObject3.getAttribute(CKA_PRIME_BITS).isUnsignedLongAttribute()); CPPUNIT_ASSERT(testObject3.getAttribute(CKA_VALUE_BITS).isUnsignedLongAttribute()); CPPUNIT_ASSERT(testObject3.getAttribute(CKA_ID).isByteStringAttribute()); // Verify that the attributes from the database are still hodling the same value as when the transaction started. CPPUNIT_ASSERT(testObject3.getAttribute(CKA_TOKEN).getBooleanValue() == value1a); CPPUNIT_ASSERT(testObject3.getAttribute(CKA_PRIME_BITS).getUnsignedLongValue() == value2a); CPPUNIT_ASSERT(testObject3.getAttribute(CKA_VALUE_BITS).getUnsignedLongValue() == value3a); CPPUNIT_ASSERT(testObject3.getAttribute(CKA_ID).getByteStringValue() == value4a); // Abort the transaction CPPUNIT_ASSERT(testObject.abortTransaction()); // Verify that after aborting the transaction the values in testObject have reverted back to their // original state. CPPUNIT_ASSERT(testObject.getAttribute(CKA_TOKEN).isBooleanAttribute()); CPPUNIT_ASSERT(testObject.getAttribute(CKA_PRIME_BITS).isUnsignedLongAttribute()); CPPUNIT_ASSERT(testObject.getAttribute(CKA_VALUE_BITS).isUnsignedLongAttribute()); CPPUNIT_ASSERT(testObject.getAttribute(CKA_ID).isByteStringAttribute()); // After aborting a transaction the testObject should be back to pre transaction state. CPPUNIT_ASSERT(testObject.getAttribute(CKA_TOKEN).getBooleanValue() == value1a); CPPUNIT_ASSERT(testObject.getAttribute(CKA_PRIME_BITS).getUnsignedLongValue() == value2a); CPPUNIT_ASSERT(testObject.getAttribute(CKA_VALUE_BITS).getUnsignedLongValue() == value3a); CPPUNIT_ASSERT(testObject.getAttribute(CKA_ID).getByteStringValue() == value4a); // Verify that testObject3 still has the original values. CPPUNIT_ASSERT(testObject3.getAttribute(CKA_TOKEN).isBooleanAttribute()); CPPUNIT_ASSERT(testObject3.getAttribute(CKA_PRIME_BITS).isUnsignedLongAttribute()); CPPUNIT_ASSERT(testObject3.getAttribute(CKA_VALUE_BITS).isUnsignedLongAttribute()); CPPUNIT_ASSERT(testObject3.getAttribute(CKA_ID).isByteStringAttribute()); // Verify that testObject3 still has the original values. CPPUNIT_ASSERT(testObject3.getAttribute(CKA_TOKEN).getBooleanValue() == value1a); CPPUNIT_ASSERT(testObject3.getAttribute(CKA_PRIME_BITS).getUnsignedLongValue() == value2a); CPPUNIT_ASSERT(testObject3.getAttribute(CKA_VALUE_BITS).getUnsignedLongValue() == value3a); CPPUNIT_ASSERT(testObject3.getAttribute(CKA_ID).getByteStringValue() == value4a); }