1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
#include "TestSupport.h"
#include <dirent.h>
#include <unistd.h>
#include <fcntl.h>
#include <pwd.h>
#include <grp.h>
#include <cassert>
#include <BackgroundEventLoop.cpp>
#include <Utils/IOUtils.h>
#include <Utils/ScopeGuard.h>
#include <json/json.h>
namespace TestSupport {
ResourceLocator *resourceLocator = NULL;
Json::Value testConfig;
void createServerInstanceDirAndGeneration(ServerInstanceDirPtr &serverInstanceDir,
ServerInstanceDir::GenerationPtr &generation)
{
string path = "/tmp/passenger-test." + toString(getpid());
serverInstanceDir.reset(new ServerInstanceDir(path));
generation = serverInstanceDir->newGeneration(geteuid() == 0,
"nobody", getPrimaryGroupName("nobody"),
geteuid(), getegid());
}
void
writeUntilFull(int fd) {
int flags, ret;
char buf[1024];
memset(buf, 0, sizeof(buf));
flags = fcntl(fd, F_GETFL);
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
while (true) {
ret = write(fd, buf, sizeof(buf));
if (ret == -1) {
int e = errno;
if (e == EAGAIN) {
break;
} else {
throw SystemException("write() failed", e);
}
}
}
while (true) {
ret = write(fd, buf, 50);
if (ret == -1) {
int e = errno;
if (e == EAGAIN) {
break;
} else {
throw SystemException("write() failed", e);
}
}
}
while (true) {
ret = write(fd, buf, 1);
if (ret == -1) {
int e = errno;
if (e == EAGAIN) {
break;
} else {
throw SystemException("write() failed", e);
}
}
}
fcntl(fd, F_SETFL, flags);
}
void
replaceStringInFile(const char *filename, const string &toFind, const string &replaceWith) {
string content = readAll(filename);
FILE *f = fopen(filename, "w");
if (f == NULL) {
int e = errno;
string message = "Cannot open file '";
message.append(filename);
message.append("' for writing");
throw FileSystemException(message, e, filename);
} else {
StdioGuard guard(f);
content = replaceString(content, toFind, replaceWith);
fwrite(content.data(), 1, content.size(), f);
}
}
bool
containsSubstring(const StaticString &str, const StaticString &substr) {
return str.find(substr) != string::npos;
}
void
writeFile(const string &filename, const string &contents) {
FILE *f = fopen(filename.c_str(), "w");
if (f == NULL) {
int e = errno;
string message = "Cannot open file '";
message.append(filename);
message.append("' for writing");
throw FileSystemException(message, e, filename);
} else {
StdioGuard guard(f);
fwrite(contents.data(), 1, contents.size(), f);
}
}
void
touchFile(const char *filename, time_t timestamp) {
FILE *f = fopen(filename, "a");
if (f != NULL) {
fclose(f);
} else if (errno != EISDIR) {
int e = errno;
string message = "Cannot touch file '";
message.append(filename);
message.append("'");
throw FileSystemException(message, e, filename);
}
if (timestamp != (time_t) -1) {
struct utimbuf times;
times.actime = timestamp;
times.modtime = timestamp;
utime(filename, ×);
}
}
vector<string>
listDir(const string &path) {
vector<string> result;
DIR *d = opendir(path.c_str());
struct dirent *ent;
if (d == NULL) {
int e = errno;
throw FileSystemException("Cannot open directory " + path,
e, path);
}
while ((ent = readdir(d)) != NULL) {
if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) {
continue;
}
result.push_back(ent->d_name);
}
return result;
}
string
getPrimaryGroupName(const string &username) {
struct passwd *user;
struct group *group;
user = getpwnam(username.c_str());
if (user == NULL) {
throw RuntimeException(string("User '") + username + "' does not exist.");
}
group = getgrgid(user->pw_gid);
if (group == NULL) {
throw RuntimeException(string("Primary group for user '") + username + "' does not exist.");
}
return group->gr_name;
}
} // namespace TestSupport
|