1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.myfaces.tobago.internal.config;
21
22 import org.apache.myfaces.tobago.context.Theme;
23 import org.apache.myfaces.tobago.context.ThemeScript;
24 import org.junit.jupiter.api.Assertions;
25 import org.junit.jupiter.api.Test;
26 import org.xml.sax.SAXException;
27
28 import javax.xml.parsers.ParserConfigurationException;
29 import java.io.IOException;
30 import java.net.URISyntaxException;
31 import java.net.URL;
32 import java.util.ArrayList;
33 import java.util.List;
34 import java.util.Map;
35
36 public class TobagoConfigMergingUnitTest {
37
38 @Test
39 public void testPreventFrameAttacksCascadingDefault()
40 throws IOException, SAXException, ParserConfigurationException, URISyntaxException {
41
42 final TobagoConfigImpl config = load(
43 "tobago-config-merge-0.xml",
44 "tobago-config-merge-1.xml");
45
46 Assertions.assertFalse(config.isPreventFrameAttacks());
47 }
48
49 @Test
50 public void testPreventFrameAttacks()
51 throws IOException, SAXException, ParserConfigurationException, URISyntaxException {
52
53 final TobagoConfigImpl config = load("tobago-config-merge-0.xml");
54
55 Assertions.assertFalse(config.isPreventFrameAttacks());
56 }
57
58 @Test
59 public void testPreventFrameAttacksDefault()
60 throws IOException, SAXException, ParserConfigurationException, URISyntaxException {
61
62 final TobagoConfigImpl config = load("tobago-config-merge-1.xml");
63
64 Assertions.assertTrue(config.isPreventFrameAttacks());
65 }
66
67 @Test
68 public void testContentSecurityPolicy()
69 throws IOException, SAXException, ParserConfigurationException, URISyntaxException {
70
71 final TobagoConfigImpl config = load(
72 "tobago-config-merge-0.xml");
73
74 Assertions.assertSame(config.getContentSecurityPolicy().getMode(), ContentSecurityPolicy.Mode.ON);
75 final Map<String, String> directiveMap = config.getContentSecurityPolicy().getDirectiveMap();
76 Assertions.assertEquals(1, directiveMap.size());
77 Assertions.assertEquals("'self'", directiveMap.get("default-src"));
78 }
79
80 @Test
81 public void testContentSecurityPolicyExtend()
82 throws IOException, SAXException, ParserConfigurationException, URISyntaxException {
83
84 final TobagoConfigImpl config = load(
85 "tobago-config-merge-0.xml",
86 "tobago-config-merge-1.xml");
87
88 Assertions.assertSame(config.getContentSecurityPolicy().getMode(), ContentSecurityPolicy.Mode.REPORT_ONLY);
89 final Map<String, String> directiveMap = config.getContentSecurityPolicy().getDirectiveMap();
90 Assertions.assertEquals(2, directiveMap.size());
91 Assertions.assertEquals("'self'", directiveMap.get("default-src"));
92 Assertions.assertEquals("http://apache.org", directiveMap.get("image-src"));
93 }
94
95 @Test
96 public void testContentSecurityPolicyOff()
97 throws IOException, SAXException, ParserConfigurationException, URISyntaxException {
98
99 final TobagoConfigImpl config = load(
100 "tobago-config-merge-0.xml",
101 "tobago-config-merge-1.xml",
102 "tobago-config-merge-2.xml");
103
104 Assertions.assertSame(config.getContentSecurityPolicy().getMode(), ContentSecurityPolicy.Mode.OFF);
105 Assertions.assertEquals(2, config.getContentSecurityPolicy().getDirectiveMap().size());
106 }
107
108 @Test
109 public void testContentSecurityPolicyNameAttribute()
110 throws IOException, SAXException, ParserConfigurationException, URISyntaxException {
111
112 final TobagoConfigImpl config = load(
113 "tobago-config-merge-0.xml",
114 "tobago-config-merge-3.xml");
115
116 Assertions.assertSame(config.getContentSecurityPolicy().getMode(), ContentSecurityPolicy.Mode.ON);
117 final Map<String, String> directiveMap = config.getContentSecurityPolicy().getDirectiveMap();
118 Assertions.assertEquals(1, directiveMap.size());
119 Assertions.assertEquals("'self' https:", directiveMap.get("default-src"));
120 }
121
122 @Test
123 public void testMimeTypes()
124 throws IOException, SAXException, ParserConfigurationException, URISyntaxException {
125
126 final TobagoConfigImpl config = load(
127 "tobago-config-merge-0.xml",
128 "tobago-config-merge-1.xml",
129 "tobago-config-merge-2.xml");
130
131 final Map<String, String> mimeTypes = config.getMimeTypes();
132 Assertions.assertEquals(3, mimeTypes.size());
133 Assertions.assertEquals("test/one", mimeTypes.get("test-1"));
134 Assertions.assertEquals("test/zwei", mimeTypes.get("test-2"));
135 Assertions.assertEquals("test/three", mimeTypes.get("test-3"));
136 }
137
138 @Test
139 public void testResourcePriority()
140 throws IOException, SAXException, ParserConfigurationException, URISyntaxException {
141
142 final TobagoConfigImpl config = load(
143 "tobago-config-5.0.xml", "tobago-config-5.0-replace.xml");
144
145 final String[] expected = new String[]{
146 "script-1.js",
147 "script-2.js",
148 "script-3-replacement.js",
149 "script-4.js",
150 "script-5.js",
151 "script-undefined-a.js",
152 "script-undefined-b.js",
153 "script-undefined-c.js",
154 "script-undefined-d.js",
155 "script-undefined-e.js"
156 };
157 final ThemeScript[] ex = new ThemeScript[expected.length];
158 int i = 0;
159 for (String script : expected) {
160 ex[i] = new ThemeScript();
161 ex[i++].setName(script);
162 }
163
164
165 final Theme defaultTheme = config.getDefaultTheme();
166 final ThemeScript[] scripts = defaultTheme.getScriptResources(true);
167
168 Assertions.assertArrayEquals(ex, scripts);
169
170 Assertions.assertEquals("some-version-2", defaultTheme.getVersion());
171 }
172
173 @Test
174 public void testMergeThemePatch()
175 throws IOException, SAXException, ParserConfigurationException, URISyntaxException {
176
177 final TobagoConfigImpl config12 = load(
178 "tobago-config-theme-merge-1.xml",
179 "tobago-config-theme-merge-2.xml");
180 Assertions.assertEquals("2.0", config12.getDefaultTheme().getVersion());
181
182 final ThemeScript[] scripts12 = config12.getDefaultTheme().getScriptResources(false);
183 Assertions.assertEquals(2, scripts12.length);
184 Assertions.assertEquals("script-1", scripts12[0].getName());
185 Assertions.assertEquals("script-2", scripts12[1].getName());
186
187 final TobagoConfigImpl config21 = load(
188 "tobago-config-theme-merge-2.xml",
189 "tobago-config-theme-merge-1.xml");
190 Assertions.assertEquals("2.0", config21.getDefaultTheme().getVersion());
191
192 final ThemeScript[] scripts21 = config21.getDefaultTheme().getScriptResources(false);
193 Assertions.assertEquals(2, scripts21.length);
194 Assertions.assertEquals("script-1", scripts21[0].getName());
195 Assertions.assertEquals("script-2", scripts21[1].getName());
196 }
197
198 public static TobagoConfigImpl load(final String... names)
199 throws IOException, SAXException, ParserConfigurationException, URISyntaxException {
200
201 final List<TobagoConfigFragment> list = new ArrayList<>();
202
203 for (final String name : names) {
204 final URL url = TobagoConfigMergingUnitTest.class.getClassLoader().getResource(name);
205 final TobagoConfigParser parser = new TobagoConfigParser();
206 list.add(parser.parse(url));
207 }
208
209 final TobagoConfigSorter sorter = new TobagoConfigSorter(list);
210 final TobagoConfigMerger merger = new TobagoConfigMerger(sorter.topologicalSort());
211 final TobagoConfigImpl result = merger.merge();
212 result.resolveThemes();
213 result.lock();
214 return result;
215 }
216 }