summaryrefslogtreecommitdiff
path: root/src/couch/test/eunit/couch_js_tests.erl
blob: ccd2cd0b57a9f61f2d4013f4b9cbfcca081b0088 (plain)
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
% Licensed under the Apache License, Version 2.0 (the "License"); you may not
% use this file except in compliance with the License. You may obtain a copy of
% the License at
%
%   http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
% License for the specific language governing permissions and limitations under
% the License.

-module(couch_js_tests).
-include_lib("eunit/include/eunit.hrl").

couch_js_test_() ->
    {
        "Test couchjs",
        {
            setup,
            fun test_util:start_couch/0,
            fun test_util:stop_couch/1,
            [
                fun should_create_sandbox/0,
                fun should_reset_properly/0,
                fun should_freeze_doc_object/0,
                fun should_roundtrip_utf8/0,
                fun should_roundtrip_modified_utf8/0,
                fun should_replace_broken_utf16/0,
                fun should_allow_js_string_mutations/0,
                {timeout, 60000, fun should_exit_on_oom/0},
                {timeout, 60000, fun should_exit_on_internal_error/0}
            ]
        }
    }.

%% erlfmt-ignore
should_create_sandbox() ->
    % Try and detect whether we can see out of the
    % sandbox or not.
    Src = <<"
        function(doc) {
            try {
                emit(false, typeof(Couch.compile_function));
            } catch (e) {
                emit(true, e.message);
            }
        }
    ">>,
    Proc = couch_query_servers:get_os_process(<<"javascript">>),
    true = couch_query_servers:proc_prompt(Proc, [<<"add_fun">>, Src]),
    Result = couch_query_servers:proc_prompt(Proc, [<<"map_doc">>, {[]}]),
    ?assertMatch([[[true, <<_/binary>>]]], Result),
    [[[true, ErrMsg]]] = Result,
    ?assertNotEqual([], binary:matches(ErrMsg, <<"not defined">>)),
    couch_query_servers:ret_os_process(Proc).

%% erlfmt-ignore
should_reset_properly() ->
    Src = <<"
        function(doc) {
            var a = [0,1,2];
            emit(a.indexOf(0), Object.foo);
            Object.foo = 43;
            [].constructor.prototype.indexOf = function(x) {return 42;};
        }
    ">>,
    Proc = couch_query_servers:get_os_process(<<"javascript">>),
    true = couch_query_servers:proc_prompt(Proc, [<<"add_fun">>, Src]),
    Doc = {[]},
    Result1 = couch_query_servers:proc_prompt(Proc, [<<"map_doc">>, Doc]),
    ?assertEqual([[[0, null]]], Result1),
    Result2 = couch_query_servers:proc_prompt(Proc, [<<"map_doc">>, Doc]),
    ?assertEqual([[[42, 43]]], Result2),
    true = couch_query_servers:proc_prompt(Proc, [<<"reset">>]),
    true = couch_query_servers:proc_prompt(Proc, [<<"add_fun">>, Src]),
    Result3 = couch_query_servers:proc_prompt(Proc, [<<"map_doc">>, Doc]),
    ?assertEqual([[[0, null]]], Result3),
    couch_query_servers:ret_os_process(Proc).

%% erlfmt-ignore
should_freeze_doc_object() ->
    Src = <<"
        function(doc) {
            emit(doc.foo, doc.bar);
            doc.foo = 1042;
            doc.bar = 1043;
            emit(doc.foo, doc.bar);
        }
    ">>,
    Proc = couch_query_servers:get_os_process(<<"javascript">>),
    true = couch_query_servers:proc_prompt(Proc, [<<"add_fun">>, Src]),
    Doc = {[{<<"bar">>, 1041}]},
    Result1 = couch_query_servers:proc_prompt(Proc, [<<"map_doc">>, Doc]),
    ?assertEqual([[[null, 1041], [null, 1041]]], Result1),
    Result2 = couch_query_servers:proc_prompt(Proc, [<<"map_doc">>, Doc]),
    ?assertEqual([[[null, 1041], [null, 1041]]], Result2),
    couch_query_servers:ret_os_process(Proc).

