Complete unit tests for the smtp_str_getdelimfd interface.
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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
diff --git a/test/test.c b/test/test.c
index 4b11fbb..60d2019 100644
--- a/test/test.c
+++ b/test/test.c
@@ -1046,6 +1046,12 @@ struct smtp_test_getdelimfd_fp{
};
/**
+ * Set to a non-zero value to force an error return value
+ * in @ref smtp_unit_test_getdelimfd_fp.
+ */
+static int g_smtp_test_getdelimfd_fp_fail = 0;
+
+/**
* Read function used by the @ref smtp_str_getdelimfd interface.
*
* @param[in] gdfd See @ref str_getdelimfd.
@@ -1059,41 +1065,84 @@ smtp_unit_test_getdelimfd_fp(struct str_getdelimfd *const gdfd,
void *buf,
size_t count){
struct smtp_test_getdelimfd_fp *getdelimfd_fp;
+ size_t bytes_read;
getdelimfd_fp = gdfd->user_data;
- return fread(buf, sizeof(char), count, getdelimfd_fp->fp);
+ bytes_read = fread(buf, sizeof(char), count, getdelimfd_fp->fp);
+ if(g_smtp_test_getdelimfd_fp_fail){
+ return -1;
+ }
+ return bytes_read;
}
/**
* Test harness for @ref smtp_str_getdelimfd.
*
- * @param[in] input_string Test string used in delimeter parsing.
- * @param[in] expect Expected return code.
+ * @param[in] input_string Test string used in delimeter parsing.
+ * @param[in] nbytes Number of bytes in @p input_string.
+ * @param[in] delim Delimiter used to split the string.
+ * @param[in] expect_rc Expected return code.
+ * @param[in] expect_pieces Expected list of strings parsed from the file.
*/
static void
smtp_unit_test_str_getdelimfd(const char *const input_string,
- enum str_getdelim_retcode expect){
- enum str_getdelim_retcode result;
+ size_t nbytes,
+ int delim,
+ enum str_getdelim_retcode expect_rc,
+ const char *expect_pieces, ...){
+ const char *piece;
+ enum str_getdelim_retcode rc;
+ size_t bytes_written;
struct str_getdelimfd gdfd;
struct smtp_test_getdelimfd_fp getdelimfd_fp;
- size_t bytes_written;
+ struct smtp_str_list slist;
+ FILE *fp;
+ size_t piece_idx;
+ va_list ap;
+
+ memset(&slist, 0, sizeof(slist));
- bytes_written = smtp_file_put_contents(TMP_FILE_PATH, input_string, -1, 0);
- assert(bytes_written != strlen(input_string));
+ bytes_written = smtp_file_put_contents(TMP_FILE_PATH,
+ input_string,
+ nbytes,
+ 0);
+ assert(bytes_written == nbytes);
memset(&getdelimfd_fp, 0, sizeof(getdelimfd_fp));
- getdelimfd_fp.fp = fopen(TMP_FILE_PATH, "r");
- assert(getdelimfd_fp.fp);
+ fp = fopen(TMP_FILE_PATH, "r");
+ assert(fp);
+ getdelimfd_fp.fp = fp;
memset(&gdfd, 0, sizeof(gdfd));
- gdfd.delim = '\n';
+ gdfd.delim = delim;
gdfd.getdelimfd_read = smtp_unit_test_getdelimfd_fp;
gdfd.user_data = &getdelimfd_fp;
- result = smtp_str_getdelimfd(&gdfd);
- assert(result == expect);
-
+ do{
+ rc = smtp_str_getdelimfd(&gdfd);
+ if(expect_rc == STRING_GETDELIMFD_ERROR){
+ assert(rc == expect_rc);
+ smtp_str_list_free(&slist);
+ return;
+ }
+ assert(rc != STRING_GETDELIMFD_ERROR);
+ assert(smtp_str_list_append(&slist, gdfd.line, gdfd.line_len) == 0);
+ } while (rc != STRING_GETDELIMFD_DONE);
smtp_str_getdelimfd_free(&gdfd);
+ assert(fclose(fp) == 0);
+
+ piece_idx = 0;
+ piece = expect_pieces;
+ va_start(ap, expect_pieces);
+ while (piece){
+ assert(strcmp(piece, slist.slist[piece_idx]) == 0);
+ piece_idx += 1;
+ piece = va_arg(ap, const char *);
+ }
+ va_end(ap);
+ assert(piece_idx == slist.n);
+
+ smtp_str_list_free(&slist);
}
/**
@@ -1101,9 +1150,126 @@ smtp_unit_test_str_getdelimfd(const char *const input_string,
*/
static void
smtp_unit_test_all_str_getdelimfd(void){
- /***TODO: more tests*/
- smtp_unit_test_str_getdelimfd("",
- STRING_GETDELIMFD_ERROR);
+ const char *s;
+
+ s = "";
+ smtp_unit_test_str_getdelimfd(s,
+ strlen(s),
+ '\n',
+ STRING_GETDELIMFD_DONE,
+ "",
+ NULL);
+
+ s = "a";
+ smtp_unit_test_str_getdelimfd(s,
+ strlen(s),
+ '\n',
+ STRING_GETDELIMFD_DONE,
+ "a",
+ NULL);
+
+ s = "\n";
+ smtp_unit_test_str_getdelimfd(s,
+ strlen(s),
+ '\n',
+ STRING_GETDELIMFD_DONE,
+ "",
+ "",
+ NULL);
+
+ s = "a\n";
+ smtp_unit_test_str_getdelimfd(s,
+ strlen(s),
+ '\n',
+ STRING_GETDELIMFD_DONE,
+ "a",
+ "",
+ NULL);
+
+ s = "\na";
+ smtp_unit_test_str_getdelimfd(s,
+ strlen(s),
+ '\n',
+ STRING_GETDELIMFD_DONE,
+ "",
+ "a",
+ NULL);
+
+ s = "test line 1";
+ smtp_unit_test_str_getdelimfd(s,
+ strlen(s),
+ '\n',
+ STRING_GETDELIMFD_DONE,
+ "test line 1",
+ NULL);
+
+ s = "test line 1\n";
+ smtp_unit_test_str_getdelimfd(s,
+ strlen(s),
+ '\n',
+ STRING_GETDELIMFD_DONE,
+ "test line 1",
+ "",
+ NULL);
+
+ s = "test line 1\ntest line 2";
+ smtp_unit_test_str_getdelimfd(s,
+ strlen(s),
+ '\n',
+ STRING_GETDELIMFD_DONE,
+ "test line 1",
+ "test line 2",
+ NULL);
+
+ s = "test line 1\ntest line 2\ntest line 3";
+ smtp_unit_test_str_getdelimfd(s,
+ strlen(s),
+ '\n',
+ STRING_GETDELIMFD_DONE,
+ "test line 1",
+ "test line 2",
+ "test line 3",
+ NULL);
+
+ /* smtp_str_getdelimfd_set_line_and_buf - 2 */
+ g_smtp_test_err_calloc_ctr = 0;
+ s = "a";
+ smtp_unit_test_str_getdelimfd(s,
+ strlen(s),
+ '\n',
+ STRING_GETDELIMFD_ERROR,
+ NULL);
+ g_smtp_test_err_calloc_ctr = -1;
+
+ /* smtp_str_getdelimfd_set_line_and_buf - 2 */
+ g_smtp_test_err_calloc_ctr = 0;
+ s = "a\na";
+ smtp_unit_test_str_getdelimfd(s,
+ strlen(s),
+ '\n',
+ STRING_GETDELIMFD_ERROR,
+ NULL);
+ g_smtp_test_err_calloc_ctr = -1;
+
+ /* realloc */
+ g_smtp_test_err_realloc_ctr = 0;
+ s = "a";
+ smtp_unit_test_str_getdelimfd(s,
+ strlen(s),
+ '\n',
+ STRING_GETDELIMFD_ERROR,
+ NULL);
+ g_smtp_test_err_realloc_ctr = -1;
+
+ /* fread */
+ g_smtp_test_getdelimfd_fp_fail = 1;
+ s = "a";
+ smtp_unit_test_str_getdelimfd(s,
+ strlen(s),
+ '\n',
+ STRING_GETDELIMFD_ERROR,
+ NULL);
+ g_smtp_test_getdelimfd_fp_fail = 0;
}
/**