File: TestELInJsp.java

package info (click to toggle)
tomcat10 10.1.52-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 47,900 kB
  • sloc: java: 375,756; xml: 59,410; jsp: 4,741; sh: 1,381; perl: 324; makefile: 25; ansic: 14
file content (525 lines) | stat: -rw-r--r-- 19,180 bytes parent folder | download | duplicates (5)
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 */
package org.apache.el;

import java.io.File;
import java.math.RoundingMode;
import java.util.Collections;

import jakarta.servlet.DispatcherType;

import org.junit.Assert;
import org.junit.Test;

import org.apache.catalina.Wrapper;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.startup.TomcatBaseTest;
import org.apache.jasper.servlet.JasperInitializer;
import org.apache.tomcat.util.buf.ByteChunk;

/**
 * Tests EL with an without JSP attributes using a test web application. Similar tests may be found in
 * {@link TestELEvaluation} and {@link org.apache.jasper.compiler.TestAttributeParser}.
 */
public class TestELInJsp extends TomcatBaseTest {

    @Test
    public void testBug36923() throws Exception {
        getTomcatInstanceTestWebapp(false, true);

        ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/bug36923.jsp");

        String result = res.toString();
        assertEcho(result, "00-${hello world}");
    }

    @Test
    public void testBug42565() throws Exception {
        getTomcatInstanceTestWebapp(false, true);

        ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/bug42565.jsp");

        String result = res.toString();
        assertEcho(result, "00-false");
        assertEcho(result, "01-false");
        assertEcho(result, "02-false");
        assertEcho(result, "03-false");
        assertEcho(result, "04-false");
        assertEcho(result, "05-false");
        assertEcho(result, "06-false");
        assertEcho(result, "07-false");
        assertEcho(result, "08-false");
        assertEcho(result, "09-false");
        assertEcho(result, "10-false");
        assertEcho(result, "11-false");
        assertEcho(result, "12-false");
        assertEcho(result, "13-false");
        assertEcho(result, "14-false");
        assertEcho(result, "15-false");
    }

    @Test
    public void testBug44994() throws Exception {
        getTomcatInstanceTestWebapp(false, true);

        ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/bug44994.jsp");

        String result = res.toString();
        assertEcho(result, "00-none");
        assertEcho(result, "01-one");
        assertEcho(result, "02-many");
    }

    @Test
    public void testBug45427() throws Exception {
        getTomcatInstanceTestWebapp(false, true);

        ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/bug45nnn/bug45427.jsp");

        String result = res.toString();
        // Warning: JSP attribute escaping != Java String escaping
        assertEcho(result, "00-hello world");
        assertEcho(result, "01-hello 'world");
        assertEcho(result, "02-hello \"world");
        assertEcho(result, "03-hello \"world");
        assertEcho(result, "04-hello world");
        assertEcho(result, "05-hello 'world");
        assertEcho(result, "06-hello 'world");
        assertEcho(result, "07-hello \"world");
        assertEcho(result, "08-hello world");
        assertEcho(result, "09-hello 'world");
        assertEcho(result, "10-hello \"world");
        assertEcho(result, "11-hello \"world");
        assertEcho(result, "12-hello world");
        assertEcho(result, "13-hello 'world");
        assertEcho(result, "14-hello 'world");
        assertEcho(result, "15-hello \"world");
    }

