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
|
/*
* Robot Testing Framework
*
* Copyright (C) 2015-2019 Istituto Italiano di Tecnologia (IIT)
*
* This library 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.
*
* This library 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 this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <robottestingframework/TestAssert.h>
#include <robottestingframework/TestRunner.h>
#include <robottestingframework/TestSuite.h>
#include <robottestingframework/WebProgressListener.h>
#include <robottestingframework/dll/Plugin.h>
#include <sstream>
#ifndef _WIN32
# include <netdb.h>
# include <netinet/in.h>
# include <sys/socket.h>
# include <sys/types.h>
# include <unistd.h>
#endif
using namespace robottestingframework;
class MyTest1 : public TestCase
{
public:
MyTest1() :
TestCase("MyTest1")
{
}
void run() override
{
ROBOTTESTINGFRAMEWORK_TEST_CHECK(3 < 5, "smaller");
}
};
class MyTest2 : public TestCase
{
public:
MyTest2() :
TestCase("MyTest2")
{
}
void run() override
{
ROBOTTESTINGFRAMEWORK_TEST_CHECK(5 == 3, "equal");
}
};
class WebProgListener : public TestCase
{
public:
WebProgListener() :
TestCase("WebProgListener")
{
}
void run() override
{
// create a test listener to collect the result
WebProgressListener web(6543, true);
// create a test result and add the listeners
TestResult result;
result.addListener(&web);
// create a test suite and the test cases
TestSuite suite("MyTestSuite");
MyTest1 test1;
MyTest2 test2;
suite.addTest(&test1);
suite.addTest(&test2);
// create a test runner
TestRunner runner;
runner.addTest(&suite);
runner.run(result);
// checking the web socket
struct sockaddr_in serv_addr;
struct hostent* server;
ROBOTTESTINGFRAMEWORK_TEST_REPORT("openning client socket");
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
#ifndef _WIN32
struct timeval tv;
tv.tv_sec = 3;
tv.tv_usec = 0;
setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (char*)&tv, sizeof(struct timeval));
#endif
ROBOTTESTINGFRAMEWORK_ASSERT_FAIL_IF_FALSE(sockfd >= 0, "opening socket");
server = gethostbyname("localhost");
ROBOTTESTINGFRAMEWORK_ASSERT_FAIL_IF_FALSE(server != nullptr, "no such host");
bzero((char*)&serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char*)server->h_addr, (char*)&serv_addr.sin_addr.s_addr, server->h_length);
serv_addr.sin_port = htons(6543);
ROBOTTESTINGFRAMEWORK_TEST_REPORT("Conneting to the web listener server");
ROBOTTESTINGFRAMEWORK_ASSERT_FAIL_IF_FALSE(connect(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) >= 0, "connecting");
char buffer[256] = "GET /status HTTP/1.0\r\n\r\n";
ROBOTTESTINGFRAMEWORK_TEST_REPORT("Writing to web listener server");
ROBOTTESTINGFRAMEWORK_ASSERT_FAIL_IF_FALSE(write(sockfd, buffer, strlen(buffer)) >= 0, "writing to socket");
bzero(buffer, 256);
ROBOTTESTINGFRAMEWORK_TEST_REPORT("Reading from web listener server");
ROBOTTESTINGFRAMEWORK_ASSERT_FAIL_IF_FALSE(read(sockfd, buffer, 255) > 0, "reading from socket");
std::stringstream ssbuff(buffer);
std::string line;
while (std::getline(ssbuff, line, '\n')) {
if (line.size() && line[0] == '{') {
break;
}
}
ROBOTTESTINGFRAMEWORK_TEST_CHECK(line.size() > 2, "Cheking the json result size returned by web listener");
line = line.substr(0, line.size() - 1);
ROBOTTESTINGFRAMEWORK_TEST_CHECK(line == "{\"name\":\"MyTestSuite\",\"testStatus\":[3,2]}",
"Cheking the json result returned by web listener");
close(sockfd);
}
};
ROBOTTESTINGFRAMEWORK_PREPARE_PLUGIN(WebProgListener)
|