forked from svaarala/duktape
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-push-buffer.c
More file actions
159 lines (130 loc) · 4.31 KB
/
Copy pathtest-push-buffer.c
File metadata and controls
159 lines (130 loc) · 4.31 KB
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
/*===
*** test_1a (duk_safe_call)
fixed size, 0 bytes (no guarantee whether ptr NULL or non-NULL)
buffer should be all zeroes
buffer should be writable
fixed size, 1024 bytes
ptr is non-NULL: 1
buffer should be all zeroes
buffer should be writable
dynamic size, 0 bytes (no guarantee whether ptr NULL or non-NULL)
buffer should be all zeroes
buffer should be writable
dynamic size, 1024 bytes
ptr is non-NULL: 1
buffer should be all zeroes
buffer should be writable
final top: 4
==> rc=0, result='undefined'
*** test_1b (duk_safe_call)
fixed size, 0 bytes (no guarantee whether ptr NULL or non-NULL)
buffer should be all zeroes
buffer should be writable
fixed size, 1024 bytes
ptr is non-NULL: 1
buffer should be all zeroes
buffer should be writable
dynamic size, 0 bytes (no guarantee whether ptr NULL or non-NULL)
buffer should be all zeroes
buffer should be writable
dynamic size, 1024 bytes
ptr is non-NULL: 1
buffer should be all zeroes
buffer should be writable
final top: 4
==> rc=0, result='undefined'
*** test_2 (duk_safe_call)
fixed size buffer, close to maximum size_t (should fail)
==> rc=1, result='RangeError: buffer too long'
*** test_3 (duk_safe_call)
dynamic size buffer, close to maximum size_t (should fail)
==> rc=1, result='RangeError: buffer too long'
===*/
#ifndef SIZE_MAX
#define SIZE_MAX ((size_t) -1)
#endif
static void rw_test(unsigned char *p, size_t sz) {
int i;
printf("buffer should be all zeroes\n");
for (i = 0; i < sz; i++) {
if (p[i] != 0) {
printf("buffer non-zero at index %d: 0x%02x\n", i, (int) p[i]);
}
}
printf("buffer should be writable\n");
for (i = 0; i < sz; i++) {
p[i] = 0xff;
}
}
/* Basic success test. */
static duk_ret_t test_1a(duk_context *ctx) {
void *buf;
duk_set_top(ctx, 0);
printf("fixed size, 0 bytes (no guarantee whether ptr NULL or non-NULL)\n");
buf = duk_push_buffer(ctx, 0, 0);
rw_test((unsigned char *) buf, 0);
printf("fixed size, 1024 bytes\n");
buf = duk_push_buffer(ctx, 1024, 0);
printf("ptr is non-NULL: %d\n", (buf != NULL ? 1 : 0));
rw_test((unsigned char *) buf, 1024);
printf("dynamic size, 0 bytes (no guarantee whether ptr NULL or non-NULL)\n");
buf = duk_push_buffer(ctx, 0, 1);
rw_test((unsigned char *) buf, 0);
printf("dynamic size, 1024 bytes\n");
buf = duk_push_buffer(ctx, 1024, 1);
printf("ptr is non-NULL: %d\n", (buf != NULL ? 1 : 0));
rw_test((unsigned char *) buf, 1024);
printf("final top: %ld\n", (long) duk_get_top(ctx));
return 0;
}
/* Same test, using shortcut functions. */
static duk_ret_t test_1b(duk_context *ctx) {
void *buf;
duk_set_top(ctx, 0);
printf("fixed size, 0 bytes (no guarantee whether ptr NULL or non-NULL)\n");
buf = duk_push_fixed_buffer(ctx, 0);
rw_test((unsigned char *) buf, 0);
printf("fixed size, 1024 bytes\n");
buf = duk_push_fixed_buffer(ctx, 1024);
printf("ptr is non-NULL: %d\n", (buf != NULL ? 1 : 0));
rw_test((unsigned char *) buf, 1024);
printf("dynamic size, 0 bytes (no guarantee whether ptr NULL or non-NULL)\n");
buf = duk_push_dynamic_buffer(ctx, 0);
rw_test((unsigned char *) buf, 0);
printf("dynamic size, 1024 bytes\n");
buf = duk_push_dynamic_buffer(ctx, 1024);
printf("ptr is non-NULL: %d\n", (buf != NULL ? 1 : 0));
rw_test((unsigned char *) buf, 1024);
printf("final top: %ld\n", (long) duk_get_top(ctx));
return 0;
}
/* Ensure that insanely large alloation fails. Don't use SIZE_MAX, as
* a current bug makes the allocated size wrap and actually causes
* other trouble (separate bug testcase exists for that:
* test-bug-push-buffer-maxsize.c).
*/
static duk_ret_t test_2(duk_context *ctx) {
void *buf;
duk_set_top(ctx, 0);
printf("fixed size buffer, close to maximum size_t (should fail)\n");
buf = duk_push_buffer(ctx, SIZE_MAX - 1024, 0);
printf("ptr is non-NULL: %d\n", (buf != NULL ? 1 : 0));
printf("final top: %ld\n", (long) duk_get_top(ctx));
return 0;
}
static duk_ret_t test_3(duk_context *ctx) {
void *buf;
duk_set_top(ctx, 0);
printf("dynamic size buffer, close to maximum size_t (should fail)\n");
buf = duk_push_buffer(ctx, SIZE_MAX - 1024, 1);
printf("ptr is non-NULL: %d\n", (buf != NULL ? 1 : 0));
printf("final top: %ld\n", (long) duk_get_top(ctx));
return 0;
}
void test(duk_context *ctx) {
/*printf("DUK_SIZE_MAX=%lf\n", (double) DUK_SIZE_MAX);*/
TEST_SAFE_CALL(test_1a);
TEST_SAFE_CALL(test_1b);
TEST_SAFE_CALL(test_2);
TEST_SAFE_CALL(test_3);
}