    @Test
    public void testBug45451() throws Exception {
        getTomcatInstanceTestWebapp(false, true);

        ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/bug45nnn/bug45451a.jsp");

        String result = res.toString();
        // Warning: JSP attribute escaping != Java String escaping
        assertEcho(result, "00-\\'hello world\\'");
        assertEcho(result, "01-\\'hello world\\'");
        assertEcho(result, "02-\\'hello world\\'");
        assertEcho(result, "03-\\'hello world\\'");

        res = getUrl("http://localhost:" + getPort() + "/test/bug45nnn/bug45451b.jsp");
        result = res.toString();
        // Warning: JSP attribute escaping != Java String escaping
        // Warning: Attributes are always unescaped before passing to the EL
        // processor
        assertEcho(result, "00-2");
        assertEcho(result, "01-${1+1}");
        assertEcho(result, "02-\\${1+1}");
        assertEcho(result, "03-\\\\${1+1}");
        assertEcho(result, "04-$500");
        // Inside an EL literal '\' is only used to escape '\', ''' and '"'
        assertEcho(result, "05-\\$");
        assertEcho(result, "06-\\${");
        assertEcho(result, "10-2");
        assertEcho(result, "11-${1+1}");
        assertEcho(result, "12-\\2");
        assertEcho(result, "13-\\${1+1}");
        assertEcho(result, "14-\\\\2");
        assertEcho(result, "15-$500");
        assertEcho(result, "16-\\$");
        assertEcho(result, "17-\\${");
        assertEcho(result, "20-2");
        assertEcho(result, "21-#{1+1}");
        assertEcho(result, "22-\\2");
        assertEcho(result, "23-\\#{1+1}");
        assertEcho(result, "24-\\\\2");
        assertEcho(result, "25-\\#");
        assertEcho(result, "26-\\#{");

        res = getUrl("http://localhost:" + getPort() + "/test/bug45nnn/bug45451c.jsp");
        result = res.toString();
        // Warning: JSP attribute escaping != Java String escaping
        // TODO - Currently we allow a single unescaped \ in attribute values
        // Review if this should cause a warning/error
        assertEcho(result, "00-${1+1}");
        assertEcho(result, "01-\\${1+1}");
        assertEcho(result, "02-\\\\${1+1}");
        assertEcho(result, "03-\\\\\\${1+1}");
        assertEcho(result, "04-\\$500");
        assertEcho(result, "10-${1+1}");
        assertEcho(result, "11-\\${1+1}");
        assertEcho(result, "12-\\${1+1}");
        assertEcho(result, "13-\\\\${1+1}");
        assertEcho(result, "14-\\\\${1+1}");
        assertEcho(result, "15-\\$500");
        assertEcho(result, "20-#{1+1}");
        assertEcho(result, "21-\\#{1+1}");
        assertEcho(result, "22-\\#{1+1}");
        assertEcho(result, "23-\\\\#{1+1}");
        assertEcho(result, "24-\\\\#{1+1}");

        res = getUrl("http://localhost:" + getPort() + "/test/bug45nnn/bug45451d.jspx");
        result = res.toString();
        // Warning: JSP attribute escaping != Java String escaping
        // \\ Is *not* an escape sequence in XML attributes
        assertEcho(result, "00-2");
        assertEcho(result, "01-${1+1}");
        assertEcho(result, "02-\\${1+1}");
        assertEcho(result, "03-\\\\${1+1}");
        assertEcho(result, "04-$500");
        assertEcho(result, "10-2");
        assertEcho(result, "11-${1+1}");
        assertEcho(result, "12-\\${1+1}");
        assertEcho(result, "13-\\\\${1+1}");
        assertEcho(result, "14-\\\\\\${1+1}");
        assertEcho(result, "15-$500");
        assertEcho(result, "20-2");
        assertEcho(result, "21-#{1+1}");
        assertEcho(result, "22-\\#{1+1}");
        assertEcho(result, "23-\\\\#{1+1}");
        assertEcho(result, "24-\\\\\\#{1+1}");

        res = getUrl("http://localhost:" + getPort() + "/test/bug45nnn/bug45451e.jsp");
        result = res.toString();
        // Warning: JSP attribute escaping != Java String escaping
        // Warning: Attributes are always unescaped before passing to the EL
        // processor
        assertEcho(result, "00-2");
        assertEcho(result, "01-${1+1}");
        assertEcho(result, "02-\\${1+1}");
        assertEcho(result, "03-\\\\${1+1}");
        assertEcho(result, "04-$500");
        assertEcho(result, "10-2");
        assertEcho(result, "11-${1+1}");
        assertEcho(result, "12-\\2");
        assertEcho(result, "13-\\${1+1}");
        assertEcho(result, "14-\\\\2");
        assertEcho(result, "15-$500");
        assertEcho(result, "20-#{1+1}");
        assertEcho(result, "21-\\#{1+1}");
        assertEcho(result, "22-\\#{1+1}");
        assertEcho(result, "23-\\\\#{1+1}");
        assertEcho(result, "24-\\\\#{1+1}");
    }

    @Test
    public void testBug45511() throws Exception {
        getTomcatInstanceTestWebapp(false, true);

        ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/bug45nnn/bug45511.jsp");

        String result = res.toString();
        assertEcho(result, "00-true");
        assertEcho(result, "01-false");
    }

    @Test
    public void testBug46596() throws Exception {
        getTomcatInstanceTestWebapp(false, true);

        ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/bug46596.jsp");
        String result = res.toString();
        assertEcho(result, "{OK}");
    }

