-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_netstring.c
46 lines (36 loc) · 914 Bytes
/
test_netstring.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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "netstring.h"
int
main()
{
char *test_data = "\xab{\xcd}\xef;";
char *enc_data;
size_t enc_size;
char *dec_data;
size_t dec_size;
printf("Test Data (%u): '%s'\n", (unsigned int)strlen(test_data), test_data);
enc_data = netstring_encode(test_data, strlen(test_data), &enc_size);
if (!enc_data)
{
fprintf(stderr, "encoding error\n");
exit(EXIT_FAILURE);
}
printf("Encoded Data (%u): '%s'\n", (unsigned int)enc_size, enc_data);
dec_data = netstring_decode(enc_data, &dec_size);
if (!dec_data)
{
fprintf(stderr, "decoding error\n");
exit(EXIT_FAILURE);
}
printf("Decoded Data (%u): '%s'\n", (unsigned int)dec_size, dec_data);
if (0 == strncmp(dec_data, test_data, dec_size))
printf("TEST SUCCESS\n");
else
printf("TEST FAILURE\n");
free(enc_data);
free(dec_data);
return EXIT_SUCCESS;
}