-
Notifications
You must be signed in to change notification settings - Fork 0
/
testSessionHandler.c
157 lines (146 loc) · 4.6 KB
/
testSessionHandler.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//
// Created by mike on 3/9/17.
//
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "rasocket.h"
#include "oneTimeBuffer.h"
#include "testSessionHandler.h"
#include "sessionHandler.h"
static bool testCreate();
static bool testAddSession();
static bool testRemoveSession();
static bool testRemoveMultipleSessions();
static bool testSessionIter();
static bool test1024Sessions();
bool testSessionHandler_usage(int argc, char **argv, char *usageStr, size_t usageLen)
{
usageStr[0] = '\0';
if(argc > ARG_ACTION + 1)
{
if(strcmp(argv[ARG_ACTION],"test") == 0 && strcmp(argv[ARG_ACTION + 1], "sessionHandler") == 0)
{
return true;
}
}
strncpy(usageStr, "socker test sessionHandler\n\trun self test", usageLen);
return false;
}
bool testSessionHandler()
{
int result = true;
result = result && testCreate();
result = result && testAddSession();
result = result && testRemoveSession();
result = result && testRemoveMultipleSessions();
result = result && testSessionIter();
result = result && test1024Sessions();
return (bool)result;
}
static bool testCreate()
{
printf((char *)"testCreate()\n");
SESSION_HANDLER_HANDLE hHandler = createSessionHandler();
assert(hHandler != SESSION_HANDLER_HANDLE_ERROR);
destroySessionHandler(hHandler);
return hHandler != NULL;
}
static bool testAddSession()
{
printf((char *)"testAddSession()\n");
SESSION_HANDLER_HANDLE shh = createSessionHandler();
bool result = addSession(shh, 3, 4, NULL, NULL);
int maxSD = getMaxSD(shh);
assert(maxSD == 4);
assert(result);
return result;
}
static bool testRemoveSession()
{
printf((char *)"testRemoveSession()\n");
SESSION_HANDLER_HANDLE shh = createSessionHandler();
addSession(shh, 3, 4, NULL, NULL);
removeSession(shh, 3);
assert(getMaxSD(shh) == -1);
return getMaxSD(shh) == -1;
}
static bool testRemoveMultipleSessions()
{
printf((char *)"testRemoveMultipleSessions()\n");
SESSION_HANDLER_HANDLE shh = createSessionHandler();
addSession(shh, 3, 4, NULL, NULL);
assert(getMaxSD(shh) == 4);
addSession(shh, 1, 2, NULL, NULL);
assert(getMaxSD(shh) == 4);
addSession(shh, 7, 8, NULL, NULL);
assert(getMaxSD(shh) == 8);
removeSession(shh, 1);
assert(getMaxSD(shh) == 8);
removeSession(shh, 7);
assert(getMaxSD(shh) == 4);
removeSession(shh, 3);
assert(getMaxSD(shh) == -1);
return getMaxSD(shh) == -1;
}
static bool testSessionIter()
{
printf((char *)"testSessionIter()\n");
SESSION_HANDLER_HANDLE shh = createSessionHandler();
addSession(shh, 3, 4, NULL, NULL);
addSession(shh, 1, 2, NULL, NULL);
addSession(shh, 7, 8, NULL, NULL);
SESSION_ITER iter = getSessionIter(shh);
assert(iter != NULL);
SOCKET sdClient, sdProxied;
ONE_TIME_BUFFER_HANDLE hFromClient, hToClient;
bool result;
result = getNextSession(iter, &sdClient, &sdProxied, &hFromClient, &hToClient);
assert(result);
assert(sdClient == 3);
result = getNextSession(iter, &sdClient, &sdProxied, &hFromClient, &hToClient);
assert(result);
assert(sdClient == 1);
result = getNextSession(iter, &sdClient, &sdProxied, &hFromClient, &hToClient);
assert(result);
assert(sdProxied == 8);
result = getNextSession(iter, &sdClient, &sdProxied, &hFromClient, &hToClient);
assert(!result);
removeSession(shh, 1);
iter = getSessionIter(shh);
getNextSession(iter, &sdClient, &sdProxied, &hFromClient, &hToClient);
result = getNextSession(iter, &sdClient, &sdProxied, &hFromClient, &hToClient);
assert(result);
assert(sdClient == 7);
removeSession(shh, 7);
iter = getSessionIter(shh);
getNextSession(iter, &sdClient, &sdProxied, &hFromClient, &hToClient);
removeSession(shh, 3);
iter = getSessionIter(shh);
result = getNextSession(iter, &sdClient, &sdProxied, &hFromClient, &hToClient);
assert(!result);
return iter != NULL;
}
static bool test1024Sessions()
{
printf((char *)"test1024Sessions()\n");
SESSION_HANDLER_HANDLE shh = createSessionHandler();
int ii;
for (ii = 0; ii < 1024; ii++)
{
addSession(shh, 3, 4, NULL, NULL);
}
SOCKET sdClient, sdProxied;
ONE_TIME_BUFFER_HANDLE hFromClient, hToClient;
bool result;
SESSION_ITER iter = getSessionIter(shh);
int jj;
for (jj = 0; jj < 1024; jj++)
{
result = getNextSession(iter, &sdClient, &sdProxied, &hFromClient, &hToClient);
assert(result);
}
result = getNextSession(iter, &sdClient, &sdProxied, &hFromClient, &hToClient);
assert(!result);
return !result;
}