    @Test
    public void testBug47413() throws Exception {
        getTomcatInstanceTestWebapp(false, true);

        ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/bug47413.jsp");

        String result = res.toString();
        assertEcho(result, "00-hello world");
        assertEcho(result, "01-hello world");
        assertEcho(result, "02-3.22");
        assertEcho(result, "03-3.22");
        assertEcho(result, "04-17");
        assertEcho(result, "05-17");
        assertEcho(result, "06-hello world");
        assertEcho(result, "07-hello world");
        assertEcho(result, "08-0.0");
        assertEcho(result, "09-0.0");
        assertEcho(result, "10-0");
        assertEcho(result, "11-0");
    }

    @Test
    public void testBug48112() throws Exception {
        getTomcatInstanceTestWebapp(false, true);

        ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/bug48nnn/bug48112.jsp");
        String result = res.toString();
        assertEcho(result, "{OK}");
    }

    @Test
    public void testBug49555() throws Exception {
        getTomcatInstanceTestWebapp(false, true);

        ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/bug49nnn/bug49555.jsp");

        String result = res.toString();
        assertEcho(result, "00-" + TesterFunctions.Inner$Class.RETVAL);
    }

    @Test
    public void testBug51544() throws Exception {
        getTomcatInstanceTestWebapp(false, true);

        ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/bug5nnnn/bug51544.jsp");

        String result = res.toString();
        assertEcho(result, "Empty list: true");
    }

    @Test
    public void testELMiscNoQuoteAttributeEL() throws Exception {
        doTestELMisc(false);
    }

    @Test
    public void testELMiscWithQuoteAttributeEL() throws Exception {
        doTestELMisc(true);
    }

    private void doTestELMisc(boolean quoteAttributeEL) throws Exception {
        Tomcat tomcat = getTomcatInstance();

        // Create the context (don't use addWebapp as we want to modify the
        // JSP Servlet settings).
        File appDir = new File("test/webapp");
        StandardContext ctxt = (StandardContext) tomcat.addContext(null, "/test", appDir.getAbsolutePath());

        ctxt.addServletContainerInitializer(new JasperInitializer(), null);

        // Configure the defaults and then tweak the JSP servlet settings
        // Note: Min value for maxLoadedJsps is 2
        Tomcat.initWebappDefaults(ctxt);
        Wrapper w = (Wrapper) ctxt.findChild("jsp");

        String jspName;
        if (quoteAttributeEL) {
            jspName = "/test/el-misc-with-quote-attribute-el.jsp";
            w.addInitParameter("quoteAttributeEL", "true");
        } else {
            jspName = "/test/el-misc-no-quote-attribute-el.jsp";
            w.addInitParameter("quoteAttributeEL", "false");
        }

        tomcat.start();

        ByteChunk res = getUrl("http://localhost:" + getPort() + jspName);
        String result = res.toString();

        assertEcho(result, "00-\\\\\\\"${'hello world'}");
        assertEcho(result, "01-\\\\\\\"\\${'hello world'}");
        assertEcho(result, "02-\\\"${'hello world'}");
        assertEcho(result, "03-\\\"\\hello world");
        assertEcho(result, "2az-04");
        assertEcho(result, "05-a2z");
        assertEcho(result, "06-az2");
        assertEcho(result, "2az-07");
        assertEcho(result, "08-a2z");
        assertEcho(result, "09-az2");
        assertEcho(result, "10-${'foo'}bar");
        assertEcho(result, "11-\\\"}");
        assertEcho(result, "12-foo\\bar\\baz");
        assertEcho(result, "13-foo\\bar\\baz");
        assertEcho(result, "14-foo\\bar\\baz");
        assertEcho(result, "15-foo\\bar\\baz");
        assertEcho(result, "16-foo\\bar\\baz");
        assertEcho(result, "17-foo\\'bar'\\"baz"");
        assertEcho(result, "18-3");
        assertEcho(result, "19-4");
        assertEcho(result, "20-4");
        assertEcho(result, "21-[{value=11}, {value=12}, {value=13}, {value=14}]");
        assertEcho(result, "22-[{value=11}, {value=12}, {value=13}, {value=14}]");
    }

    @Test
    public void testScriptingExpression() throws Exception {
        getTomcatInstanceTestWebapp(false, true);

        ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/script-expr.jsp");
        String result = res.toString();
        assertEcho(result, "00-hello world");
        assertEcho(result, "01-hello \"world");
        assertEcho(result, "02-hello \\\"world");
        assertEcho(result, "03-hello ${world");
        assertEcho(result, "04-hello \\${world");
        assertEcho(result, "05-hello world");
        assertEcho(result, "06-hello \"world");
        assertEcho(result, "07-hello \\\"world");
        assertEcho(result, "08-hello ${world");
        assertEcho(result, "09-hello \\${world");
        assertEcho(result, "10-hello <% world");
        assertEcho(result, "11-hello %> world");
    }

