diff --git a/libretroshare/src/util/rssharedptr.h b/libretroshare/src/util/rssharedptr.h index e188c6940..738ba9b0b 100644 --- a/libretroshare/src/util/rssharedptr.h +++ b/libretroshare/src/util/rssharedptr.h @@ -21,26 +21,43 @@ public: RsSharedPtr() : mShared(NULL), mCount(NULL) {} RsSharedPtr(T* shared) - : mShared(shared), mCount(new int(0)) + : mShared(shared), mCount(new int(0)), mSharedPtrMutex(new RsMutex("SharedMutex")) { mCount++; } RsSharedPtr(const RsSharedPtr& rsp) { + rsp.lock(); mShared = rsp.mShared; mCount = rsp.mCount; mCount++; + mSharedPtrMutex = rsp.mSharedPtrMutex; + rsp.unlock(); + } void operator=(const RsSharedPtr& rsp) { + rsp.lock(); + mSharedPtrMutex = rsp.mSharedPtrMutex; DecrementAndDeleteIfLast(); mShared = rsp.mShared; RepointAndIncrement(rsp.mCount); + + mSharedPtrMutex->unlock(); } - T* release() { mCount--; T* temp = mShared; mShared = NULL; return temp; } + T* release() { + + lock(); + + mCount--; T* temp = mShared; mShared = NULL; + + unlock(); + + return temp; + } T* get() { return mShared; } T& operator*(){ return *mShared; } @@ -72,10 +89,14 @@ private: } + void lock() const { mSharedPtrMutex->lock(); } + void unlock() const { mSharedPtrMutex->unlock(); } + private: int* mCount; T* mShared; + RsMutex* mSharedPtrMutex; }; diff --git a/tests/unittests/libretroshare/gxs/nxs_test/nxsgrpsync_test.cc b/tests/unittests/libretroshare/gxs/nxs_test/nxsgrpsync_test.cc index b908fe6f9..9057debf4 100644 --- a/tests/unittests/libretroshare/gxs/nxs_test/nxsgrpsync_test.cc +++ b/tests/unittests/libretroshare/gxs/nxs_test/nxsgrpsync_test.cc @@ -29,8 +29,6 @@ NxsGrpSync::NxsGrpSync() for(int i =0; i < numPeers; i++) { - - RsPeerId id = RsPeerId::random(); mPeerIds.push_back(id); } @@ -68,8 +66,8 @@ NxsGrpSync::NxsGrpSync() init_item(meta); grp->metaData = meta; meta->mSubscribeFlags = GXS_SERV::GROUP_SUBSCRIBE_SUBSCRIBED; - RsNxsGrp* grp_copy = grp->clone(); + RsGxsGroupId grpId = grp->grpId; RsGeneralDataService::GrpStoreMap gsp; gsp.insert(std::make_pair(grp, meta)); @@ -78,9 +76,8 @@ NxsGrpSync::NxsGrpSync() // the expected result is that each peer has the group of the others it = mPeerIds.begin(); for(; it != mPeerIds.end(); it++) - { - if(mit->first != *it) - mExpectedResult[*it].push_back(grp_copy); + { + mExpectedResult[*it].push_back(grpId); } } @@ -99,31 +96,6 @@ RsGeneralDataService* NxsGrpSync::getDataService(const RsPeerId& peerId) return mDataServices[peerId]; } -bool NxsGrpSync::checkTestPassed() -{ - // look at data store, as all peers should have same - // number of groups - DataMap::iterator mit = mDataServices.begin(); - std::list vals; - for(; mit != mDataServices.end(); mit++) - { - RsGxsGroupId::std_vector grpIds; - mit->second->retrieveGroupIds(grpIds); - vals.push_back(grpIds.size()); - } - - std::list::iterator lit = vals.begin(); - int prev = *lit; - bool passed = true; - for(; lit != vals.end(); lit++) - { - passed &= *lit == prev; - prev = *lit; - } - - return passed; -} - RsNxsNetMgr* NxsGrpSync::getDummyNetManager(const RsPeerId& peerId) { return mNxsNetMgrs[peerId]; @@ -161,3 +133,8 @@ RsServiceInfo rs_nxs_test::NxsGrpSync::getServiceInfo() { return mServInfo; } +const NxsGrpTestScenario::ExpectedMap& rs_nxs_test::NxsGrpSync::getExpectedMap() { + return mExpectedResult; +} + + diff --git a/tests/unittests/libretroshare/gxs/nxs_test/nxsgrpsync_test.h b/tests/unittests/libretroshare/gxs/nxs_test/nxsgrpsync_test.h index 212ce3d2d..c23a0ece7 100644 --- a/tests/unittests/libretroshare/gxs/nxs_test/nxsgrpsync_test.h +++ b/tests/unittests/libretroshare/gxs/nxs_test/nxsgrpsync_test.h @@ -8,13 +8,12 @@ #ifndef NXSGRPSYNC_TEST_H_ #define NXSGRPSYNC_TEST_H_ - -#include "nxstestscenario.h" +#include "nxsgrptestscenario.h" namespace rs_nxs_test { - class NxsGrpSync : public NxsTestScenario + class NxsGrpSync : public NxsGrpTestScenario { public: @@ -23,18 +22,20 @@ namespace rs_nxs_test 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; diff --git a/tests/unittests/libretroshare/gxs/nxs_test/nxsgrptestscenario.cc b/tests/unittests/libretroshare/gxs/nxs_test/nxsgrptestscenario.cc new file mode 100644 index 000000000..7f5c23a35 --- /dev/null +++ b/tests/unittests/libretroshare/gxs/nxs_test/nxsgrptestscenario.cc @@ -0,0 +1,56 @@ +/* + * nxsgrptestscenario.cpp + * + * Created on: 23 Apr 2014 + * Author: crispy + */ + +#include "nxsgrptestscenario.h" +#include + +namespace rs_nxs_test { + +NxsGrpTestScenario::NxsGrpTestScenario() { +} + +NxsGrpTestScenario::~NxsGrpTestScenario() { +} + +bool NxsGrpTestScenario::checkTestPassed() +{ + const ExpectedMap& exMap = getExpectedMap(); + + ExpectedMap::const_iterator mit = exMap.begin(); + + bool passed = true; + + for(; mit != exMap.end(); mit++) + { + const RsPeerId& pid = mit->first; + RsGxsGroupId::std_vector expGrpIds = mit->second; + RsGeneralDataService* ds = getDataService(pid); + RsGxsGroupId::std_vector grpIds; + ds->retrieveGroupIds(grpIds); + + RsGxsGroupId::std_vector result(expGrpIds.size()+grpIds.size()); + std::sort(grpIds.begin(), grpIds.end()); + std::sort(expGrpIds.begin(), expGrpIds.end()); + RsGxsGroupId::std_vector::iterator it = std::set_difference(grpIds.begin(), grpIds.end(), + expGrpIds.begin(), expGrpIds.end(), result.begin()); + + result.resize(it - result.begin()); + + passed &= result.size() == 0; + } + + return passed; +} + +bool NxsGrpTestScenario::checkDeepTestPassed() +{ + return false; +} + +} + + /* namespace rs_nxs_test */ diff --git a/tests/unittests/libretroshare/gxs/nxs_test/nxsgrptestscenario.h b/tests/unittests/libretroshare/gxs/nxs_test/nxsgrptestscenario.h new file mode 100644 index 000000000..a8963cbe9 --- /dev/null +++ b/tests/unittests/libretroshare/gxs/nxs_test/nxsgrptestscenario.h @@ -0,0 +1,33 @@ +/* + * nxsgrptestscenario.h + * + * Created on: 23 Apr 2014 + * Author: crispy + */ + +#ifndef NXSGRPTESTSCENARIO_H_ +#define NXSGRPTESTSCENARIO_H_ + +#include "nxstestscenario.h" + +namespace rs_nxs_test { + +class NxsGrpTestScenario : public NxsTestScenario { +public: + + typedef std::map ExpectedMap; + + NxsGrpTestScenario(); + virtual ~NxsGrpTestScenario(); + + bool checkTestPassed(); + bool checkDeepTestPassed(); + +protected: + + RsDataService* createDataStore(const RsPeerId& peerId); + virtual const ExpectedMap& getExpectedMap() = 0; +}; + +} /* namespace rs_nxs_test */ +#endif /* NXSGRPTESTSCENARIO_H_ */ diff --git a/tests/unittests/libretroshare/gxs/nxs_test/nxsmsgsync_test.h b/tests/unittests/libretroshare/gxs/nxs_test/nxsmsgsync_test.h index 49b06329f..761cc44a4 100644 --- a/tests/unittests/libretroshare/gxs/nxs_test/nxsmsgsync_test.h +++ b/tests/unittests/libretroshare/gxs/nxs_test/nxsmsgsync_test.h @@ -8,41 +8,39 @@ #ifndef NXSMSGSYNC_TEST_H_ #define NXSMSGSYNC_TEST_H_ +#include "nxstestscenario.h" - - class NxsMessageTest : public NxsTestScenario + class NxsMessageTest : public rs_nxs_test::NxsTestScenario { - public: - NxsMessageTest(uint16_t servtype); - virtual ~NxsMessageTest(); - std::string getTestName(); + NxsMessageTest(); + 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(); - RsGeneralDataService* getDataService(const RsPeerId& peer); - - /*! - * Call to remove files created - * in the test directory - */ - void cleanUp(); - - bool testPassed(); - - private: - void setUpDataBases(); - void populateStore(RsGeneralDataService* dStore); + RsServiceInfo getServiceInfo(); private: - std::string mTestName; - std::map mPeerStoreMap; - std::set mStoreNames; + std::list mPeerIds; + typedef std::map DataMap; + typedef std::map > ExpectedMap; + + DataMap mDataServices; + std::map mNxsNetMgrs; + RsGixsReputation* mRep; + RsGcxs* mCircles; + RsServiceInfo mServInfo; + + ExpectedMap mExpectedResult; + uint16_t mServType; - RsMutex mMsgTestMtx; - }; diff --git a/tests/unittests/libretroshare/gxs/nxs_test/nxstestscenario.h b/tests/unittests/libretroshare/gxs/nxs_test/nxstestscenario.h index fbc1b4ed5..3741d90b6 100644 --- a/tests/unittests/libretroshare/gxs/nxs_test/nxstestscenario.h +++ b/tests/unittests/libretroshare/gxs/nxs_test/nxstestscenario.h @@ -31,12 +31,14 @@ namespace rs_nxs_test virtual void getPeers(std::list& peerIds) = 0; virtual RsGeneralDataService* getDataService(const RsPeerId& peerId) = 0; virtual bool checkTestPassed() = 0; + virtual bool checkDeepTestPassed() = 0; virtual RsNxsNetMgr* getDummyNetManager(const RsPeerId& peerId) = 0; virtual RsGcxs* getDummyCircles(const RsPeerId& peerId) = 0; virtual RsGixsReputation* getDummyReputations(const RsPeerId& peerId) = 0; virtual uint16_t getServiceType() = 0; virtual RsServiceInfo getServiceInfo() = 0; + }; } diff --git a/tests/unittests/libretroshare/gxs/nxs_test/rsgxsnetservice_test.cc b/tests/unittests/libretroshare/gxs/nxs_test/rsgxsnetservice_test.cc index dd5c02ee1..519a42613 100644 --- a/tests/unittests/libretroshare/gxs/nxs_test/rsgxsnetservice_test.cc +++ b/tests/unittests/libretroshare/gxs/nxs_test/rsgxsnetservice_test.cc @@ -18,7 +18,7 @@ TEST(libretroshare_gxs, gxs_grp_sync) tHub.StartTest(); // wait for ten seconds - rs_nxs_test::NxsTestHub::Wait(10); + rs_nxs_test::NxsTestHub::Wait(15); tHub.EndTest(); diff --git a/tests/unittests/libretroshare/serialiser/rsbaseitem_test.cc b/tests/unittests/libretroshare/serialiser/rsbaseitem_test.cc index bfa818b79..fc5b1a88e 100644 --- a/tests/unittests/libretroshare/serialiser/rsbaseitem_test.cc +++ b/tests/unittests/libretroshare/serialiser/rsbaseitem_test.cc @@ -45,7 +45,7 @@ TEST(libretroshare_serialiser, RsTlvFileItem) /* initialise */ i1.filesize = 101010; - i1.hash = "ABCDEFEGHE"; + i1.hash = RsFileHash("ABCDEFEGHE"); i1.name = "TestFile.txt"; i1.pop = 12; i1.age = 456; @@ -103,7 +103,7 @@ TEST(libretroshare_serialiser, RsTlvFileSet) { RsTlvFileItem fi; fi.filesize = 16 + i * i; - fi.hash = "ABCDEF"; + fi.hash = RsFileHash("ABCDEF"); std::ostringstream out; out << "File" << i << "_inSet.txt"; fi.name = out.str(); @@ -130,7 +130,7 @@ TEST(libretroshare_serialiser, RsTlvFileData) /* initialise */ d1.file.filesize = 101010; - d1.file.hash = "ABCDEFEGHE"; + d1.file.hash = RsFileHash("ABCDEFEGHE"); d1.file.name = ""; d1.file.age = 0; d1.file.pop = 0; diff --git a/tests/unittests/libretroshare/serialiser/rsturtleitem_test.cc b/tests/unittests/libretroshare/serialiser/rsturtleitem_test.cc index 23157efe8..eff26d032 100644 --- a/tests/unittests/libretroshare/serialiser/rsturtleitem_test.cc +++ b/tests/unittests/libretroshare/serialiser/rsturtleitem_test.cc @@ -136,7 +136,7 @@ RsSerialType* init_item(RsTurtleOpenTunnelItem& item) item.depth = rand() ; item.request_id = rand() ; item.partial_tunnel_id = rand() ; - item.file_hash = std::string("c0edcfecc0844ef175d61dd589ab288d262b6bc8") ; + item.file_hash = RsFileHash("c0edcfecc0844ef175d61dd589ab288d262b6bc8") ; return new RsTurtleSerialiser(); } bool operator==(const RsTurtleOpenTunnelItem& it1,const RsTurtleOpenTunnelItem& it2) @@ -189,7 +189,7 @@ bool operator==(const RsTurtleStringSearchRequestItem& it1,const RsTurtleStringS } RsSerialType* init_item(TurtleFileInfo& info) { - info.hash = "3f753e8ac3b94ab9fddfad94480f747bf4418370"; + info.hash = RsFileHash("3f753e8ac3b94ab9fddfad94480f747bf4418370"); info.name = "toto.png"; info.size = 0x3392085443897ull ; return new RsTurtleSerialiser(); diff --git a/tests/unittests/libretroshare/serialiser/tlvstack_test.cc b/tests/unittests/libretroshare/serialiser/tlvstack_test.cc index 2abd661c9..0acba2709 100644 --- a/tests/unittests/libretroshare/serialiser/tlvstack_test.cc +++ b/tests/unittests/libretroshare/serialiser/tlvstack_test.cc @@ -63,13 +63,13 @@ TEST(libretroshare_serialiser, test_RsTlvStack) /* initialise */ fi1->filesize = 101010; - fi1->hash = "ABCDEFEGHE"; + fi1->hash = RsFileHash("ABCDEFEGHE"); fi1->name = "TestFile.txt"; fi1->pop = 12; fi1->age = 456; fi2->filesize = 101010; - fi2->hash = "ABCDEFEGHE"; + fi2->hash = RsFileHash("ABCDEFEGHE"); fi2->name = "TestFile.txt"; fi2->pop = 0; fi2->age = 0;; diff --git a/tests/unittests/libretroshare/serialiser/tlvtypes_test.cc b/tests/unittests/libretroshare/serialiser/tlvtypes_test.cc index 9f9fb3265..b4cfd1e33 100644 --- a/tests/unittests/libretroshare/serialiser/tlvtypes_test.cc +++ b/tests/unittests/libretroshare/serialiser/tlvtypes_test.cc @@ -47,7 +47,7 @@ TEST(libretroshare_serialiser, test_RsTlvFileItem) /* initialise */ i1.filesize = 101010; - i1.hash = "ABCDEFEGHE"; + i1.hash = RsFileHash("ABCDEFEGHE"); i1.name = "TestFile.txt"; i1.pop = 12; i1.age = 456; @@ -105,7 +105,7 @@ TEST(libretroshare_serialiser, test_RsTlvFileSet) { RsTlvFileItem fi; fi.filesize = 16 + i * i; - fi.hash = "ABCDEF"; + fi.hash = RsFileHash("ABCDEF"); std::ostringstream out; out << "File" << i << "_inSet.txt"; fi.name = out.str();