Remove code duplication in test fixtures
[libt2n] / test / test_fixtures.hxx
index 882457c..255202e 100644 (file)
@@ -26,11 +26,40 @@ on this file might be covered by the GNU General Public License.
 #include <sys/types.h>
 #include <signal.h>
 
+#include <socket_server.hxx>
+#include <string>
+
 class KillChildOnShutdownFixture
 {
 protected:
     pid_t child_pid;
 
+    // this is an evil hack to get access to real_write, don't ever do this in an app!!!
+    class real_write_connection: public libt2n::socket_server_connection
+    {
+        public:
+            void real_write(const std::string& data)
+                { socket_write(data); }
+    };
+
+    //
+    // common functions used by some tests
+    //
+    void send_hello(std::string hello_string, libt2n::socket_server* ss, int conn_id)
+    {
+        libt2n::server_connection *sc=ss->get_connection(conn_id);
+        sc->write(hello_string);
+    }
+
+    void send_raw_socket(std::string hello_string, libt2n::socket_server* ss, int conn_id)
+    {
+        libt2n::socket_server_connection *ssc=dynamic_cast<libt2n::socket_server_connection*>(ss->get_connection(conn_id));
+
+        // this is an evil hack to get access to real_write, don't ever do this in an app!!!
+        real_write_connection *rwc=(real_write_connection*)ssc;
+        rwc->real_write(hello_string);
+    }
+
 public:
     KillChildOnShutdownFixture()
         : child_pid(0)