示例#1
0
void test_a_dbobject_with_an_object::should_store_double_attributes()
{
    bool value1 = true;
    bool value1a = false;

    // Create the test object
    {
        DBObject testObject(connection);
        CPPUNIT_ASSERT(testObject.find(1));
        CPPUNIT_ASSERT(testObject.isValid());

        OSAttribute attr1(value1);

        CPPUNIT_ASSERT(testObject.setAttribute(CKA_SIGN, attr1));
    }

    // Now read back the object
    {
        DBObject testObject(connection);
        CPPUNIT_ASSERT(testObject.find(1));
        CPPUNIT_ASSERT(testObject.isValid());

        CPPUNIT_ASSERT(testObject.attributeExists(CKA_SIGN));
        CPPUNIT_ASSERT(!testObject.attributeExists(CKA_ID));

        CPPUNIT_ASSERT(testObject.getAttribute(CKA_SIGN).isBooleanAttribute());

        CPPUNIT_ASSERT(testObject.getAttribute(CKA_SIGN).getBooleanValue());

        OSAttribute attr1(value1a);

        // Change the attributes
        CPPUNIT_ASSERT(testObject.isValid());

        CPPUNIT_ASSERT(testObject.setAttribute(CKA_SIGN, attr1));

        // Check the attributes
        CPPUNIT_ASSERT(testObject.getAttribute(CKA_SIGN).isBooleanAttribute());

        CPPUNIT_ASSERT(testObject.getAttribute(CKA_SIGN).getBooleanValue() == value1a);
    }

    // Now re-read back the object
    {
        DBObject testObject(connection);
        CPPUNIT_ASSERT(testObject.find(1));
        CPPUNIT_ASSERT(testObject.isValid());

        CPPUNIT_ASSERT(testObject.attributeExists(CKA_SIGN));
        CPPUNIT_ASSERT(!testObject.attributeExists(CKA_ID));

        CPPUNIT_ASSERT(testObject.getAttribute(CKA_SIGN).isBooleanAttribute());

        CPPUNIT_ASSERT(testObject.getAttribute(CKA_SIGN).getBooleanValue() == value1a);
    }
}
示例#2
0
BOOST_AUTO_TEST_CASE_TEMPLATE(decorator_formatting, CharT, char_types)
{
    typedef logging::record_view record_view;
    typedef logging::attribute_set attr_set;
    typedef std::basic_string< CharT > string;
    typedef logging::basic_formatting_ostream< CharT > osstream;
    typedef logging::basic_formatter< CharT > formatter;
    typedef test_strings< CharT > data;

    attrs::constant< string > attr1(data::printable_chars());

    attr_set set1;
    set1[data::attr1()] = attr1;

    record_view rec = make_record_view(set1);

    {
        string str1, str2;
        osstream strm1(str1), strm2(str2);
        formatter f = expr::stream << expr::make_xml_decor< CharT >()[ expr::stream << expr::attr< string >(data::attr1()) ];
        f(rec, strm1);
        strm2 << data::escaped_chars();
        BOOST_CHECK(equal_strings(strm1.str(), strm2.str()));
    }
}
示例#3
0
/***********************************************************************//**
 * @brief Test XML attributes
 **************************************************************************/
void TestGXml::test_GXml_attributes(void)
{
    // Test attribute name-value constructors
    GXmlAttribute attr1("test", "1.0");
    test_assert(attr1.value() == "1.0", "Test if value()= 1.0",
                "Unexpected attribute "+attr1.value());
    GXmlAttribute attr2("test", "\"1.0\"");
    test_assert(attr2.value() == "1.0", "Test if value()= 1.0",
                "Unexpected attribute "+attr2.value());
    GXmlAttribute attr3("test", "'1.0'");
    test_assert(attr3.value() == "1.0", "Test if value()= 1.0",
                "Unexpected attribute "+attr3.value());
    GXmlAttribute attr4("test", "''1.0'");
    test_assert(attr4.value() == "'1.0", "Test if value()= '1.0",
                "Unexpected attribute "+attr4.value());
    GXmlAttribute attr5("test", "'1.0");
    test_assert(attr5.value() == "'1.0", "Test if value()= '1.0",
                "Unexpected attribute "+attr5.value());
    GXmlAttribute attr6("test", "1.0'");
    test_assert(attr6.value() == "1.0'", "Test if value()= 1.0'",
                "Unexpected attribute "+attr6.value());
    GXmlAttribute attr7("test", "\"1.0");
    test_assert(attr7.value() == "\"1.0", "Test if value()= \"1.0",
                "Unexpected attribute "+attr7.value());
    GXmlAttribute attr8("test", "1.0\"");
    test_assert(attr8.value() == "1.0\"", "Test if value()= 1.0\"",
                "Unexpected attribute "+attr8.value());
    GXmlAttribute attr9("test", "\"\"1.0\"");
    test_assert(attr9.value() == "\"1.0", "Test if value()= \"1.0",
                "Unexpected attribute "+attr9.value());

    // Return
    return;
}
示例#4
0
// The test checks that Boost.Format formatting works
BOOST_AUTO_TEST_CASE_TEMPLATE(formatting, CharT, char_types)
{
    typedef logging::attribute_set attr_set;
    typedef std::basic_string< CharT > string;
    typedef logging::basic_formatting_ostream< CharT > osstream;
    typedef logging::record_view record_view;
    typedef logging::basic_formatter< CharT > formatter;
    typedef format_test_data< CharT > data;

    attrs::constant< int > attr1(10);
    attrs::constant< double > attr2(5.5);

    attr_set set1;
    set1[data::attr1()] = attr1;
    set1[data::attr2()] = attr2;

    record_view rec = make_record_view(set1);

    {
        string str1, str2;
        osstream strm1(str1), strm2(str2);
        formatter f = expr::format(data::format1()) % expr::attr< int >(data::attr1()) % expr::attr< double >(data::attr2());
        f(rec, strm1);
        strm2 << 10 << ", " << 5.5;
        BOOST_CHECK(equal_strings(strm1.str(), strm2.str()));
    }
}
void SessionObjectTests::testMixedAttr()
{
	ByteString value3 = "BDEBDBEDBBDBEBDEBE792759537328";

    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);
}
示例#6
0
int main(int argc, char* argv[]) {
  PNEAGraph p;
  TStr attr1 = "double";
  TStr attr2 = "triple";
  TStr attr3 = "double-float";
  p = TNEAGraph::New();
  p->AddNode();
  p->AddNode();
  p->AddNode();
  for (TNEAGraph::TNodeI NI = p->BegNI(); NI < p->EndNI(); NI++) {
    printf("Node id: %d %s\n", NI.GetId(), attr1());
    p->AddIntAttrDat(NI.GetId(), NI.GetId()*2, attr1);
    p->AddIntAttrDat(NI.GetId(), NI.GetId()*3, attr2);
    p->AddFltAttrDat(NI.GetId(), (float) (NI.GetId()*2), attr3);
    //    p->AddStrAttrDat(NI.GetId(), "Test value", attr1);
  }
  p->AddNode();
  for (TNEAGraph::TNodeI NI = p->BegNI(); NI < p->EndNI(); NI++) {
    int IntVal = p->GetIntAttrDat(NI.GetId(), attr1)();
    printf("Node id: %d, value: %d\n", NI.GetId(), IntVal);
    printf("Node id: %d, value: %d\n", NI.GetId(), p->GetIntAttrDat(NI.GetId(), attr2)());
    printf("Node id: %d, value: %f\n", NI.GetId(), p->GetFltAttrDat(NI.GetId(), attr3)());
    //printf("Node id: %d, value: %s\n", NI.GetId(), p->GetStrAttrDat(NI.GetId(), attr1)());
  }
  p->Clr();
  printf("Yay!\n");
}
void test_a_dbobject_with_an_object::should_store_unsigned_long_attributes()
{
	// Add unsigned long attributes to the object
	{
		DBObject testObject(connection);
		CPPUNIT_ASSERT(testObject.find(1));
		CPPUNIT_ASSERT(testObject.isValid());

		unsigned long value1 = 0x12345678;
		unsigned long value2 = 0x87654321;
		unsigned long value3 = 0x01010101;
		unsigned long value4 = 0x10101010;
		unsigned long value5 = 0xABCDEF;

		OSAttribute attr1(value1);
		OSAttribute attr2(value2);
		OSAttribute attr3(value3);
		OSAttribute attr4(value4);
		OSAttribute attr5(value5);

		CPPUNIT_ASSERT(testObject.setAttribute(CKA_MODULUS_BITS, attr1));
		CPPUNIT_ASSERT(testObject.setAttribute(CKA_PRIME_BITS, attr2));
		CPPUNIT_ASSERT(testObject.setAttribute(CKA_AUTH_PIN_FLAGS, attr3));
		CPPUNIT_ASSERT(testObject.setAttribute(CKA_SUBPRIME_BITS, attr4));
		CPPUNIT_ASSERT(testObject.setAttribute(CKA_KEY_TYPE, attr5));
	}

	// Now read back the object
	{
		DBObject testObject(connection);
		CPPUNIT_ASSERT(testObject.find(1));
		CPPUNIT_ASSERT(testObject.isValid());

		CPPUNIT_ASSERT(testObject.attributeExists(CKA_MODULUS_BITS));
		CPPUNIT_ASSERT(testObject.attributeExists(CKA_PRIME_BITS));
		CPPUNIT_ASSERT(testObject.attributeExists(CKA_AUTH_PIN_FLAGS));
		CPPUNIT_ASSERT(testObject.attributeExists(CKA_SUBPRIME_BITS));
		CPPUNIT_ASSERT(testObject.attributeExists(CKA_KEY_TYPE));
		CPPUNIT_ASSERT(!testObject.attributeExists(CKA_ID));

		CPPUNIT_ASSERT(testObject.getAttribute(CKA_MODULUS_BITS)->isUnsignedLongAttribute());
		CPPUNIT_ASSERT(testObject.getAttribute(CKA_PRIME_BITS)->isUnsignedLongAttribute());
		CPPUNIT_ASSERT(testObject.getAttribute(CKA_AUTH_PIN_FLAGS)->isUnsignedLongAttribute());
		CPPUNIT_ASSERT(testObject.getAttribute(CKA_SUBPRIME_BITS)->isUnsignedLongAttribute());
		CPPUNIT_ASSERT(testObject.getAttribute(CKA_KEY_TYPE)->isUnsignedLongAttribute());

		CPPUNIT_ASSERT_EQUAL(testObject.getAttribute(CKA_MODULUS_BITS)->getUnsignedLongValue(), (unsigned long)0x12345678);
		CPPUNIT_ASSERT_EQUAL(testObject.getAttribute(CKA_PRIME_BITS)->getUnsignedLongValue(), (unsigned long)0x87654321);
		CPPUNIT_ASSERT_EQUAL(testObject.getAttribute(CKA_AUTH_PIN_FLAGS)->getUnsignedLongValue(), (unsigned long)0x01010101);
		CPPUNIT_ASSERT_EQUAL(testObject.getAttribute(CKA_SUBPRIME_BITS)->getUnsignedLongValue(), (unsigned long)0x10101010);
		CPPUNIT_ASSERT_EQUAL(testObject.getAttribute(CKA_KEY_TYPE)->getUnsignedLongValue(), (unsigned long)0xABCDEF);

		unsigned long value6 = 0x90909090;
		OSAttribute attr6(value6);

		CPPUNIT_ASSERT(testObject.setAttribute(CKA_CLASS, attr6));
		CPPUNIT_ASSERT(testObject.getAttribute(CKA_CLASS)->isUnsignedLongAttribute());
		CPPUNIT_ASSERT_EQUAL(testObject.getAttribute(CKA_CLASS)->getUnsignedLongValue(), value6);
	}
}
示例#8
0
// The test checks that message formatting work
BOOST_AUTO_TEST_CASE_TEMPLATE(message_formatting, CharT, char_types)
{
    typedef logging::attribute_set attr_set;
    typedef std::basic_string< CharT > string;
    typedef logging::basic_formatting_ostream< CharT > osstream;
    typedef logging::record_view record_view;
    typedef logging::basic_formatter< CharT > formatter;
    typedef message_test_data< CharT > data;

    attrs::constant< int > attr1(10);
    attrs::constant< double > attr2(5.5);

    attr_set set1;
    set1[data::attr1()] = attr1;
    set1[data::attr2()] = attr2;
    set1[data::message().get_name()] = attrs::constant< string >(data::some_test_string());

    record_view rec = make_record_view(set1);

    {
        string str1, str2;
        osstream strm1(str1), strm2(str2);
        formatter f = expr::stream << data::message();
        f(rec, strm1);
        strm2 << data::some_test_string();
        BOOST_CHECK(equal_strings(strm1.str(), strm2.str()));
    }
}
示例#9
0
// The test checks that Boost.Format formatting works
BOOST_AUTO_TEST_CASE_TEMPLATE(formatting, CharT, char_types)
{
    typedef logging::basic_attribute_set< CharT > attr_set;
    typedef std::basic_string< CharT > string;
    typedef std::basic_ostringstream< CharT > osstream;
    typedef logging::basic_record< CharT > record;
    typedef boost::function< void (osstream&, record const&) > formatter;
    typedef format_test_data< CharT > data;

    attrs::constant< int > attr1(10);
    attrs::constant< double > attr2(5.5);

    attr_set set1;
    set1[data::attr1()] = attr1;
    set1[data::attr2()] = attr2;

    record rec = make_record(set1);
    rec.message() = data::some_test_string();

    {
        osstream strm1;
        formatter f = fmt::format(data::format1()) % fmt::attr< int >(data::attr1()) % fmt::attr< double >(data::attr2());
        f(strm1, rec);
        osstream strm2;
        strm2 << 10 << ", " << 5.5;
        BOOST_CHECK(equal_strings(strm1.str(), strm2.str()));
    }
}
void SeExprEdAnimCurveControl::editGraphClicked(){
#ifdef SEEXPR_USE_ANIMLIB
    QDialog* dialog=new QDialog(this);
    CETool* tool=new CETool;
    animlib::AnimAttrID attr1("","");
    animlib::AnimCurve& anim=*new animlib::AnimCurve(attr1);
    anim=_editable->curve;
    
    QWidget *widg;
    tool->map(widg,0);
    QVBoxLayout* layout=new QVBoxLayout();
    dialog->resize(QSize(1024,640));
    dialog->setLayout(layout);
    layout->addWidget(widg);
    tool->addCurve(&anim);

    QDialogButtonBox* buttonbar=new QDialogButtonBox();
    buttonbar->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);
    connect(buttonbar,SIGNAL(accepted()),dialog,SLOT(accept()));
    connect(buttonbar,SIGNAL(rejected()),dialog,SLOT(reject()));
    layout->addWidget(buttonbar);

    if(dialog->exec()==QDialog::Accepted){
        // copy points back from child
        _editable->curve=anim;
        _preview->sample(_editable->curve);
        _preview->repaint();
        emit controlChanged(_id);
    }
