You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

uoSocketClientServerBaseTest.cpp 3.8KB

3 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*********************************************************************
  2. * Software License Agreement (AGPL-3 License)
  3. *
  4. * OpenViBE SDK Test Software
  5. * Based on OpenViBE V1.1.0, Copyright (C) Inria, 2006-2015
  6. * Copyright (C) Inria, 2015-2017,V1.0
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program.
  19. * If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include "socket/IConnection.h"
  22. #include "socket/IConnectionClient.h"
  23. #include "socket/IConnectionServer.h"
  24. #include "ovtAssert.h"
  25. int uoSocketClientServerBaseTest(int argc, char* argv[])
  26. {
  27. OVT_ASSERT(argc == 3, "Failure to retrieve tests arguments. Expecting: server_name port_number");
  28. const std::string name = argv[1];
  29. char* end;
  30. const size_t port = strtol(argv[2], &end, 10);
  31. // basic tests on server and clients
  32. Socket::IConnectionServer* server = Socket::createConnectionServer();
  33. Socket::IConnectionClient* client = Socket::createConnectionClient();
  34. OVT_ASSERT(!server->isConnected(), "Failure to check for connection state before connection happens");
  35. OVT_ASSERT(server->listen(port) && server->isConnected(), "Failure to make socket listening for input connections");
  36. OVT_ASSERT(!server->listen(port), "Failure to generate connection error if the socket is already connected");
  37. OVT_ASSERT(!server->isReadyToReceive(), "Failure to check for readyness to receive when no client is connected");
  38. OVT_ASSERT(server->close() && !server->isConnected(), "Failure to close connection");
  39. OVT_ASSERT(!client->isConnected(), "Failure to check for connection state before connection happens");
  40. OVT_ASSERT(!client->connect(name.c_str(), port) && !client->isConnected(), "Failure to generate connection error due to no server currently running");
  41. OVT_ASSERT(server->listen(port), "Failure to make socket listening for input connections after a disconnection");
  42. OVT_ASSERT(!client->connect("bad_server_name", port) && !client->isConnected(), "Failure to generate connection error caused by wrong server name");
  43. OVT_ASSERT(!client->connect(name.c_str(), 0) && !client->isConnected(), "Failure to generate connection error caused by wrong port number");
  44. OVT_ASSERT(client->connect(name.c_str(), port) && client->isConnected(), "Failure to connect to server");
  45. OVT_ASSERT(client->close() && !client->isConnected(), "Failure to disconnect");
  46. // Test method getSocketPort
  47. size_t guessedPort;
  48. OVT_ASSERT(server->getSocketPort(guessedPort), "Failure to get socket informations");
  49. OVT_ASSERT(guessedPort == port, "Get Socket information should return server port.");
  50. OVT_ASSERT(client->connect(name.c_str(), guessedPort) && client->isConnected(), "Failure to connect to server");
  51. OVT_ASSERT(client->close() && !client->isConnected(), "Failure to disconnect");
  52. OVT_ASSERT(server->close() && !server->isConnected(), "Failure to close connection");
  53. // Test to connect using port 0
  54. OVT_ASSERT(server->listen(0) && server->isConnected(), "Failure to make socket listening for input connections");
  55. OVT_ASSERT(server->getSocketPort(guessedPort), "Failure to get socket informations");
  56. OVT_ASSERT(client->connect(name.c_str(), guessedPort) && client->isConnected(), "Failure to connect to server");
  57. OVT_ASSERT(client->close() && !client->isConnected(), "Failure to disconnect");
  58. OVT_ASSERT(server->close() && !server->isConnected(), "Failure to close connection");
  59. // Release ressources
  60. server->release();
  61. client->release();
  62. return EXIT_SUCCESS;
  63. }