瀏覽代碼

More network tests

Kajetan Johannes Hammerle 11 月之前
父節點
當前提交
67f259f022
共有 6 個文件被更改,包括 151 次插入10 次删除
  1. 1 0
      CMakeLists.txt
  2. 1 0
      include/core/Network.h
  3. 14 0
      src/ErrorSimulator.c
  4. 12 0
      src/ErrorSimulator.h
  5. 28 10
      src/Network.c
  6. 95 0
      test/modules/NetworkTests.c

+ 1 - 0
CMakeLists.txt

@@ -30,6 +30,7 @@ else()
         set(LINK_OPTIONS ${COMPILE_OPTIONS})
     endif()
     set(LOG_LEVEL 4)
+    list(APPEND SRC "src/ErrorSimulator.c")
 endif()
 
 if(CMAKE_C_COMPILER_ID STREQUAL "GNU")

+ 1 - 0
include/core/Network.h

@@ -52,6 +52,7 @@ typedef void (*CoreClientOnPacket)(CoreInPacket*);
 bool coreClientStart(void);
 void coreClientStop(void);
 bool coreClientConnect(const char* server, CorePort port, int timeoutTicks);
+void coreClientTimeout(u32 timeout, u32 timeoutMin, u32 timeoutMax);
 void coreClientDisconnect(int timeoutTicks);
 void coreClientSend(const CoreOutPacket* p, CorePacketSendMode mode);
 void coreClientTick(void);

+ 14 - 0
src/ErrorSimulator.c

@@ -0,0 +1,14 @@
+#ifdef ERROR_SIMULATOR
+
+#include "ErrorSimulator.h"
+
+int coreFailCounter = -1;
+
+bool coreFail(void) {
+    if(coreFailCounter < 0) {
+        return false;
+    }
+    return coreFailCounter == 0 ? true : coreFailCounter-- == 0;
+}
+
+#endif

+ 12 - 0
src/ErrorSimulator.h

@@ -0,0 +1,12 @@
+#ifndef CORE_ERROR_SIMULATOR_H
+#define CORE_ERROR_SIMULATOR_H
+
+#ifdef ERROR_SIMULATOR
+extern int coreFailCounter;
+bool coreFail(void);
+#define CORE_FAIL(x, e) (coreFail() ? e : (x))
+#else
+#define CORE_FAIL(x, e) x
+#endif
+
+#endif

+ 28 - 10
src/Network.c

@@ -7,6 +7,8 @@
 #include <enet.h>
 #include <string.h>
 
