summaryrefslogtreecommitdiff
path: root/i/marvin/src/socket/test_socket_databuffer.cc
diff options
context:
space:
mode:
Diffstat (limited to 'i/marvin/src/socket/test_socket_databuffer.cc')
-rw-r--r--i/marvin/src/socket/test_socket_databuffer.cc125
1 files changed, 125 insertions, 0 deletions
diff --git a/i/marvin/src/socket/test_socket_databuffer.cc b/i/marvin/src/socket/test_socket_databuffer.cc
new file mode 100644
index 0000000..f6f1a40
--- /dev/null
+++ b/i/marvin/src/socket/test_socket_databuffer.cc
@@ -0,0 +1,125 @@
+/* test_socket.cc - Programme de test des sockets. */
+/* Simulotron - Programme de simulation de robot {{{
+ *
+ * Copyright (C) 2005 Nicolas Haller
+ *
+ * Robot APB Team/Efrei 2005.
+ * Web: http://assos.efrei.fr/robot/
+ * Email: robot AT efrei DOT fr
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * }}} */
+#include "socket_server.hh"
+#include "socket_databuffer.hh"
+#include "data/data_buffer.hh"
+#include "utils/errno_exception.hh"
+
+#include <iostream>
+#include <sys/wait.h>
+
+//Chaine et char de test
+const std::string strDepart = "J'aime la prog système.";
+const char a = 'f';
+DataBuffer db1, db2;
+
+int testSockServ(SocketServer & sockServ);
+int testSockClient(int pauseMode);
+
+int main(void)
+{
+ pid_t pid;
+ int resultServer = 0, resultClient;
+ // Pause mode pour débugger le fork
+ int pauseMode = 0;
+
+ // Création de la socket serveur;
+ SocketServer sockServ(std::string(),4242);
+ // Mise en écoute du socket serveur
+ sockServ.listen(12);
+
+ // on fork pour traiter le client et le serveur
+ pid = fork();
+ if(pid == 0) //Processus fils
+ testSockClient(pauseMode);
+ else //Processus parent
+ resultServer = testSockServ(sockServ);
+
+ // On analyse les résultats
+ wait(&resultClient);
+ if (!(WIFEXITED(resultClient) && WEXITSTATUS(resultClient) == 0))
+ {
+ std::cerr << "ECHEC: Problème coté client" << std::endl;
+ exit (-1);
+ }
+ if (resultServer != 0)
+ {
+ std::cerr << "ECHEC: Problème coté serveur" << std::endl;
+ exit (-1);
+ }
+
+ return 0;
+}
+
+int testSockServ(SocketServer & sockServ)
+{
+ try
+ {
+ // On crée le DataBuffer
+ db1.write(reinterpret_cast<const uint8_t *>(strDepart.c_str()),
+ strDepart.size());
+ db1.write(reinterpret_cast<const uint8_t *>(&a), sizeof(const char));
+ // Acceptation d'une connection
+ SocketDataBuffer sockFork(sockServ);
+ // On écrit la DataBuffer dans la socket
+ sockFork.write(db1);
+ }
+ catch(errno_exception & chier)
+ {
+ std::cout << "CHIER !! Une exception a été lancé coté serveur!!" << std::endl;
+ std::cout << chier.what() << std::endl;
+ return 1;
+ }
+ return 0;
+}
+
+int testSockClient(int pauseMode)
+{
+ // Chance pour debbuger le fork
+ while(pauseMode)
+ sleep(10);
+ try
+ {
+ // Création de la socket client
+ SocketDataBuffer sockDataBuffer;
+ // Demande de connection
+ sockDataBuffer.connect(std::string("127.0.0.1"), 4242);
+ // Reception du message serveur
+ sockDataBuffer.read(db2, true);
+ // Paye ta comparaison de DataBuffer
+ if(db1.size() != db2.size())
+ {
+ std::cout << "CHIER!! Les DB ne font pas la même size" << std::endl;
+ exit (1);
+ }
+ }
+ catch(errno_exception & chier)
+ {
+ std::cout << "CHIER !! Une exception a été lancé coté client!!" << std::endl;
+ std::cout << chier.what() << std::endl;
+ exit (1);
+ }
+ exit (0);
+}