File size: 8,136 Bytes
864071c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#include "unity/unity.h"
#define PCRE2_CODE_UNIT_WIDTH 8
#include "pcre2.h"
#include "pcre2_compile.h"

#include <string.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>

/* The wrapper is defined in the module under test. We declare it with a
   void* for the first parameter to avoid needing eclass_context's definition. */
extern BOOL test_compile_eclass_nested(void *context,
                                       BOOL negated,
                                       uint32_t **pptr,
                                       PCRE2_UCHAR **pcode,
                                       eclass_op_info *pop_info,
                                       PCRE2_SIZE *lengthptr);

static void assert_bits_all(uint8_t *bits, uint8_t value)
{
  for (int i = 0; i < 32; i++)
  {
    TEST_ASSERT_EQUAL_HEX8_MESSAGE(value, bits[i], "Bitset value mismatch");
  }
}

static void assert_single_op(const eclass_op_info *info, int expected_op)
{
  TEST_ASSERT_EQUAL_UINT_MESSAGE(1, info->length, "Expected single opcode length");
  TEST_ASSERT_EQUAL_INT_MESSAGE(expected_op, info->op_single_type, "op_single_type mismatch");
  TEST_ASSERT_NOT_NULL(info->code_start);
  TEST_ASSERT_EQUAL_INT8_MESSAGE((int8_t)expected_op, (int8_t)info->code_start[0], "Emitted opcode mismatch");
}

void setUp(void) {
  /* Setup code here, or leave empty */
}

void tearDown(void) {
  /* Cleanup code here, or leave empty */
}

/* [] => NONE */
void test_compile_eclass_nested_empty_returns_none(void)
{
  uint32_t tokens[] = {
    (META_CLASS | CLASS_IS_ECLASS),
    META_CLASS_EMPTY,
    META_CLASS_END
  };
  uint32_t *ptr = tokens;
  PCRE2_UCHAR codebuf[16] = {0};
  PCRE2_UCHAR *code = codebuf;
  eclass_op_info info;

  BOOL ok = test_compile_eclass_nested(NULL, FALSE, &ptr, &code, &info, NULL);
  TEST_ASSERT_TRUE(ok);
  TEST_ASSERT_EQUAL_UINT32(META_CLASS_END, *ptr);
  assert_single_op(&info, ECL_NONE);
  assert_bits_all(info.bits.classbits, 0x00);
  TEST_ASSERT_EQUAL_PTR(info.code_start + info.length, code);
}

/* [^] (via META_CLASS_NOT around a nested class with empty) => ANY */
void test_compile_eclass_nested_empty_with_outer_not_returns_any(void)
{
  uint32_t tokens[] = {
    (META_CLASS_NOT | CLASS_IS_ECLASS),
    META_CLASS_EMPTY,
    META_CLASS_END
  };
  uint32_t *ptr = tokens;
  PCRE2_UCHAR codebuf[16] = {0};
  PCRE2_UCHAR *code = codebuf;
  eclass_op_info info;

  BOOL ok = test_compile_eclass_nested(NULL, FALSE, &ptr, &code, &info, NULL);
  TEST_ASSERT_TRUE(ok);
  TEST_ASSERT_EQUAL_UINT32(META_CLASS_END, *ptr);
  assert_single_op(&info, ECL_ANY);
  assert_bits_all(info.bits.classbits, 0xFF);
  TEST_ASSERT_EQUAL_PTR(info.code_start + info.length, code);
}

/* ![] (META_ECLASS_NOT unary) => ANY */
void test_compile_eclass_nested_unary_not_operator(void)
{
  uint32_t tokens[] = {
    (META_CLASS | CLASS_IS_ECLASS),
    META_ECLASS_NOT,
    META_CLASS_EMPTY,
    META_CLASS_END
  };
  uint32_t *ptr = tokens;
  PCRE2_UCHAR codebuf[16] = {0};
  PCRE2_UCHAR *code = codebuf;
  eclass_op_info info;

  BOOL ok = test_compile_eclass_nested(NULL, FALSE, &ptr, &code, &info, NULL);
  TEST_ASSERT_TRUE(ok);
  TEST_ASSERT_EQUAL_UINT32(META_CLASS_END, *ptr);
  assert_single_op(&info, ECL_ANY);
  assert_bits_all(info.bits.classbits, 0xFF);
  TEST_ASSERT_EQUAL_PTR(info.code_start + info.length, code);
}

/* [] && [^] => NONE */
void test_compile_eclass_nested_binary_and_folding(void)
{
  uint32_t tokens[] = {
    (META_CLASS | CLASS_IS_ECLASS),
    META_CLASS_EMPTY,
    META_ECLASS_AND,
    META_CLASS_EMPTY_NOT,
    META_CLASS_END
  };
  uint32_t *ptr = tokens;
  PCRE2_UCHAR codebuf[32] = {0};
  PCRE2_UCHAR *code = codebuf;
  eclass_op_info info;

  BOOL ok = test_compile_eclass_nested(NULL, FALSE, &ptr, &code, &info, NULL);
  TEST_ASSERT_TRUE(ok);
  TEST_ASSERT_EQUAL_UINT32(META_CLASS_END, *ptr);
  assert_single_op(&info, ECL_NONE);
  assert_bits_all(info.bits.classbits, 0x00);
  TEST_ASSERT_EQUAL_PTR(info.code_start + info.length, code);
}

