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 Tomcat10ContainerAdapterTest.
54   */
55  @ExtendWith(MockitoExtension.class)
56  class Tomcat10ContainerAdapterTest {
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 Tomcat10ContainerAdapter adapter = new Tomcat10ContainerAdapter();
72      Valve valve = adapter.createValve();
73      assertEquals("Tomcat10AgentValve[Container is null]", valve.toString());
74    }
75  
76    /**
77     * Can bound to null.
78     */
79    @Test
80    void canBoundToNull() {
81      final Tomcat10ContainerAdapter adapter = new Tomcat10ContainerAdapter();
82      assertFalse(adapter.canBoundTo(null));
83    }
84  
85    /**
86     * Can bound to tomcat 10.1, tomee 10.0, nsjsp 10.1, vmware tc 10.1.
87     *
88     * @param container the container
89     */
90    @ParameterizedTest
91    @ValueSource(strings = {"Apache Tomcat/10.1", "Apache Tomcat (TomEE)/10.0",
92        "NonStop(tm) Servlets For JavaServer Pages(tm) v10.1", "Vmware tc..../10.1"})
93    void canBoundTo(String container) {
94      final Tomcat10ContainerAdapter adapter = new Tomcat10ContainerAdapter();
95      assertTrue(adapter.canBoundTo(container));
96    }
97  
98    /**
99     * Can not bound to other containers.
100    *
101    * @param container the container
102    */
103   @ParameterizedTest
104   @ValueSource(strings = {"Vmware tc", "Other"})
105   void cannotBoundToOthers(String container) {
106     final Tomcat10ContainerAdapter adapter = new Tomcat10ContainerAdapter();
107     assertFalse(adapter.canBoundTo(container));
108   }
109 
110   /**
111    * Filter mappings.
112    */
113   @Test
114   void filterMappings() {
115     final Tomcat10ContainerAdapter adapter = new Tomcat10ContainerAdapter();
116     FilterMap map = new FilterMap();
117     map.addServletName("psi-probe");
118     map.addURLPattern("/psi-probe");
119     assertEquals(2, adapter.getFilterMappings(map, "dispatcherMap", "filterClass").size());
120   }
121 
122   /**
123    * Creates the jsp compilation context.
124    */
125   @Test
126   void createJspCompilationContext() {
127     final Tomcat10ContainerAdapter adapter = new Tomcat10ContainerAdapter();
128     Mockito.when(this.options.getGeneratedJspPackageName()).thenReturn("org.apache.jsp");
129     JspCompilationContext jspContext = adapter.createJspCompilationContext("name", this.options,
130         null, null, ClassLoader.getSystemClassLoader());
131     assertEquals("org.apache.jsp.name", jspContext.getFQCN());
132   }
133 
134   /**
135    * Adds the context resource link.
136    */
137   @Test
138   void addContextResourceLink() {
139     NamingResourcesImpl namingResources = Mockito.mock(NamingResourcesImpl.class);
140     Mockito.when(context.getNamingResources()).thenReturn(namingResources);
141     Mockito.when(namingResources.findResourceLinks())
142         .thenReturn(new ContextResourceLink[] {new ContextResourceLink()});
143 
144     final Tomcat10ContainerAdapter adapter = new Tomcat10ContainerAdapter();
145     final List<ApplicationResource> list = new ArrayList<ApplicationResource>();
146     adapter.addContextResourceLink(context, list);
147     assertFalse(list.isEmpty());
148   }
149 
150   /**
151    * Adds the context resource.
152    */
153   @Test
154   void addContextResource() {
155     NamingResourcesImpl namingResources = Mockito.mock(NamingResourcesImpl.class);
156     Mockito.when(context.getNamingResources()).thenReturn(namingResources);
157     Mockito.when(namingResources.findResources())
158         .thenReturn(new ContextResource[] {new ContextResource()});
159 
160     final Tomcat10ContainerAdapter adapter = new Tomcat10ContainerAdapter();
161     final List<ApplicationResource> list = new ArrayList<ApplicationResource>();
162     adapter.addContextResource(context, list);
163     assertFalse(list.isEmpty());
164   }
165 
166   /**
167    * Gets the application filter maps.
168    */
169   @Test
170   void applicationFilterMaps() {
171     Mockito.when(context.findFilterMaps()).thenReturn(new FilterMap[] {new FilterMap()});
172 
173     final Tomcat10ContainerAdapter adapter = new Tomcat10ContainerAdapter();
174     assertEquals(0, adapter.getApplicationFilterMaps(context).size());
175   }
176 
177   /**
178    * Gets the application filter maps async.
179    *
180    * @param dispatcher the dispatcher
181    */
182   @ParameterizedTest
183   @ValueSource(strings = {"ASYNC", "ERROR", "FORWARD", "INCLUDE", "NONE"})
184   void applicationFilterMapsTypes(String dispatcher) {
185     FilterMap filterMap = new FilterMap();
186     filterMap.setDispatcher(dispatcher);
187     Mockito.when(context.findFilterMaps()).thenReturn(new FilterMap[] {filterMap});
188 
189     Mockito.when(context.findFilterDef(Mockito.any())).thenReturn(new FilterDef());
190 
191     final Tomcat10ContainerAdapter adapter = new Tomcat10ContainerAdapter();
192     assertEquals(0, adapter.getApplicationFilterMaps(context).size());
193   }
194 
195   /**
196    * Gets the application filter maps throws on unknown dispatcher mapping.
197    */
198   @Test
199   void applicationFilterMapsThrowsOnUnknownDispatcherMapping() {
200     Context mockContext = Mockito.mock(Context.class);
201     FilterMap filterMap = Mockito.mock(FilterMap.class);
202 
203     // Return an invalid dispatcher mapping value
204     Mockito.when(filterMap.getDispatcherMapping()).thenReturn(-1);
205     Mockito.when(mockContext.findFilterMaps()).thenReturn(new FilterMap[] {filterMap});
206 
207     Tomcat10ContainerAdapter adapter = new Tomcat10ContainerAdapter();
208 
209     assertThrows(NullPointerException.class, () -> adapter.getApplicationFilterMaps(mockContext));
210   }
211 
212   /**
213    * Application filters.
214    */
215   @Test
216   void applicationFilters() {
217     Mockito.when(context.findFilterDefs()).thenReturn(new FilterDef[] {new FilterDef()});
218 
219     final Tomcat10ContainerAdapter adapter = new Tomcat10ContainerAdapter();
220     assertEquals(1, adapter.getApplicationFilters(context).size());
221   }
222 
223   /**
224    * Application init params.
225    */
226   @Test
227   void applicationInitParams() {
228     Mockito.when(context.findApplicationParameters())
229         .thenReturn(new ApplicationParameter[] {new ApplicationParameter()});
230 
231     ServletContext servletContext = Mockito.mock(ServletContext.class);
232     Mockito.when(context.getServletContext()).thenReturn(servletContext);
233 
234     List<String> initParams = new ArrayList<>();
235     initParams.add("name");
236     Enumeration<String> initParameterNames = Collections.enumeration(initParams);
237     Mockito.when(servletContext.getInitParameterNames()).thenReturn(initParameterNames);
238 
239     final Tomcat10ContainerAdapter adapter = new Tomcat10ContainerAdapter();
240     assertEquals(1, adapter.getApplicationInitParams(context).size());
241   }
242 
243   /**
244    * Application init params none.
245    */
246   @Test
247   void applicationInitParamsNone() {
248     Mockito.when(context.findApplicationParameters())
249         .thenReturn(new ApplicationParameter[] {(ApplicationParameter) null});
250 
251     ServletContext servletContext = Mockito.mock(ServletContext.class);
252     Mockito.when(context.getServletContext()).thenReturn(servletContext);
253 
254     List<String> initParams = new ArrayList<>();
255     initParams.add("name");
256     Enumeration<String> initParameterNames = Collections.enumeration(initParams);
257     Mockito.when(servletContext.getInitParameterNames()).thenReturn(initParameterNames);
258 
259     Mockito.when(context.findParameter(Mockito.any())).thenReturn(null);
260 
261     final Tomcat10ContainerAdapter adapter = new Tomcat10ContainerAdapter();
262     assertEquals(1, adapter.getApplicationInitParams(context).size());
263   }
264 
265   /**
266    * Application init params not override attempt.
267    */
268   @Test
269   void applicationInitParamsNotOverrideAttempt() {
270     ApplicationParameter appParam = new ApplicationParameter();
271     appParam.setName("noOverride");
272     appParam.setOverride(false);
273     Mockito.when(context.findApplicationParameters())
274         .thenReturn(new ApplicationParameter[] {appParam});
275 
276     ServletContext servletContext = Mockito.mock(ServletContext.class);
277     Mockito.when(context.getServletContext()).thenReturn(servletContext);
278 
279     List<String> initParams = new ArrayList<>();
280     initParams.add("name");
281     Enumeration<String> initParameterNames = Collections.enumeration(initParams);
282     Mockito.when(servletContext.getInitParameterNames()).thenReturn(initParameterNames);
283 
284     Mockito.when(context.findParameter(Mockito.any())).thenReturn("name");
285 
286     final Tomcat10ContainerAdapter adapter = new Tomcat10ContainerAdapter();
287     assertEquals(1, adapter.getApplicationInitParams(context).size());
288   }
289 
290   /**
291    * Application init params not override attempt.
292    */
293   @Test
294   void applicationInitParamsOverrideAttempt() {
295     ApplicationParameter appParam = new ApplicationParameter();
296     appParam.setName("override");
297     appParam.setOverride(false);
298     Mockito.when(context.findApplicationParameters())
299         .thenReturn(new ApplicationParameter[] {appParam});
300 
301     ServletContext servletContext = Mockito.mock(ServletContext.class);
302     Mockito.when(context.getServletContext()).thenReturn(servletContext);
303 
304     List<String> initParams = new ArrayList<>();
305     initParams.add("override");
306     Enumeration<String> initParameterNames = Collections.enumeration(initParams);
307     Mockito.when(servletContext.getInitParameterNames()).thenReturn(initParameterNames);
308 
309     Mockito.when(context.findParameter(Mockito.any())).thenReturn("override");
310 
311     final Tomcat10ContainerAdapter adapter = new Tomcat10ContainerAdapter();
312     assertEquals(1, adapter.getApplicationInitParams(context).size());
313   }
314 
315   /**
316    * Resource exists.
317    */
318   @Test
319   void resourceExists() {
320     WebResourceRoot webResourceRoot = Mockito.mock(WebResourceRoot.class);
321     Mockito.when(context.getResources()).thenReturn(webResourceRoot);
322 
323     final Tomcat10ContainerAdapter adapter = new Tomcat10ContainerAdapter();
324     assertFalse(adapter.resourceExists("name", context));
325   }
326 
327   /**
328    * Resource exists when true.
329    */
330   @Test
331   void resourceExistsWhenTrue() {
332     WebResourceRoot webResourceRoot = Mockito.mock(WebResourceRoot.class);
333     Mockito.when(context.getResources()).thenReturn(webResourceRoot);
334 
335     WebResource webResource = Mockito.mock(WebResource.class);
336     Mockito.when(webResourceRoot.getResource("name")).thenReturn(webResource);
337 
338     final Tomcat10ContainerAdapter adapter = new Tomcat10ContainerAdapter();
339     assertTrue(adapter.resourceExists("name", context));
340   }
341 
342   /**
343    * Resource stream.
344    *
345    * @throws IOException Signals that an I/O exception has occurred.
346    */
347   @Test
348   void resourceStream() throws IOException {
349     WebResourceRoot webResourceRoot = Mockito.mock(WebResourceRoot.class);
350     Mockito.when(context.getResources()).thenReturn(webResourceRoot);
351 
352     WebResource webResource = Mockito.mock(WebResource.class);
353     Mockito.when(webResourceRoot.getResource("name")).thenReturn(webResource);
354 
355     final Tomcat10ContainerAdapter adapter = new Tomcat10ContainerAdapter();
356     assertNull(adapter.getResourceStream("name", context));
357   }
358 
359   /**
360    * Resource attributes.
361    */
362   @Test
363   void resourceAttributes() {
364     WebResourceRoot webResourceRoot = Mockito.mock(WebResourceRoot.class);
365     Mockito.when(context.getResources()).thenReturn(webResourceRoot);
366 
367     WebResource webResource = Mockito.mock(WebResource.class);
368     Mockito.when(webResourceRoot.getResource("name")).thenReturn(webResource);
369 
370     final Tomcat10ContainerAdapter adapter = new Tomcat10ContainerAdapter();
371     assertNotNull(adapter.getResourceAttributes("name", context));
372   }
373 
374   /**
375    * Gets the naming token.
376    */
377   @Test
378   void namingToken() {
379     Mockito.when(context.getNamingToken()).thenReturn(new Object());
380 
381     final Tomcat10ContainerAdapter adapter = new Tomcat10ContainerAdapter();
382     assertNotNull(adapter.getNamingToken(context));
383   }
384 
385   /**
386    * Gets the naming token with security token check false.
387    */
388   @Test
389   void namingTokenWithSecurityTokenCheckFalse() {
390     Mockito.when(context.getNamingToken()).thenReturn(new Object());
391 
392     try (MockedStatic<ContextAccessController> mocked =
393         Mockito.mockStatic(ContextAccessController.class)) {
394       mocked.when(() -> ContextAccessController.checkSecurityToken(Mockito.any(), Mockito.any()))
395           .thenReturn(false);
396 
397       final Tomcat10ContainerAdapter adapter = new Tomcat10ContainerAdapter();
398       assertNotNull(adapter.getNamingToken(context));
399     }
400   }
401 
402   /**
403    * Application filters when none.
404    */
405   @Test
406   void applicationFiltersWhenNone() {
407     Mockito.when(context.findFilterDefs()).thenReturn(new FilterDef[] {(FilterDef) null});
408 
409     final Tomcat10ContainerAdapter adapter = new Tomcat10ContainerAdapter();
410     assertTrue(adapter.getApplicationFilters(context).isEmpty());
411   }
412 
413   /**
414    * Application filter maps when none.
415    */
416   @Test
417   void applicationFilterMapsWhenNone() {
418     Mockito.when(context.findFilterMaps()).thenReturn(new FilterMap[] {(FilterMap) null});
419 
420     final Tomcat10ContainerAdapter adapter = new Tomcat10ContainerAdapter();
421     assertTrue(adapter.getApplicationFilterMaps(context).isEmpty());
422   }
423 
424 }