1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements.  See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership.  The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License.  You may obtain a copy of the License at
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 
19 package org.apache.hadoop.yarn.webapp.view;
20 
21 import java.io.PrintWriter;
22 import java.io.StringWriter;
23 
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
26 
27 import org.apache.hadoop.yarn.webapp.ResponseInfo;
28 import org.apache.hadoop.yarn.webapp.test.WebAppTests;
29 import org.junit.Before;
30 import org.junit.Test;
31 
32 public class TestInfoBlock {
33 
34   public static StringWriter sw;
35 
36   public static PrintWriter pw;
37 
38   static final String JAVASCRIPT = "<script>alert('text')</script>";
39   static final String JAVASCRIPT_ESCAPED =
40       "&lt;script&gt;alert('text')&lt;/script&gt;";
41 
42   public static class JavaScriptInfoBlock extends InfoBlock{
43 
44     static ResponseInfo resInfo;
45 
46     static {
47       resInfo = new ResponseInfo();
48       resInfo._("User_Name", JAVASCRIPT);
49     }
50 
51     @Override
writer()52     public PrintWriter writer() {
53       return TestInfoBlock.pw;
54     }
55 
JavaScriptInfoBlock(ResponseInfo info)56     JavaScriptInfoBlock(ResponseInfo info) {
57       super(resInfo);
58     }
59 
JavaScriptInfoBlock()60     public JavaScriptInfoBlock() {
61       super(resInfo);
62     }
63   }
64 
65   public static class MultilineInfoBlock extends InfoBlock{
66 
67     static ResponseInfo resInfo;
68 
69     static {
70       resInfo = new ResponseInfo();
71       resInfo._("Multiple_line_value", "This is one line.");
72       resInfo._("Multiple_line_value", "This is first line.\nThis is second line.");
73     }
74 
75     @Override
writer()76     public PrintWriter writer() {
77       return TestInfoBlock.pw;
78     }
79 
MultilineInfoBlock(ResponseInfo info)80     MultilineInfoBlock(ResponseInfo info) {
81       super(resInfo);
82     }
83 
MultilineInfoBlock()84     public MultilineInfoBlock() {
85       super(resInfo);
86     }
87   }
88 
89   @Before
setup()90   public void setup() {
91     sw = new StringWriter();
92     pw = new PrintWriter(sw);
93   }
94 
95   @Test(timeout=60000L)
testMultilineInfoBlock()96   public void testMultilineInfoBlock() throws Exception{
97 
98     WebAppTests.testBlock(MultilineInfoBlock.class);
99     TestInfoBlock.pw.flush();
100     String output = TestInfoBlock.sw.toString().replaceAll(" +", " ");
101     String expectedMultilineData1 = String.format("<tr class=\"odd\">%n"
102       + " <th>%n Multiple_line_value%n </th>%n"
103       + " <td>%n This is one line.%n </td>%n");
104     String expectedMultilineData2 = String.format("<tr class=\"even\">%n"
105       + " <th>%n Multiple_line_value%n </th>%n <td>%n <div>%n"
106       + " This is first line.%n </div>%n <div>%n"
107       + " This is second line.%n </div>%n");
108     assertTrue(output.contains(expectedMultilineData1) && output.contains(expectedMultilineData2));
109   }
110 
111   @Test(timeout=60000L)
testJavaScriptInfoBlock()112   public void testJavaScriptInfoBlock() throws Exception{
113     WebAppTests.testBlock(JavaScriptInfoBlock.class);
114     TestInfoBlock.pw.flush();
115     String output = TestInfoBlock.sw.toString();
116     assertFalse(output.contains("<script>"));
117     assertTrue(output.contains(JAVASCRIPT_ESCAPED));
118   }
119 }
120