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
|
/***********************************************************************
test/uds.cpp - Tests the Unix domain socket verifier in
UnixDomainSocketConnection. This test always succeeds on Windows!
Copyright (c) 2007-2008 by Educational Technology Resources, Inc.
Others may also hold copyrights on code in this file. See the
CREDITS.txt file in the top directory of the distribution for details.
This file is part of MySQL++.
MySQL++ is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
MySQL++ is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public
License along with MySQL++; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
USA
***********************************************************************/
#include <connection.h>
#include <exceptions.h>
#include <iostream>
#include <sstream>
#include <string>
#if !defined(MYSQLPP_PLATFORM_WINDOWS)
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/un.h>
#if !defined(AF_LOCAL)
# define AF_LOCAL AF_UNIX
#endif
#include <errno.h>
#include <string.h>
static const char* success_path = "test_uds_success.sock";
static const char* failure_path = "test_uds_failure.sock";
static int
make_socket(const char* path, mode_t mode)
{
// Just in case a socket with this name exists already, try to
// remove it. Only a failure if it exists and we can't remove it.
if ((unlink(path) < 0) && (errno != ENOENT)) {
return -1;
}
// Create the domain socket
int fd = socket(AF_LOCAL, SOCK_STREAM, 0);
if (fd < 0) {
return -1;
}
// Bind the socket to the named file
struct sockaddr_un saun;
memset(&saun, 0, sizeof(saun));
saun.sun_family = AF_LOCAL;
strncpy(saun.sun_path, path, sizeof(saun.sun_path));
saun.sun_path[sizeof(saun.sun_path) - 1] = '\0';
if (bind(fd, reinterpret_cast<sockaddr*>(&saun), sizeof(saun)) < 0) {
return -1;
}
// Change the socket's mode as requested
if (chmod(path, mode) < 0) {
return -1;
}
return fd;
}
static void
test_success()
{
std::string error;
int fd = make_socket(success_path, S_IREAD | S_IWRITE);
if (fd >= 0) {
bool fail = !mysqlpp::UnixDomainSocketConnection::is_socket(
success_path, &error);
if (fail) {
throw mysqlpp::SelfTestFailed(error);
}
}
else {
std::ostringstream outs;
outs << "Failed to create test domain socket: " << strerror(errno);
throw mysqlpp::SelfTestFailed(outs.str());
}
}
static void
test_failure()
{
int fd = make_socket(failure_path, S_IREAD);
if (fd < 0) {
std::ostringstream outs;
outs << "Failed to create test domain socket: " << strerror(errno);
throw mysqlpp::SelfTestFailed(outs.str());
}
if (mysqlpp::UnixDomainSocketConnection::is_socket(failure_path)) {
throw mysqlpp::SelfTestFailed("Failed to fail on read-only socket");
}
else if (mysqlpp::UnixDomainSocketConnection::is_socket(
"BogusBogus.sock")) {
throw mysqlpp::SelfTestFailed("Failed to fail on bad file name");
}
else {
close(fd);
unlink(failure_path);
fd = creat(failure_path, S_IREAD | S_IWRITE);
bool success = mysqlpp::UnixDomainSocketConnection::is_socket(
failure_path);
if (success) {
throw mysqlpp::SelfTestFailed("Failed to fail on non-socket");
}
}
}
#endif
int
main()
{
#if defined(MYSQLPP_PLATFORM_WINDOWS)
// Test not appropriate to this platform. Always succeed.
return 0;
#else
try {
test_success();
unlink(success_path);
test_failure();
unlink(failure_path);
return 0;
}
catch (mysqlpp::SelfTestFailed& e) {
std::cerr << "TCP address parse error: " << e.what() << std::endl;
return 1;
}
catch (std::exception& e) {
std::cerr << "Unexpected test failure: " << e.what() << std::endl;
return 2;
}
#endif
}
|