Mercurial > hg > release > thermostat-1.0
changeset 1320:9732c67863d5
Agent Proxy server tests
This patch includes test cases for the previous Agent Proxy server patch.
Reviewed-by: jerboaa
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2013-November/008714.html
PR1460
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/agent/proxy/server/src/test/java/com/redhat/thermostat/agent/proxy/server/AgentProxyControlImplTest.java Thu Nov 14 11:33:02 2013 -0500 @@ -0,0 +1,172 @@ +/* + * Copyright 2012, 2013 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.agent.proxy.server; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.io.File; +import java.rmi.RemoteException; +import java.util.Properties; + +import javax.security.auth.Subject; + +import org.junit.Before; +import org.junit.Test; + +import com.redhat.thermostat.agent.proxy.server.AgentProxyControlImpl.VirtualMachineUtils; +import com.sun.tools.attach.VirtualMachine; + +public class AgentProxyControlImplTest { + + private AgentProxyControlImpl control; + private VirtualMachine vm; + private VirtualMachineUtils vmUtils; + + @Before + public void setup() throws Exception { + vmUtils = mock(VirtualMachineUtils.class); + vm = mock(VirtualMachine.class); + + // Mock VM properties + Properties agentProps = mock(Properties.class); + when(agentProps.getProperty("com.sun.management.jmxremote.localConnectorAddress")) + .thenReturn(null).thenReturn("myJmxUrl"); + when(vm.getAgentProperties()).thenReturn(agentProps); + Properties sysProps = mock(Properties.class); + when(sysProps.getProperty("java.home")).thenReturn("/path/to/java/home"); + when(vm.getSystemProperties()).thenReturn(sysProps); + + when(vmUtils.attach(anyString())).thenReturn(vm); + control = new AgentProxyControlImpl(0, vmUtils); + } + + @Test + public void testAttach() throws Exception { + Subject subject = new Subject(); + addPrincipal(subject); + control.attach(subject); + + verify(vmUtils).attach("0"); + verify(vm, times(2)).getAgentProperties(); + verify(vm).getSystemProperties(); + verify(vm).loadAgent("/path/to/java/home" + File.separator + "lib" + File.separator + "management-agent.jar"); + } + + @Test(expected=SecurityException.class) + public void testAttachDenied() throws Exception { + Subject subject = new Subject(); + control.attach(subject); + } + + @Test + public void testIsAttached() throws Exception { + Subject subject = new Subject(); + addPrincipal(subject); + + assertFalse(control.isAttached(subject)); + control.attach(subject); + assertTrue(control.isAttached(subject)); + } + + @Test(expected=SecurityException.class) + public void testIsAttachedDenied() throws Exception { + Subject subject = new Subject(); + control.isAttached(subject); + } + + @Test + public void testGetAddress() throws Exception { + Subject subject = new Subject(); + addPrincipal(subject); + + control.attach(subject); + String addr = control.getConnectorAddress(subject); + assertEquals("myJmxUrl", addr); + } + + @Test(expected=SecurityException.class) + public void testGetAddressDenied() throws Exception { + Subject subject = new Subject(); + control.getConnectorAddress(subject); + } + + @Test(expected=RemoteException.class) + public void testGetAddressNotAttached() throws Exception { + Subject subject = new Subject(); + addPrincipal(subject); + + control.getConnectorAddress(subject); + } + + @Test + public void testDetach() throws Exception { + Subject subject = new Subject(); + addPrincipal(subject); + + control.attach(subject); + control.detach(subject); + verify(vm).detach(); + } + + @Test + public void testDetachNotAttached() throws Exception { + Subject subject = new Subject(); + addPrincipal(subject); + + control.detach(subject); + verify(vm, never()).detach(); + } + + @Test(expected=SecurityException.class) + public void testDetachDenied() throws Exception { + Subject subject = new Subject(); + control.detach(subject); + } + + private void addPrincipal(Subject subject) { + subject.getPrincipals().add(new AgentProxyPrincipal("TEST")); + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/agent/proxy/server/src/test/java/com/redhat/thermostat/agent/proxy/server/AgentProxyControlWrapperTest.java Thu Nov 14 11:33:02 2013 -0500 @@ -0,0 +1,94 @@ +/* + * Copyright 2012, 2013 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.agent.proxy.server; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +import javax.security.auth.Subject; + +import org.junit.Before; +import org.junit.Test; + +public class AgentProxyControlWrapperTest { + + private AgentProxyControlWrapper control; + private AgentProxyControlImpl impl; + private Subject user; + private AgentProxyLoginContext context; + private ShutdownListener listener; + private RegistryUtils registryUtils; + + @Before + public void setup() throws Exception { + user = new Subject(); + context = mock(AgentProxyLoginContext.class); + impl = mock(AgentProxyControlImpl.class); + listener = mock(ShutdownListener.class); + registryUtils = mock(RegistryUtils.class); + control = new AgentProxyControlWrapper(user, context, impl, listener, registryUtils); + } + + @Test + public void testAttach() throws Exception { + control.attach(); + verify(impl).attach(user); + } + + @Test + public void testIsAttached() throws Exception { + control.isAttached(); + verify(impl).isAttached(user); + } + + @Test + public void testGetAddress() throws Exception { + control.getConnectorAddress(); + verify(impl).getConnectorAddress(user); + } + + @Test + public void testDetach() throws Exception { + control.detach(); + + verify(impl).detach(user); + verify(context).logout(); + verify(listener).shutdown(); + verify(registryUtils).unexportObject(control); + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/agent/proxy/server/src/test/java/com/redhat/thermostat/agent/proxy/server/AgentProxyLoginContextTest.java Thu Nov 14 11:33:02 2013 -0500 @@ -0,0 +1,105 @@ +/* + * Copyright 2012, 2013 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.agent.proxy.server; + +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; +import static org.mockito.Matchers.same; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import javax.security.auth.Subject; +import javax.security.auth.callback.Callback; +import javax.security.auth.callback.CallbackHandler; +import javax.security.auth.login.LoginContext; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.ArgumentCaptor; + +import com.redhat.thermostat.agent.proxy.server.AgentProxyLoginContext.ContextCreator; +import com.redhat.thermostat.agent.proxy.server.AgentProxyLoginModule.AgentProxyCallback; + +public class AgentProxyLoginContextTest { + + private AgentProxyLoginContext context; + private ContextCreator creator; + private Subject user; + private UnixCredentials creds; + private LoginContext unixContext; + private LoginContext ourContext; + + @Before + public void setup() throws Exception { + user = new Subject(); + creds = new UnixCredentials(9000, 9001, 0); + creator = mock(ContextCreator.class); + unixContext = mock(LoginContext.class); + ourContext = mock(LoginContext.class); + when(creator.createContext("UnixLogin", user)).thenReturn(unixContext); + when(creator.createContext(eq("AgentProxyLogin"), same(user), any(CallbackHandler.class))).thenReturn(ourContext); + context = new AgentProxyLoginContext(user, creds, creator); + } + + @Test + public void testCreate() throws Exception { + verify(creator).createContext("UnixLogin", user); + ArgumentCaptor<CallbackHandler> captor = ArgumentCaptor.forClass(CallbackHandler.class); + verify(creator).createContext(eq("AgentProxyLogin"), same(user), captor.capture()); + CallbackHandler handler = captor.getValue(); + + AgentProxyCallback callback = mock(AgentProxyCallback.class); + handler.handle(new Callback[] { callback }); + verify(callback).setTargetCredentials(creds); + } + + @Test + public void testLogin() throws Exception { + context.login(); + verify(unixContext).login(); + verify(ourContext).login(); + } + + @Test + public void testLogout() throws Exception { + context.logout(); + verify(ourContext).logout(); + verify(unixContext).logout(); + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/agent/proxy/server/src/test/java/com/redhat/thermostat/agent/proxy/server/AgentProxyLoginImplTest.java Thu Nov 14 11:33:02 2013 -0500 @@ -0,0 +1,117 @@ +/* + * Copyright 2012, 2013 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.agent.proxy.server; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.same; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.rmi.RemoteException; +import java.rmi.registry.Registry; + +import javax.security.auth.Subject; +import javax.security.auth.login.LoginException; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.ArgumentCaptor; + +import com.redhat.thermostat.agent.proxy.common.AgentProxyControl; +import com.redhat.thermostat.agent.proxy.server.AgentProxyLoginImpl.LoginContextCreator; + +public class AgentProxyLoginImplTest { + + private RegistryUtils registryUtils; + private Registry registry; + private UnixCredentials creds; + private LoginContextCreator contextCreator; + private AgentProxyLoginContext context; + + @Before + public void setup() throws Exception { + registry = mock(Registry.class); + registryUtils = mock(RegistryUtils.class); + when(registryUtils.getRegistry()).thenReturn(registry); + creds = new UnixCredentials(9000, 9001, 0); + contextCreator = mock(LoginContextCreator.class); + context = mock(AgentProxyLoginContext.class); + when(contextCreator.createContext(any(Subject.class), same(creds))).thenReturn(context); + } + + @Test + public void testLoginSuccess() throws Exception { + ShutdownListener listener = mock(ShutdownListener.class); + AgentProxyLoginImpl proxyLogin = new AgentProxyLoginImpl(creds, 0, listener, contextCreator, registryUtils); + AgentProxyControl stub = proxyLogin.login(); + + ArgumentCaptor<AgentProxyControl> captor = ArgumentCaptor.forClass(AgentProxyControl.class); + verify(registryUtils).exportObject(captor.capture()); + AgentProxyControl control = captor.getValue(); + + assertTrue(control instanceof AgentProxyControlWrapper); + assertFalse(stub instanceof AgentProxyControlWrapper); + } + + @Test + public void testLoginFailure() throws Exception { + ShutdownListener listener = mock(ShutdownListener.class); + + // Simulate login failure + LoginException ex = new LoginException("TEST"); + doThrow(ex).when(context).login(); + + AgentProxyLoginImpl proxyLogin = new AgentProxyLoginImpl(creds, 0, listener, contextCreator, registryUtils); + + try { + proxyLogin.login(); + fail("Expected exception from login"); + } catch (RemoteException e) { + assertEquals(ex, e.getCause()); + } + + verify(registryUtils, never()).exportObject(any(AgentProxyControl.class)); + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/agent/proxy/server/src/test/java/com/redhat/thermostat/agent/proxy/server/AgentProxyLoginModuleTest.java Thu Nov 14 11:33:02 2013 -0500 @@ -0,0 +1,246 @@ +/* + * Copyright 2012, 2013 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.agent.proxy.server; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.*; + +import java.util.HashMap; +import java.util.Set; + +import javax.security.auth.Subject; +import javax.security.auth.callback.Callback; +import javax.security.auth.callback.CallbackHandler; +import javax.security.auth.login.LoginException; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +import com.redhat.thermostat.agent.proxy.server.AgentProxyLoginModule.AgentProxyCallback; + +public class AgentProxyLoginModuleTest { + + private AgentProxyLoginModule module; + private CallbackHandler handler; + private Subject subject; + + @Before + public void setup() throws Exception { + module = new AgentProxyLoginModule(); + subject = new Subject(); + handler = mock(CallbackHandler.class); + final UnixCredentials creds = new UnixCredentials(9000, 9001, 0); + doAnswer(new Answer<Void>() { + @Override + public Void answer(InvocationOnMock invocation) throws Throwable { + Callback[] callbacks = (Callback[]) invocation.getArguments()[0]; + for (Callback callback : callbacks) { + if (callback instanceof AgentProxyCallback) { + ((AgentProxyCallback) callback).setTargetCredentials(creds); + } + } + return null; + } + }).when(handler).handle(any(Callback[].class)); + module.initialize(subject, handler, new HashMap<String, Object>(), new HashMap<String, Object>()); + } + + @Test + public void testLoginSuccess() throws Exception { + addPrincipals(); + + assertTrue(module.login()); + + AgentProxyPrincipal principal = module.getPrincipal(); + assertNotNull(principal); + assertEquals("TEST", principal.getName()); + assertTrue(module.isLoggedIn()); + assertFalse(module.isCommitted()); + assertTrue(subject.getPrincipals(AgentProxyPrincipal.class).isEmpty()); + } + + @SuppressWarnings("restriction") + @Test + public void testLoginBadUid() throws Exception { + subject.getPrincipals().add(new com.sun.security.auth.UnixPrincipal("TEST")); + subject.getPrincipals().add(new com.sun.security.auth.UnixNumericUserPrincipal(8000)); + subject.getPrincipals().add(new com.sun.security.auth.UnixNumericGroupPrincipal(9001, true)); + + verifyFailedLogin(); + } + + @SuppressWarnings("restriction") + @Test + public void testLoginMissingUid() throws Exception { + subject.getPrincipals().add(new com.sun.security.auth.UnixPrincipal("TEST")); + subject.getPrincipals().add(new com.sun.security.auth.UnixNumericGroupPrincipal(9001, true)); + + verifyFailedLogin(); + } + + @SuppressWarnings("restriction") + @Test + public void testLoginBadGid() throws Exception { + subject.getPrincipals().add(new com.sun.security.auth.UnixPrincipal("TEST")); + subject.getPrincipals().add(new com.sun.security.auth.UnixNumericUserPrincipal(9000)); + subject.getPrincipals().add(new com.sun.security.auth.UnixNumericGroupPrincipal(8001, true)); + + verifyFailedLogin(); + } + + @SuppressWarnings("restriction") + @Test + public void testLoginMissingGid() throws Exception { + subject.getPrincipals().add(new com.sun.security.auth.UnixPrincipal("TEST")); + subject.getPrincipals().add(new com.sun.security.auth.UnixNumericUserPrincipal(9000)); + + verifyFailedLogin(); + } + + @SuppressWarnings("restriction") + @Test + public void testLoginMissingUsername() throws Exception { + subject.getPrincipals().add(new com.sun.security.auth.UnixNumericUserPrincipal(9000)); + subject.getPrincipals().add(new com.sun.security.auth.UnixNumericGroupPrincipal(9001, true)); + + verifyFailedLogin(); + } + + @Test + public void testCommitSuccess() throws Exception { + addPrincipals(); + + assertTrue(module.login()); + assertTrue(module.commit()); + + assertTrue(module.isLoggedIn()); + assertTrue(module.isCommitted()); + Set<AgentProxyPrincipal> principals = subject.getPrincipals(AgentProxyPrincipal.class); + assertFalse(principals.isEmpty()); + assertEquals(module.getPrincipal(), principals.iterator().next()); + } + + @Test + public void testCommitNotLoggedIn() throws Exception { + addPrincipals(); + + assertFalse(module.commit()); + + assertFalse(module.isLoggedIn()); + assertFalse(module.isCommitted()); + assertTrue(subject.getPrincipals(AgentProxyPrincipal.class).isEmpty()); + } + + @Test + public void testAbortNotLoggedIn() throws Exception { + addPrincipals(); + + assertFalse(module.abort()); + + verifyStateReset(); + } + + @Test + public void testAbortNotCommitted() throws Exception { + addPrincipals(); + + assertTrue(module.login()); + assertTrue(module.abort()); + + verifyStateReset(); + } + + @Test + public void testAbortCommitted() throws Exception { + addPrincipals(); + + assertTrue(module.login()); + assertTrue(module.commit()); + assertTrue(module.abort()); + + verifyStateReset(); + } + + @Test + public void testLogout() throws Exception { + addPrincipals(); + + assertTrue(module.login()); + assertTrue(module.commit()); + assertTrue(module.logout()); + + verifyStateReset(); + } + + @SuppressWarnings("restriction") + private void addPrincipals() { + subject.getPrincipals().add(new com.sun.security.auth.UnixPrincipal("TEST")); + subject.getPrincipals().add(new com.sun.security.auth.UnixNumericUserPrincipal(9000)); + subject.getPrincipals().add(new com.sun.security.auth.UnixNumericGroupPrincipal(9001, true)); + } + + private void verifyFailedLogin() { + try { + module.login(); + fail("Expected LoginException"); + } catch (LoginException e) { + assertFalse(module.isLoggedIn()); + assertNull(module.getPrincipal()); + assertFalse(module.isCommitted()); + assertTrue(subject.getPrincipals(AgentProxyPrincipal.class).isEmpty()); + } + } + + @SuppressWarnings("restriction") + private void verifyStateReset() { + assertFalse(module.isLoggedIn()); + assertFalse(module.isCommitted()); + assertNull(module.getPrincipal()); + assertTrue(subject.getPrincipals(AgentProxyPrincipal.class).isEmpty()); + assertFalse(subject.getPrincipals(com.sun.security.auth.UnixPrincipal.class).isEmpty()); + assertFalse(subject.getPrincipals(com.sun.security.auth.UnixNumericUserPrincipal.class).isEmpty()); + assertFalse(subject.getPrincipals(com.sun.security.auth.UnixNumericGroupPrincipal.class).isEmpty()); + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/agent/proxy/server/src/test/java/com/redhat/thermostat/agent/proxy/server/AgentProxyTest.java Thu Nov 14 11:33:02 2013 -0500 @@ -0,0 +1,141 @@ +/* + * Copyright 2012, 2013 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.agent.proxy.server; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.rmi.RemoteException; +import java.rmi.registry.Registry; +import java.util.Timer; +import java.util.TimerTask; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.ArgumentCaptor; + +import com.redhat.thermostat.agent.proxy.common.AgentProxyListener; +import com.redhat.thermostat.agent.proxy.common.AgentProxyLogin; + +public class AgentProxyTest { + + private AgentProxyNativeUtils nativeUtils; + private RegistryUtils registryUtils; + private Registry registry; + private AgentProxyLogin loginStub; + private AgentProxyListener listener; + private Timer timeoutTimer; + + @Before + public void setup() throws Exception { + registry = mock(Registry.class); + listener = mock(AgentProxyListener.class); + when(registry.lookup(AgentProxyListener.REMOTE_PREFIX + "0")).thenReturn(listener); + registryUtils = mock(RegistryUtils.class); + when(registryUtils.getRegistry()).thenReturn(registry); + loginStub = mock(AgentProxyLogin.class); + when(registryUtils.exportObject(any(AgentProxyLogin.class))).thenReturn(loginStub); + + nativeUtils = mock(AgentProxyNativeUtils.class); + ProcessUserInfoBuilder builder = mock(ProcessUserInfoBuilder.class); + when(builder.build(0)).thenReturn(new UnixCredentials(9000, 9001, 0)); + timeoutTimer = mock(Timer.class); + AgentProxy.setRegistryUtils(registryUtils); + AgentProxy.setNativeUtils(nativeUtils); + AgentProxy.setProcessUserInfoBuilder(builder); + AgentProxy.setTimeoutTimer(timeoutTimer); + } + + @Test + public void testMainSuccess() throws Exception { + assertFalse(AgentProxy.isBound()); + + // Invoke main with PID of 0 + AgentProxy.main(new String[] { "0" }); + + assertTrue(AgentProxy.isBound()); + + // Verify timeout set + verify(timeoutTimer).schedule(any(TimerTask.class), any(Long.class)); + + // Verify native library loaded and credentials properly set + verify(nativeUtils).loadLibrary(); + verify(nativeUtils).setCredentials(9000, 9001); + + // Verify login object exported + AgentProxyLogin proxyLogin = AgentProxy.getAgentProxyLogin(); + verify(registryUtils).exportObject(proxyLogin); + verify(registry).rebind(AgentProxyLogin.REMOTE_PREFIX + "0", loginStub); + + // Verify listener notified with positive response + verify(listener).serverStarted(); + + // Shutdown server + ShutdownListener shutdownListener = AgentProxy.getShutdownListener(); + shutdownListener.shutdown(); + + // Verify login object unexported + verify(registry).unbind(AgentProxyLogin.REMOTE_PREFIX + "0"); + verify(registryUtils).unexportObject(proxyLogin); + + assertFalse(AgentProxy.isBound()); + } + + @Test + public void testMainFailure() throws Exception { + // Simulate failure binding the login object + RemoteException ex = new RemoteException("TEST"); + doThrow(ex).when(registry).rebind(AgentProxyLogin.REMOTE_PREFIX + "0", loginStub); + + // Invoke main with PID of 0 + AgentProxy.main(new String[] { "0" }); + + // Verify listener notified with negative response + ArgumentCaptor<Exception> errorCaptor = ArgumentCaptor.forClass(Exception.class); + verify(listener).serverFailedToStart(errorCaptor.capture()); + assertEquals(ex, errorCaptor.getValue().getCause()); + + assertFalse(AgentProxy.isBound()); + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/agent/proxy/server/src/test/java/com/redhat/thermostat/agent/proxy/server/ProcessUserInfoBuilderTest.java Thu Nov 14 11:33:02 2013 -0500 @@ -0,0 +1,84 @@ +/* + * Copyright 2012, 2013 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.agent.proxy.server; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Matchers.anyInt; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.io.IOException; +import java.io.StringReader; + +import org.junit.Test; + +import com.redhat.thermostat.common.tools.ApplicationException; + +public class ProcessUserInfoBuilderTest { + + @Test + public void testBuild() throws IOException { + StringReader reader = new StringReader("Uid: 2000 2000 2000 2000\nGid: 2001 2001 2001 2001"); + ProcDataSource source = mock(ProcDataSource.class); + when(source.getStatusReader(anyInt())).thenReturn(reader); + ProcessUserInfoBuilder builder = new ProcessUserInfoBuilder(source); + UnixCredentials creds = builder.build(1); + + assertEquals(2000, creds.getUid()); + assertEquals(2001, creds.getGid()); + assertEquals(1, creds.getPid()); + } + + @Test(expected=IOException.class) + public void testBuildErrorUid() throws IOException, ApplicationException { + StringReader reader = new StringReader("Gid: 2001 2001 2001 2001"); + ProcDataSource source = mock(ProcDataSource.class); + when(source.getStatusReader(anyInt())).thenReturn(reader); + ProcessUserInfoBuilder builder = new ProcessUserInfoBuilder(source); + builder.build(0); + } + + @Test(expected=IOException.class) + public void testBuildErrorGid() throws IOException, ApplicationException { + StringReader reader = new StringReader("Uid: 2000 2000 2000 2000"); + ProcDataSource source = mock(ProcDataSource.class); + when(source.getStatusReader(anyInt())).thenReturn(reader); + ProcessUserInfoBuilder builder = new ProcessUserInfoBuilder(source); + builder.build(0); + } + +}