/* [] || [^] => ANY */
void test_compile_eclass_nested_binary_or_folding(void)
{
  uint32_t tokens[] = {
    (META_CLASS | CLASS_IS_ECLASS),
    META_CLASS_EMPTY,
    META_ECLASS_OR,
    META_CLASS_EMPTY_NOT,
    META_CLASS_END
  };
  uint32_t *ptr = tokens;
  PCRE2_UCHAR codebuf[32] = {0};
  PCRE2_UCHAR *code = codebuf;
  eclass_op_info info;

  BOOL ok = test_compile_eclass_nested(NULL, FALSE, &ptr, &code, &info, NULL);
  TEST_ASSERT_TRUE(ok);
  TEST_ASSERT_EQUAL_UINT32(META_CLASS_END, *ptr);
  assert_single_op(&info, ECL_ANY);
  assert_bits_all(info.bits.classbits, 0xFF);
  TEST_ASSERT_EQUAL_PTR(info.code_start + info.length, code);
}

/* [^] -- [^] => ANY && !ANY => NONE */
void test_compile_eclass_nested_binary_sub_folding(void)
{
  uint32_t tokens[] = {
    (META_CLASS | CLASS_IS_ECLASS),
    META_CLASS_EMPTY_NOT,
    META_ECLASS_SUB,
    META_CLASS_EMPTY_NOT,
    META_CLASS_END
  };
  uint32_t *ptr = tokens;
  PCRE2_UCHAR codebuf[32] = {0};
  PCRE2_UCHAR *code = codebuf;
  eclass_op_info info;

  BOOL ok = test_compile_eclass_nested(NULL, FALSE, &ptr, &code, &info, NULL);
  TEST_ASSERT_TRUE(ok);
  TEST_ASSERT_EQUAL_UINT32(META_CLASS_END, *ptr);
  assert_single_op(&info, ECL_NONE);
  assert_bits_all(info.bits.classbits, 0x00);
  TEST_ASSERT_EQUAL_PTR(info.code_start + info.length, code);
}

/* [^] XOR [^] => NONE */
void test_compile_eclass_nested_binary_xor_folding(void)
{
  uint32_t tokens[] = {
    (META_CLASS | CLASS_IS_ECLASS),
    META_CLASS_EMPTY_NOT,
    META_ECLASS_XOR,
    META_CLASS_EMPTY_NOT,
    META_CLASS_END
  };
  uint32_t *ptr = tokens;
  PCRE2_UCHAR codebuf[32] = {0};
  PCRE2_UCHAR *code = codebuf;
  eclass_op_info info;

  BOOL ok = test_compile_eclass_nested(NULL, FALSE, &ptr, &code, &info, NULL);
  TEST_ASSERT_TRUE(ok);
  TEST_ASSERT_EQUAL_UINT32(META_CLASS_END, *ptr);
  assert_single_op(&info, ECL_NONE);
  assert_bits_all(info.bits.classbits, 0x00);
  TEST_ASSERT_EQUAL_PTR(info.code_start + info.length, code);
}

/* Nested class inside: ([^]) && ([]) => NONE */
void test_compile_eclass_nested_with_nested_operand(void)
{
  uint32_t tokens[] = {
    (META_CLASS | CLASS_IS_ECLASS),
      /* Nested start */
      (META_CLASS | CLASS_IS_ECLASS),
      META_CLASS_EMPTY_NOT,      /* inner => ANY */
      META_CLASS_END,            /* end inner */
      META_ECLASS_AND,
      META_CLASS_EMPTY,          /* outer RHS => NONE */
    META_CLASS_END               /* end outer */
  };
  uint32_t *ptr = tokens;
  PCRE2_UCHAR codebuf[32] = {0};
  PCRE2_UCHAR *code = codebuf;
  eclass_op_info info;

  BOOL ok = test_compile_eclass_nested(NULL, FALSE, &ptr, &code, &info, NULL);
  TEST_ASSERT_TRUE(ok);
  TEST_ASSERT_EQUAL_UINT32(META_CLASS_END, *ptr);
  assert_single_op(&info, ECL_NONE);
  assert_bits_all(info.bits.classbits, 0x00);
  TEST_ASSERT_EQUAL_PTR(info.code_start + info.length, code);
}

/* lengthptr counting for a single operand: should be 1 */
void test_compile_eclass_nested_lengthptr_single_operand_counts_one(void)
{
  uint32_t tokens[] = {
    (META_CLASS | CLASS_IS_ECLASS),
    META_CLASS_EMPTY_NOT,  /* ANY */
    META_CLASS_END
  };
  uint32_t *ptr = tokens;
  PCRE2_UCHAR codebuf[16] = {0};
  PCRE2_UCHAR *code = codebuf;
  eclass_op_info info;
  PCRE2_SIZE length = 0;

  BOOL ok = test_compile_eclass_nested(NULL, FALSE, &ptr, &code, &info, &length);
  TEST_ASSERT_TRUE(ok);
  TEST_ASSERT_EQUAL_UINT32(META_CLASS_END, *ptr);
  TEST_ASSERT_EQUAL_UINT(1, length);
  /* In lengthptr mode, code pointer should not advance. */
  TEST_ASSERT_EQUAL_PTR(codebuf, code);
}

int main(void)
{
  UNITY_BEGIN();
  RUN_TEST(test_compile_eclass_nested_empty_returns_none);
  RUN_TEST(test_compile_eclass_nested_empty_with_outer_not_returns_any);
  RUN_TEST(test_compile_eclass_nested_unary_not_operator);
  RUN_TEST(test_compile_eclass_nested_binary_and_folding);
  RUN_TEST(test_compile_eclass_nested_binary_or_folding);
  RUN_TEST(test_compile_eclass_nested_binary_sub_folding);
  RUN_TEST(test_compile_eclass_nested_binary_xor_folding);
  RUN_TEST(test_compile_eclass_nested_with_nested_operand);
  RUN_TEST(test_compile_eclass_nested_lengthptr_single_operand_counts_one);
  return UNITY_END();
}