diff --git a/libretroshare/src/gxs/rsdataservice.cc b/libretroshare/src/gxs/rsdataservice.cc index 5837bcc46..b4a439bca 100644 --- a/libretroshare/src/gxs/rsdataservice.cc +++ b/libretroshare/src/gxs/rsdataservice.cc @@ -191,6 +191,8 @@ RsDataService::RsDataService(const std::string &serviceDir, const std::string &d mMsgOffSetColumns.push_back(KEY_NXS_FILE_LEN); grpIdColumn.push_back(KEY_GRP_ID); + + mMsgIdColumn.push_back(KEY_MSG_ID); } RsDataService::~RsDataService(){ @@ -1344,6 +1346,32 @@ int RsDataService::retrieveGroupIds(std::vector &grpIds) return 1; } +int RsDataService::retrieveMsgIds(const RsGxsGroupId& grpId, + RsGxsMessageId::std_vector& msgIds) { + + RetroCursor* c = mDb->sqlQuery(MSG_TABLE_NAME, mMsgIdColumn, KEY_GRP_ID+ "='" + grpId.toStdString() + "'", ""); + + if(c) + { + bool valid = c->moveToFirst(); + + while(valid) + { + std::string msgId; + c->getString(0, msgId); + msgIds.push_back(RsGxsMessageId(msgId)); + valid = c->moveToNext(); + } + delete c; + }else + { + return 0; + } + + return 1; + +} + bool RsDataService::locked_updateMessageEntries(const MsgUpdates& updates) { // start a transaction diff --git a/libretroshare/src/gxs/rsdataservice.h b/libretroshare/src/gxs/rsdataservice.h index 6c73bf044..e8ca4d3af 100644 --- a/libretroshare/src/gxs/rsdataservice.h +++ b/libretroshare/src/gxs/rsdataservice.h @@ -113,6 +113,14 @@ public: */ int retrieveGroupIds(std::vector &grpIds); + /*! + * Retrives all msg ids in store + * @param grpId groupId of message ids to retrieve + * @param msgId msgsids retrieved + * @return error code + */ + int retrieveMsgIds(const RsGxsGroupId& grpId, RsGxsMessageId::std_vector& msgId); + /*! * @return the cache size set for this RsGeneralDataService in bytes */ @@ -249,6 +257,7 @@ private: std::list msgColumns; std::list msgMetaColumns; std::list mMsgOffSetColumns; + std::list mMsgIdColumn; std::list grpColumns; std::list grpMetaColumns; diff --git a/libretroshare/src/gxs/rsgds.h b/libretroshare/src/gxs/rsgds.h index f1bc577a7..1de6d6f3b 100644 --- a/libretroshare/src/gxs/rsgds.h +++ b/libretroshare/src/gxs/rsgds.h @@ -118,6 +118,7 @@ public: public: typedef std::map GrpStoreMap; + typedef std::map MsgStoreMap; RsGeneralDataService(){} virtual ~RsGeneralDataService(){return;} @@ -179,6 +180,14 @@ public: */ virtual int retrieveGroupIds(std::vector& grpIds) = 0; + /*! + * Retrives all msg ids in store + * @param grpId groupId of message ids to retrieve + * @param msgId msgsids retrieved + * @return error code + */ + virtual int retrieveMsgIds(const RsGxsGroupId& grpId, RsGxsMessageId::std_vector& msgId) = 0; + /*! * @return the cache size set for this RsGeneralDataService in bytes */ diff --git a/tests/unittests/libretroshare/gxs/nxs_test/nxsgrpsync_test.cc b/tests/unittests/libretroshare/gxs/nxs_test/nxsgrpsync_test.cc index 9057debf4..fa401be91 100644 --- a/tests/unittests/libretroshare/gxs/nxs_test/nxsgrpsync_test.cc +++ b/tests/unittests/libretroshare/gxs/nxs_test/nxsgrpsync_test.cc @@ -13,14 +13,7 @@ using namespace rs_nxs_test; -template -void copy_all_but(T& ex, const std::list& s, std::list& d) -{ - typename std::list::const_iterator cit = s.begin(); - for(; cit != s.end(); cit++) - if(*cit != ex) - d.push_back(*cit); -} + NxsGrpSync::NxsGrpSync() { @@ -48,13 +41,15 @@ NxsGrpSync::NxsGrpSync() RsNxsNetMgr* mgr = new rs_nxs_test::RsNxsNetDummyMgr(*it, otherPeers); mNxsNetMgrs.insert(std::make_pair(*it, mgr)); - RsNxsSimpleDummyReputation::RepMap reMap; - std::list membership; - // now reputation service - mRep = new RsNxsSimpleDummyReputation(reMap, true); - mCircles = new RsNxsSimpleDummyCircles(membership, true); } + RsNxsSimpleDummyReputation::RepMap reMap; + std::list membership; + // now reputation service + mRep = new RsNxsSimpleDummyReputation(reMap, true); + mCircles = new RsNxsSimpleDummyCircles(membership, true); + + // lets create some a group each for all peers DataMap::iterator mit = mDataServices.begin(); for(; mit != mDataServices.end(); mit++) diff --git a/tests/unittests/libretroshare/gxs/nxs_test/nxsgrptestscenario.h b/tests/unittests/libretroshare/gxs/nxs_test/nxsgrptestscenario.h index a8963cbe9..59e741298 100644 --- a/tests/unittests/libretroshare/gxs/nxs_test/nxsgrptestscenario.h +++ b/tests/unittests/libretroshare/gxs/nxs_test/nxsgrptestscenario.h @@ -20,8 +20,8 @@ public: NxsGrpTestScenario(); virtual ~NxsGrpTestScenario(); - bool checkTestPassed(); - bool checkDeepTestPassed(); + virtual bool checkTestPassed(); + virtual bool checkDeepTestPassed(); protected: diff --git a/tests/unittests/libretroshare/gxs/nxs_test/nxsmsgsync_test.cc b/tests/unittests/libretroshare/gxs/nxs_test/nxsmsgsync_test.cc index ae894ccd3..c252b3624 100644 --- a/tests/unittests/libretroshare/gxs/nxs_test/nxsmsgsync_test.cc +++ b/tests/unittests/libretroshare/gxs/nxs_test/nxsmsgsync_test.cc @@ -7,4 +7,143 @@ +#include "nxsmsgsync_test.h" +#include "retroshare/rstypes.h" +#include "gxs/rsdataservice.h" +#include "nxsdummyservices.h" +#include "../common/data_support.h" +#include +using namespace rs_nxs_test; + +rs_nxs_test::NxsMsgSync::NxsMsgSync() +{ + int numPeers = 2; + + for(int i =0; i < numPeers; i++) + { + RsPeerId id = RsPeerId::random(); + mPeerIds.push_back(id); + } + + + std::list::iterator it = mPeerIds.begin(); + for(; it != mPeerIds.end(); it++) + { + // data stores + RsGeneralDataService* ds = new RsDataService("./", "grp_store_" + + it->toStdString(), mServType, NULL, "key"); + mDataServices.insert(std::make_pair(*it, ds)); + + // net managers + std::list otherPeers; + copy_all_but(*it, mPeerIds, otherPeers); + RsNxsNetMgr* mgr = new rs_nxs_test::RsNxsNetDummyMgr(*it, otherPeers); + mNxsNetMgrs.insert(std::make_pair(*it, mgr)); + } + + RsNxsSimpleDummyReputation::RepMap reMap; + std::list membership; + // now reputation service + mRep = new RsNxsSimpleDummyReputation(reMap, true); + mCircles = new RsNxsSimpleDummyCircles(membership, true); + + // lets create 2 groups and all peers will have them + int nGrps = 2; + + NxsMsgTestScenario::ExpectedMap& expMap = mExpectedResult; + for(int i=0; i < nGrps; i++) + { + std::auto_ptr grp = std::auto_ptr(new RsNxsGrp(mServType)); + + init_item(*grp); + RsGxsGrpMetaData* meta = new RsGxsGrpMetaData(); + init_item(meta); + meta->mReputationCutOff = 0; + meta->mGroupId = grp->grpId; + grp->metaData = meta; + meta->mSubscribeFlags = GXS_SERV::GROUP_SUBSCRIBE_SUBSCRIBED; + + RsGxsGroupId grpId = grp->grpId; + + // the expected result is that each peer has the group of the others + it = mPeerIds.begin(); + + DataMap::iterator mit = mDataServices.begin(); + + // add a clone of group into the peer's service + // then create 2 msgs for each peer for each group + for(; mit != mDataServices.end(); mit++) + { + // first store grp + RsGeneralDataService* ds = mit->second; + RsNxsGrp* grp_clone = grp->clone(); + RsGeneralDataService::GrpStoreMap gsp; + gsp.insert(std::make_pair(grp_clone, grp_clone->metaData)); + ds->storeGroup(gsp); + + RsGxsGroupId grpId = grp->grpId; + RsPeerId peerId = mit->first; + + NxsMsgTestScenario::ExpectedMsgs expMsgs; + int nMsgs = 2; // and each grp for each peer gets a unique message + for(int j=0; j < nMsgs; j++) + { + RsNxsMsg* msg = new RsNxsMsg(mServType); + init_item(*msg); + msg->grpId = grp->grpId; + RsGxsMsgMetaData* msgMeta = new RsGxsMsgMetaData(); + init_item(msgMeta); + msgMeta->mGroupId = grp->grpId; + RsGeneralDataService::MsgStoreMap msm; + msm.insert(std::make_pair(msg , msgMeta)); + RsGxsMessageId msgId = msg->msgId; + ds->storeMessage(msm); + + it = mPeerIds.begin(); + + // the expectation is that all peers have the same messages + for(; it != mPeerIds.end(); it++) + { + NxsMsgTestScenario::ExpectedMsgs& expMsgs = expMap[peerId]; + expMsgs[grpId].push_back(msgId); + } + } + } + } +} + +void rs_nxs_test::NxsMsgSync::getPeers(std::list& peerIds) { + peerIds = mPeerIds; +} + +RsGeneralDataService* rs_nxs_test::NxsMsgSync::getDataService( + const RsPeerId& peerId) { + return mDataServices[peerId]; +} + +RsNxsNetMgr* rs_nxs_test::NxsMsgSync::getDummyNetManager( + const RsPeerId& peerId) { + return mNxsNetMgrs[peerId]; +} + +RsGcxs* rs_nxs_test::NxsMsgSync::getDummyCircles(const RsPeerId& peerId) { + return mCircles; +} + +RsGixsReputation* rs_nxs_test::NxsMsgSync::getDummyReputations( + const RsPeerId& peerId) { + return mRep; +} + +uint16_t rs_nxs_test::NxsMsgSync::getServiceType() { + return mServType; +} + +RsServiceInfo rs_nxs_test::NxsMsgSync::getServiceInfo() { + return mServInfo; +} + +const NxsMsgTestScenario::ExpectedMap& rs_nxs_test::NxsMsgSync::getExpectedMap() { + return mExpectedResult; +} diff --git a/tests/unittests/libretroshare/gxs/nxs_test/nxsmsgsync_test.h b/tests/unittests/libretroshare/gxs/nxs_test/nxsmsgsync_test.h index 761cc44a4..459074fc2 100644 --- a/tests/unittests/libretroshare/gxs/nxs_test/nxsmsgsync_test.h +++ b/tests/unittests/libretroshare/gxs/nxs_test/nxsmsgsync_test.h @@ -8,28 +8,31 @@ #ifndef NXSMSGSYNC_TEST_H_ #define NXSMSGSYNC_TEST_H_ -#include "nxstestscenario.h" +#include "nxsmsgtestscenario.h" +namespace rs_nxs_test { - class NxsMessageTest : public rs_nxs_test::NxsTestScenario + class NxsMsgSync : public NxsMsgTestScenario { public: - NxsMessageTest(); + NxsMsgSync(); void getPeers(std::list& peerIds); RsGeneralDataService* getDataService(const RsPeerId& peerId); - bool checkTestPassed(); RsNxsNetMgr* getDummyNetManager(const RsPeerId& peerId); RsGcxs* getDummyCircles(const RsPeerId& peerId); RsGixsReputation* getDummyReputations(const RsPeerId& peerId); uint16_t getServiceType(); RsServiceInfo getServiceInfo(); + protected: + + const ExpectedMap& getExpectedMap(); + private: std::list mPeerIds; typedef std::map DataMap; - typedef std::map > ExpectedMap; DataMap mDataServices; std::map mNxsNetMgrs; @@ -37,13 +40,13 @@ RsGcxs* mCircles; RsServiceInfo mServInfo; - ExpectedMap mExpectedResult; + NxsMsgTestScenario::ExpectedMap mExpectedResult; uint16_t mServType; }; - +} #endif /* NXSMSGSYNC_TEST_H_ */ diff --git a/tests/unittests/libretroshare/gxs/nxs_test/nxsmsgtestscenario.cc b/tests/unittests/libretroshare/gxs/nxs_test/nxsmsgtestscenario.cc new file mode 100644 index 000000000..c21c8987a --- /dev/null +++ b/tests/unittests/libretroshare/gxs/nxs_test/nxsmsgtestscenario.cc @@ -0,0 +1,72 @@ +/* + * nxsmsgtestscenario.cpp + * + * Created on: 26 Apr 2014 + * Author: crispy + */ + +#include "nxsmsgtestscenario.h" + +namespace rs_nxs_test { + +NxsMsgTestScenario::NxsMsgTestScenario() { + // TODO Auto-generated constructor stub + +} + +NxsMsgTestScenario::~NxsMsgTestScenario() { + // TODO Auto-generated destructor stub +} + +bool NxsMsgTestScenario::checkTestPassed() { + + // so we expect all peers to all messages for each group + const ExpectedMap& exMap = getExpectedMap(); + + ExpectedMap::const_iterator mit = exMap.begin(); + + bool passed = true; + + + for(; mit != exMap.end(); mit++) + { + const RsPeerId& pid = mit->first; + + const ExpectedMsgs& exMsgs = mit->second; + ExpectedMsgs::const_iterator cit = exMsgs.begin(); + RsGeneralDataService* ds = getDataService(pid); + + for(; cit != exMsgs.end(); cit++) + { + const RsGxsGroupId& grpId = cit->first; + RsGxsMessageId::std_vector expMsgIds = cit->second; + RsGxsMessageId::std_vector msgIds; + + ds->retrieveMsgIds(grpId, msgIds); + + RsGxsMessageId::std_vector result(expMsgIds.size()+msgIds.size()); + + std::sort(msgIds.begin(), msgIds.end()); + std::sort(expMsgIds.begin(), expMsgIds.end()); + + RsGxsMessageId::std_vector::iterator it = std::set_difference(msgIds.begin(), msgIds.end(), + expMsgIds.begin(), expMsgIds.end(), result.begin()); + + result.resize(it - result.begin()); + + passed &= result.size() == 0; + } + + } + + return passed; + +} + +bool NxsMsgTestScenario::checkDeepTestPassed() { + return false; +} + +} + + /* namespace rs_nxs_test */ diff --git a/tests/unittests/libretroshare/gxs/nxs_test/nxsmsgtestscenario.h b/tests/unittests/libretroshare/gxs/nxs_test/nxsmsgtestscenario.h new file mode 100644 index 000000000..ca615d6fd --- /dev/null +++ b/tests/unittests/libretroshare/gxs/nxs_test/nxsmsgtestscenario.h @@ -0,0 +1,33 @@ +/* + * nxsmsgtestscenario.h + * + * Created on: 26 Apr 2014 + * Author: crispy + */ + +#ifndef NXSMSGTESTSCENARIO_H_ +#define NXSMSGTESTSCENARIO_H_ + +#include "nxstestscenario.h" + +namespace rs_nxs_test { + +class NxsMsgTestScenario : public NxsTestScenario { +public: + NxsMsgTestScenario(); + virtual ~NxsMsgTestScenario(); + + typedef std::map ExpectedMsgs; + typedef std::map ExpectedMap; + + bool checkTestPassed(); + bool checkDeepTestPassed(); + +protected: + + RsDataService* createDataStore(const RsPeerId& peerId); + virtual const ExpectedMap& getExpectedMap() = 0; +}; + +} /* namespace rs_nxs_test */ +#endif /* NXSMSGTESTSCENARIO_H_ */ diff --git a/tests/unittests/libretroshare/gxs/nxs_test/rsgxsnetservice_test.cc b/tests/unittests/libretroshare/gxs/nxs_test/rsgxsnetservice_test.cc index 519a42613..d4d3e9428 100644 --- a/tests/unittests/libretroshare/gxs/nxs_test/rsgxsnetservice_test.cc +++ b/tests/unittests/libretroshare/gxs/nxs_test/rsgxsnetservice_test.cc @@ -8,6 +8,7 @@ #include #include "nxsgrpsync_test.h" +#include "nxsmsgsync_test.h" #include "nxstesthub.h" TEST(libretroshare_gxs, gxs_grp_sync) @@ -18,7 +19,22 @@ TEST(libretroshare_gxs, gxs_grp_sync) tHub.StartTest(); // wait for ten seconds - rs_nxs_test::NxsTestHub::Wait(15); + rs_nxs_test::NxsTestHub::Wait(10); + + tHub.EndTest(); + + ASSERT_TRUE(tHub.testsPassed()); +} + +TEST(libretroshare_gxs, gxs_msg_sync) +{ + rs_nxs_test::NxsTestScenario::pointer gsync_test = rs_nxs_test::NxsTestScenario::pointer( + new rs_nxs_test::NxsMsgSync); + rs_nxs_test::NxsTestHub tHub(gsync_test); + tHub.StartTest(); + + // wait for ten seconds + rs_nxs_test::NxsTestHub::Wait(10); tHub.EndTest(); diff --git a/tests/unittests/unittests.pro b/tests/unittests/unittests.pro index fc74b9d93..458ee4047 100644 --- a/tests/unittests/unittests.pro +++ b/tests/unittests/unittests.pro @@ -3,7 +3,7 @@ CONFIG += bitdht CONFIG += gxs debug -LIBS += -lgtest +LIBS += -lgtest.a gxs { DEFINES += RS_ENABLE_GXS @@ -27,7 +27,7 @@ linux-* { LIBS += -lssl -lupnp -lixml -lXss -lgnome-keyring LIBS *= -lcrypto -ldl -lX11 -lz - gxs { +gxs { LIBS += ../../supportlibs/pegmarkdown/lib/libpegmarkdown.a # We need a explicit path here, to force using the home version of sqlite3 that really encrypts the database. @@ -261,12 +261,16 @@ HEADERS += libretroshare/gxs/common/data_support.h \ SOURCES += libretroshare/gxs/common/data_support.cc \ HEADERS += libretroshare/gxs/nxs_test/nxsdummyservices.h \ + libretroshare/gxs/nxs_test/nxsgrptestscenario.h \ + libretroshare/gxs/nxs_test/nxsmsgtestscenario.h \ libretroshare/gxs/nxs_test/nxsgrpsync_test.h \ libretroshare/gxs/nxs_test/nxsmsgsync_test.h \ libretroshare/gxs/nxs_test/nxstesthub.h \ libretroshare/gxs/nxs_test/nxstestscenario.h SOURCES += libretroshare/gxs/nxs_test/nxsdummyservices.cc \ + libretroshare/gxs/nxs_test/nxsgrptestscenario.cc \ + libretroshare/gxs/nxs_test/nxsmsgtestscenario.cc \ libretroshare/gxs/nxs_test/nxsgrpsync_test.cc \ libretroshare/gxs/nxs_test/nxsmsgsync_test.cc \ libretroshare/gxs/nxs_test/nxstesthub.cc \