Create repository
[fwd.git] / test / test_qdsocket.c
1 /*
2  * Test for qdsocket.c
3  *
4  * Copyright (C) 2015 Chris Jaekl
5  */
6
7 #include <string.h>
8
9 #include "qdtestcase.h"
10
11 #include "mock_io.h"
12 #include "qdsocket.h"
13 #include "qdtypes.h"
14
15 void setUp(void)
16 {
17   //This is run before EACH TEST
18 }
19
20 void tearDown(void)
21 {
22 }
23
24 void test_qdSockInit(void)
25 {
26     struct qdSocket sock;
27
28     qdSockInit(&sock);
29
30     TEST_ASSERT_EQUAL(0, sock.sd);
31     TEST_ASSERT_EQUAL(0, sock.toWrite);
32     TEST_ASSERT_EQUAL(FALSE, sock.closeRequested);
33 }
34
35 void test_qdSockWrite_socketBusy(void)
36 {
37     ssize_t ret;
38     struct qdSocket sock;
39     char buf[32];
40
41     memset(buf, 'A', 32);
42
43     qdSockInit(&sock);
44
45     qdMock_resetWriteBuf(0);
46     
47     ret = qdSockWrite(&sock, buf, 32);
48
49     TEST_ASSERT_EQUAL(0, ret);
50     TEST_ASSERT_EQUAL(32, sock.toWrite);
51     TEST_ASSERT_EQUAL(FALSE, sock.closeRequested);
52     TEST_ASSERT_EQUAL(0, memcmp(buf, sock.buf, 32));
53
54     TEST_ASSERT_EQUAL(0, qdMock_getNumBytesWritten());
55 }
56
57 void test_qdSockWrite_partWritten(void)
58 {
59     ssize_t ret;
60     struct qdSocket sock;
61     char buf[32];
62
63     memcpy(buf, "abcdefghijklmnopqrstuvwxyz789012", 32);
64
65     qdSockInit(&sock);
66
67     qdMock_resetWriteBuf(17);
68
69     ret = qdSockWrite(&sock, buf, 32);
70
71     TEST_ASSERT_EQUAL(17, ret);
72     TEST_ASSERT_EQUAL(15, sock.toWrite);
73     TEST_ASSERT_EQUAL(FALSE, sock.closeRequested);
74     TEST_ASSERT_EQUAL(0, memcmp(buf + 17, sock.buf, 15));
75
76     TEST_ASSERT_EQUAL(17, qdMock_getNumBytesWritten());
77     TEST_ASSERT_EQUAL(0, memcmp(buf, qdMock_getBytesWritten(), 17));
78
79     qdMock_resetWriteBuf(17);
80
81     ret = qdSockFlush(&sock);
82
83     TEST_ASSERT_EQUAL(15, ret);
84     TEST_ASSERT_EQUAL(0, sock.toWrite);
85     TEST_ASSERT_EQUAL(FALSE, sock.closeRequested);
86
87     TEST_ASSERT_EQUAL(15, qdMock_getNumBytesWritten());
88     TEST_ASSERT_EQUAL(0, memcmp(buf + 17, qdMock_getBytesWritten(), 15));
89 }
90
91 #define WWVB_SIZE 33
92 void test_qdSockWrite_writeWithVariedBuffering(void)
93 {
94     ssize_t ret;
95     struct qdSocket sock;
96     char buf[WWVB_SIZE];
97     const char * expected;
98     const char * actual;
99     int chunkSize;
100
101     memcpy(buf, "abcdefghijklmnopqrstuvwxyz7890123", WWVB_SIZE);
102
103     // Constrain socket writes to chunkSize bytes at a time.
104     // Try various different chunk sizes, and confirm that the
105     // whole write eventually succeeds, after enough calls to 
106     // qdSockFlush().
107     // 
108     for (chunkSize = 1; chunkSize <= (WWVB_SIZE + 1); ++chunkSize) {
109         qdSockInit(&sock);
110         qdMock_resetWriteBuf(chunkSize);
111
112         expected = &(buf[0]);
113         ret = qdSockWrite(&sock, buf, WWVB_SIZE);
114
115         while (ret > 0) {
116             actual = qdMock_getBytesWritten();
117             while (ret > 0) {
118                 TEST_ASSERT_EQUAL(*expected, *actual);
119                 ++expected;
120                 ++actual;
121                 --ret;
122             }
123
124             qdMock_resetWriteBuf(chunkSize);
125             ret = qdSockFlush(&sock);
126         }
127     }
128 }
129
130
131 // TODO:  need a lot more tests here
132
133 int main(void)
134 {
135   UnityBegin("test_qdsocket.c");
136
137   // RUN_TEST calls runTest
138   RUN_TEST(test_qdSockInit);
139   RUN_TEST(test_qdSockWrite_socketBusy);
140   RUN_TEST(test_qdSockWrite_partWritten);
141   RUN_TEST(test_qdSockWrite_writeWithVariedBuffering);
142
143   UnityEnd();
144   return 0;
145 }