+#include "ErrorSimulator.h"
+
 typedef CoreOutPacket OutPacket;
 
 void coreInitInPacket(CoreInPacket* in, const void* data, size_t n) {
@@ -188,7 +190,7 @@ OutPacket* coreOutPacketWrite(OutPacket* out, const void* buffer, size_t n) {
 static int enetCounter = 0;
 
 static bool addENet(void) {
-    if(enetCounter == 0 && enet_initialize() != 0) {
+    if(enetCounter == 0 && CORE_FAIL(enet_initialize() != 0, true)) {
         return true;
     }
     enetCounter++;
@@ -233,7 +235,7 @@ bool coreClientStart(void) {
         CORE_LOG_ERROR("Client cannot initialize enet");
         return true;
     }
-    client.client = enet_host_create(nullptr, 1, 2, 0, 0);
+    client.client = CORE_FAIL(enet_host_create(nullptr, 1, 2, 0, 0), nullptr);
     if(client.client == nullptr) {
         coreClientStop();
         CORE_LOG_ERROR("Cannot create enet client host");
@@ -270,7 +272,8 @@ bool coreClientConnect(const char* server, CorePort port, int timeoutTicks) {
     enet_address_set_host(&address, server);
     address.port = port;
 
-    client.connection = enet_host_connect(client.client, &address, 3, 0);
+    client.connection =
+        CORE_FAIL(enet_host_connect(client.client, &address, 3, 0), nullptr);
     if(client.connection == nullptr) {
         CORE_LOG_ERROR("Cannot create connection");
         return true;
@@ -280,6 +283,12 @@ bool coreClientConnect(const char* server, CorePort port, int timeoutTicks) {
     return false;
 }
 
+void coreClientTimeout(u32 timeout, u32 timeoutMin, u32 timeoutMax) {
+    if(client.connection != nullptr) {
+        enet_peer_timeout(client.connection, timeout, timeoutMin, timeoutMax);
+    }
+}
+
 void coreClientDisconnect(int timeoutTicks) {
     if(client.connection == nullptr) {
         return;
@@ -302,12 +311,9 @@ void coreClientSend(const OutPacket* p, CorePacketSendMode mode) {
                    enet_packet_create(p->data.buffer, p->data.size, flags[i]));
 }
 
-void coreClientTick(void) {
-    if(client.client == nullptr) {
-        return;
-    }
+static void tickClientEvents(void) {
     ENetEvent e;
-    while(enet_host_service(client.client, &e, 0) > 0) {
+    while(enet_host_service(client.client, &e, 0) >= 0) {
         switch(e.type) {
             case ENET_EVENT_TYPE_CONNECT:
                 client.connectTicks = -1;
@@ -316,10 +322,10 @@ void coreClientTick(void) {
             case ENET_EVENT_TYPE_DISCONNECT_TIMEOUT:
             case ENET_EVENT_TYPE_DISCONNECT:
                 client.disconnectTicks = 0;
+                client.connectTicks = 0;
                 client.onDisconnect();
                 client.connection = nullptr;
                 break;
-            case ENET_EVENT_TYPE_NONE: return;
             case ENET_EVENT_TYPE_RECEIVE: {
                 CoreInPacket in;
                 coreInitInPacket(&in, e.packet->data, e.packet->dataLength);
@@ -327,8 +333,16 @@ void coreClientTick(void) {
                 enet_packet_destroy(e.packet);
                 break;
             }
+            case ENET_EVENT_TYPE_NONE: return;
         }
     }
+}
+
+void coreClientTick(void) {
+    if(client.client == nullptr) {
+        return;
+    }
+    tickClientEvents();
     if(client.connectTicks >= 1 &&
        ++client.connectTicks > client.connectTimeoutTicks) {
         client.connectTicks = 0;
@@ -402,7 +416,8 @@ bool coreServerStart(CorePort port, size_t maxClients) {
     }
 
     ENetAddress address = {.host = ENET_HOST_ANY, .port = port};
-    server.server = enet_host_create(&address, maxClients, 3, 0, 0);
+    server.server =
+        CORE_FAIL(enet_host_create(&address, maxClients, 3, 0, 0), nullptr);
     if(server.server == nullptr) {
         coreServerStop();
         CORE_LOG_ERROR("Cannot create enet server host");
@@ -518,6 +533,9 @@ void coreServerSend(CoreClient clientId, const CoreOutPacket* p,
 }
 
 void coreServerDisconnect(CoreClient clientId) {
+    if(server.server == nullptr) {
+        return;
+    }
     ENetPeer** peer = coreHashMapSearchPointer(&server.clients, &clientId);
     if(peer != nullptr) {
         enet_peer_disconnect(*peer, 0);

+ 95 - 0
test/modules/NetworkTests.c

@@ -1,6 +1,8 @@
+#include <core/Utility.h>
 #include <string.h>
 
 #include "../Tests.h"
+#include "../src/ErrorSimulator.h"
 #include "core/Network.h"
 
 #define TEST_READ(Type, type, value)                                           \
@@ -322,6 +324,93 @@ static void testStop(void) {
     CORE_TEST_TRUE(disconnected);
 }
 
+static void testClientStartFails(void) {
+    CORE_TEST_FALSE(coreClientStart());
+    CORE_TEST_TRUE(coreClientStart());
+    coreClientStop();
+#ifdef ERROR_SIMULATOR
+    coreFailCounter = 0;
+    CORE_TEST_TRUE(coreClientStart());
+    coreFailCounter = 1;
+    CORE_TEST_TRUE(coreClientStart());
+    coreFailCounter = -1;
+#endif
+}
+
+static void testClientConnectionFails(void) {
+    coreClientResetHandler();
+    CORE_TEST_TRUE(coreClientConnect("", 54321, 100));
+    CORE_TEST_FALSE(coreClientStart());
+#ifdef ERROR_SIMULATOR
+    coreFailCounter = 0;
+    CORE_TEST_TRUE(coreClientConnect("", 54321, 100));
+    coreFailCounter = -1;
+#endif
+    CORE_TEST_FALSE(coreClientConnect("", 54321, 100));
+    CORE_TEST_TRUE(coreClientConnect("", 54321, 100));
+    tickClient(100);
+    coreClientStop();
+}
+
+static void testInvalidClientAccess(void) {
+    coreClientDisconnect(0);
+    coreClientSend(nullptr, 0);
+    coreClientTick();
+}
+
+static void testServerStartFails(void) {
+    CORE_TEST_TRUE(coreServerStart(54321, 0));
+#ifdef ERROR_SIMULATOR
+    coreFailCounter = 0;
+    CORE_TEST_TRUE(coreServerStart(54321, 5));
+    coreFailCounter = 1;
+    CORE_TEST_TRUE(coreServerStart(54321, 5));
+    coreFailCounter = -1;
+#endif
+    CORE_TEST_FALSE(coreServerStart(54321, 5));
+    CORE_TEST_TRUE(coreServerStart(54321, 5));
+    coreServerStop();
+}
+
+static void testServerClosesOnConnected(void) {
+    clientDisconnected = false;
+    CORE_TEST_FALSE(coreServerStart(54321, 5));
+    CORE_TEST_FALSE(coreClientStart());
+    coreClientSetDisconnectHandler(onClientDisconnect);
+    CORE_TEST_FALSE(coreClientConnect("127.0.0.1", 54321, 50));
+    tick(100);
+    CORE_TEST_TRUE(coreClientIsConnected());
+    coreServerStop();
+
+    coreClientTimeout(500, 500, 500);
+    for(int i = 0; i < 500 && coreClientIsConnected(); i++) {
+        coreClientTick();
+        coreSleep(10000000);
+    }
+    CORE_TEST_FALSE(coreClientIsConnected());
+    CORE_TEST_TRUE(clientDisconnected);
+    coreClientStop();
+}
+
+static void testClientClosesOnConnected(void) {
+    serverDisconnect = false;
+    CORE_TEST_FALSE(coreServerStart(54321, 5));
+    CORE_TEST_FALSE(coreClientStart());
+    coreServerSetDisconnectHandler(onServerDisconnect);
+    CORE_TEST_FALSE(coreClientConnect("127.0.0.1", 54321, 50));
+    tick(100);
+    CORE_TEST_TRUE(coreClientIsConnected());
+    coreClientStop();
+
+    // coreServerTimeout(500, 500, 500);
+    for(int i = 0; i < 500 && !serverDisconnect; i++) {
+        coreServerTick();
+        coreSleep(10000000);
+    }
+    CORE_TEST_TRUE(serverDisconnect);
+    coreServerStop();
+}
+
 void coreTestNetwork(void) {
     testWriteRead();
     testTooShortBuffer();
@@ -332,4 +421,10 @@ void coreTestNetwork(void) {
     testConnect(CORE_RELIABLE);
     testDisconnect();
     testStop();
+    testClientStartFails();
+    testClientConnectionFails();
+    testInvalidClientAccess();
+    testServerStartFails();
+    testServerClosesOnConnected();
+    testClientClosesOnConnected();
 }