TEST(test_iter, const_iterator) { std::cout << "test const iterator" << std::endl; const KeySet ks4 (5, *Key ("user/key4/1", KEY_END), *Key ("user/key4/2", KEY_END), *Key ("user/key4/3", KEY_VALUE, "value", KEY_END), KS_END); KeySet::iterator it = ks4.begin(); ASSERT_EQ(it->getName() , "user/key4/1" ) << "name wrong"; ASSERT_EQ((*it).getName() , "user/key4/1" ) << "name wrong"; it+=1; ASSERT_EQ(it->getName() , "user/key4/2" ) << "name wrong"; ASSERT_EQ((*it).getName() , "user/key4/2" ) << "name wrong"; it+=1; ASSERT_EQ(it->getName() , "user/key4/3" ) << "name wrong"; ASSERT_EQ((*it).getName() , "user/key4/3" ) << "name wrong"; it+=1; ASSERT_EQ(it , ks4.end() ) << "not at end"; it-=1; ASSERT_EQ(it->getName() , "user/key4/3" ) << "name wrong"; ASSERT_EQ((*it).getName() , "user/key4/3" ) << "name wrong"; it-=1; ASSERT_EQ(it->getName() , "user/key4/2" ) << "name wrong"; ASSERT_EQ((*it).getName() , "user/key4/2" ) << "name wrong"; it-=1; ASSERT_EQ(it->getName() , "user/key4/1" ) << "name wrong"; ASSERT_EQ((*it).getName() , "user/key4/1" ) << "name wrong"; ASSERT_EQ(it , ks4.begin() ) << "not at begin"; ASSERT_EQ(it->getName() , "user/key4/1" ) << "name wrong"; ASSERT_EQ((*it).getName() , "user/key4/1" ) << "name wrong"; it++; ASSERT_EQ(it->getName() , "user/key4/2" ) << "name wrong"; ASSERT_EQ((*it).getName() , "user/key4/2" ) << "name wrong"; it++; ASSERT_EQ(it->getName() , "user/key4/3" ) << "name wrong"; ASSERT_EQ((*it).getName() , "user/key4/3" ) << "name wrong"; it++; ASSERT_EQ(it , ks4.end() ) << "not at end"; it--; ASSERT_EQ(it->getName() , "user/key4/3" ) << "name wrong"; ASSERT_EQ((*it).getName() , "user/key4/3" ) << "name wrong"; it--; ASSERT_EQ(it->getName() , "user/key4/2" ) << "name wrong"; ASSERT_EQ((*it).getName() , "user/key4/2" ) << "name wrong"; it--; }
/** * @brief Add all keys of a keyset * * Will not update the underlying keyset (is fixed at * construction) * * @param ks */ void add(KeySet const & ks) { for (KeySet::iterator it = ks.begin(); it != ks.end(); ++it) { add(*it); } }
/* ************************************************************************* */ TEST(Marginals, order) { NonlinearFactorGraph fg; fg += PriorFactor<Pose2>(0, Pose2(), noiseModel::Unit::Create(3)); fg += BetweenFactor<Pose2>(0, 1, Pose2(1,0,0), noiseModel::Unit::Create(3)); fg += BetweenFactor<Pose2>(1, 2, Pose2(1,0,0), noiseModel::Unit::Create(3)); fg += BetweenFactor<Pose2>(2, 3, Pose2(1,0,0), noiseModel::Unit::Create(3)); Values vals; vals.insert(0, Pose2()); vals.insert(1, Pose2(1,0,0)); vals.insert(2, Pose2(2,0,0)); vals.insert(3, Pose2(3,0,0)); vals.insert(100, Point2(0,1)); vals.insert(101, Point2(1,1)); fg += BearingRangeFactor<Pose2,Point2>(0, 100, vals.at<Pose2>(0).bearing(vals.at<Point2>(100)), vals.at<Pose2>(0).range(vals.at<Point2>(100)), noiseModel::Unit::Create(2)); fg += BearingRangeFactor<Pose2,Point2>(0, 101, vals.at<Pose2>(0).bearing(vals.at<Point2>(101)), vals.at<Pose2>(0).range(vals.at<Point2>(101)), noiseModel::Unit::Create(2)); fg += BearingRangeFactor<Pose2,Point2>(1, 100, vals.at<Pose2>(1).bearing(vals.at<Point2>(100)), vals.at<Pose2>(1).range(vals.at<Point2>(100)), noiseModel::Unit::Create(2)); fg += BearingRangeFactor<Pose2,Point2>(1, 101, vals.at<Pose2>(1).bearing(vals.at<Point2>(101)), vals.at<Pose2>(1).range(vals.at<Point2>(101)), noiseModel::Unit::Create(2)); fg += BearingRangeFactor<Pose2,Point2>(2, 100, vals.at<Pose2>(2).bearing(vals.at<Point2>(100)), vals.at<Pose2>(2).range(vals.at<Point2>(100)), noiseModel::Unit::Create(2)); fg += BearingRangeFactor<Pose2,Point2>(2, 101, vals.at<Pose2>(2).bearing(vals.at<Point2>(101)), vals.at<Pose2>(2).range(vals.at<Point2>(101)), noiseModel::Unit::Create(2)); fg += BearingRangeFactor<Pose2,Point2>(3, 100, vals.at<Pose2>(3).bearing(vals.at<Point2>(100)), vals.at<Pose2>(3).range(vals.at<Point2>(100)), noiseModel::Unit::Create(2)); fg += BearingRangeFactor<Pose2,Point2>(3, 101, vals.at<Pose2>(3).bearing(vals.at<Point2>(101)), vals.at<Pose2>(3).range(vals.at<Point2>(101)), noiseModel::Unit::Create(2)); Marginals marginals(fg, vals); KeySet set = fg.keys(); FastVector<Key> keys(set.begin(), set.end()); JointMarginal joint = marginals.jointMarginalCovariance(keys); LONGS_EQUAL(3, (long)joint(0,0).rows()); LONGS_EQUAL(3, (long)joint(1,1).rows()); LONGS_EQUAL(3, (long)joint(2,2).rows()); LONGS_EQUAL(3, (long)joint(3,3).rows()); LONGS_EQUAL(2, (long)joint(100,100).rows()); LONGS_EQUAL(2, (long)joint(101,101).rows()); }
MojErr MojDbQueryPlan::rangesFromKeySets(const KeySet& lowerKeys, const KeySet& upperKeys, const KeySet& prefixKeys, const MojDbQuery::WhereClause* clause) { MojAssert(lowerKeys.size() == upperKeys.size() && lowerKeys.size() == prefixKeys.size()); MojUInt32 index = 0; KeySet::ConstIterator lowerIter = lowerKeys.begin(); KeySet::ConstIterator upperIter = upperKeys.begin(); KeySet::ConstIterator prefixIter = prefixKeys.begin(); while (lowerIter != lowerKeys.end()) { MojErr err = rangesFromKeys(*lowerIter, *upperIter, *prefixIter, index, clause); MojErrCheck(err); ++index; ++lowerIter; ++upperIter; ++prefixIter; } return MojErrNone; }
MojErr MojDbIndex::notifyWatches(const KeySet& keys, MojDbStorageTxn* txn) { LOG_TRACE("Entering function %s", __FUNCTION__); MojAssert(txn); MojThreadReadGuard guard(m_lock); for (KeySet::ConstIterator i = keys.begin(); i != keys.end(); ++i) { MojErr err = notifyWatches(*i, txn); MojErrCheck(err); } return MojErrNone; }
MojErr MojDbIndex::notifyWatches(const KeySet& keys, MojDbStorageTxn* txn) { MojAssert(txn); MojLogTrace(s_log); MojThreadReadGuard guard(m_lock); for (KeySet::ConstIterator i = keys.begin(); i != keys.end(); ++i) { MojErr err = notifyWatches(*i, txn); MojErrCheck(err); } return MojErrNone; }
kdb::KeySet Plugin::getNeededConfig() { Key neededConfigKey ("system/elektra/modules", KEY_END); neededConfigKey.addName(pluginName); neededConfigKey.addName("config/needs"); KeySet d (info.dup()); KeySet config = d.cut(neededConfigKey); KeySet ret; Key oldParent = neededConfigKey; Key newParent("system", KEY_END); for (KeySet::iterator i = config.begin(); i != config.end(); ++i) { Key k(i->dup()); ret.append(kdb::tools::helper::rebaseKey(k, oldParent, newParent)); } return ret; }
MojErr MojDbIndex::insertKeys(const KeySet& keys, MojDbStorageTxn* txn) { LOG_TRACE("Entering function %s", __FUNCTION__); int count = 0; for (KeySet::ConstIterator i = keys.begin(); i != keys.end(); ++i) { MojErr err = m_index->insert(*i, txn); #if defined(MOJ_DEBUG_LOGGING) char s[1024]; size_t size = (*i).size(); MojErr err2 = MojByteArrayToHex((*i).data(), size, s); MojErrCheck(err2); if (size > 16) // if the object-id is in key strncat(s, (char *)((*i).data()) + (size - 17), 16); LOG_DEBUG("[db_mojodb] insertKey %d for: %s; key= %s ; err= %d\n", count+1, this->m_name.data(), s, err); #endif MojErrCheck(err); count ++; } return MojErrNone; }
MojErr MojDbIndex::insertKeys(const KeySet& keys, MojDbStorageTxn* txn) { MojLogTrace(s_log); int count = 0; for (KeySet::ConstIterator i = keys.begin(); i != keys.end(); ++i) { MojErr err = m_index->insert(*i, txn); #if defined(MOJ_DEBUG_LOGGING) char s[1024]; size_t size = (*i).size(); MojErr err2 = MojByteArrayToHex((*i).data(), size, s); MojErrCheck(err2); if (size > 16) // if the object-id is in key strncat(s, (char *)((*i).data()) + (size - 17), 16); MojLogInfo(s_log, _T("insertKey %d for: %s; key= %s ; err= %d\n"), count+1, this->m_name.data(), s, err); #endif MojErrCheck(err); count ++; } return MojErrNone; }
/** * @brief loads all users from the database into the cache */ void StorageEngine::loadAllUsers () { using namespace kdb; // register exclusive access boost::unique_lock<boost::shared_mutex> lock (m_mutex_userCache); // flush cache this->m_userCache.clear (); std::string parentKeyStr = Config::instance ().getConfig ().get<std::string> ("kdb.path.users"); std::regex regex (ELEKTRA_REST_ENTRY_SCHEMA_USERS); KDB kdb; KeySet ks; kdb.get (ks, parentKeyStr); auto elem = ks.begin (); while (elem != ks.end ()) { kdb::Key k = elem.get (); if (std::regex_match (k.getName ().erase (0, parentKeyStr.length () + 1), regex)) { kdbrest::model::User user = static_cast<kdbrest::model::User> (k); elem++; // the next element must be a sub-key of this element while (elem != ks.end () && elem.get ().isBelow (user)) { user.addSubkey (elem.get ()); elem++; } this->m_userCache.push_back (user); continue; // we don't have to increase manually anymore } elem++; } }
MojErr MojDbIndex::delKeys(const KeySet& keys, MojDbStorageTxn* txn, bool forcedel) { LOG_TRACE("Entering function %s", __FUNCTION__); int count = 0; for (KeySet::ConstIterator i = keys.begin(); i != keys.end(); ++i) { MojErr err = m_index->del(*i, txn); #if defined(MOJ_DEBUG_LOGGING) char s[1024]; char *s2 = NULL; if (m_kind) s2 = (char *)(m_kind->id().data()); size_t size = (*i).size(); MojErr err2 = MojByteArrayToHex((*i).data(), size, s); MojErrCheck(err2); if (size > 16) // if the object-id is in key strncat(s, (char *)((*i).data()) + (size - 17), 16); LOG_DEBUG("[db_mojodb] delKey %d for: %s - %s; key= %s ; err= %d\n", count+1, s2, this->m_name.data(), s, err); #endif // This has some potential risk if (err == MojErrInternalIndexOnDel) { m_delMisses++; #if defined(MOJ_DEBUG_LOGGING) LOG_DEBUG("[db_mojodb] delKey %d for: %s - %s; key= %s; err = %d \n", count+1, s2, this->m_name.data(), s, err); #endif if (forcedel) err = MojErrNone; } MojErrCheck(err); count++; } return MojErrNone; }
TEST(test_iter, const_iterate) { std::cout << "testing const iterate" << std::endl; const KeySet ks2 (5, *Key ("user/key2/1", KEY_END), *Key ("user/key2/2", KEY_END), *Key ("user/key2/3", KEY_VALUE, "value", KEY_END), KS_END); ASSERT_EQ((*ks2.begin()).getName() , "user/key2/1" ) << "name wrong"; ASSERT_EQ(ks2.begin()->getName() , "user/key2/1" ) << "name wrong"; ASSERT_EQ((ks2.begin()[0])->getName() , "user/key2/1" ) << "name wrong"; ASSERT_EQ((*(++ks2.begin())).getName() , "user/key2/2") << "name wrong"; ASSERT_EQ((++ks2.begin())->getName() , "user/key2/2" ) << "name wrong"; ASSERT_EQ((*(ks2.begin()+1)).getName() , "user/key2/2") << "name wrong"; ASSERT_EQ((ks2.begin()+1)->getName() , "user/key2/2" ) << "name wrong"; ASSERT_EQ((ks2.begin()[1])->getName() , "user/key2/2" ) << "name wrong"; ASSERT_EQ((*(ks2.begin()+2)).getName() , "user/key2/3") << "name wrong"; ASSERT_EQ((ks2.begin()+2)->getName() , "user/key2/3" ) << "name wrong"; ASSERT_EQ((ks2.begin()[2])->getName() , "user/key2/3" ) << "name wrong"; ASSERT_EQ((ks2.begin()+3) , ks2.end() ) << "end iterator broken"; ASSERT_EQ((*ks2.rbegin()).getName() , "user/key2/3" ) << "name wrong"; ASSERT_EQ(ks2.rbegin()->getName() , "user/key2/3" ) << "name wrong"; ASSERT_EQ((ks2.rbegin()[0])->getName() , "user/key2/3" ) << "name wrong"; ASSERT_EQ((*(++ks2.rbegin())).getName() , "user/key2/2") << "name wrong"; ASSERT_EQ((++ks2.rbegin())->getName() , "user/key2/2" ) << "name wrong"; ASSERT_EQ((*(ks2.rbegin()+1)).getName() , "user/key2/2") << "name wrong"; ASSERT_EQ((ks2.rbegin()+1)->getName() , "user/key2/2" ) << "name wrong"; ASSERT_EQ((ks2.rbegin()[1])->getName() , "user/key2/2" ) << "name wrong"; ASSERT_EQ((*(ks2.rbegin()+2)).getName() , "user/key2/1") << "name wrong"; ASSERT_EQ((ks2.rbegin()+2)->getName() , "user/key2/1" ) << "name wrong"; ASSERT_EQ((ks2.rbegin()[2])->getName() , "user/key2/1" ) << "name wrong"; ASSERT_EQ((ks2.rbegin()+3) , ks2.rend() ) << "end iterator broken"; ASSERT_EQ((*ks2.cbegin()).getName() , "user/key2/1" ) << "name wrong"; ASSERT_EQ(ks2.cbegin()->getName() , "user/key2/1" ) << "name wrong"; ASSERT_EQ((ks2.cbegin()[0])->getName() , "user/key2/1" ) << "name wrong"; ASSERT_EQ((*(++ks2.cbegin())).getName() , "user/key2/2") << "name wrong"; ASSERT_EQ((++ks2.cbegin())->getName() , "user/key2/2" ) << "name wrong"; ASSERT_EQ((*(ks2.cbegin()+1)).getName() , "user/key2/2") << "name wrong"; ASSERT_EQ((ks2.cbegin()+1)->getName() , "user/key2/2" ) << "name wrong"; ASSERT_EQ((ks2.cbegin()[1])->getName() , "user/key2/2" ) << "name wrong"; ASSERT_EQ((*(ks2.cbegin()+2)).getName() , "user/key2/3") << "name wrong"; ASSERT_EQ((ks2.cbegin()+2)->getName() , "user/key2/3" ) << "name wrong"; ASSERT_EQ((ks2.cbegin()[2])->getName() , "user/key2/3" ) << "name wrong"; ASSERT_EQ((ks2.cbegin()+3) , ks2.cend() ) << "end iterator broken"; ASSERT_EQ((*ks2.crbegin()).getName() , "user/key2/3" ) << "name wrong"; ASSERT_EQ(ks2.crbegin()->getName() , "user/key2/3" ) << "name wrong"; ASSERT_EQ((ks2.crbegin()[0])->getName() , "user/key2/3" ) << "name wrong"; ASSERT_EQ((*(++ks2.crbegin())).getName() , "user/key2/2") << "name wrong"; ASSERT_EQ((++ks2.crbegin())->getName() , "user/key2/2" ) << "name wrong"; ASSERT_EQ((*(ks2.crbegin()+1)).getName() , "user/key2/2") << "name wrong"; ASSERT_EQ((ks2.crbegin()+1)->getName() , "user/key2/2" ) << "name wrong"; ASSERT_EQ((ks2.crbegin()[1])->getName() , "user/key2/2" ) << "name wrong"; ASSERT_EQ((*(ks2.crbegin()+2)).getName() , "user/key2/1") << "name wrong"; ASSERT_EQ((ks2.crbegin()+2)->getName() , "user/key2/1" ) << "name wrong"; ASSERT_EQ((ks2.crbegin()[2])->getName() , "user/key2/1" ) << "name wrong"; ASSERT_EQ((ks2.crbegin()+3) , ks2.crend() ) << "end iterator broken"; }
void writePreserving(QFile& inFile, QTextStream& out, const AttributeMap& data) { FNTRACE("ini", "", "writePreserving", QString("in, out, %1 rows of data").arg(data.size())); int i, j; QChar c; QString name; size_t lineNumber = 0; QString line; typedef QSet<AttributeMap::key_type> KeySet; KeySet unwrittenKeys = KeySet::fromList(data.keys()); if (!inFile.open(QIODevice::ReadOnly | QIODevice::Text)) ETHROW(Exception(QString("Unable to open %1 for reading.").arg(inFile.fileName()))); QTextStream in(&inFile); while (!(line = in.readLine()).isNull()) { ++lineNumber; //std::cout << lineNumber << ": " << line.toStdString() << ":\n"; for (i = 0; line[i].isSpace(); ++i) { out << line[i]; } if (line[i].isNull()) { out << '\n'; continue; } else if (line[i] == COMMENT_BEGIN) { out << line.mid(i) << '\n'; continue; } for (j = i; !(c = line[i]).isNull() && c!=COMMENT_BEGIN && c!=ASSIGNER; ++i) ; //std::cout << "non-empty, "; if (c != ASSIGNER) // non-empty line that isn't assignment -> invalid ETHROW(ParseError(inFile.fileName(), lineNumber, "Unknown non-empty line.")); for (--i; line[i].isSpace(); --i) ; // last character of name name = line.mid(j, i - j + 1); //std::cout << "name: " << name.toStdString() << ' '; for ( ; line[j]!=ASSIGNER; ++j) out << line[j]; out << ASSIGNER; i = j + 1; for ( ; line[i].isSpace(); ++i) out << line[i]; if (data.contains(name)) { //std::cout << "known "; out << data.value(name); unwrittenKeys.remove(name); for (j = i; !line[i].isNull() && line[i]!=COMMENT_BEGIN; ++i) ; // EOL || comment for (--i; line[i].isSpace(); --i) ; // last character of value ++i; // first character after value } out << line.mid(i) << '\n'; //std::cout << '\n'; } if (unwrittenKeys.size()) { out << '\n' << COMMENT_BEGIN << "Following lines were generated by Ds1edit Loader:\n"; for (KeySet::iterator key = unwrittenKeys.begin(); key != unwrittenKeys.end(); ++key) out << (*key) << ' ' << ASSIGNER << ' ' << data.value(*key) << '\n'; } //in.resize(in.pos()+1); inFile.close(); }
int main(int argc, const char** argv) { //WDBG_INIT(WDBG_LIBLOW, "CryptTest", NULL, "E:\\Logs"); { cout << endl << "Testing Random Number Generator..." << endl; for (int i=1; i <= 100; i++) { unsigned short aShort = Randomizer::GetShort(); cout << std::hex << aShort << std::dec << " "; if ((i % 5) == 0) cout << endl; } cout << endl; } { cout << endl << "Testing Key Classes..." << endl; KeySet aKeySet; aKeySet.insert(new BFSymmetricKey(CryptKeyBase::KEYLEN_DEF)); aKeySet.insert(new BFSymmetricKey(4)); BFSymmetricKey aKey(12); aKeySet.insert(new BFSymmetricKey(aKey.GetKeyLen(), aKey.GetKey())); aKeySet.insert(new EGPrivateKey(CryptKeyBase::KEYLEN_DEF)); aKeySet.insert(new EGPrivateKey(4)); EGPrivateKey aPvKey(12); EGPrivateKey aPvKey1(0); aKeySet.insert(new EGPrivateKey(aPvKey.GetKeyLen(), aPvKey.GetKey())); aKeySet.insert(new EGPrivateKey(aPvKey1)); const EGPublicKey& aPubKey = dynamic_cast<const EGPublicKey&>(aPvKey1.GetPublicKey()); aKeySet.insert(new EGPublicKey(aPvKey)); aKeySet.insert(new EGPublicKey(aPubKey)); // Output keys KeySet::iterator anItr(aKeySet.begin()); for (int i=1; anItr != aKeySet.end(); i++) { cout << "Key " << i << ": " << *(*anItr) << endl; delete *anItr; anItr = aKeySet.erase(anItr); } cout << endl; } { cout << endl << "Testing SymmetricCrypt..." << endl; BFSymmetricKey* myKey = new BFSymmetricKey(8); BFSymmetricKey* badKey = new BFSymmetricKey(12); char* iStr = "Mike has no life!"; BFSymmetricKey::CryptReturn encrypt(myKey->Encrypt(reinterpret_cast<unsigned char*>(iStr), strlen(iStr)+1)); BFSymmetricKey::CryptReturn decrypt(myKey->Decrypt(encrypt.first, encrypt.second)); cout << "Input: " << iStr << endl; cout << "Output: " << reinterpret_cast<const char*>(decrypt.first ? decrypt.first : (const unsigned char*)"NULL") << endl; delete encrypt.first; delete decrypt.first; delete myKey; delete badKey; } { cout << endl << "Testing ElGamal Crypt..." << endl; EGPrivateKey* myPrivKey = new EGPrivateKey(8); EGPrivateKey* badKey = new EGPrivateKey(12); EGPublicKey* myPubKey = new EGPublicKey(*myPrivKey); char* iStr = "Mike has no life!"; EGPublicKey::CryptReturn encrypt(myPubKey->Encrypt(reinterpret_cast<unsigned char*>(iStr), strlen(iStr)+1)); EGPrivateKey::CryptReturn decrypt(myPrivKey->Decrypt(encrypt.first, encrypt.second)); cout << "Input: " << iStr << endl; cout << "Output: " << reinterpret_cast<const char*>(decrypt.first ? decrypt.first : (const unsigned char*)"NULL") << endl; delete encrypt.first; delete decrypt.first; delete myPrivKey; delete myPubKey; delete badKey; } { cout << endl << "Testing ElGamal Sigs..." << endl; EGPrivateKey* myPrivKey = new EGPrivateKey(8); EGPrivateKey* badKey = new EGPrivateKey(12); EGPublicKey* myPubKey = new EGPublicKey(*myPrivKey); char* iStr = "Mike has no life!"; EGPrivateKey::CryptReturn sig(myPrivKey->Sign(reinterpret_cast<unsigned char*>(iStr), strlen(iStr)+1)); int tstSig = myPubKey->Verify(sig.first, sig.second, reinterpret_cast<unsigned char*>(iStr), strlen(iStr)+1); cout << "Msg: " << iStr << " sigLen=" << sig.second << endl; cout << "Verify: " << (tstSig ? "TRUE" : "FALSE") << endl; delete sig.first; delete myPrivKey; delete myPubKey; } return 0; }