View Javadoc
1   /*
2    * Licensed under the GPL License. You may not use this file except in compliance with the License.
3    * You may obtain a copy of the License at
4    *
5    *   https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
6    *
7    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
8    * WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
9    * PURPOSE.
10   */
11  package psiprobe;
12  
13  import static org.junit.jupiter.api.Assertions.assertEquals;
14  import static org.junit.jupiter.api.Assertions.assertFalse;
15  import static org.junit.jupiter.api.Assertions.assertNotNull;
16  import static org.junit.jupiter.api.Assertions.assertNull;
17  import static org.junit.jupiter.api.Assertions.assertThrows;
18  import static org.junit.jupiter.api.Assertions.assertTrue;
19  
20  import jakarta.servlet.ServletContext;
21  
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.Collections;
25  import java.util.Enumeration;
26  import java.util.List;
27  
28  import org.apache.catalina.Context;
29  import org.apache.catalina.Valve;
30  import org.apache.catalina.WebResource;
31  import org.apache.catalina.WebResourceRoot;
32  import org.apache.catalina.deploy.NamingResourcesImpl;
33  import org.apache.jasper.EmbeddedServletOptions;
34  import org.apache.jasper.JspCompilationContext;
35  import org.apache.naming.ContextAccessController;
36  import org.apache.tomcat.util.descriptor.web.ApplicationParameter;
37  import org.apache.tomcat.util.descriptor.web.ContextResource;
38  import org.apache.tomcat.util.descriptor.web.ContextResourceLink;
39  import org.apache.tomcat.util.descriptor.web.FilterDef;
40  import org.apache.tomcat.util.descriptor.web.FilterMap;
41  import org.junit.jupiter.api.Test;
42  import org.junit.jupiter.api.extension.ExtendWith;
43  import org.junit.jupiter.params.ParameterizedTest;
44  import org.junit.jupiter.params.provider.ValueSource;
45  import org.mockito.Mock;
46  import org.mockito.MockedStatic;
47  import org.mockito.Mockito;
48  import org.mockito.junit.jupiter.MockitoExtension;
49  
50  import psiprobe.model.ApplicationResource;
51  
52  /**
53   * The Class Tomcat11ContainerAdapterTest.
54   */
55  @ExtendWith(MockitoExtension.class)
56  class Tomcat11ContainerAdapterTest {
57  
58    /** The context. */
59    @Mock
60    Context context;
61  
62    /** The embedded servlet options. */
63    @Mock
64    EmbeddedServletOptions options;
65  
66    /**
67     * Creates the valve.
68     */
69    @Test
70    void createValve() {
71      final Tomcat11ContainerAdapter adapter = new Tomcat11ContainerAdapter();
72      Valve valve = adapter.createValve();
73      assertEquals("Tomcat11AgentValve[Container is null]", valve.toString());
74    }
75  
76    /**
77     * Can bound to null.
78     */
79    @Test
80    void canBoundToNull() {
81      final Tomcat11ContainerAdapter adapter = new Tomcat11ContainerAdapter();
82      assertFalse(adapter.canBoundTo(null));
83    }
84  
85    /**
86     * Can bound to tomcat 11, tomee 11.0, nsjsp 11.0, vmware tc 11.0.
87     *
88     * @param container the container
89     */
90    @ParameterizedTest
91    @ValueSource(strings = {"Apache Tomcat/11.0",
92        // "Apache Tomcat (TomEE)/11.0",
93        // "NonStop(tm) Servlets For JavaServer Pages(tm) v11.0",
94        "Vmware tc..../11.0"})
95    void canBoundTo(String container) {
96      final Tomcat11ContainerAdapter adapter = new Tomcat11ContainerAdapter();
97      assertTrue(adapter.canBoundTo(container));
98    }
99  
100   /**
101    * Can not bound to other containers.
102    *
103    * @param container the container
104    */
105   @ParameterizedTest
106   @ValueSource(strings = {"Vmware tc", "Other"})
107   void cannotBoundToOthers(String container) {
108     final Tomcat11ContainerAdapter adapter = new Tomcat11ContainerAdapter();
109     assertFalse(adapter.canBoundTo(container));
110   }
111 
112   /**
113    * Filter mappings.
114    */
115   @Test
116   void filterMappings() {
117     final Tomcat11ContainerAdapter adapter = new Tomcat11ContainerAdapter();
118     FilterMap map = new FilterMap();
119     map.addServletName("psi-probe");
120     map.addURLPattern("/psi-probe");
121     assertEquals(2, adapter.getFilterMappings(map, "dispatcherMap", "filterClass").size());
122   }
123 
124   /**
125    * Creates the jsp compilation context.
126    */
127   @Test
128   void createJspCompilationContext() {
129     final Tomcat11ContainerAdapter adapter = new Tomcat11ContainerAdapter();
130     Mockito.when(this.options.getGeneratedJspPackageName()).thenReturn("org.apache.jsp");
131     JspCompilationContext jspContext = adapter.createJspCompilationContext("name", this.options,
132         null, null, ClassLoader.getSystemClassLoader());
133     assertEquals("org.apache.jsp.name", jspContext.getFQCN());
134   }
135 
136   /**
137    * Adds the context resource link.
138    */
139   @Test
140   void addContextResourceLink() {
141     NamingResourcesImpl namingResources = Mockito.mock(NamingResourcesImpl.class);
142     Mockito.when(context.getNamingResources()).thenReturn(namingResources);
143     Mockito.when(namingResources.findResourceLinks())
144         .thenReturn(new ContextResourceLink[] {new ContextResourceLink()});
145 
146     final Tomcat11ContainerAdapter adapter = new Tomcat11ContainerAdapter();
147     final List<ApplicationResource> list = new ArrayList<ApplicationResource>();
148     adapter.addContextResourceLink(context, list);
149     assertFalse(list.isEmpty());
150   }
151 
152   /**
153    * Adds the context resource.
154    */
155   @Test
156   void addContextResource() {
157     NamingResourcesImpl namingResources = Mockito.mock(NamingResourcesImpl.class);
158     Mockito.when(context.getNamingResources()).thenReturn(namingResources);
159     Mockito.when(namingResources.findResources())
160         .thenReturn(new ContextResource[] {new ContextResource()});
161 
162     final Tomcat11ContainerAdapter adapter = new Tomcat11ContainerAdapter();
163     final List<ApplicationResource> list = new ArrayList<ApplicationResource>();
164     adapter.addContextResource(context, list);
165     assertFalse(list.isEmpty());
166   }
167 
168   /**
169    * Gets the application filter maps.
170    */
171   @Test
172   void applicationFilterMaps() {
173     Mockito.when(context.findFilterMaps()).thenReturn(new FilterMap[] {new FilterMap()});
174 
175     final Tomcat11ContainerAdapter adapter = new Tomcat11ContainerAdapter();
176     assertEquals(0, adapter.getApplicationFilterMaps(context).size());
177   }
178 
179   /**
180    * Gets the application filter maps async.
181    *
182    * @param dispatcher the dispatcher
183    */
184   @ParameterizedTest
185   @ValueSource(strings = {"ASYNC", "ERROR", "FORWARD", "INCLUDE", "NONE"})
186   void applicationFilterMapsTypes(String dispatcher) {
187     FilterMap filterMap = new FilterMap();
188     filterMap.setDispatcher(dispatcher);
189     Mockito.when(context.findFilterMaps()).thenReturn(new FilterMap[] {filterMap});
190 
191     Mockito.when(context.findFilterDef(Mockito.any())).thenReturn(new FilterDef());
192 
193     final Tomcat11ContainerAdapter adapter = new Tomcat11ContainerAdapter();
194     assertEquals(0, adapter.getApplicationFilterMaps(context).size());
195   }
196 
197   /**
198    * Gets the application filter maps throws on unknown dispatcher mapping.
199    */
200   @Test
201   void applicationFilterMapsThrowsOnUnknownDispatcherMapping() {
202     Context mockContext = Mockito.mock(Context.class);
203     FilterMap filterMap = Mockito.mock(FilterMap.class);
204 
205     // Return an invalid dispatcher mapping value
206     Mockito.when(filterMap.getDispatcherMapping()).thenReturn(-1);
207     Mockito.when(mockContext.findFilterMaps()).thenReturn(new FilterMap[] {filterMap});
208 
209     Tomcat11ContainerAdapter adapter = new Tomcat11ContainerAdapter();
210 
211     assertThrows(NullPointerException.class, () -> adapter.getApplicationFilterMaps(mockContext));
212   }
213 
214   /**
215    * Application filters.
216    */
217   @Test
218   void applicationFilters() {
219     Mockito.when(context.findFilterDefs()).thenReturn(new FilterDef[] {new FilterDef()});
220 
221     final Tomcat11ContainerAdapter adapter = new Tomcat11ContainerAdapter();
222     assertEquals(1, adapter.getApplicationFilters(context).size());
223   }
224 
225   /**
226    * Application init params.
227    */
228   @Test
229   void applicationInitParams() {
230     Mockito.when(context.findApplicationParameters())
231         .thenReturn(new ApplicationParameter[] {new ApplicationParameter()});
232 
233     ServletContext servletContext = Mockito.mock(ServletContext.class);
234     Mockito.when(context.getServletContext()).thenReturn(servletContext);
235 
236     List<String> initParams = new ArrayList<>();
237     initParams.add("name");
238     Enumeration<String> initParameterNames = Collections.enumeration(initParams);
239     Mockito.when(servletContext.getInitParameterNames()).thenReturn(initParameterNames);
240 
241     final Tomcat11ContainerAdapter adapter = new Tomcat11ContainerAdapter();
242     assertEquals(1, adapter.getApplicationInitParams(context).size());
243   }
244 
245   /**
246    * Application init params none.
247    */
248   @Test
249   void applicationInitParamsNone() {
250     Mockito.when(context.findApplicationParameters())
251         .thenReturn(new ApplicationParameter[] {(ApplicationParameter) null});
252 
253     ServletContext servletContext = Mockito.mock(ServletContext.class);
254     Mockito.when(context.getServletContext()).thenReturn(servletContext);
255 
256     List<String> initParams = new ArrayList<>();
257     initParams.add("name");
258     Enumeration<String> initParameterNames = Collections.enumeration(initParams);
259     Mockito.when(servletContext.getInitParameterNames()).thenReturn(initParameterNames);
260 
261     Mockito.when(context.findParameter(Mockito.any())).thenReturn(null);
262 
263     final Tomcat11ContainerAdapter adapter = new Tomcat11ContainerAdapter();
264     assertEquals(1, adapter.getApplicationInitParams(context).size());
265   }
266 
267   /**
268    * Application init params not override attempt.
269    */
270   @Test
271   void applicationInitParamsNotOverrideAttempt() {
272     ApplicationParameter appParam = new ApplicationParameter();
273     appParam.setName("noOverride");
274     appParam.setOverride(false);
275     Mockito.when(context.findApplicationParameters())
276         .thenReturn(new ApplicationParameter[] {appParam});
277 
278     ServletContext servletContext = Mockito.mock(ServletContext.class);
279     Mockito.when(context.getServletContext()).thenReturn(servletContext);
280 
281     List<String> initParams = new ArrayList<>();
282     initParams.add("name");
283     Enumeration<String> initParameterNames = Collections.enumeration(initParams);
284     Mockito.when(servletContext.getInitParameterNames()).thenReturn(initParameterNames);
285 
286     Mockito.when(context.findParameter(Mockito.any())).thenReturn("name");
287 
288     final Tomcat11ContainerAdapter adapter = new Tomcat11ContainerAdapter();
289     assertEquals(1, adapter.getApplicationInitParams(context).size());
290   }
291 
292   /**
293    * Application init params not override attempt.
294    */
295   @Test
296   void applicationInitParamsOverrideAttempt() {
297     ApplicationParameter appParam = new ApplicationParameter();
298     appParam.setName("override");
299     appParam.setOverride(false);
300     Mockito.when(context.findApplicationParameters())
301         .thenReturn(new ApplicationParameter[] {appParam});
302 
303     ServletContext servletContext = Mockito.mock(ServletContext.class);
304     Mockito.when(context.getServletContext()).thenReturn(servletContext);
305 
306     List<String> initParams = new ArrayList<>();
307     initParams.add("override");
308     Enumeration<String> initParameterNames = Collections.enumeration(initParams);
309     Mockito.when(servletContext.getInitParameterNames()).thenReturn(initParameterNames);
310 
311     Mockito.when(context.findParameter(Mockito.any())).thenReturn("override");
312 
313     final Tomcat11ContainerAdapter adapter = new Tomcat11ContainerAdapter();
314     assertEquals(1, adapter.getApplicationInitParams(context).size());
315   }
316 
317   /**
318    * Resource exists.
319    */
320   @Test
321   void resourceExists() {
322     WebResourceRoot webResourceRoot = Mockito.mock(WebResourceRoot.class);
323     Mockito.when(context.getResources()).thenReturn(webResourceRoot);
324 
325     final Tomcat11ContainerAdapter adapter = new Tomcat11ContainerAdapter();
326     assertFalse(adapter.resourceExists("name", context));
327   }
328 
329   /**
330    * Resource exists when true.
331    */
332   @Test
333   void resourceExistsWhenTrue() {
334     WebResourceRoot webResourceRoot = Mockito.mock(WebResourceRoot.class);
335     Mockito.when(context.getResources()).thenReturn(webResourceRoot);
336 
337     WebResource webResource = Mockito.mock(WebResource.class);
338     Mockito.when(webResourceRoot.getResource("name")).thenReturn(webResource);
339 
340     final Tomcat11ContainerAdapter adapter = new Tomcat11ContainerAdapter();
341     assertTrue(adapter.resourceExists("name", context));
342   }
343 
344   /**
345    * Resource stream.
346    *
347    * @throws IOException Signals that an I/O exception has occurred.
348    */
349   @Test
350   void resourceStream() throws IOException {
351     WebResourceRoot webResourceRoot = Mockito.mock(WebResourceRoot.class);
352     Mockito.when(context.getResources()).thenReturn(webResourceRoot);
353 
354     WebResource webResource = Mockito.mock(WebResource.class);
355     Mockito.when(webResourceRoot.getResource("name")).thenReturn(webResource);
356 
357     final Tomcat11ContainerAdapter adapter = new Tomcat11ContainerAdapter();
358     assertNull(adapter.getResourceStream("name", context));
359   }
360 
361   /**
362    * Resource attributes.
363    */
364   @Test
365   void resourceAttributes() {
366     WebResourceRoot webResourceRoot = Mockito.mock(WebResourceRoot.class);
367     Mockito.when(context.getResources()).thenReturn(webResourceRoot);
368 
369     WebResource webResource = Mockito.mock(WebResource.class);
370     Mockito.when(webResourceRoot.getResource("name")).thenReturn(webResource);
371 
372     final Tomcat11ContainerAdapter adapter = new Tomcat11ContainerAdapter();
373     assertNotNull(adapter.getResourceAttributes("name", context));
374   }
375 
376   /**
377    * Gets the naming token.
378    */
379   @Test
380   void namingToken() {
381     Mockito.when(context.getNamingToken()).thenReturn(new Object());
382 
383     final Tomcat11ContainerAdapter adapter = new Tomcat11ContainerAdapter();
384     assertNotNull(adapter.getNamingToken(context));
385   }
386 
387   /**
388    * Gets the naming token with security token check false.
389    */
390   @Test
391   void namingTokenWithSecurityTokenCheckFalse() {
392     Mockito.when(context.getNamingToken()).thenReturn(new Object());
393 
394     try (MockedStatic<ContextAccessController> mocked =
395         Mockito.mockStatic(ContextAccessController.class)) {
396       mocked.when(() -> ContextAccessController.checkSecurityToken(Mockito.any(), Mockito.any()))
397           .thenReturn(false);
398 
399       final Tomcat11ContainerAdapter adapter = new Tomcat11ContainerAdapter();
400       assertNotNull(adapter.getNamingToken(context));
401     }
402   }
403 
404   /**
405    * Application filters when none.
406    */
407   @Test
408   void applicationFiltersWhenNone() {
409     Mockito.when(context.findFilterDefs()).thenReturn(new FilterDef[] {(FilterDef) null});
410 
411     final Tomcat11ContainerAdapter adapter = new Tomcat11ContainerAdapter();
412     assertTrue(adapter.getApplicationFilters(context).isEmpty());
413   }
414 
415   /**
416    * Application filter maps when none.
417    */
418   @Test
419   void applicationFilterMapsWhenNone() {
420     Mockito.when(context.findFilterMaps()).thenReturn(new FilterMap[] {(FilterMap) null});
421 
422     final Tomcat11ContainerAdapter adapter = new Tomcat11ContainerAdapter();
423     assertTrue(adapter.getApplicationFilterMaps(context).isEmpty());
424   }
425 
426 }