This repository was archived by the owner on May 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathmodp_bjavascript_test.c
137 lines (112 loc) · 3.46 KB
/
modp_bjavascript_test.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
/* we compile as C90 but use snprintf */
#define _ISOC99_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "minunit.h"
#include "modp_bjavascript.h"
/**
* Tests input where no escaping happens
*/
static char* testNoEscape(void)
{
char buf[100];
const char* s1 = "this is a string";
const size_t len1 = strlen(s1);
size_t d = modp_bjavascript_encode(buf, s1, len1);
mu_assert_int_equals(len1, d);
mu_assert_str_equals(buf, s1);
size_t sz = modp_bjavascript_encode_strlen(s1, len1);
mu_assert_int_equals(sz, len1);
return 0;
}
static char* testSimpleEscape(void)
{
char buf[100];
const char* s1 = "\\this\nis a string\n";
const char* s2 = "\\\\this\\nis a string\\n";
const size_t len1 = strlen(s1);
const size_t len2 = strlen(s2);
size_t d = modp_bjavascript_encode(buf, s1, len1);
mu_assert_int_equals(len2, d);
mu_assert_str_equals(buf, s2);
size_t sz = modp_bjavascript_encode_strlen(s1, len1);
mu_assert_int_equals(sz, len2);
/*
* Test the Raw escape '\' --> '\\'
*/
char ibuf[] = { '\\', '\0' };
memset(buf, 0, sizeof(buf));
d = modp_bjavascript_encode(buf, ibuf, (size_t)1);
mu_assert_int_equals(d, 2);
mu_assert_int_equals(buf[0], '\\');
mu_assert_int_equals(buf[1], '\\');
mu_assert_int_equals(buf[2], 0);
return 0;
}
static char* testSQuoteEscape(void)
{
char buf[100];
const char* s1 = "this is a 'string'\n";
const char* s2 = "this is a \\'string\\'\\n";
const size_t len1 = strlen(s1);
const size_t len2 = strlen(s2);
size_t d = modp_bjavascript_encode(buf, s1, len1);
mu_assert_int_equals(len2, d);
mu_assert_str_equals(buf, s2);
size_t sz = modp_bjavascript_encode_strlen(s1, len1);
mu_assert_int_equals(sz, len2);
char ibuf[] = { '\'', '\0' };
memset(buf, 0, sizeof(buf));
d = modp_bjavascript_encode(buf, ibuf, (size_t)1);
mu_assert_int_equals(d, 2);
mu_assert_int_equals(buf[0], '\\');
mu_assert_int_equals(buf[1], '\'');
mu_assert_int_equals(buf[2], '\0');
return 0;
}
static char* testDQuoteEscape(void)
{
char buf[100];
const char* s1 = "this is a \"string\"\n";
const char* s2 = "this is a \\\"string\\\"\\n";
const size_t len1 = strlen(s1);
const size_t len2 = strlen(s2);
size_t d = modp_bjavascript_encode(buf, s1, len1);
mu_assert_int_equals(len2, d);
mu_assert_str_equals(buf, s2);
size_t sz = modp_bjavascript_encode_strlen(s1, len1);
mu_assert_int_equals(sz, len2);
char ibuf[] = { '\"', '\0' };
memset(buf, 0, sizeof(buf));
d = modp_bjavascript_encode(buf, ibuf, (size_t)1);
mu_assert_int_equals(d, 2);
mu_assert_int_equals(buf[0], '\\');
mu_assert_int_equals(buf[1], '\"');
mu_assert_int_equals(buf[2], '\0');
return 0;
}
static char* testBinaryEscape(void)
{
char buf[100];
const char s1[] = { 1, 2, 3, 4, 0 };
const char* s2 = "\\x01\\x02\\x03\\x04";
const size_t len1 = strlen(s1);
const size_t len2 = strlen(s2);
size_t d = modp_bjavascript_encode(buf, s1, len1);
mu_assert_int_equals(len2, d);
mu_assert_str_equals(buf, s2);
size_t sz = modp_bjavascript_encode_strlen(s1, len1);
mu_assert_int_equals(sz, len2);
return 0;
}
static char* all_tests(void)
{
mu_run_test(testNoEscape);
mu_run_test(testSimpleEscape);
mu_run_test(testBinaryEscape);
mu_run_test(testSQuoteEscape);
mu_run_test(testDQuoteEscape);
return 0;
}
UNITTESTS