test_script_sender.cpp
Go to the documentation of this file.
1 // -- BEGIN LICENSE BLOCK ----------------------------------------------
2 // Copyright 2022 Universal Robots A/S
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 //
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 //
14 // * Neither the name of the {copyright_holder} nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 // POSSIBILITY OF SUCH DAMAGE.
29 // -- END LICENSE BLOCK ------------------------------------------------
30 
31 #include <gtest/gtest.h>
34 
35 using namespace urcl;
36 
37 class ScriptSenderTest : public ::testing::Test
38 {
39 protected:
40  class Client : public comm::TCPSocket
41  {
42  public:
43  Client(const int& port)
44  {
45  std::string host = "127.0.0.1";
46  TCPSocket::setup(host, port);
47  timeval tv;
48  tv.tv_sec = 1;
49  tv.tv_usec = 0;
50  TCPSocket::setReceiveTimeout(tv);
51  }
52 
53  void send(const std::string& text)
54  {
55  size_t len = text.size();
56  const uint8_t* data = reinterpret_cast<const uint8_t*>(text.c_str());
57  size_t written;
58  TCPSocket::write(data, len, written);
59  }
60 
61  std::string recv()
62  {
63  std::stringstream result;
64  char character;
65  size_t read_chars = 99;
66  while (read_chars > 0)
67  {
68  if (!TCPSocket::read((uint8_t*)&character, 1, read_chars))
69  {
70  break;
71  }
72  result << character;
73  if (character == '\n')
74  {
75  break;
76  }
77  }
78  return result.str();
79  }
80 
81  protected:
82  virtual bool open(int socket_fd, struct sockaddr* address, size_t address_len)
83  {
84  return ::connect(socket_fd, address, address_len) == 0;
85  }
86  };
87 
88  void SetUp()
89  {
90  program_ = "test program\n";
91  script_sender_.reset(new control::ScriptSender(50002, program_));
92  client_.reset(new Client(50002));
93  }
94 
95  void TearDown()
96  {
97  if (client_->getState() == comm::SocketState::Connected)
98  {
99  client_->close();
100  }
101  }
102 
103  std::unique_ptr<control::ScriptSender> script_sender_;
104  std::unique_ptr<Client> client_;
105  std::string program_;
106 };
107 
108 TEST_F(ScriptSenderTest, wrong_request_command)
109 {
110  std::string wrong_request = "request\n";
111  client_->send(wrong_request);
112 
113  // We shouldn't receive an answer when sending the wrong request to the server, the result should therefore be an
114  // empty string
115  std::string actual_answer = client_->recv();
116  std::string expected_answer = "";
117  EXPECT_EQ(expected_answer, actual_answer);
118 }
119 
120 TEST_F(ScriptSenderTest, request_program)
121 {
122  std::string request = "request_program\n";
123  client_->send(request);
124 
125  std::string received_program = client_->recv();
126  EXPECT_EQ(program_, received_program);
127 }
128 
129 int main(int argc, char* argv[])
130 {
131  ::testing::InitGoogleTest(&argc, argv);
132 
133  return RUN_ALL_TESTS();
134 }
std::unique_ptr< control::ScriptSender > script_sender_
TEST_F(ScriptSenderTest, wrong_request_command)
virtual bool open(int socket_fd, struct sockaddr *address, size_t address_len)
The ScriptSender class starts a TCPServer for a robot to connect to and waits for a request to receiv...
Definition: script_sender.h:47
void send(const std::string &text)
Class for TCP socket abstraction.
Definition: tcp_socket.h:48
Socket is connected and ready to use.
std::unique_ptr< Client > client_
int main(int argc, char *argv[])


ur_client_library
Author(s): Thomas Timm Andersen, Simon Rasmussen, Felix Exner, Lea Steffen, Tristan Schnell
autogenerated on Tue Jul 4 2023 02:09:47