    @Test
    public void testELMethod() throws Exception {
        getTomcatInstanceTestWebapp(false, true);

        ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/el-method.jsp");
        String result = res.toString();
        assertEcho(result, "00-Hello JUnit from Tomcat");
        assertEcho(result, "01-Hello JUnit from Tomcat");
        assertEcho(result, "02-Hello JUnit from Tomcat");
        assertEcho(result, "03-Hello JUnit from Tomcat");
        assertEcho(result, "04-Hello JUnit from Tomcat");
        assertEcho(result, "05-Hello JUnit from Tomcat");
    }


    @Test
    public void testBug56029() throws Exception {
        getTomcatInstanceTestWebapp(true, true);

        ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/bug5nnnn/bug56029.jspx");

        String result = res.toString();

        Assert.assertTrue(result.contains("[1]:[1]"));
    }


    @Test
    public void testBug56147() throws Exception {
        getTomcatInstanceTestWebapp(true, true);

        ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/bug5nnnn/bug56147.jsp");

        String result = res.toString();
        assertEcho(result, "00-OK");
    }


    @Test
    public void testBug56612() throws Exception {
        getTomcatInstanceTestWebapp(false, true);

        ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/bug5nnnn/bug56612.jsp");

        String result = res.toString();
        Assert.assertTrue(result.contains("00-''"));
    }


    /*
     * java.lang should be imported by default
     */
    @Test
    public void testBug57141() throws Exception {
        getTomcatInstanceTestWebapp(false, true);

        ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/bug5nnnn/bug57141.jsp");

        String result = res.toString();
        assertEcho(result, "00-true");
        assertEcho(result, "01-false");
        assertEcho(result, "02-2147483647");
    }


    /*
     * BZ https://bz.apache.org/bugzilla/show_bug.cgi?id=57142 jakarta.servlet, jakarta.servlet.http and
     * jakarta.servlet.jsp should be imported by default.
     */
    @Test
    public void testBug57142() throws Exception {
        getTomcatInstanceTestWebapp(false, true);

        ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/bug5nnnn/bug57142.jsp");

        String result = res.toString();
        // jakarta.servlet
        assertEcho(result, "00-" + DispatcherType.ASYNC);
        // No obvious static fields for jakarta.servlet.http
        // Could hack something with HttpUtils...
        // No obvious static fields for jakarta.servlet.jsp
        // Wild card (package) import
        assertEcho(result, "01-" + RoundingMode.HALF_UP);
        // Class import
        assertEcho(result, "02-" + Collections.EMPTY_LIST.size());
    }


    /*
     * BZ https://bz.apache.org/bugzilla/show_bug.cgi?id=57441 Can't validate function names defined in lambdas (or via
     * imports)
     */
    @Test
    public void testBug57441() throws Exception {
        getTomcatInstanceTestWebapp(false, true);

        ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/bug5nnnn/bug57441.jsp");

        String result = res.toString();
        assertEcho(result, "00-11");
    }


    @Test
    public void testBug60032() throws Exception {
        getTomcatInstanceTestWebapp(false, true);

        ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/bug6nnnn/bug60032.jsp");
        String result = res.toString();
        assertEcho(result, "{OK}");
    }


    @Test
    public void testBug60431() throws Exception {
        getTomcatInstanceTestWebapp(false, true);

        ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/bug6nnnn/bug60431.jsp");
        String result = res.toString();
        assertEcho(result, "01-OK");
        assertEcho(result, "02-OK");
        assertEcho(result, "03-OK");
    }


    @Test
    public void testBug61854a() throws Exception {
        getTomcatInstanceTestWebapp(true, true);

        ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/bug6nnnn/bug61854.jsp");
        String result = res.toString();
        assertEcho(result, "01-OK");
    }


    @Test
    public void testBug69635() throws Exception {
        getTomcatInstanceTestWebapp(true, true);

        ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/bug6nnnn/bug69635.jsp");
        String result = res.toString();
        assertEcho(result, "01-3");
    }


    // Assertion for text contained with <p></p>, e.g. printed by tags:echo
    private static void assertEcho(String result, String expected) {
        Assert.assertTrue(result, result.indexOf("<p>" + expected + "</p>") > 0);
    }
}