%% erlfmt-ignore
should_roundtrip_utf8() ->
    % Try round tripping UTF-8 both directions through
    % couchjs. These tests use hex encoded values of
    % Ä (C384) and Ü (C39C) so as to avoid odd editor/Erlang encoding
    % strangeness.
    Src = <<
        "function(doc) {
          emit(doc.value, \"", 16#C3,  16#9C, "\");
        }
    ">>,
    Proc = couch_query_servers:get_os_process(<<"javascript">>),
    true = couch_query_servers:proc_prompt(Proc, [<<"add_fun">>, Src]),
    Doc =
        {[
            {<<"value">>, <<16#C3, 16#84>>}
        ]},
    Result = couch_query_servers:proc_prompt(Proc, [<<"map_doc">>, Doc]),
    ?assertEqual([[[<<16#C3, 16#84>>, <<16#C3, 16#9C>>]]], Result),
    couch_query_servers:ret_os_process(Proc).

%% erlfmt-ignore
should_roundtrip_modified_utf8() ->
    % Mimicking the test case from the mailing list
    Src = <<"
        function(doc) {
          emit(doc.value.toLowerCase(), \"", 16#C3, 16#9C, "\");
        }
    ">>,
    Proc = couch_query_servers:get_os_process(<<"javascript">>),
    true = couch_query_servers:proc_prompt(Proc, [<<"add_fun">>, Src]),
    Doc =
        {[
            {<<"value">>, <<16#C3, 16#84>>}
        ]},
    Result = couch_query_servers:proc_prompt(Proc, [<<"map_doc">>, Doc]),
    ?assertEqual([[[<<16#C3, 16#A4>>, <<16#C3, 16#9C>>]]], Result),
    couch_query_servers:ret_os_process(Proc).

%% erlfmt-ignore
should_replace_broken_utf16() ->
    % This test reverse the surrogate pair of
    % the Boom emoji U+1F4A5
    Src = <<"
        function(doc) {
            emit(doc.value.split(\"\").reverse().join(\"\"), 1);
        }
    ">>,
    Proc = couch_query_servers:get_os_process(<<"javascript">>),
    true = couch_query_servers:proc_prompt(Proc, [<<"add_fun">>, Src]),
    Doc =
        {[
            {<<"value">>, list_to_binary(xmerl_ucs:to_utf8([16#1F4A5]))}
        ]},
    Result = couch_query_servers:proc_prompt(Proc, [<<"map_doc">>, Doc]),
    % Invalid UTF-8 gets replaced with the 16#FFFD replacement
    % marker
    Markers = list_to_binary(xmerl_ucs:to_utf8([16#FFFD, 16#FFFD])),
    ?assertEqual([[[Markers, 1]]], Result),
    couch_query_servers:ret_os_process(Proc).

%% erlfmt-ignore
should_allow_js_string_mutations() ->
    % This binary corresponds to this string: мама мыла раму
    % Which I'm told translates to: "mom was washing the frame"
    MomWashedTheFrame = <<
        16#D0,
        16#BC,
        16#D0,
        16#B0,
        16#D0,
        16#BC,
        16#D0,
        16#B0,
        16#20,
        16#D0,
        16#BC,
        16#D1,
        16#8B,
        16#D0,
        16#BB,
        16#D0,
        16#B0,
        16#20,
        16#D1,
        16#80,
        16#D0,
        16#B0,
        16#D0,
        16#BC,
        16#D1,
        16#83
    >>,
    Mom = <<16#D0, 16#BC, 16#D0, 16#B0, 16#D0, 16#BC, 16#D0, 16#B0>>,
    Washed = <<16#D0, 16#BC, 16#D1, 16#8B, 16#D0, 16#BB, 16#D0, 16#B0>>,
    Src1 = <<"
        function(doc) {
          emit(\"length\", doc.value.length);
        }
    ">>,
    Src2 = <<"
        function(doc) {
          emit(\"substring\", doc.value.substring(5, 9));
        }
    ">>,
    Src3 = <<"
        function(doc) {
          emit(\"slice\", doc.value.slice(0, 4));
        }
    ">>,
    Proc = couch_query_servers:get_os_process(<<"javascript">>),
    true = couch_query_servers:proc_prompt(Proc, [<<"add_fun">>, Src1]),
    true = couch_query_servers:proc_prompt(Proc, [<<"add_fun">>, Src2]),
    true = couch_query_servers:proc_prompt(Proc, [<<"add_fun">>, Src3]),
    Doc = {[{<<"value">>, MomWashedTheFrame}]},
    Result = couch_query_servers:proc_prompt(Proc, [<<"map_doc">>, Doc]),
    Expect = [
        [[<<"length">>, 14]],
        [[<<"substring">>, Washed]],
        [[<<"slice">>, Mom]]
    ],
    ?assertEqual(Expect, Result),
    couch_query_servers:ret_os_process(Proc).

%% erlfmt-ignore
should_exit_on_oom() ->
    Src = <<"
        var state = [];
        function(doc) {
            var val = \"0123456789ABCDEF\";
            for(var i = 0; i < 665535; i++) {
                state.push([val, val]);
                emit(null, null);
             }
        }
    ">>,
    Proc = couch_query_servers:get_os_process(<<"javascript">>),
    true = couch_query_servers:proc_prompt(Proc, [<<"add_fun">>, Src]),
    trigger_oom(Proc).

%% erlfmt-ignore
should_exit_on_internal_error() ->
    % A different way to trigger OOM which previously used to
    % throw an InternalError on SM. Check that we still exit on that
    % type of error
    Src = <<"
        function(doc) {
            function mkstr(l) {
                var r = 'r';
                while (r.length < l) {r = r + r;}
                return r;
            }
            var s = mkstr(96*1024*1024);
            var a = [s,s,s,s,s,s,s,s];
            var j = JSON.stringify(a);
            emit(42, j.length);}
    ">>,
    Proc = couch_query_servers:get_os_process(<<"javascript">>),
    true = couch_query_servers:proc_prompt(Proc, [<<"reset">>]),
    true = couch_query_servers:proc_prompt(Proc, [<<"add_fun">>, Src]),
    Doc = {[]},
    try
        couch_query_servers:proc_prompt(Proc, [<<"map_doc">>, Doc])
    catch
        % Expect either an internal error thrown if it catches it and
        % emits an error log before dying
        throw:{<<"InternalError">>, _} ->
            ok;
        % gen_server may die before replying
        exit:{noproc, {gen_server,call, _}} ->
            ok;
        % or it may die with an epipe if it crashes while we send/recv data to it
        exit:{epipe, {gen_server, call, _}} ->
            ok;
        % It may fail and just exit the process. That's expected as well
        throw:{os_process_error, _} ->
            ok
    end.

trigger_oom(Proc) ->
    Status =
        try
            couch_query_servers:proc_prompt(Proc, [<<"map_doc">>, <<"{}">>]),
            continue
        catch
            throw:{os_process_error, {exit_status, 1}} ->
                done
        end,
    case Status of
        continue -> trigger_oom(Proc);
        done -> ok
    end.