123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- #include <util/pid_file.h>
- #include <gtest/gtest.h>
- #include <fstream>
- #include <signal.h>
- #include <stdint.h>
- namespace {
- using namespace isc::util;
- const char* TESTNAME = "pid_file.test";
- class PIDFileTest : public ::testing::Test {
- public:
-
-
-
-
-
- static std::string absolutePath(const std::string& filename);
-
-
-
-
-
-
- int randomizePID(const uint32_t start, const uint32_t range) {
- int pid;
- for (pid = (random() % range) + start;
- kill(pid, 0) == 0;
- ++pid)
- ;
- return (pid);
- }
- protected:
-
- virtual void SetUp() {
- removeTestFile();
- }
-
- virtual void TearDown() {
- removeTestFile();
- }
- private:
-
- void removeTestFile() const {
- static_cast<void>(remove(absolutePath(TESTNAME).c_str()));
- }
- };
- std::string
- PIDFileTest::absolutePath(const std::string& filename) {
- std::ostringstream s;
- s << TEST_DATA_BUILDDIR << "/" << filename;
- return (s.str());
- }
- TEST_F(PIDFileTest, writeAndDelete) {
- PIDFile pid_file(absolutePath(TESTNAME));
- std::ifstream fs;
- int pid(0);
-
- pid_file.write(10);
-
- fs.open(absolutePath(TESTNAME).c_str(), std::ifstream::in);
- fs >> pid;
- EXPECT_TRUE(fs.good());
- EXPECT_EQ(pid, 10);
- fs.close();
-
- pid_file.write(20);
-
- fs.open(absolutePath(TESTNAME).c_str(), std::ifstream::in);
- fs >> pid;
- EXPECT_TRUE(fs.good());
- EXPECT_EQ(pid, 20);
- fs.close();
-
- pid_file.deleteFile();
-
- fs.open(absolutePath(TESTNAME).c_str(), std::ifstream::in);
- EXPECT_FALSE(fs.good());
- fs.close();
- }
- TEST_F(PIDFileTest, pidInUse) {
- PIDFile pid_file(absolutePath(TESTNAME));
-
- pid_file.write();
-
- EXPECT_EQ(getpid(), pid_file.check());
- }
- TEST_F(PIDFileTest, pidNotInUse) {
- PIDFile pid_file(absolutePath(TESTNAME));
- int pid;
-
- pid = randomizePID(10000, 10000);
-
- pid_file.write(pid);
-
- if (pid_file.check() == 0) {
- return;
- }
-
- pid = randomizePID(10000, 40000);
-
- pid_file.write(pid);
-
- EXPECT_EQ(0, pid_file.check());
- }
- TEST_F(PIDFileTest, pidGarbage) {
- PIDFile pid_file(absolutePath(TESTNAME));
- std::ofstream fs;
-
- fs.open(absolutePath(TESTNAME).c_str(), std::ofstream::out);
- fs << "text" << std::endl;
- fs.close();
-
- EXPECT_THROW(pid_file.check(), PIDCantReadPID);
- }
- TEST_F(PIDFileTest, pidWriteFail) {
- PIDFile pid_file(absolutePath(TESTNAME));
-
-
- pid_file.write(10);
- chmod(absolutePath(TESTNAME).c_str(), S_IRUSR);
-
- EXPECT_THROW(pid_file.write(10), PIDFileError);
-
- chmod(absolutePath(TESTNAME).c_str(), S_IRUSR | S_IWUSR);
- }
- TEST_F(PIDFileTest, noDeleteFile) {
- PIDFile pid_file(absolutePath(TESTNAME));
-
- pid_file.deleteFile();
- }
- }
|