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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
|
#include "TestSupport.h"
#include <boost/bind.hpp>
#include "ApplicationPool/Pool.h"
#include "ApplicationPool/Server.h"
#include "ApplicationPool/Client.h"
#include "Utils.h"
#include <string>
#include <cstring>
#include <unistd.h>
#include <errno.h>
using namespace Passenger;
using namespace Passenger::ApplicationPool;
using namespace boost;
using namespace std;
namespace tut {
struct ApplicationPool_ServerTest {
ServerInstanceDirPtr serverInstanceDir;
ServerInstanceDir::GenerationPtr generation;
string socketFilename;
AccountsDatabasePtr accountsDatabase;
AccountPtr clientAccount;
shared_ptr<MessageServer> messageServer;
shared_ptr<Pool> realPool;
shared_ptr<Server> poolServer;
shared_ptr<Client> pool, pool2;
shared_ptr<oxt::thread> serverThread;
~ApplicationPool_ServerTest() {
if (serverThread != NULL) {
serverThread->interrupt_and_join();
}
}
void initializePool() {
createServerInstanceDirAndGeneration(serverInstanceDir, generation);
socketFilename = generation->getPath() + "/socket";
accountsDatabase = ptr(new AccountsDatabase());
clientAccount = accountsDatabase->add("test", "12345", false);
messageServer = ptr(new MessageServer(socketFilename, accountsDatabase));
realPool = ptr(new Pool("../helper-scripts/passenger-spawn-server", generation));
poolServer = ptr(new Server(realPool));
messageServer->addHandler(poolServer);
serverThread = ptr(new oxt::thread(
boost::bind(&MessageServer::mainLoop, messageServer.get())
));
pool = ptr(new Client());
pool2 = ptr(new Client());
pool->connect(socketFilename, "test", "12345");
pool2->connect(socketFilename, "test", "12345");
}
SessionPtr spawnRackApp() {
PoolOptions options("stub/rack");
options.appType = "rack";
return pool->get(options);
}
/* A StringListCreator which not only returns a dummy value, but also
* increments a counter each time getItems() is called. */
class DummyStringListCreator: public StringListCreator {
public:
mutable int counter;
DummyStringListCreator() {
counter = 0;
}
virtual const StringListPtr getItems() const {
StringListPtr result = ptr(new StringList());
counter++;
result->push_back("hello");
result->push_back("world");
return result;
}
};
class SlowClient: public Client {
private:
unsigned int timeToSendUsername;
unsigned int timeToSendPassword;
protected:
virtual void sendUsername(MessageChannel &channel, const string &username) {
if (timeToSendUsername > 0) {
usleep(timeToSendUsername * 1000);
}
channel.writeScalar(username);
}
virtual void sendPassword(MessageChannel &channel, const StaticString &userSuppliedPassword) {
if (timeToSendPassword > 0) {
usleep(timeToSendPassword * 1000);
}
channel.writeScalar(userSuppliedPassword.c_str(), userSuppliedPassword.size());
}
public:
SlowClient(unsigned int timeToSendUsername,
unsigned int timeToSendPassword)
: Client()
{
this->timeToSendUsername = timeToSendUsername;
this->timeToSendPassword = timeToSendPassword;
}
};
};
DEFINE_TEST_GROUP(ApplicationPool_ServerTest);
TEST_METHOD(1) {
// When calling get() with a PoolOptions object,
// options.environmentVariables->getItems() isn't called unless
// the pool had to spawn something.
initializePool();
shared_ptr<DummyStringListCreator> strList = ptr(new DummyStringListCreator());
PoolOptions options("stub/rack");
options.appType = "rack";
options.environmentVariables = strList;
SessionPtr session1 = pool->get(options);
session1.reset();
ensure_equals("(1)", strList->counter, 1);
session1 = pool->get(options);
session1.reset();
ensure_equals("(2)", strList->counter, 1);
}
TEST_METHOD(5) {
// get() requires GET rights.
initializePool();
try {
clientAccount->setRights(Account::SET_PARAMETERS);
spawnRackApp();
fail("SecurityException expected");
} catch (const SecurityException &e) {
// Pass.
}
clientAccount->setRights(Account::GET);
spawnRackApp(); // Should not throw SecurityException now.
}
TEST_METHOD(6) {
// clear() requires CLEAR rights.
initializePool();
try {
clientAccount->setRights(Account::SET_PARAMETERS);
pool->clear();
fail("SecurityException expected");
} catch (const SecurityException &e) {
// Pass.
}
clientAccount->setRights(Account::CLEAR);
pool->clear(); // Should not throw SecurityException now.
}
TEST_METHOD(7) {
// setMaxIdleTime() requires SET_PARAMETERS rights.
initializePool();
try {
clientAccount->setRights(Account::GET_PARAMETERS);
pool->setMaxIdleTime(60);
fail("SecurityException expected");
} catch (const SecurityException &e) {
// Pass.
}
clientAccount->setRights(Account::SET_PARAMETERS);
pool->setMaxIdleTime(60); // Should not throw SecurityException now.
}
TEST_METHOD(8) {
// setMax() requires SET_PARAMETERS rights.
initializePool();
try {
clientAccount->setRights(Account::GET_PARAMETERS);
pool->setMax(60);
fail("SecurityException expected");
} catch (const SecurityException &e) {
// Pass.
}
clientAccount->setRights(Account::SET_PARAMETERS);
pool->setMax(60); // Should not throw SecurityException now.
}
TEST_METHOD(9) {
// getActive() requires GET_PARAMETERS rights.
initializePool();
try {
clientAccount->setRights(Account::SET_PARAMETERS);
pool->getActive();
fail("SecurityException expected");
} catch (const SecurityException &e) {
// Pass.
}
clientAccount->setRights(Account::GET_PARAMETERS);
pool->getActive(); // Should not throw SecurityException now.
}
TEST_METHOD(10) {
// getCount() requires GET_PARAMETERS rights.
initializePool();
try {
clientAccount->setRights(Account::SET_PARAMETERS);
pool->getCount();
fail("SecurityException expected");
} catch (const SecurityException &e) {
// Pass.
}
clientAccount->setRights(Account::GET_PARAMETERS);
pool->getCount(); // Should not throw SecurityException now.
}
TEST_METHOD(11) {
// setMaxPerApp() requires SET_PARAMETERS rights.
initializePool();
try {
clientAccount->setRights(Account::GET_PARAMETERS);
pool->setMaxPerApp(2);
fail("SecurityException expected");
} catch (const SecurityException &e) {
// Pass.
}
clientAccount->setRights(Account::SET_PARAMETERS);
pool->setMaxPerApp(2); // Should not throw SecurityException now.
}
TEST_METHOD(12) {
// getSpawnServerPid() requires GET_PARAMETERS rights.
initializePool();
try {
clientAccount->setRights(Account::SET_PARAMETERS);
pool->getSpawnServerPid();
fail("SecurityException expected");
} catch (const SecurityException &e) {
// Pass.
}
clientAccount->setRights(Account::GET_PARAMETERS);
pool->getSpawnServerPid(); // Should not throw SecurityException now.
}
TEST_METHOD(13) {
// inspect() requires INSPECT_BASIC_INFO rights.
initializePool();
try {
clientAccount->setRights(Account::SET_PARAMETERS);
pool->inspect();
fail("SecurityException expected");
} catch (const SecurityException &e) {
// Pass.
}
clientAccount->setRights(Account::INSPECT_BASIC_INFO);
pool->inspect(); // Should not throw SecurityException now.
}
TEST_METHOD(14) {
// toXml() requires INSPECT_BASIC_INFO rights.
initializePool();
try {
clientAccount->setRights(Account::SET_PARAMETERS);
pool->toXml();
fail("SecurityException expected");
} catch (const SecurityException &e) {
// Pass.
}
clientAccount->setRights(Account::INSPECT_BASIC_INFO);
pool->toXml(); // Should not throw SecurityException now.
}
TEST_METHOD(15) {
// toXml() only prints private information if the client has the INSPECT_SENSITIVE_INFO right.
initializePool();
PoolOptions options("stub/rack");
options.appType = "rack";
pool->get(options);
clientAccount->setRights(Account::INSPECT_BASIC_INFO);
ensure("Does not contain private information", pool->toXml().find("<server_sockets>") == string::npos);
clientAccount->setRights(Account::INSPECT_BASIC_INFO | Account::INSPECT_SENSITIVE_INFO);
ensure("Contains private information", pool->toXml().find("<server_sockets>") != string::npos);
}
}
|