diff --git a/Makefile b/Makefile index 58019c3..f5af7c8 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,10 @@ all: src src: $(MAKE) -C src -test: src +test-fw: src + $(MAKE) -C test/fw_dist_test + +test: src test-fw $(MAKE) -C test clean: diff --git a/test/fw_dist_test/Makefile b/test/fw_dist_test/Makefile new file mode 100644 index 0000000..21de79e --- /dev/null +++ b/test/fw_dist_test/Makefile @@ -0,0 +1,30 @@ + +LDLIBS=-lstdc++ -lpEpAdapter +LDFLAGS=-L../../src/ +CXXFLAGS=-std=c++11 -g +CXXFLAGS+=-I./src +TEST_EXTRA_OBJS=../framework/utils.o + +# Src +SRC=$(wildcard src/*.cc) +OBJ=$(subst .cc,.o,$(SRC)) + +# Tests +TEST_SRC=$(wildcard test/*.cc) +TEST_OBJ=$(subst .cc,,$(TEST_SRC)) + + +.PHONY: all clean rmtestdata +.DEFAULT_GOAL := all + +all: $(TEST_OBJ) + +$(TEST_OBJ): $(OBJ) $(TEST_EXTRA_OBJS) + + +clean: + rm -f $(OBJ) + rm -f $(TEST_OBJ) + rm -rf src/*.dSYM + rm -rf test/*.dSYM + diff --git a/test/fw_dist_test/src/fw_dist_test.cc b/test/fw_dist_test/src/fw_dist_test.cc new file mode 100644 index 0000000..fe63a94 --- /dev/null +++ b/test/fw_dist_test/src/fw_dist_test.cc @@ -0,0 +1,80 @@ +#include "fw_dist_test.hh" +#include "../../../src/std_utils.hh" +#include "../../framework/utils.hh" +#include +#include +#include +#include + +using namespace std; +namespace pEp { + namespace Test { + string dummy; + + DistTest::DistTest() : mode(TestMode::ALICE) + { + alice_home = data_dir + "/" + "alice/"; + bob_home = data_dir + "/" + "bob/"; + data_dir_recreate(); + } + + void DistTest::run() + { + if (mode == TestMode::ALICE) { + alice_main(); + } else if (mode == TestMode::ALICE_BOB) { + pid_t pid; + pid = fork(); + + if (pid == pid_t(0)) { + cout << "Child PID:" << pid << endl; + run_alice_main(); + } else if (pid > pid_t(0)) { + cout << "Parent PID:" << pid << endl; + run_bob_main(); + } else { + cerr << "error forking" << endl; + } + } + } + + void DistTest::run_alice_main() + { + cout << "New process for Alice" << endl; + setenv("HOME", alice_home.c_str(), 1); + mkdir(alice_home.c_str(), 0770); + cout << "HOME: " << getenv("HOME") << endl; + alice_main(); + } + + void DistTest::run_bob_main() + { + cout << "New process for bob" << endl; + setenv("HOME", bob_home.c_str(), 1); + mkdir(bob_home.c_str(), 0770); + cout << "HOME: " << getenv("HOME") << endl; + bob_main(); + } + + + void DistTest::data_dir_delete() + { + try { + Utils::path_delete_all(data_dir); + } catch (const exception& e) { + TESTLOG("DistTest: - could not delete data dir: " + data_dir); + } + } + + void DistTest::data_dir_create() + { + Utils::dir_create(data_dir); + } + + void DistTest::data_dir_recreate() { + data_dir_delete(); + data_dir_create(); + }; + + } // namespace Test +} // namespace pEp diff --git a/test/fw_dist_test/src/fw_dist_test.hh b/test/fw_dist_test/src/fw_dist_test.hh new file mode 100644 index 0000000..4ad0b9b --- /dev/null +++ b/test/fw_dist_test/src/fw_dist_test.hh @@ -0,0 +1,43 @@ +// This file is under GNU General Public License 3.0 +// see LICENSE.txt + +#ifndef LIBPEPADAPTER_FW_DIST_TEST_HH +#define LIBPEPADAPTER_FW_DIST_TEST_HH + +#include +#include + +namespace pEp { + namespace Test { + + class DistTest { + public: + DistTest(); + + enum class TestMode + { + ALICE, + ALICE_BOB + }; + TestMode mode; + + std::string data_dir = "./data"; + std::string alice_home; + std::string bob_home; + + std::function alice_main{}; + std::function bob_main{}; + void run(); + + private: + void run_alice_main(); + void run_bob_main(); + + void data_dir_delete(); + void data_dir_create(); + void data_dir_recreate(); + }; + }; // namespace Test +}; // namespace pEp + +#endif // LIBPEPADAPTER_FW_DIST_TEST_HH diff --git a/test/fw_dist_test/test/test_fw_dist_test.cc b/test/fw_dist_test/test/test_fw_dist_test.cc new file mode 100644 index 0000000..91086b6 --- /dev/null +++ b/test/fw_dist_test/test/test_fw_dist_test.cc @@ -0,0 +1,40 @@ +#include "fw_dist_test.hh" +#include "../../framework/utils.hh" + +#include + +using namespace std; +using namespace pEp; + +Test::DistTest test_fw; + + +void alice_main() +{ + cout << "HYA FROM ALICE" << endl; + while (true) { + cout << "1" << endl; + sleep_millis(1000); + } +} + +void bob_main() +{ + cout << "HYA FROM BOB" << endl; + while (true) { + cout << "2" << endl; + sleep_millis(1000); + } +} + + +int main(int argc, char* argv[]) +{ + test_fw.mode = Test::DistTest::TestMode::ALICE_BOB; + test_fw.alice_main = &::alice_main; + test_fw.bob_main = &::bob_main; + test_fw.run(); + + + cout << "HDUFGD" << endl; +} \ No newline at end of file