#endif
}
示例#11
0
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());
}
bool derived_obj_t::push_leaf_attr_to_npu (nas_attr_id_t attr_id,
                                           npu_id_t npu_id)
{
    t_std_error rc = STD_ERR_OK;

    switch (attr_id)
    {
        case BASE_UT_ATTR1:
            if ((rc = ndi_ut_obj_attr1_set (npu_id, ndi_obj_id(npu_id),
                                            attr1()))
                != STD_ERR_OK)
            {
                throw nas::base_exception {rc, __PRETTY_FUNCTION__,
                                          std::string {"NDI Fail: Set Attr1 in NPU "}
                                         + std::to_string (npu_id)};
            }
            break;
        case BASE_UT_ATTR2:
            if ((rc = ndi_ut_obj_attr2_set (npu_id, ndi_obj_id(npu_id),
                                            attr2()))
                != STD_ERR_OK)
            {
                throw nas::base_exception {rc, __PRETTY_FUNCTION__,
                                          std::string {"NDI Fail: Set Attr2 in NPU "}
                                         + std::to_string (npu_id)};
            }
            break;
        default:
            STD_ASSERT (0);
    }

    return true;
}
示例#13
0
void test_a_dbobject_with_an_object::should_store_binary_attributes()
{
    ByteString value1 = "010203040506070809";
    ByteString value2 = "ABABABABABABABABABABABABABABABABAB";
    unsigned long value3 = 0xBDED;
    ByteString value4 = "98A7E5D798A7E5D798A7E5D798A7E5D798A7E5D798A7E5D7";
    ByteString value5 = "ABCDABCDABCDABCDABCDABCDABCDABCD";

    // Create the test object
    {
        DBObject testObject(connection);
        CPPUNIT_ASSERT(testObject.find(1));
        CPPUNIT_ASSERT(testObject.isValid());

        OSAttribute attr1(value1);
        OSAttribute attr2(value2);
        OSAttribute attr3(value3);
        OSAttribute attr4(value4);
        OSAttribute attr5(value5);

        CPPUNIT_ASSERT(testObject.setAttribute(CKA_MODULUS, attr1));
        CPPUNIT_ASSERT(testObject.setAttribute(CKA_COEFFICIENT, attr2));
        CPPUNIT_ASSERT(testObject.setAttribute(CKA_VALUE_BITS, attr3));
        CPPUNIT_ASSERT(testObject.setAttribute(CKA_PUBLIC_EXPONENT, attr4));
        CPPUNIT_ASSERT(testObject.setAttribute(CKA_SUBJECT, attr5));
    }

    // Now read back the object
    {
        DBObject testObject(connection);
        CPPUNIT_ASSERT(testObject.find(1));
        CPPUNIT_ASSERT(testObject.isValid());

        CPPUNIT_ASSERT(testObject.attributeExists(CKA_MODULUS));
        CPPUNIT_ASSERT(testObject.attributeExists(CKA_COEFFICIENT));
        CPPUNIT_ASSERT(testObject.attributeExists(CKA_VALUE_BITS));
        CPPUNIT_ASSERT(testObject.attributeExists(CKA_PUBLIC_EXPONENT));
        CPPUNIT_ASSERT(testObject.attributeExists(CKA_SUBJECT));
        CPPUNIT_ASSERT(!testObject.attributeExists(CKA_ID));

        CPPUNIT_ASSERT(testObject.getAttribute(CKA_MODULUS).isByteStringAttribute());
        CPPUNIT_ASSERT(testObject.getAttribute(CKA_COEFFICIENT).isByteStringAttribute());
        CPPUNIT_ASSERT(testObject.getAttribute(CKA_VALUE_BITS).isUnsignedLongAttribute());
        CPPUNIT_ASSERT(testObject.getAttribute(CKA_PUBLIC_EXPONENT).isByteStringAttribute());
        CPPUNIT_ASSERT(testObject.getAttribute(CKA_SUBJECT).isByteStringAttribute());

        CPPUNIT_ASSERT(testObject.getAttribute(CKA_MODULUS).getByteStringValue() == value1);
        CPPUNIT_ASSERT(testObject.getAttribute(CKA_COEFFICIENT).getByteStringValue() == value2);
        CPPUNIT_ASSERT(testObject.getAttribute(CKA_VALUE_BITS).getUnsignedLongValue() == value3);
        CPPUNIT_ASSERT(testObject.getAttribute(CKA_PUBLIC_EXPONENT).getByteStringValue() == value4);
        CPPUNIT_ASSERT(testObject.getAttribute(CKA_SUBJECT).getByteStringValue() == value5);

        ByteString value6 = "909090908080808080807070707070FF";
        OSAttribute attr6(value6);

        CPPUNIT_ASSERT(testObject.setAttribute(CKA_ISSUER, attr6));
        CPPUNIT_ASSERT(testObject.getAttribute(CKA_ISSUER).isByteStringAttribute());
        CPPUNIT_ASSERT(testObject.getByteStringValue(CKA_ISSUER) == value6);
    }
}
示例#14
0
void test_a_dbobject_with_an_object::should_store_boolean_attributes()
{
    {
        DBObject testObject(connection);
        CPPUNIT_ASSERT(testObject.find(1));
        CPPUNIT_ASSERT(testObject.isValid());

        bool value1 = true;
        bool value2 = false;
        bool value3 = true;
        bool value4 = true;
        bool value5 = false;

        OSAttribute attr1(value1);
        OSAttribute attr2(value2);
        OSAttribute attr3(value3);
        OSAttribute attr4(value4);
        OSAttribute attr5(value5);

        CPPUNIT_ASSERT(testObject.setAttribute(CKA_TOKEN, attr1));
        CPPUNIT_ASSERT(testObject.setAttribute(CKA_SENSITIVE, attr2));
        CPPUNIT_ASSERT(testObject.setAttribute(CKA_EXTRACTABLE, attr3));
        CPPUNIT_ASSERT(testObject.setAttribute(CKA_NEVER_EXTRACTABLE, attr4));
        CPPUNIT_ASSERT(testObject.setAttribute(CKA_SIGN, attr5));
    }

    {
        DBObject testObject(connection);
        CPPUNIT_ASSERT(testObject.find(1));
        CPPUNIT_ASSERT(testObject.isValid());

        CPPUNIT_ASSERT(testObject.attributeExists(CKA_TOKEN));
        CPPUNIT_ASSERT(testObject.attributeExists(CKA_SENSITIVE));
        CPPUNIT_ASSERT(testObject.attributeExists(CKA_EXTRACTABLE));
        CPPUNIT_ASSERT(testObject.attributeExists(CKA_NEVER_EXTRACTABLE));
        CPPUNIT_ASSERT(testObject.attributeExists(CKA_SIGN));
        CPPUNIT_ASSERT(!testObject.attributeExists(CKA_ID));

        CPPUNIT_ASSERT(testObject.getAttribute(CKA_TOKEN).isBooleanAttribute());
        CPPUNIT_ASSERT(testObject.getAttribute(CKA_SENSITIVE).isBooleanAttribute());
        CPPUNIT_ASSERT(testObject.getAttribute(CKA_EXTRACTABLE).isBooleanAttribute());
        CPPUNIT_ASSERT(testObject.getAttribute(CKA_NEVER_EXTRACTABLE).isBooleanAttribute());
        CPPUNIT_ASSERT(testObject.getAttribute(CKA_SIGN).isBooleanAttribute());

        CPPUNIT_ASSERT(testObject.getAttribute(CKA_TOKEN).getBooleanValue());
        CPPUNIT_ASSERT(!testObject.getAttribute(CKA_SENSITIVE).getBooleanValue());
        CPPUNIT_ASSERT(testObject.getAttribute(CKA_EXTRACTABLE).getBooleanValue());
        CPPUNIT_ASSERT(testObject.getAttribute(CKA_NEVER_EXTRACTABLE).getBooleanValue());
        CPPUNIT_ASSERT(!testObject.getAttribute(CKA_SIGN).getBooleanValue());

        bool value6 = true;
        OSAttribute attr6(value6);

        CPPUNIT_ASSERT(testObject.setAttribute(CKA_VERIFY, attr6));
        CPPUNIT_ASSERT(testObject.getAttribute(CKA_VERIFY).isBooleanAttribute());
        CPPUNIT_ASSERT_EQUAL(testObject.getAttribute(CKA_VERIFY).getBooleanValue(), value6);
        CPPUNIT_ASSERT_EQUAL(testObject.getBooleanValue(CKA_VERIFY, false), value6);
    }
}
void* derived_obj_t::alloc_fill_ndi_obj (nas::mem_alloc_helper_t& mem_trakr)
{
    ndi_ut_obj_t* ndi_ut_obj_p = mem_trakr.alloc<ndi_ut_obj_t> (1);

    ndi_ut_obj_p->attr1 = attr1();
    ndi_ut_obj_p->attr2 = attr2();

    return ndi_ut_obj_p;
}
示例#16
0
void SessionObjectTests::testBoolAttr()
{
    SessionObject testObject(NULL, 1, 1);

	CPPUNIT_ASSERT(testObject.isValid());

	bool value1 = true;
	bool value2 = false;
	bool value3 = true;
	bool value4 = true;
	bool value5 = false;

	OSAttribute attr1(value1);
	OSAttribute attr2(value2);
	OSAttribute attr3(value3);
	OSAttribute attr4(value4);
	OSAttribute attr5(value5);

	CPPUNIT_ASSERT(testObject.setAttribute(CKA_TOKEN, attr1));
	CPPUNIT_ASSERT(testObject.setAttribute(CKA_SENSITIVE, attr2));
	CPPUNIT_ASSERT(testObject.setAttribute(CKA_EXTRACTABLE, attr3));
	CPPUNIT_ASSERT(testObject.setAttribute(CKA_NEVER_EXTRACTABLE, attr4));
	CPPUNIT_ASSERT(testObject.setAttribute(CKA_SIGN, attr5));

	CPPUNIT_ASSERT(testObject.isValid());

	CPPUNIT_ASSERT(testObject.attributeExists(CKA_TOKEN));
	CPPUNIT_ASSERT(testObject.attributeExists(CKA_SENSITIVE));
	CPPUNIT_ASSERT(testObject.attributeExists(CKA_EXTRACTABLE));
	CPPUNIT_ASSERT(testObject.attributeExists(CKA_NEVER_EXTRACTABLE));
	CPPUNIT_ASSERT(testObject.attributeExists(CKA_SIGN));
	CPPUNIT_ASSERT(!testObject.attributeExists(CKA_ID));

	CPPUNIT_ASSERT(testObject.getAttribute(CKA_TOKEN)->isBooleanAttribute());
	CPPUNIT_ASSERT(testObject.getAttribute(CKA_SENSITIVE)->isBooleanAttribute());
	CPPUNIT_ASSERT(testObject.getAttribute(CKA_EXTRACTABLE)->isBooleanAttribute());
	CPPUNIT_ASSERT(testObject.getAttribute(CKA_NEVER_EXTRACTABLE)->isBooleanAttribute());
	CPPUNIT_ASSERT(testObject.getAttribute(CKA_SIGN)->isBooleanAttribute());
	CPPUNIT_ASSERT(testObject.getAttribute(CKA_ID) == NULL);

	CPPUNIT_ASSERT(testObject.getAttribute(CKA_TOKEN)->getBooleanValue() == true);
	CPPUNIT_ASSERT(testObject.getAttribute(CKA_SENSITIVE)->getBooleanValue() == false);
	CPPUNIT_ASSERT(testObject.getAttribute(CKA_EXTRACTABLE)->getBooleanValue() == true);
	CPPUNIT_ASSERT(testObject.getAttribute(CKA_NEVER_EXTRACTABLE)->getBooleanValue() == true);
	CPPUNIT_ASSERT(testObject.getAttribute(CKA_SIGN)->getBooleanValue() == false);

	bool value6 = true;
	OSAttribute attr6(value6);

	CPPUNIT_ASSERT(testObject.setAttribute(CKA_VERIFY, attr6));
	CPPUNIT_ASSERT(testObject.getAttribute(CKA_VERIFY)->isBooleanAttribute());
	CPPUNIT_ASSERT(testObject.getAttribute(CKA_VERIFY)->getBooleanValue() == value6);
}
示例#17
0
void SessionObjectTests::testByteStrAttr()
{
	ByteString value1 = "010203040506070809";
	ByteString value2 = "ABABABABABABABABABABABABABABABABAB";
	ByteString value3 = "BDEBDBEDBBDBEBDEBE792759537328";
	ByteString value4 = "98A7E5D798A7E5D798A7E5D798A7E5D798A7E5D798A7E5D7";
	ByteString value5 = "ABCDABCDABCDABCDABCDABCDABCDABCD";

    SessionObject testObject(NULL, 1, 1);

	CPPUNIT_ASSERT(testObject.isValid());

	OSAttribute attr1(value1);
	OSAttribute attr2(value2);
	OSAttribute attr3(value3);
	OSAttribute attr4(value4);
	OSAttribute attr5(value5);

	CPPUNIT_ASSERT(testObject.setAttribute(CKA_MODULUS, attr1));
	CPPUNIT_ASSERT(testObject.setAttribute(CKA_COEFFICIENT, attr2));
	CPPUNIT_ASSERT(testObject.setAttribute(CKA_VALUE_BITS, attr3));
	CPPUNIT_ASSERT(testObject.setAttribute(CKA_PUBLIC_EXPONENT, attr4));
	CPPUNIT_ASSERT(testObject.setAttribute(CKA_SUBJECT, attr5));

	CPPUNIT_ASSERT(testObject.isValid());

	CPPUNIT_ASSERT(testObject.attributeExists(CKA_MODULUS));
	CPPUNIT_ASSERT(testObject.attributeExists(CKA_COEFFICIENT));
	CPPUNIT_ASSERT(testObject.attributeExists(CKA_VALUE_BITS));
	CPPUNIT_ASSERT(testObject.attributeExists(CKA_PUBLIC_EXPONENT));
	CPPUNIT_ASSERT(testObject.attributeExists(CKA_SUBJECT));
	CPPUNIT_ASSERT(!testObject.attributeExists(CKA_ID));

	CPPUNIT_ASSERT(testObject.getAttribute(CKA_MODULUS)->isByteStringAttribute());
	CPPUNIT_ASSERT(testObject.getAttribute(CKA_COEFFICIENT)->isByteStringAttribute());
	CPPUNIT_ASSERT(testObject.getAttribute(CKA_VALUE_BITS)->isByteStringAttribute());
	CPPUNIT_ASSERT(testObject.getAttribute(CKA_PUBLIC_EXPONENT)->isByteStringAttribute());
	CPPUNIT_ASSERT(testObject.getAttribute(CKA_SUBJECT)->isByteStringAttribute());

	CPPUNIT_ASSERT(testObject.getAttribute(CKA_MODULUS)->getByteStringValue() == value1);
	CPPUNIT_ASSERT(testObject.getAttribute(CKA_COEFFICIENT)->getByteStringValue() == value2);
	CPPUNIT_ASSERT(testObject.getAttribute(CKA_VALUE_BITS)->getByteStringValue() == value3);
	CPPUNIT_ASSERT(testObject.getAttribute(CKA_PUBLIC_EXPONENT)->getByteStringValue() == value4);
	CPPUNIT_ASSERT(testObject.getAttribute(CKA_SUBJECT)->getByteStringValue() == value5);

	ByteString value6 = "909090908080808080807070707070FF";
	OSAttribute attr6(value6);

	CPPUNIT_ASSERT(testObject.setAttribute(CKA_ISSUER, attr6));
	CPPUNIT_ASSERT(testObject.getAttribute(CKA_ISSUER)->isByteStringAttribute());
	CPPUNIT_ASSERT(testObject.getAttribute(CKA_ISSUER)->getByteStringValue() == value6);
}
示例#18
0
// The test checks invokers specialized on a single attribute value type
BOOST_AUTO_TEST_CASE_TEMPLATE(single_type, CharT, char_types)
{
    typedef logging::basic_attribute_set< CharT > attr_set;
    typedef logging::basic_attribute_values_view< CharT > values_view;
    typedef test_data< CharT > data;

    attrs::constant< int > attr1(10);
    attrs::constant< double > attr2(5.5);
    attrs::constant< std::string > attr3("Hello, world!");

    attr_set set1, set2, set3;
    set1[data::attr1()] = attr1;
    set1[data::attr2()] = attr2;

    values_view view1(set1, set2, set3);
    view1.freeze();

    my_receiver recv;

    logging::value_visitor_invoker< CharT, int > invoker1(data::attr1());
    logging::value_visitor_invoker< CharT, double > invoker2(data::attr2());
    logging::value_visitor_invoker< CharT, std::string > invoker3(data::attr3());
    logging::value_visitor_invoker< CharT, char > invoker4(data::attr1());
    logging::value_visitor_invoker< CharT, int > invoker5(data::attr2());

    // These two extractors will find their values in the view
    recv.set_expected(10);
    BOOST_CHECK(invoker1(view1, recv));

    recv.set_expected(5.5);
    BOOST_CHECK(invoker2(view1, recv));

    // This one will not
    recv.set_expected();
    BOOST_CHECK(!invoker3(view1, recv));

    // But it will find it in this view
    set1[data::attr3()] = attr3;

    values_view view2(set1, set2, set3);
    view2.freeze();

    recv.set_expected("Hello, world!");
    BOOST_CHECK(invoker3(view2, recv));

    // This one will find the sought attribute value, but it will have an incorrect type
    recv.set_expected();
    BOOST_CHECK(!invoker4(view1, recv));

    // This one is the same, but there is a value of the requested type in the view
    BOOST_CHECK(!invoker5(view1, recv));
}
示例#19
0
void SessionObjectTests::testULongAttr()
{
    SessionObject testObject(NULL, 1, 1);

	CPPUNIT_ASSERT(testObject.isValid());

	unsigned long value1 = 0x12345678;
	unsigned long value2 = 0x87654321;
	unsigned long value3 = 0x01010101;
	unsigned long value4 = 0x10101010;
	unsigned long value5 = 0xABCDEF;

	OSAttribute attr1(value1);
	OSAttribute attr2(value2);
	OSAttribute attr3(value3);
	OSAttribute attr4(value4);
	OSAttribute attr5(value5);

	CPPUNIT_ASSERT(testObject.setAttribute(CKA_MODULUS_BITS, attr1));
	CPPUNIT_ASSERT(testObject.setAttribute(CKA_PRIME_BITS, attr2));
	CPPUNIT_ASSERT(testObject.setAttribute(CKA_AUTH_PIN_FLAGS, attr3));
	CPPUNIT_ASSERT(testObject.setAttribute(CKA_SUBPRIME_BITS, attr4));
	CPPUNIT_ASSERT(testObject.setAttribute(CKA_KEY_TYPE, attr5));
		
	CPPUNIT_ASSERT(testObject.isValid());

	CPPUNIT_ASSERT(testObject.attributeExists(CKA_MODULUS_BITS));
	CPPUNIT_ASSERT(testObject.attributeExists(CKA_PRIME_BITS));
	CPPUNIT_ASSERT(testObject.attributeExists(CKA_AUTH_PIN_FLAGS));
	CPPUNIT_ASSERT(testObject.attributeExists(CKA_SUBPRIME_BITS));
	CPPUNIT_ASSERT(testObject.attributeExists(CKA_KEY_TYPE));
	CPPUNIT_ASSERT(!testObject.attributeExists(CKA_ID));

	CPPUNIT_ASSERT(testObject.getAttribute(CKA_MODULUS_BITS)->isUnsignedLongAttribute());
	CPPUNIT_ASSERT(testObject.getAttribute(CKA_PRIME_BITS)->isUnsignedLongAttribute());
	CPPUNIT_ASSERT(testObject.getAttribute(CKA_AUTH_PIN_FLAGS)->isUnsignedLongAttribute());
	CPPUNIT_ASSERT(testObject.getAttribute(CKA_SUBPRIME_BITS)->isUnsignedLongAttribute());
	CPPUNIT_ASSERT(testObject.getAttribute(CKA_KEY_TYPE)->isUnsignedLongAttribute());

	CPPUNIT_ASSERT(testObject.getAttribute(CKA_MODULUS_BITS)->getUnsignedLongValue() == 0x12345678);
	CPPUNIT_ASSERT(testObject.getAttribute(CKA_PRIME_BITS)->getUnsignedLongValue() == 0x87654321);
	CPPUNIT_ASSERT(testObject.getAttribute(CKA_AUTH_PIN_FLAGS)->getUnsignedLongValue() == 0x01010101);
	CPPUNIT_ASSERT(testObject.getAttribute(CKA_SUBPRIME_BITS)->getUnsignedLongValue() == 0x10101010);
	CPPUNIT_ASSERT(testObject.getAttribute(CKA_KEY_TYPE)->getUnsignedLongValue() == 0xABCDEF);

	unsigned long value6 = 0x90909090;
	OSAttribute attr6(value6);

	CPPUNIT_ASSERT(testObject.setAttribute(CKA_CLASS, attr6));
	CPPUNIT_ASSERT(testObject.getAttribute(CKA_CLASS)->isUnsignedLongAttribute());
	CPPUNIT_ASSERT(testObject.getAttribute(CKA_CLASS)->getUnsignedLongValue() == value6);
}
示例#20
0
void test_a_dbobject_with_an_object::should_store_array_attributes()
{
    bool value1 = true;
    unsigned long value2 = 0x87654321;
    ByteString value3 = "BDEBDBEDBBDBEBDEBE792759537328";

    // Create the test object
    {
        DBObject testObject(connection);
        CPPUNIT_ASSERT(testObject.find(1));
        CPPUNIT_ASSERT(testObject.isValid());

        OSAttribute attr1(value1);
        OSAttribute attr2(value2);
        OSAttribute attr3(value3);

        std::map<CK_ATTRIBUTE_TYPE,OSAttribute> mattr;
        mattr.insert(std::pair<CK_ATTRIBUTE_TYPE,OSAttribute> (CKA_TOKEN, attr1));
        mattr.insert(std::pair<CK_ATTRIBUTE_TYPE,OSAttribute> (CKA_PRIME_BITS, attr2));
        mattr.insert(std::pair<CK_ATTRIBUTE_TYPE,OSAttribute> (CKA_VALUE_BITS, attr3));
        OSAttribute attra(mattr);

        CPPUNIT_ASSERT(testObject.setAttribute(CKA_WRAP_TEMPLATE, attra));
    }

    // Now read back the object
    {
        DBObject testObject(connection);
        CPPUNIT_ASSERT(testObject.find(1));
        CPPUNIT_ASSERT(testObject.isValid());

        CPPUNIT_ASSERT(testObject.attributeExists(CKA_WRAP_TEMPLATE));
        CPPUNIT_ASSERT(!testObject.attributeExists(CKA_UNWRAP_TEMPLATE));

        std::map<CK_ATTRIBUTE_TYPE,OSAttribute> mattrb = testObject.getAttribute(CKA_WRAP_TEMPLATE).getArrayValue();
        CPPUNIT_ASSERT(mattrb.size() == 3);
        CPPUNIT_ASSERT(mattrb.find(CKA_TOKEN) != mattrb.end());
        CPPUNIT_ASSERT(mattrb.at(CKA_TOKEN).isBooleanAttribute());
        CPPUNIT_ASSERT(mattrb.at(CKA_TOKEN).getBooleanValue() == true);
        CPPUNIT_ASSERT(mattrb.find(CKA_PRIME_BITS) != mattrb.end());
        CPPUNIT_ASSERT(mattrb.at(CKA_PRIME_BITS).isUnsignedLongAttribute());
        CPPUNIT_ASSERT(mattrb.at(CKA_PRIME_BITS).getUnsignedLongValue() == 0x87654321);
        CPPUNIT_ASSERT(mattrb.find(CKA_VALUE_BITS) != mattrb.end());
        CPPUNIT_ASSERT(mattrb.at(CKA_VALUE_BITS).isByteStringAttribute());
        CPPUNIT_ASSERT(mattrb.at(CKA_VALUE_BITS).getByteStringValue() == value3);
    }
}
示例#21
0
// The test checks invokers specialized with type lists
BOOST_AUTO_TEST_CASE_TEMPLATE(multiple_types, CharT, char_types)
{
    typedef logging::basic_attribute_set< CharT > attr_set;
    typedef logging::basic_attribute_values_view< CharT > values_view;
    typedef test_data< CharT > data;
    typedef mpl::vector< int, double, std::string, char >::type types;

    attrs::constant< int > attr1(10);
    attrs::constant< double > attr2(5.5);
    attrs::constant< std::string > attr3("Hello, world!");

    attr_set set1, set2, set3;
    set1[data::attr1()] = attr1;
    set1[data::attr2()] = attr2;

    values_view view1(set1, set2, set3);
    view1.freeze();

    my_receiver recv;

    logging::value_visitor_invoker< CharT, types > invoker1(data::attr1());
    logging::value_visitor_invoker< CharT, types > invoker2(data::attr2());
    logging::value_visitor_invoker< CharT, types > invoker3(data::attr3());

    // These two extractors will find their values in the view
    recv.set_expected(10);
    BOOST_CHECK(invoker1(view1, recv));

    recv.set_expected(5.5);
    BOOST_CHECK(invoker2(view1, recv));

    // This one will not
    recv.set_expected();
    BOOST_CHECK(!invoker3(view1, recv));

    // But it will find it in this view
    set1[data::attr3()] = attr3;

    values_view view2(set1, set2, set3);
    view2.freeze();

    recv.set_expected("Hello, world!");
    BOOST_CHECK(invoker3(view2, recv));
}
示例#22
0
// The test checks that time_duration formatting work
BOOST_AUTO_TEST_CASE_TEMPLATE(time_duration, CharT, char_types)
{
    typedef logging::attribute_set attr_set;
    typedef std::basic_string< CharT > string;
    typedef logging::basic_formatting_ostream< CharT > osstream;
    typedef logging::record_view record_view;
    typedef logging::basic_formatter< CharT > formatter;
    typedef test_data< CharT > data;
    typedef date_time_formats< CharT > formats;
    typedef boost::date_time::time_facet< ptime, CharT > facet;

    duration t1(14, 40, 15);
    attrs::constant< duration > attr1(t1);

    attr_set set1;
    set1[data::attr1()] = attr1;

    record_view rec = make_record_view(set1);

    // Check for various formats specification
    {
        string str1, str2;
        osstream strm1(str1), strm2(str2);
        formatter f = expr::stream << expr::format_date_time< duration >(data::attr1(), formats::default_time_duration_format().c_str());
        f(rec, strm1);
        facet* fac = new facet();
        fac->time_duration_format(formats::default_time_duration_format().c_str());
        strm2.imbue(std::locale(strm2.getloc(), fac));
        strm2 << t1;
        BOOST_CHECK(equal_strings(strm1.str(), strm2.str()));
    }
    {
        string str1, str2;
        osstream strm1(str1), strm2(str2);
        formatter f = expr::stream << expr::format_date_time< duration >(data::attr1(), formats::time_duration_format().c_str());
        f(rec, strm1);
        facet* fac = new facet();
        fac->time_duration_format(formats::time_duration_format().c_str());
        strm2.imbue(std::locale(strm2.getloc(), fac));
        strm2 << t1;
        BOOST_CHECK(equal_strings(strm1.str(), strm2.str()));
    }
}
示例#23
0
void test_a_dbobject_with_an_object::should_store_mixed_attributes()
{
    bool value1 = true;
    unsigned long value2 = 0x87654321;
    unsigned long value3 = 0xBDEBDBED;

    // Create the test object
    {
        DBObject testObject(connection);
        CPPUNIT_ASSERT(testObject.find(1));
        CPPUNIT_ASSERT(testObject.isValid());

        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));
    }

    // Now read back the object
    {
        DBObject testObject(connection);
        CPPUNIT_ASSERT(testObject.find(1));
        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).isUnsignedLongAttribute());

        CPPUNIT_ASSERT(testObject.getAttribute(CKA_TOKEN).getBooleanValue());
        CPPUNIT_ASSERT_EQUAL(testObject.getAttribute(CKA_PRIME_BITS).getUnsignedLongValue(), value2);
        CPPUNIT_ASSERT(testObject.getAttribute(CKA_VALUE_BITS).getUnsignedLongValue() == value3);
    }
}
示例#24
0
// The test checks that default formatting work
BOOST_AUTO_TEST_CASE_TEMPLATE(default_formatting, CharT, char_types)
{
    typedef logging::record_view record_view;
    typedef logging::attribute_set attr_set;
    typedef std::basic_string< CharT > string;
    typedef logging::basic_formatting_ostream< CharT > osstream;
    typedef logging::basic_formatter< CharT > formatter;
    typedef test_data< CharT > data;

    attrs::constant< int > attr1(10);
    attrs::constant< double > attr2(5.5);
    attrs::constant< my_class > attr3(my_class(77));

    attr_set set1;
    set1[data::attr1()] = attr1;
    set1[data::attr2()] = attr2;
    set1[data::attr3()] = attr3;

    record_view rec = make_record_view(set1);

    // Check for various modes of attribute value type specification
    {
        string str1, str2;
        osstream strm1(str1), strm2(str2);
        formatter f = expr::stream << expr::attr< int >(data::attr1()) << expr::attr< double >(data::attr2());
        f(rec, strm1);
        strm2 << 10 << 5.5;
        BOOST_CHECK(equal_strings(strm1.str(), strm2.str()));
    }
    {
        string str1, str2;
        osstream strm1(str1), strm2(str2);
        formatter f = expr::stream << expr::attr< logging::numeric_types >(data::attr1()) << expr::attr< logging::numeric_types >(data::attr2());
        f(rec, strm1);
        strm2 << 10 << 5.5;
        BOOST_CHECK(equal_strings(strm1.str(), strm2.str()));
    }
    // Check that custom types as attribute values are also supported
    {
        string str1, str2;
        osstream strm1(str1), strm2(str2);
        formatter f = expr::stream << expr::attr< my_class >(data::attr3());
        f(rec, strm1);
        strm2 << my_class(77);
        BOOST_CHECK(equal_strings(strm1.str(), strm2.str()));
    }
    // Check how missing attribute values are handled
    {
        string str1;
        osstream strm1(str1);
        formatter f = expr::stream
            << expr::attr< int >(data::attr1())
            << expr::attr< int >(data::attr4()).or_throw()
            << expr::attr< double >(data::attr2());
        BOOST_CHECK_THROW(f(rec, strm1), logging::runtime_error);
    }
    {
        string str1, str2;
        osstream strm1(str1), strm2(str2);
        formatter f = expr::stream
            << expr::attr< int >(data::attr1())
            << expr::attr< int >(data::attr4())
            << expr::attr< double >(data::attr2());
        f(rec, strm1);
        strm2 << 10 << 5.5;
        BOOST_CHECK(equal_strings(strm1.str(), strm2.str()));
    }
}
示例#25
0
// Test node attribute functionality
void ManipulateNodeEdgeAttributes() {
  int NNodes = 1000;
  int NEdges = 1000;
  const char *FName = "demo.graph.dat";

  PNEANet Graph;
  PNEANet Graph1;
  int i;
  int x, y;
  bool t;

  Graph = TNEANet::New();
  t = Graph->Empty();

  // create the nodes
  for (i = 0; i < NNodes; i++) {
    Graph->AddNode(i);
  }

  // create the edges 
  for (i = 0; i < NEdges; i++) {
    x = rand() % NNodes;
    y = rand() % NNodes;
    Graph->AddEdge(x, y, i);
  }

  // create attributes and fill all nodes
  TStr attr1 = "str";
  TStr attr2 = "int";
  TStr attr3 = "float";
  TStr attr4 = "default";

  // Test vertical int iterator for node 3, 50, 700, 900
  // Check if we can set defaults to 0 for Int data.
  Graph->AddIntAttrN(attr2, 0);
  Graph->AddIntAttrDatN(3, 3*2, attr2);
  Graph->AddIntAttrDatN(50, 50*2, attr2);
  Graph->AddIntAttrDatN(700, 700*2, attr2);
  Graph->AddIntAttrDatN(900, 900*2, attr2);
  int NodeId = 0;
  for (TNEANet::TAIntI NI = Graph->BegNAIntI(attr2);
    NI < Graph->EndNAIntI(attr2); NI++) {
    // Check if defaults are now 0.
    if (NI.GetDat()() != 0) {
      printf("Attribute: %s, Node: %i, Val: %i\n", attr2(), NodeId, NI.GetDat()());
      NodeId++;
    }
  } 

  // Test vertical flt iterator for node 3, 50, 700, 900
  Graph->AddFltAttrDatN(5, 3.41, attr3);
  Graph->AddFltAttrDatN(50, 2.718, attr3);
  Graph->AddFltAttrDatN(300, 150.0, attr3);
  Graph->AddFltAttrDatN(653, 653, attr3);
  NodeId = 0;
  for (TNEANet::TAFltI NI = Graph->BegNAFltI(attr3);
    NI < Graph->EndNAFltI(attr3); NI++) {
    if (NI.GetDat() != TFlt::Mn) {
      printf("Attribute: %s, Node: %i, Val: %f\n", attr3(), NodeId, NI.GetDat()());
      NodeId++;
    } 
  }

  // Test vertical str iterator for node 3, 50, 700, 900
  Graph->AddStrAttrDatN(10, "abc", attr1);
  Graph->AddStrAttrDatN(20, "def", attr1);
  Graph->AddStrAttrDatN(400, "ghi", attr1);
  // this does not show since ""=null
  Graph->AddStrAttrDatN(455, "", attr1);
  NodeId = 0;
  
  for (TNEANet::TAStrI NI = Graph->BegNAStrI(attr1);
    NI < Graph->EndNAStrI(attr1); NI++) {
    if (NI.GetDat() != TStr::GetNullStr()) {
      printf("Attribute: %s, Node: %i, Val: %s\n", attr1(), NodeId, NI.GetDat()());
      NodeId++;
    }
  } 

  // Test vertical iterator over many types (must skip default/deleted attr) 
  int NId = 55;
  Graph->AddStrAttrDatN(NId, "aaa", attr1);
  Graph->AddIntAttrDatN(NId, 3*2, attr2);
  Graph->AddFltAttrDatN(NId, 3.41, attr3);
  Graph->AddStrAttrDatN(80, "dont appear", attr4); // should not show up
  TStrV NIdAttrName;
  Graph->AttrNameNI(NId, NIdAttrName);
  int AttrLen = NIdAttrName.Len();
  for (int i = 0; i < AttrLen; i++) {
    printf("Vertical Node: %i, Attr: %s\n", NId, NIdAttrName[i]());
  } 

  Graph->DelAttrDatN(NId, attr2);
  Graph->AttrNameNI(NId, NIdAttrName);
  AttrLen = NIdAttrName.Len();
  for (int i = 0; i < AttrLen; i++) {
    printf("Vertical Node (no int) : %i, Attr: %s\n", NId, NIdAttrName[i]());
  } 

  Graph->AddIntAttrDatN(NId, 3*2, attr2);
  Graph->DelAttrN(attr1);
  Graph->AttrNameNI(NId, NIdAttrName);
  AttrLen = NIdAttrName.Len();
  for (int i = 0; i < AttrLen; i++) {
    printf("Vertical Node (no str) : %i, Attr: %s\n", NId, NIdAttrName[i]());
  } 

  TStrV NIdAttrValue;
  Graph->AttrValueNI(NId, NIdAttrValue);
  AttrLen = NIdAttrValue.Len();
  for (int i = 0; i < AttrLen; i++) {
    printf("Vertical Node (no str) : %i, Attr_Val: %s\n", NId, NIdAttrValue[i]());
  } 

  for (i = 0; i <NNodes; i++) {
    Graph->AddIntAttrDatN(i, 70, attr2);
  }

  {
    TFOut FOut(FName);
    Graph->Save(FOut);
    FOut.Flush();
  }

  {
    TFIn FIn(FName);
    Graph1 = TNEANet::Load(FIn);
  }

  int total = 0;
  for (TNEANet::TAIntI NI = Graph1->BegNAIntI(attr2);
    NI < Graph1->EndNAIntI(attr2); NI++) {
    total += NI.GetDat();
  }

  printf("Average: %i (should be 70)\n", total/NNodes);

  Graph1->Clr();

  // Test vertical int iterator for edge
  Graph->AddIntAttrDatE(3, 3*2, attr2);
  Graph->AddIntAttrDatE(55, 55*2, attr2);
  Graph->AddIntAttrDatE(705, 705*2, attr2);
  Graph->AddIntAttrDatE(905, 905*2, attr2);
  int EdgeId = 0;
  for (TNEANet::TAIntI EI = Graph->BegEAIntI(attr2);
    EI < Graph->EndEAIntI(attr2); EI++) {
    if (EI.GetDat() != TInt::Mn) {
       printf("E Attribute: %s, Edge: %i, Val: %i\n", attr2(), EdgeId, EI.GetDat()());
       EdgeId++;
    }
  } 
  
  // Test vertical flt iterator for edge
  Graph->AddFltAttrE(attr3, 0.00);
  Graph->AddFltAttrDatE(5, 4.41, attr3);
  Graph->AddFltAttrDatE(50, 3.718, attr3);
  Graph->AddFltAttrDatE(300, 151.0, attr3);
  Graph->AddFltAttrDatE(653, 654, attr3);
  EdgeId = 0;
  for (TNEANet::TAFltI EI = Graph->BegEAFltI(attr3);
    EI < Graph->EndEAFltI(attr3); EI++) {
    // Check if defaults are set to 0.
    if (EI.GetDat() != 0.00) {
      printf("E Attribute: %s, Edge: %i, Val: %f\n", attr3(), EdgeId, EI.GetDat()());
      EdgeId++;
    } 
  }

  // Test vertical str iterator for edge
  Graph->AddStrAttrDatE(10, "abc", attr1);
  Graph->AddStrAttrDatE(20, "def", attr1);
  Graph->AddStrAttrDatE(400, "ghi", attr1);
  // this does not show since ""=null
  Graph->AddStrAttrDatE(455, "", attr1);
  EdgeId = 0;
  for (TNEANet::TAStrI EI = Graph->BegEAStrI(attr1);
    EI < Graph->EndEAStrI(attr1); EI++) {
    if (EI.GetDat() != TStr::GetNullStr()) {
        printf("E Attribute: %s, Edge: %i, Val: %s\n", attr1(), EdgeId, EI.GetDat()());
	EdgeId++;
    }
  } 


  // Test vertical iterator over many types (must skip default/deleted attr) 
  int EId = 55;
  Graph->AddStrAttrDatE(EId, "aaa", attr1);
  Graph->AddIntAttrDatE(EId, 3*2, attr2);
  Graph->AddFltAttrDatE(EId, 3.41, attr3);
  Graph->AddStrAttrDatE(80, "dont appear", attr4); // should not show up
  TStrV EIdAttrName;
  Graph->AttrNameEI(EId, EIdAttrName);
  AttrLen = EIdAttrName.Len();
  for (int i = 0; i < AttrLen; i++) {
    printf("Vertical Edge: %i, Attr: %s\n", EId, EIdAttrName[i]());
  } 

  Graph->DelAttrDatE(EId, attr2);
  Graph->AttrNameEI(EId, EIdAttrName);
  AttrLen = EIdAttrName.Len();
  for (int i = 0; i < AttrLen; i++) {
    printf("Vertical Edge (no int) : %i, Attr: %s\n", EId, EIdAttrName[i]());
  } 

  Graph->AddIntAttrDatE(EId, 3*2, attr2);
  Graph->DelAttrE(attr1);
  Graph->AttrNameEI(EId, EIdAttrName);
  AttrLen = EIdAttrName.Len();
  for (int i = 0; i < AttrLen; i++) {
    printf("Vertical Edge (no str) : %i, Attr: %s\n", EId, EIdAttrName[i]());
  } 

  TStrV EIdAttrValue;
  Graph->AttrValueEI(EId, EIdAttrValue);
  AttrLen = EIdAttrValue.Len();
  for (int i = 0; i < AttrLen; i++) {
    printf("Vertical Edge (no str) : %i, Attr_Val: %s\n", EId, EIdAttrValue[i]());
  } 

  for (i = 0; i <NEdges; i++) {
    Graph->AddIntAttrDatE(i, 70, attr2);
  }

  {
    TFOut FOut(FName);
    Graph->Save(FOut);
    FOut.Flush();
    Graph->Clr();
  }

  {
    TFIn FIn(FName);
    Graph1 = TNEANet::Load(FIn);
  }

  total = 0;
  for (TNEANet::TAIntI EI = Graph1->BegNAIntI(attr2);
    EI < Graph1->EndNAIntI(attr2); EI++) {
    total += EI.GetDat();
  }

  printf("Average: %i (should be 70)\n", total/NEdges);

  //Graph1->Dump();
  Graph1->Clr();
}
示例#26
0
void main()
{
	std::cout<< "\n PNode<T> class demonstration";
	std::cout<< "\n ============================\n";
	//initialize
	PNode<std::string > root("root");
	PNode<std::string > child1("child1");
	PNode<std::string > child2("child2");
	PNode<std::string > grandchild11("grandchild11");
	PNode<std::string > grandchild21("grandchild21");
	PNode<std::string > grandchild22("grandchild22");
	PNode<std::string > grandgrandchild211("grandgrandchild211");
	PNode<std::string > child3("child3");
	
	//test adding
	child1.add(&grandchild11);
	grandchild21.add(&grandgrandchild211);
	child2.add(&grandchild21);
	child2.add(&grandchild22);
	root.add(&child1);
	root.add(&child2);
	root.add(&child3);
	WalkTree(&root);
	
	//test removing, mark operation
	std::cout<<"\n\n removing first child";
	root.remove(&child1);
	std::cout<<"\n unmarked all nodes";
	root.setMarkState(true);
	child1.setMarkState(true);
	grandchild11.setMarkState(true);
	child2.setMarkState(true);
	grandchild21.setMarkState(true);
	grandchild22.setMarkState(true);
	grandgrandchild211.setMarkState(true);
	child3.setMarkState(true);

	//show the tree
	WalkTree(&root);
	std::cout<<"\n\n";
	
	std::cout<<"===========================XMLValues Test==================================="<<std::endl;
	XMLValue xmlv(XMLValue::TAG, "MyTag");
	Attribute attr1("ATTR1","1");
	Attribute attr2("ATTR2","2");
	Attribute attr3("ATTR3","3");
	xmlv.AddAttr("ATTR1","1");
	xmlv.AddAttr("ATTR2","2");
	xmlv.AddAttr("ATTR3","3");

	//test == operator
	std::cout<<"Attribute Compare:\n"<<"attr1 == attr1: "<< (attr1 == attr1) <<"; attr2 == attr1: "<< (attr2 == attr1)<<std::endl;

	//test search and find attributes
	for(size_t i=0; i< xmlv.attributes().size();++i)
		std::cout<<xmlv.attributes().at(i).AttrName.c_str()<<"  "<<xmlv.attributes().at(i).AttrValue.c_str()  <<std::endl;
	std::cout<<"Find ATTR2's index: "<<xmlv.Find("ATTR2","2")<<std::endl;
	std::cout<<"Find ATTR4's index: "<<xmlv.Find("ATTR4","4")<<std::endl;
	//test removing attributes
	xmlv.RemoveAttr("ATTR2","2");
	std::cout<<"====================ATTR2 Removed==========================\n";
	for(size_t i=0; i< xmlv.attributes().size();++i)
		std::cout<<xmlv.attributes().at(i).AttrName.c_str()<<"  "<<xmlv.attributes().at(i).AttrValue.c_str()  <<std::endl;
	std::cout<<"=================Nodes with XMLValues======================"<<std::endl;
	PNode<XMLValue> xmlroot(xmlv);
	PNode<XMLValue> xmlcld1(XMLValue(XMLValue::TAG,"xmlChild1"));
	PNode<XMLValue> xmlcld2(XMLValue(XMLValue::TAG,"xmlChild2"));
	xmlroot.add(&xmlcld1);
	xmlroot.add(&xmlcld2);
	PNode<XMLValue> *pTemp;

	//test searching through the tree
	while (pTemp = xmlroot.nextUnmarkedChild())
	{
		std::cout<<"The child of root: "<<pTemp->value().TagName()<<std::endl; 
		pTemp->setMarkState(true);
	}

	std::cout<<"=================Children of Nodes======================"<<std::endl;
	for (size_t i = 0; i < xmlroot.getChildren().size(); i++)
		std::cout<<"a Child of ROOT: "<<xmlroot.getChildren()[i].TagName()<<std::endl;
	::system("pause");
}
示例#27
0
文件: loadGTF.cpp 项目: Bambooie/STAR
uint loadGTF(SjdbClass &sjdbLoci, Parameters *P) {//load gtf file, add junctions to P->sjdb
    //returns number of added junctions
    if (P->sjdbOverhang>0 && P->sjdbGTFfile!="-") {       
        ifstream sjdbStreamIn ( P->sjdbGTFfile.c_str() );   
        if (sjdbStreamIn.fail()) {
            ostringstream errOut;
            errOut << "FATAL error, could not open file sjdbGTFfile=" << P->sjdbGTFfile <<"\n";
            exitWithError(errOut.str(),std::cerr, P->inOut->logMain, EXIT_CODE_INPUT_FILES, *P);
        };    
        
        std::map <string,uint> transcriptIDnumber;

        uint exonN=0;
        while (sjdbStreamIn.good()) {//count the number of exons
            string chr1,ddd2,featureType;            
            sjdbStreamIn >> chr1 >> ddd2 >> featureType;
            if (chr1.substr(0,1)!="#" && featureType==P->sjdbGTFfeatureExon) {
                exonN++;
            };
            sjdbStreamIn.ignore(1000000000,'\n'); //ignore the rest of the line
        };
        uint* exonLoci=new uint [exonN*GTF_exonLoci_size];
        char* transcriptStrand = new char [exonN];
        vector <string> transcriptID;

        exonN=0;//re-calculate
        sjdbStreamIn.clear();
        sjdbStreamIn.seekg(0,ios::beg);
        while (sjdbStreamIn.good()) {

            string oneLine,chr1,ddd2,featureType;
            getline(sjdbStreamIn,oneLine);
            istringstream oneLineStream (oneLine);
            
            oneLineStream >> chr1 >> ddd2 >> featureType;
            if (chr1.substr(0,1)!="#" && featureType==P->sjdbGTFfeatureExon) {//exonic line, process
                
                if (P->sjdbGTFchrPrefix!="-") chr1=P->sjdbGTFchrPrefix + chr1;
                
                if (P->chrNameIndex.count(chr1)==0) {//chr not in Genome
                    P->inOut->logMain << "WARNING: while processing sjdbGTFfile=" << P->sjdbGTFfile <<": chromosome '"<<chr1<<"' not found in Genome fasta files for line:\n";
                    P->inOut->logMain << oneLine <<"\n"<<flush;          
                    continue; //do not process exons/transcripts on missing chromosomes
                };
                
                uint ex1,ex2;
                char str1;
                oneLineStream >> ex1 >> ex2 >> ddd2 >> str1 >> ddd2; //read all fields except the last

                string oneLine1;
                getline(oneLineStream, oneLine1);//get the last field
                replace(oneLine1.begin(),oneLine1.end(),';',' ');//to separate attributes
                replace(oneLine1.begin(),oneLine1.end(),'=',' ');//for GFF3 processing
                oneLineStream.str(oneLine1);
                oneLineStream.clear();

                string trID(""), attr1("");
                while (oneLineStream.good()) {
                    oneLineStream >> attr1;
                    if (attr1==P->sjdbGTFtagExonParentTranscript) {
                        oneLineStream >> trID;
                        trID.erase(remove(trID.begin(),trID.end(),'"'),trID.end());
                        trID.erase(remove(trID.begin(),trID.end(),';'),trID.end());
//                         cout <<trID<<endl;
                    };
                };
                if (trID=="") {//no transcript ID
                    P->inOut->logMain << "WARNING: while processing sjdbGTFfile=" << P->sjdbGTFfile <<": no transcript_id for exon feature for line:\n";
                    P->inOut->logMain << oneLine <<"\n"<<flush;
                } else {
                    transcriptIDnumber.insert(std::pair <string,uint> (trID,(uint) transcriptIDnumber.size()));//insert new element if necessary with a new numeric value
                    if (transcriptID.size() < transcriptIDnumber.size()) transcriptID.push_back(trID);
                    if (str1=='+') {
                       transcriptStrand[transcriptIDnumber[trID]]=1;
                    } else if (str1=='-') {
                       transcriptStrand[transcriptIDnumber[trID]]=2;
                    } else {
                       transcriptStrand[transcriptIDnumber[trID]]=0;
                    };
                };
                
                exonLoci[GTF_exonTrID(exonN)]=transcriptIDnumber[trID];
                exonLoci[GTF_exonStart(exonN)]=ex1+P->chrStart[P->chrNameIndex[chr1]]-1;
                exonLoci[GTF_exonEnd(exonN)]=ex2+P->chrStart[P->chrNameIndex[chr1]]-1;
                ++exonN;                

            };//if (chr1.substr(0,1)!="#" && featureType=="exon")
        };//
示例#28
0
文件: loadGTF.cpp 项目: alexf101/STAR
uint loadGTF(SjdbClass &sjdbLoci, Parameters *P, string dirOut) {//load gtf file, add junctions to P->sjdb
    //returns number of added junctions
    if (P->sjdbOverhang>0 && P->sjdbGTFfile!="-") {       
        time_t rawTime;
        time(&rawTime);
        P->inOut->logMain     << timeMonthDayTime(rawTime) <<" ..... Processing annotations GTF\n" <<flush;
        *P->inOut->logStdOut  << timeMonthDayTime(rawTime) <<" ..... Processing annotations GTF\n" <<flush;           
        
        ifstream sjdbStreamIn ( P->sjdbGTFfile.c_str() );   
        if (sjdbStreamIn.fail()) {
            ostringstream errOut;
            errOut << "FATAL error, could not open file sjdbGTFfile=" << P->sjdbGTFfile <<"\n";
            exitWithError(errOut.str(),std::cerr, P->inOut->logMain, EXIT_CODE_INPUT_FILES, *P);
        };    
        
        if (P->chrNameIndex.size()==0) 
        {
            for (uint ii=0;ii<P->nChrReal;ii++) {
                P->chrNameIndex[P->chrName[ii]]=ii;
            };        
        };
        std::map <string,uint> transcriptIDnumber, geneIDnumber;

        uint exonN=0;
        while (sjdbStreamIn.good()) {//count the number of exons
            string chr1,ddd2,featureType;            
            sjdbStreamIn >> chr1 >> ddd2 >> featureType;
            if (chr1.substr(0,1)!="#" && featureType==P->sjdbGTFfeatureExon) {
                exonN++;
            };
            sjdbStreamIn.ignore(1000000000,'\n'); //ignore the rest of the line
        };
        
        if (exonN==0)
        {
            P->inOut->logMain << "WARNING: found no exons in sjdbGTFfile=" << P->sjdbGTFfile <<endl;
            return 0;
        };
        uint* exonLoci=new uint [exonN*GTF_exonLoci_size];
        char* transcriptStrand = new char [exonN];
        vector <string> transcriptID, geneID;

        exonN=0;//re-calculate
        sjdbStreamIn.clear();
        sjdbStreamIn.seekg(0,ios::beg);
        while (sjdbStreamIn.good()) {

            string oneLine,chr1,ddd2,featureType;
            getline(sjdbStreamIn,oneLine);
            istringstream oneLineStream (oneLine);
            
            oneLineStream >> chr1 >> ddd2 >> featureType;
            if (chr1.substr(0,1)!="#" && featureType==P->sjdbGTFfeatureExon) {//exonic line, process
                
                if (P->sjdbGTFchrPrefix!="-") chr1=P->sjdbGTFchrPrefix + chr1;
                
                if (P->chrNameIndex.count(chr1)==0) {//chr not in Genome
                    P->inOut->logMain << "WARNING: while processing sjdbGTFfile=" << P->sjdbGTFfile <<": chromosome '"<<chr1<<"' not found in Genome fasta files for line:\n";
                    P->inOut->logMain << oneLine <<"\n"<<flush;          
                    continue; //do not process exons/transcripts on missing chromosomes
                };
                
                uint ex1,ex2;
                char str1;
                oneLineStream >> ex1 >> ex2 >> ddd2 >> str1 >> ddd2; //read all fields except the last

                string oneLine1;
                getline(oneLineStream, oneLine1);//get the last field
                replace(oneLine1.begin(),oneLine1.end(),';',' ');//to separate attributes
                replace(oneLine1.begin(),oneLine1.end(),'=',' ');//for GFF3 processing
                oneLineStream.str(oneLine1);
                oneLineStream.clear();

                string trID(""), gID(""), attr1("");
                while (oneLineStream.good()) {
                    oneLineStream >> attr1;
                    if (attr1==P->sjdbGTFtagExonParentTranscript) {
                        oneLineStream >> trID;
                        trID.erase(remove(trID.begin(),trID.end(),'"'),trID.end());
                        trID.erase(remove(trID.begin(),trID.end(),';'),trID.end());
                    } else if (attr1==P->sjdbGTFtagExonParentGene) {
                        oneLineStream >> gID;
                        gID.erase(remove(gID.begin(),gID.end(),'"'),gID.end());
                        gID.erase(remove(gID.begin(),gID.end(),';'),gID.end());
                    };
                };
示例#29
0
void test_a_dbobject_with_an_object::can_refresh_attributes()
{
    bool value1 = true;
    bool value1a = false;
    ByteString value2 = "BDEBDBEDBBDBEBDEBE792759537328";
    ByteString value2a = "466487346943785684957634";
    ByteString value3 = "0102010201020102010201020102010201020102";

    // Create the test object
    {
        DBObject testObject(connection);
        CPPUNIT_ASSERT(testObject.find(1));
        CPPUNIT_ASSERT(testObject.isValid());

        OSAttribute attr1(value1);
        OSAttribute attr2(value2);

        CPPUNIT_ASSERT(testObject.setAttribute(CKA_SIGN, attr1));
        CPPUNIT_ASSERT(testObject.setAttribute(CKA_SUBJECT, attr2));
    }

    // Now read back the object
    {
        DBObject testObject(connection);
        CPPUNIT_ASSERT(testObject.find(1));
        CPPUNIT_ASSERT(testObject.isValid());

        CPPUNIT_ASSERT(testObject.attributeExists(CKA_SIGN));
        CPPUNIT_ASSERT(testObject.attributeExists(CKA_SUBJECT));
        CPPUNIT_ASSERT(!testObject.attributeExists(CKA_ID));

        CPPUNIT_ASSERT(testObject.getAttribute(CKA_SIGN).isBooleanAttribute());
        CPPUNIT_ASSERT(testObject.getAttribute(CKA_SUBJECT).isByteStringAttribute());

        CPPUNIT_ASSERT(testObject.getAttribute(CKA_SIGN).getBooleanValue());
        CPPUNIT_ASSERT(testObject.getAttribute(CKA_SUBJECT).getByteStringValue() == value2);

        OSAttribute attr1(value1a);
        OSAttribute attr2(value2a);

        // Change the attributes
        CPPUNIT_ASSERT(testObject.isValid());

        CPPUNIT_ASSERT(testObject.setAttribute(CKA_SIGN, attr1));
        CPPUNIT_ASSERT(testObject.setAttribute(CKA_SUBJECT, attr2));

        // Check the attributes
        CPPUNIT_ASSERT(testObject.getAttribute(CKA_SIGN).isBooleanAttribute());
        CPPUNIT_ASSERT(testObject.getAttribute(CKA_SUBJECT).isByteStringAttribute());

        CPPUNIT_ASSERT(testObject.getAttribute(CKA_SIGN).getBooleanValue() == value1a);
        CPPUNIT_ASSERT(testObject.getAttribute(CKA_SUBJECT).getByteStringValue() == value2a);

        // Open the object a second time
        DBObject testObject2(connection);
        CPPUNIT_ASSERT(testObject2.find(1));
        CPPUNIT_ASSERT(testObject2.isValid());

        // Check the attributes on the second instance
        CPPUNIT_ASSERT(testObject2.getAttribute(CKA_SIGN).isBooleanAttribute());
        CPPUNIT_ASSERT(testObject2.getAttribute(CKA_SUBJECT).isByteStringAttribute());

        CPPUNIT_ASSERT(testObject2.getAttribute(CKA_SIGN).getBooleanValue() == value1a);
        CPPUNIT_ASSERT(testObject2.getAttribute(CKA_SUBJECT).getByteStringValue() == value2a);

        // Add an attribute on the second object
        OSAttribute attr3(value3);

        CPPUNIT_ASSERT(testObject.setAttribute(CKA_ID, attr3));

        // Check the attribute
        CPPUNIT_ASSERT(testObject2.attributeExists(CKA_ID));
        CPPUNIT_ASSERT(testObject2.getAttribute(CKA_ID).isByteStringAttribute());
        CPPUNIT_ASSERT(testObject2.getAttribute(CKA_ID).getByteStringValue() == value3);

        // Now check that the first instance also knows about it
        CPPUNIT_ASSERT(testObject.attributeExists(CKA_ID));
        CPPUNIT_ASSERT(testObject.getAttribute(CKA_ID).isByteStringAttribute());
        CPPUNIT_ASSERT(testObject.getAttribute(CKA_ID).getByteStringValue() == value3);
    }
}
示例#30
0
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);
}