1717package com .cloud .api ;
1818
1919import static org .mockito .ArgumentMatchers .nullable ;
20+ import com .cloud .api .auth .ListUserTwoFactorAuthenticatorProvidersCmd ;
21+ import com .cloud .api .auth .SetupUserTwoFactorAuthenticationCmd ;
22+ import com .cloud .api .auth .ValidateUserTwoFactorAuthenticationCodeCmd ;
23+ import com .cloud .server .ManagementServer ;
24+ import com .cloud .user .Account ;
25+ import com .cloud .user .AccountManagerImpl ;
26+ import com .cloud .user .AccountService ;
27+ import com .cloud .user .User ;
28+ import com .cloud .user .UserAccount ;
29+ import com .cloud .utils .HttpUtils ;
30+ import com .cloud .vm .UserVmManager ;
31+ import org .apache .cloudstack .api .ApiConstants ;
32+ import org .apache .cloudstack .api .auth .APIAuthenticationManager ;
33+ import org .apache .cloudstack .api .auth .APIAuthenticationType ;
34+ import org .apache .cloudstack .api .auth .APIAuthenticator ;
35+ import org .apache .cloudstack .api .command .admin .config .ListCfgsByCmd ;
36+ import org .apache .cloudstack .framework .config .ConfigKey ;
37+ import org .apache .cloudstack .framework .config .impl .ConfigDepotImpl ;
38+ import org .junit .After ;
39+ import org .junit .Assert ;
40+ import org .junit .Before ;
41+ import org .junit .Test ;
42+ import org .junit .runner .RunWith ;
43+ import org .mockito .Mock ;
44+ import org .mockito .Mockito ;
45+ import org .mockito .junit .MockitoJUnitRunner ;
2046
2147import java .io .IOException ;
2248import java .io .PrintWriter ;
@@ -105,17 +131,20 @@ public class ApiServletTest {
105131 @ Mock
106132 AccountService accountMgr ;
107133
108- @ Mock ConfigKey <Boolean > useForwardHeader ;
134+ @ Mock
135+ ConfigDepotImpl mockConfigDepot ;
136+
109137 StringWriter responseWriter ;
110138
111139 ApiServlet servlet ;
112- ApiServlet spyServlet ;
140+
141+ private ConfigDepotImpl originalConfigDepot ;
142+
113143 @ SuppressWarnings ("unchecked" )
114144 @ Before
115145 public void setup () throws SecurityException , NoSuchFieldException ,
116- IllegalArgumentException , IllegalAccessException , IOException , UnknownHostException {
146+ IllegalArgumentException , IllegalAccessException , IOException {
117147 servlet = new ApiServlet ();
118- spyServlet = Mockito .spy (servlet );
119148 responseWriter = new StringWriter ();
120149 Mockito .when (response .getWriter ()).thenReturn (
121150 new PrintWriter (responseWriter ));
@@ -139,6 +168,7 @@ public void setup() throws SecurityException, NoSuchFieldException,
139168 apiServerField .setAccessible (true );
140169 apiServerField .set (servlet , apiServer );
141170
171+ setupConfigDepotMock ();
142172 }
143173
144174 /**
@@ -159,6 +189,33 @@ public void cleanupEnvironmentHacks() throws Exception {
159189 Field smsField = ApiDBUtils .class .getDeclaredField ("s_ms" );
160190 smsField .setAccessible (true );
161191 smsField .set (null , null );
192+ restoreConfigDepot ();
193+ }
194+
195+ private void setupConfigDepotMock () throws NoSuchFieldException , IllegalAccessException {
196+ Field depotField = ConfigKey .class .getDeclaredField ("s_depot" );
197+ depotField .setAccessible (true );
198+ originalConfigDepot = (ConfigDepotImpl ) depotField .get (null );
199+ depotField .set (null , mockConfigDepot );
200+ Mockito .when (mockConfigDepot .getConfigStringValue (
201+ Mockito .anyString (),
202+ Mockito .any (ConfigKey .Scope .class ),
203+ Mockito .any ()
204+ )).thenReturn (null );
205+ }
206+
207+ private void restoreConfigDepot () throws Exception {
208+ Field depotField = ConfigKey .class .getDeclaredField ("s_depot" );
209+ depotField .setAccessible (true );
210+ depotField .set (null , originalConfigDepot );
211+ }
212+
213+ private void setConfigValue (String configName , String value ) {
214+ Mockito .when (mockConfigDepot .getConfigStringValue (
215+ Mockito .eq (configName ),
216+ Mockito .eq (ConfigKey .Scope .Global ),
217+ Mockito .isNull ()
218+ )).thenReturn (value );
162219 }
163220
164221 @ Test
@@ -269,43 +326,40 @@ public void processRequestInContextLogin() throws UnknownHostException {
269326
270327 @ Test
271328 public void getClientAddressWithXForwardedFor () throws UnknownHostException {
272- String [] proxynet = { "127.0.0.0/8" } ;
273- Mockito . when ( spyServlet . proxyNets ()). thenReturn ( proxynet );
274- Mockito .when (spyServlet . doUseForwardHeaders ()).thenReturn (true );
329+ setConfigValue ( "proxy.header.verify" , "true" ) ;
330+ setConfigValue ( "proxy.cidr" , "127.0.0.0/8" );
331+ Mockito .when (request . getRemoteAddr ()).thenReturn ("127.0.0.1" );
275332 Mockito .when (request .getHeader (Mockito .eq ("X-Forwarded-For" ))).thenReturn ("192.168.1.1" );
276- Assert .assertEquals (InetAddress .getByName ("192.168.1.1" ), spyServlet .getClientAddress (request ));
333+ Assert .assertEquals (InetAddress .getByName ("192.168.1.1" ), ApiServlet .getClientAddress (request ));
277334 }
278335
279336 @ Test
280337 public void getClientAddressWithHttpXForwardedFor () throws UnknownHostException {
281- String [] proxynet = {"127.0.0.0/8" };
282- Mockito .when (spyServlet .proxyNets ()).thenReturn (proxynet );
283- Mockito .when (spyServlet .doUseForwardHeaders ()).thenReturn (true );
338+ setConfigValue ("proxy.header.verify" , "true" );
339+ setConfigValue ("proxy.cidr" , "127.0.0.0/8" );
284340 Mockito .when (request .getHeader (Mockito .eq ("HTTP_X_FORWARDED_FOR" ))).thenReturn ("192.168.1.1" );
285- Assert .assertEquals (InetAddress .getByName ("192.168.1.1" ), spyServlet .getClientAddress (request ));
341+ Assert .assertEquals (InetAddress .getByName ("192.168.1.1" ), ApiServlet .getClientAddress (request ));
286342 }
287343
288344 @ Test
289345 public void getClientAddressWithRemoteAddr () throws UnknownHostException {
290- String [] proxynet = {"127.0.0.0/8" };
291- Mockito .when (spyServlet .proxyNets ()).thenReturn (proxynet );
292- Mockito .when (spyServlet .doUseForwardHeaders ()).thenReturn (true );
293- Assert .assertEquals (InetAddress .getByName ("127.0.0.1" ), spyServlet .getClientAddress (request ));
346+ setConfigValue ("proxy.header.verify" , "true" );
347+ setConfigValue ("proxy.cidr" , "127.0.0.0/8" );
348+ Assert .assertEquals (InetAddress .getByName ("127.0.0.1" ), ApiServlet .getClientAddress (request ));
294349 }
295350
296351 @ Test
297352 public void getClientAddressWithHttpClientIp () throws UnknownHostException {
298- String [] proxynet = {"127.0.0.0/8" };
299- Mockito .when (spyServlet .proxyNets ()).thenReturn (proxynet );
300- Mockito .when (spyServlet .doUseForwardHeaders ()).thenReturn (true );
353+ setConfigValue ("proxy.header.verify" , "true" );
354+ setConfigValue ("proxy.cidr" , "127.0.0.0/8" );
301355 Mockito .when (request .getHeader (Mockito .eq ("HTTP_CLIENT_IP" ))).thenReturn ("192.168.1.1" );
302- Assert .assertEquals (InetAddress .getByName ("192.168.1.1" ), spyServlet .getClientAddress (request ));
356+ Assert .assertEquals (InetAddress .getByName ("192.168.1.1" ), ApiServlet .getClientAddress (request ));
303357 }
304358
305359 @ Test
306360 public void getClientAddressDefault () throws UnknownHostException {
307361 Mockito .when (request .getRemoteAddr ()).thenReturn ("127.0.0.1" );
308- Assert .assertEquals (InetAddress .getByName ("127.0.0.1" ), spyServlet .getClientAddress (request ));
362+ Assert .assertEquals (InetAddress .getByName ("127.0.0.1" ), ApiServlet .getClientAddress (request ));
309363 }
310364
311365 @ Test
0 commit comments