1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*- */
2 /* vi: set expandtab shiftwidth=4 tabstop=4: */
3 
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include "modp_bjavascript.h"
8 #include "minunit.h"
9 
10 /**
11  * Tests input where no escaping happens
12  */
testNoEscape()13 static char* testNoEscape()
14 {
15     char buf[100];
16     const char* s1 = "this is a string";
17     const int len1 = strlen(s1);
18     int d = modp_bjavascript_encode(buf, s1, len1);
19 
20     mu_assert_int_equals(len1, d);
21     mu_assert_str_equals(buf, s1);
22 
23     int sz = modp_bjavascript_encode_strlen(s1, len1);
24     mu_assert_int_equals(sz, len1);
25 
26     return 0;
27 }
28 
testSimpleEscape()29 static char* testSimpleEscape()
30 {
31     char buf[100];
32     const char* s1 = "\\this\nis a string\n";
33     const char* s2 = "\\\\this\\nis a string\\n";
34     const int len1 = strlen(s1);
35     const int len2 = strlen(s2);
36     int d = modp_bjavascript_encode(buf, s1, len1);
37 
38     mu_assert_int_equals(len2, d);
39     mu_assert_str_equals(buf, s2);
40 
41     int sz = modp_bjavascript_encode_strlen(s1, len1);
42     mu_assert_int_equals(sz, len2);
43 
44     /*
45      * Test the Raw escape '\' --> '\\'
46      */
47     char ibuf[] = {'\\', '\0'};
48     memset(buf, 0, sizeof(buf));
49     d = modp_bjavascript_encode(buf,ibuf, 1);
50     mu_assert_int_equals(buf[0], '\\');
51     mu_assert_int_equals(buf[1], '\\');
52     mu_assert_int_equals(buf[2], 0);
53 
54     return 0;
55 }
56 
testSQuoteEscape()57 static char* testSQuoteEscape()
58 {
59     char buf[100];
60     const char* s1 = "this is a 'string'\n";
61     const char* s2 = "this is a \\'string\\'\\n";
62     const int len1 = strlen(s1);
63     const int len2 = strlen(s2);
64     int d = modp_bjavascript_encode(buf, s1, len1);
65 
66     mu_assert_int_equals(len2, d);
67     mu_assert_str_equals(buf, s2);
68 
69     int sz = modp_bjavascript_encode_strlen(s1, len1);
70     mu_assert_int_equals(sz, len2);
71 
72     char ibuf[] = {'\'', '\0'};
73     memset(buf, 0, sizeof(buf));
74     d = modp_bjavascript_encode(buf, ibuf, 1);
75     mu_assert_int_equals(buf[0], '\\');
76     mu_assert_int_equals(buf[1], '\'');
77     mu_assert_int_equals(buf[2], '\0');
78 
79     return 0;
80 }
81 
testDQuoteEscape()82 static char* testDQuoteEscape()
83 {
84     char buf[100];
85     const char* s1 = "this is a \"string\"\n";
86     const char* s2 = "this is a \\\"string\\\"\\n";
87     const int len1 = strlen(s1);
88     const int len2 = strlen(s2);
89     int d = modp_bjavascript_encode(buf, s1, len1);
90 
91     mu_assert_int_equals(len2, d);
92     mu_assert_str_equals(buf, s2);
93 
94     int sz = modp_bjavascript_encode_strlen(s1, len1);
95     mu_assert_int_equals(sz, len2);
96     char ibuf[] = {'\"', '\0'};
97     memset(buf, 0, sizeof(buf));
98     d = modp_bjavascript_encode(buf, ibuf, 1);
99     mu_assert_int_equals(buf[0], '\\');
100     mu_assert_int_equals(buf[1], '\"');
101     mu_assert_int_equals(buf[2], '\0');
102     return 0;
103 }
104 
testBinaryEscape()105 static char* testBinaryEscape()
106 {
107     char buf[100];
108     const char s1[] = {1,2,3,4,0};
109     const char* s2 = "\\x01\\x02\\x03\\x04";
110     const int len1 = strlen(s1);
111     const int len2 = strlen(s2);
112     int d = modp_bjavascript_encode(buf, s1, len1);
113 
114     mu_assert_int_equals(len2, d);
115     mu_assert_str_equals(buf, s2);
116 
117     int sz = modp_bjavascript_encode_strlen(s1, len1);
118     mu_assert_int_equals(sz, len2);
119     return 0;
120 }
121 
all_tests()122 static char* all_tests()
123 {
124     mu_run_test(testNoEscape);
125     mu_run_test(testSimpleEscape);
126     mu_run_test(testBinaryEscape);
127     mu_run_test(testSQuoteEscape);
128     mu_run_test(testDQuoteEscape);
129     return 0;
130 }
131 
132 UNITTESTS
133 
134 
135