Run Easymock automation tests on LambdaTest cloud grid
Perform automation testing on 3000+ real desktop and mobile devices online.
/*******************************************************************************
* Copyright (c) 2011, 2017 SAP AG and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Lazar Kirchev, SAP AG - initial API and implementation
*******************************************************************************/
package org.eclipse.equinox.console.telnet;
import static org.junit.Assert.fail;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Dictionary;
import java.util.Hashtable;
import org.apache.felix.service.command.CommandProcessor;
import org.apache.felix.service.command.CommandSession;
import org.easymock.EasyMock;
import org.eclipse.equinox.console.common.ConsoleInputStream;
import org.junit.Test;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.cm.ManagedService;
public class TelnetCommandWithConfigAdminTests {
private static final int TEST_CONTENT = 100;
private static final String STOP_COMMAND = "stop";
private static final String HOST = "localhost";
private static final String TELNET_PORT = "2223";
private static final long WAIT_TIME = 5000;
private static final String USE_CONFIG_ADMIN_PROP = "osgi.console.useConfigAdmin";
private static final String TRUE = "true";
private static final String FALSE = "false";
private ManagedService configurator;
@Test
public void testTelnetCommandWithConfigAdminEnabledTelnet() throws Exception {
CommandSession session = EasyMock.createMock(CommandSession.class);
session.put((String)EasyMock.anyObject(), EasyMock.anyObject());
EasyMock.expectLastCall().times(3);
EasyMock.expect(session.execute((String)EasyMock.anyObject())).andReturn(new Object());
session.close();
EasyMock.expectLastCall();
EasyMock.replay(session);
CommandProcessor processor = EasyMock.createMock(CommandProcessor.class);
EasyMock.expect(processor.createSession((ConsoleInputStream)EasyMock.anyObject(), (PrintStream)EasyMock.anyObject(), (PrintStream)EasyMock.anyObject())).andReturn(session);
EasyMock.replay(processor);
final ServiceRegistration<?> registration = EasyMock.createMock(ServiceRegistration.class);
registration.setProperties((Dictionary<String, ?>)EasyMock.anyObject());
EasyMock.expectLastCall();
EasyMock.replay(registration);
BundleContext context = EasyMock.createMock(BundleContext.class);
EasyMock.expect(context.getProperty(USE_CONFIG_ADMIN_PROP)).andReturn(TRUE);
EasyMock.expect(
(ServiceRegistration) context.registerService(
(String)EasyMock.anyObject(),
(ManagedService)EasyMock.anyObject(),
(Dictionary<String, ?>)EasyMock.anyObject())
).andAnswer(() -> {
configurator = (ManagedService) EasyMock.getCurrentArguments()[1];
return registration;
});
EasyMock.expect(
context.registerService(
(String)EasyMock.anyObject(),
(TelnetCommand)EasyMock.anyObject(),
(Dictionary<String, ?>)EasyMock.anyObject())).andReturn(null);
EasyMock.replay(context);
TelnetCommand command = new TelnetCommand(processor, context);
command.startService();
Dictionary<String, String> props = new Hashtable<>();
props.put("port", TELNET_PORT);
props.put("host", HOST);
props.put("enabled", TRUE);
configurator.updated(props);
try (Socket socketClient = new Socket(HOST, Integer.parseInt(TELNET_PORT));){
OutputStream outClient = socketClient.getOutputStream();
outClient.write(TEST_CONTENT);
outClient.write('\n');
outClient.flush();
// wait for the accept thread to finish execution
try {
Thread.sleep(WAIT_TIME);
} catch (InterruptedException ie) {
// do nothing
}
} finally {
command.telnet(new String[] {STOP_COMMAND});
}
EasyMock.verify(context);
}
@Test
public void testTelnetCommandWithConfigAdminDisabledTelnet() throws Exception {
disabledTelnet(false);
}
@Test
public void testTelnetCommandWithConfigAdminDisabledTelnetByDefault() throws Exception {
disabledTelnet(true);
}
private void disabledTelnet(boolean isDefault) throws Exception {
CommandSession session = EasyMock.createMock(CommandSession.class);
session.put((String)EasyMock.anyObject(), EasyMock.anyObject());
EasyMock.expectLastCall().times(4);
EasyMock.expect(session.execute((String)EasyMock.anyObject())).andReturn(new Object());
session.close();
EasyMock.expectLastCall();
EasyMock.replay(session);
CommandProcessor processor = EasyMock.createMock(CommandProcessor.class);
EasyMock.expect(processor.createSession((ConsoleInputStream)EasyMock.anyObject(), (PrintStream)EasyMock.anyObject(), (PrintStream)EasyMock.anyObject())).andReturn(session);
EasyMock.replay(processor);
final ServiceRegistration<?> registration = EasyMock.createMock(ServiceRegistration.class);
registration.setProperties((Dictionary<String, ?>)EasyMock.anyObject());
EasyMock.expectLastCall();
EasyMock.replay(registration);
BundleContext context = EasyMock.createMock(BundleContext.class);
EasyMock.expect(context.getProperty(USE_CONFIG_ADMIN_PROP)).andReturn(TRUE);
EasyMock.expect(
(ServiceRegistration) context.registerService(
(String)EasyMock.anyObject(),
(ManagedService)EasyMock.anyObject(),
(Dictionary<String, ?>)EasyMock.anyObject())
).andAnswer(() -> {
configurator = (ManagedService) EasyMock.getCurrentArguments()[1];
return registration;
});
EasyMock.expect(
context.registerService(
(String)EasyMock.anyObject(),
(TelnetCommand)EasyMock.anyObject(),
(Dictionary<String, ?>)EasyMock.anyObject())).andReturn(null);
EasyMock.replay(context);
TelnetCommand command = new TelnetCommand(processor, context);
command.startService();
Dictionary<String, String> props = new Hashtable<>();
props.put("port", TELNET_PORT);
props.put("host", HOST);
if (isDefault == false) {
props.put("enabled", FALSE);
}
configurator.updated(props);
try (Socket socketClient = new Socket(HOST, Integer.parseInt(TELNET_PORT))){
fail("It should not be possible to open a socket to " + HOST + ":" + TELNET_PORT);
} catch (IOException e) {
// this is ok, there should be an exception
} finally {
try {
command.telnet(new String[] {STOP_COMMAND});
} catch (IllegalStateException e) {
//this is expected
}
}
EasyMock.verify(context);
}
}
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.mediaframeworktest.unit;
import android.content.ContentValues;
import android.content.IContentProvider;
import android.media.MediaInserter;
import android.net.Uri;
import android.provider.MediaStore.Audio;
import android.provider.MediaStore.Files;
import android.provider.MediaStore.Images;
import android.provider.MediaStore.Video;
import android.test.InstrumentationTestCase;
import android.test.suitebuilder.annotation.SmallTest;
import dalvik.annotation.TestTargetClass;
import org.easymock.EasyMock;
import org.easymock.IArgumentMatcher;
@TestTargetClass(MediaInserter.class)
public class MediaInserterTest extends InstrumentationTestCase {
private MediaInserter mMediaInserter;
private static final int TEST_BUFFER_SIZE = 10;
private IContentProvider mMockProvider;
private String mPackageName;
private int mFilesCounter;
private int mAudioCounter;
private int mVideoCounter;
private int mImagesCounter;
private static final String sVolumeName = "external";
private static final Uri sAudioUri = Audio.Media.getContentUri(sVolumeName);
private static final Uri sVideoUri = Video.Media.getContentUri(sVolumeName);
private static final Uri sImagesUri = Images.Media.getContentUri(sVolumeName);
private static final Uri sFilesUri = Files.getContentUri(sVolumeName);
private static class MediaUriMatcher implements IArgumentMatcher {
private Uri mUri;
private MediaUriMatcher(Uri uri) {
mUri = uri;
}
@Override
public boolean matches(Object argument) {
if (!(argument instanceof Uri)) {
return false;
}
Uri actualUri = (Uri) argument;
if (actualUri == mUri) return true;
return false;
}
@Override
public void appendTo(StringBuffer buffer) {
buffer.append("expected a TableUri '").append(mUri).append("'");
}
private static Uri expectMediaUri(Uri in) {
EasyMock.reportMatcher(new MediaUriMatcher(in));
return null;
}
}
@Override
protected void setUp() throws Exception {
super.setUp();
mMockProvider = EasyMock.createMock(IContentProvider.class);
mMediaInserter = new MediaInserter(mMockProvider,
mPackageName, TEST_BUFFER_SIZE);
mPackageName = getInstrumentation().getContext().getPackageName();
mFilesCounter = 0;
mAudioCounter = 0;
mVideoCounter = 0;
mImagesCounter = 0;
}
private ContentValues createFileContent() {
ContentValues values = new ContentValues();
values.put("_data", "/mnt/sdcard/file" + ++mFilesCounter);
return values;
}
private ContentValues createAudioContent() {
ContentValues values = new ContentValues();
values.put("_data", "/mnt/sdcard/audio" + ++mAudioCounter);
return values;
}
private ContentValues createVideoContent() {
ContentValues values = new ContentValues();
values.put("_data", "/mnt/sdcard/video" + ++mVideoCounter);
return values;
}
private ContentValues createImageContent() {
ContentValues values = new ContentValues();
values.put("_data", "/mnt/sdcard/image" + ++mImagesCounter);
return values;
}
private ContentValues createContent(Uri uri) {
if (uri == sFilesUri) return createFileContent();
else if (uri == sAudioUri) return createAudioContent();
else if (uri == sVideoUri) return createVideoContent();
else if (uri == sImagesUri) return createImageContent();
else throw new IllegalArgumentException("Unknown URL: " + uri.toString());
}
private void fillBuffer(Uri uri, int numberOfFiles) throws Exception {
ContentValues values;
for (int i = 0; i < numberOfFiles; ++i) {
values = createContent(uri);
mMediaInserter.insert(uri, values);
}
}
@SmallTest
public void testInsertContentsLessThanBufferSize() throws Exception {
EasyMock.replay(mMockProvider);
fillBuffer(sFilesUri, TEST_BUFFER_SIZE - 4);
fillBuffer(sAudioUri, TEST_BUFFER_SIZE - 3);
fillBuffer(sVideoUri, TEST_BUFFER_SIZE - 2);
fillBuffer(sImagesUri, TEST_BUFFER_SIZE - 1);
EasyMock.verify(mMockProvider);
}
@SmallTest
public void testInsertContentsEqualToBufferSize() throws Exception {
EasyMock.expect(mMockProvider.bulkInsert(mPackageName,
(Uri) EasyMock.anyObject(), (ContentValues[]) EasyMock.anyObject())).andReturn(1);
EasyMock.expectLastCall().times(4);
EasyMock.replay(mMockProvider);
fillBuffer(sFilesUri, TEST_BUFFER_SIZE);
fillBuffer(sAudioUri, TEST_BUFFER_SIZE);
fillBuffer(sVideoUri, TEST_BUFFER_SIZE);
fillBuffer(sImagesUri, TEST_BUFFER_SIZE);
EasyMock.verify(mMockProvider);
}
@SmallTest
public void testInsertContentsMoreThanBufferSize() throws Exception {
EasyMock.expect(mMockProvider.bulkInsert(mPackageName,
(Uri) EasyMock.anyObject(), (ContentValues[]) EasyMock.anyObject())).andReturn(1);
EasyMock.expectLastCall().times(4);
EasyMock.replay(mMockProvider);
fillBuffer(sFilesUri, TEST_BUFFER_SIZE + 1);
fillBuffer(sAudioUri, TEST_BUFFER_SIZE + 2);
fillBuffer(sVideoUri, TEST_BUFFER_SIZE + 3);
fillBuffer(sImagesUri, TEST_BUFFER_SIZE + 4);
EasyMock.verify(mMockProvider);
}
@SmallTest
public void testFlushAllWithEmptyContents() throws Exception {
EasyMock.replay(mMockProvider);
mMediaInserter.flushAll();
EasyMock.verify(mMockProvider);
}
@SmallTest
public void testFlushAllWithSomeContents() throws Exception {
EasyMock.expect(mMockProvider.bulkInsert(mPackageName,
(Uri) EasyMock.anyObject(), (ContentValues[]) EasyMock.anyObject())).andReturn(1);
EasyMock.expectLastCall().times(4);
EasyMock.replay(mMockProvider);
fillBuffer(sFilesUri, TEST_BUFFER_SIZE - 4);
fillBuffer(sAudioUri, TEST_BUFFER_SIZE - 3);
fillBuffer(sVideoUri, TEST_BUFFER_SIZE - 2);
fillBuffer(sImagesUri, TEST_BUFFER_SIZE - 1);
mMediaInserter.flushAll();
EasyMock.verify(mMockProvider);
}
@SmallTest
public void testInsertContentsAfterFlushAll() throws Exception {
EasyMock.expect(mMockProvider.bulkInsert(mPackageName,
(Uri) EasyMock.anyObject(), (ContentValues[]) EasyMock.anyObject())).andReturn(1);
EasyMock.expectLastCall().times(8);
EasyMock.replay(mMockProvider);
fillBuffer(sFilesUri, TEST_BUFFER_SIZE - 4);
fillBuffer(sAudioUri, TEST_BUFFER_SIZE - 3);
fillBuffer(sVideoUri, TEST_BUFFER_SIZE - 2);
fillBuffer(sImagesUri, TEST_BUFFER_SIZE - 1);
mMediaInserter.flushAll();
fillBuffer(sFilesUri, TEST_BUFFER_SIZE + 1);
fillBuffer(sAudioUri, TEST_BUFFER_SIZE + 2);
fillBuffer(sVideoUri, TEST_BUFFER_SIZE + 3);
fillBuffer(sImagesUri, TEST_BUFFER_SIZE + 4);
EasyMock.verify(mMockProvider);
}
@SmallTest
public void testInsertContentsWithDifferentSizePerContentType() throws Exception {
EasyMock.expect(mMockProvider.bulkInsert(mPackageName,
MediaUriMatcher.expectMediaUri(sFilesUri),
(ContentValues[]) EasyMock.anyObject())).andReturn(1);
EasyMock.expectLastCall().times(1);
EasyMock.expect(mMockProvider.bulkInsert(mPackageName,
MediaUriMatcher.expectMediaUri(sAudioUri),
(ContentValues[]) EasyMock.anyObject())).andReturn(1);
EasyMock.expectLastCall().times(2);
EasyMock.expect(mMockProvider.bulkInsert(mPackageName,
MediaUriMatcher.expectMediaUri(sVideoUri),
(ContentValues[]) EasyMock.anyObject())).andReturn(1);
EasyMock.expectLastCall().times(3);
EasyMock.expect(mMockProvider.bulkInsert(mPackageName,
MediaUriMatcher.expectMediaUri(sImagesUri),
(ContentValues[]) EasyMock.anyObject())).andReturn(1);
EasyMock.expectLastCall().times(4);
EasyMock.replay(mMockProvider);
for (int i = 0; i < TEST_BUFFER_SIZE; ++i) {
fillBuffer(sFilesUri, 1);
fillBuffer(sAudioUri, 2);
fillBuffer(sVideoUri, 3);
fillBuffer(sImagesUri, 4);
}
EasyMock.verify(mMockProvider);
}
}
/*******************************************************************************
* Copyright (c) 2011 SAP AG
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Lazar Kirchev, SAP AG - initial API and implementation
*******************************************************************************/
package org.eclipse.equinox.console.ssh;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringBufferInputStream;
import java.util.Collection;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import org.apache.felix.service.command.CommandProcessor;
import org.apache.felix.service.command.CommandSession;
import org.apache.sshd.ClientChannel;
import org.apache.sshd.ClientSession;
import org.apache.sshd.SshClient;
import org.apache.sshd.client.future.ConnectFuture;
import org.apache.sshd.client.future.DefaultConnectFuture;
import org.apache.sshd.common.RuntimeSshException;
import org.apache.sshd.server.Environment;
import org.easymock.EasyMock;
import org.easymock.IAnswer;
import org.eclipse.equinox.console.common.ConsoleInputStream;
import org.eclipse.equinox.console.storage.DigestUtil;
import org.eclipse.equinox.console.storage.SecureUserStore;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.BundleListener;
import org.osgi.framework.Filter;
import org.osgi.framework.FrameworkListener;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceListener;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.cm.ManagedService;
public class SshCommandWithConfigAdminTests {
private static final int TEST_CONTENT = 100;
private static final String USER_STORE_FILE_NAME = "org.eclipse.equinox.console.jaas.file";
private static final String JAAS_CONFIG_FILE_NAME = "jaas.config";
private static final String JAAS_CONFIG_PROPERTY_NAME = "java.security.auth.login.config";
private static final String DEFAULT_USER_STORAGE = "osgi.console.ssh.useDefaultSecureStorage";
private static final String STORE_FILE_NAME = SshCommandTests.class.getName() + "_store";
private static final String GOGO_SHELL_COMMAND = "gosh --login --noshutdown";
private static final String TRUE = "true";
private static final String FALSE = "false";
private static final String USERNAME = "username";
private static final String PASSWORD = "password";
private static final String STOP_COMMAND = "stop";
private static final String TERM_PROPERTY = "TERM";
private static final String XTERM = "XTERM";
private static final String HOST = "localhost";
private static final String SSH_PORT = "2222";
private static final long WAIT_TIME = 5000;
private static final String USE_CONFIG_ADMIN_PROP = "osgi.console.useConfigAdmin";
private ManagedService configurator;
@Before
public void init() throws Exception {
clean();
initStore();
initJaasConfigFile();
}
@Test
public void testSshCommandWithConfigAdmin() throws Exception {
CommandSession session = EasyMock.createMock(CommandSession.class);
EasyMock.makeThreadSafe(session, true);
session.put((String)EasyMock.anyObject(), EasyMock.anyObject());
EasyMock.expectLastCall().times(5);
EasyMock.expect(session.execute(GOGO_SHELL_COMMAND)).andReturn(null);
session.close();
EasyMock.expectLastCall();
EasyMock.replay(session);
CommandProcessor processor = EasyMock.createMock(CommandProcessor.class);
EasyMock.expect(processor.createSession((ConsoleInputStream)EasyMock.anyObject(), (PrintStream)EasyMock.anyObject(), (PrintStream)EasyMock.anyObject())).andReturn(session);
EasyMock.replay(processor);
final ServiceRegistration<?> registration = EasyMock.createMock(ServiceRegistration.class);
registration.setProperties((Dictionary)EasyMock.anyObject());
EasyMock.expectLastCall();
EasyMock.replay(registration);
BundleContext context = EasyMock.createMock(BundleContext.class);
EasyMock.makeThreadSafe(context, true);
EasyMock.expect(context.getProperty(USE_CONFIG_ADMIN_PROP)).andReturn(TRUE);
EasyMock.expect(context.getProperty(DEFAULT_USER_STORAGE)).andReturn(TRUE).anyTimes();
EasyMock.expect(
(ServiceRegistration) context.registerService(
(String)EasyMock.anyObject(),
(ManagedService)EasyMock.anyObject(),
(Dictionary<String, ?>)EasyMock.anyObject())
).andAnswer((IAnswer<ServiceRegistration<?>>) new IAnswer<ServiceRegistration<?>>() {
public ServiceRegistration<?> answer() {
configurator = (ManagedService) EasyMock.getCurrentArguments()[1];
return registration;
}
});
EasyMock.expect(
context.registerService(
(String)EasyMock.anyObject(),
(SshCommand)EasyMock.anyObject(),
(Dictionary<String, ?>)EasyMock.anyObject())).andReturn(null);
EasyMock.replay(context);
Map<String, String> environment = new HashMap<String, String>();
environment.put(TERM_PROPERTY, XTERM);
Environment env = EasyMock.createMock(Environment.class);
EasyMock.expect(env.getEnv()).andReturn(environment);
EasyMock.replay(env);
SshCommand command = new SshCommand(processor, context);
Dictionary props = new Hashtable();
props.put("port", SSH_PORT);
props.put("host", HOST);
props.put("enabled", TRUE);
configurator.updated(props);
SshClient client = SshClient.setUpDefaultClient();
client.start();
try {
ConnectFuture connectFuture = client.connect(HOST, Integer.valueOf(SSH_PORT));
DefaultConnectFuture defaultConnectFuture = (DefaultConnectFuture) connectFuture;
try {
Thread.sleep(WAIT_TIME);
} catch (InterruptedException ie) {
// do nothing
}
ClientSession sshSession = defaultConnectFuture.getSession();
int ret = ClientSession.WAIT_AUTH;
sshSession.authPassword(USERNAME, PASSWORD);
ret = sshSession.waitFor(ClientSession.WAIT_AUTH | ClientSession.CLOSED | ClientSession.AUTHED, 0);
if ((ret & ClientSession.CLOSED) != 0) {
System.err.println("error");
System.exit(-1);
}
ClientChannel channel = sshSession.createChannel("shell");
channel.setIn(new StringBufferInputStream(TEST_CONTENT + "\n"));
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
channel.setOut(byteOut);
channel.setErr(byteOut);
channel.open();
try {
Thread.sleep(WAIT_TIME);
} catch (InterruptedException ie) {
// do nothing
}
byte[] output = byteOut.toByteArray();
Assert.assertEquals("Output not as expected",Integer.toString(TEST_CONTENT), new String(output).trim());
sshSession.close(true);
} finally {
client.stop();
}
command.ssh(new String[] {STOP_COMMAND});
return;
}
@Test
public void testSshCommandWithConfigAdminDisabledSsh() throws Exception {
testDisabled(false);
}
@Test
public void testSshCommandWithConfigAdminDisabledSshByDefault() throws Exception {
testDisabled(true);
}
private void testDisabled(boolean isDefault) throws Exception {
CommandSession session = EasyMock.createMock(CommandSession.class);
session.put((String)EasyMock.anyObject(), EasyMock.anyObject());
EasyMock.expectLastCall().times(4);
EasyMock.expect(session.execute(GOGO_SHELL_COMMAND)).andReturn(null);
session.close();
EasyMock.expectLastCall();
EasyMock.replay(session);
CommandProcessor processor = EasyMock.createMock(CommandProcessor.class);
EasyMock.expect(processor.createSession((ConsoleInputStream)EasyMock.anyObject(), (PrintStream)EasyMock.anyObject(), (PrintStream)EasyMock.anyObject())).andReturn(session);
EasyMock.replay(processor);
final ServiceRegistration<?> registration = EasyMock.createMock(ServiceRegistration.class);
registration.setProperties((Dictionary)EasyMock.anyObject());
EasyMock.expectLastCall();
EasyMock.replay(registration);
BundleContext context = EasyMock.createMock(BundleContext.class);
EasyMock.expect(context.getProperty(USE_CONFIG_ADMIN_PROP)).andReturn(TRUE);
EasyMock.expect(context.getProperty(DEFAULT_USER_STORAGE)).andReturn(TRUE).anyTimes();
EasyMock.expect(
(ServiceRegistration) context.registerService(
(String)EasyMock.anyObject(),
(ManagedService)EasyMock.anyObject(),
(Dictionary<String, ?>)EasyMock.anyObject())
).andAnswer((IAnswer<ServiceRegistration<?>>) new IAnswer<ServiceRegistration<?>>() {
public ServiceRegistration<?> answer() {
configurator = (ManagedService) EasyMock.getCurrentArguments()[1];
return registration;
}
});
EasyMock.expect(
context.registerService(
(String)EasyMock.anyObject(),
(SshCommand)EasyMock.anyObject(),
(Dictionary<String, ?>)EasyMock.anyObject())).andReturn(null);
EasyMock.replay(context);
Map<String, String> environment = new HashMap<String, String>();
environment.put(TERM_PROPERTY, XTERM);
Environment env = EasyMock.createMock(Environment.class);
EasyMock.expect(env.getEnv()).andReturn(environment);
EasyMock.replay(env);
SshCommand command = new SshCommand(processor, context);
Dictionary props = new Hashtable();
props.put("port", SSH_PORT);
props.put("host", HOST);
if (isDefault == false) {
props.put("enabled", FALSE);
}
configurator.updated(props);
SshClient client = SshClient.setUpDefaultClient();
client.start();
try {
ConnectFuture connectFuture = client.connect(HOST, Integer.valueOf(SSH_PORT));
DefaultConnectFuture defaultConnectFuture = (DefaultConnectFuture) connectFuture;
try {
Thread.sleep(WAIT_TIME);
} catch (InterruptedException ie) {
// do nothing
}
ClientSession sshSession;
try {
sshSession = defaultConnectFuture.getSession();
Assert.fail("It should not be possible to connect to " + HOST + ":" + SSH_PORT);
} catch (RuntimeSshException e) {
//this is expected
}
} finally {
client.stop();
}
try {
command.ssh(new String[] {STOP_COMMAND});
} catch (IllegalStateException e) {
// this is expected
}
return;
}
@After
public void cleanUp() {
clean();
}
private void clean() {
System.setProperty(USER_STORE_FILE_NAME, "");
File file = new File(STORE_FILE_NAME);
if (file.exists()) {
file.delete();
}
System.setProperty(JAAS_CONFIG_PROPERTY_NAME, "");
File jaasConfFile = new File(JAAS_CONFIG_FILE_NAME);
if (jaasConfFile.exists()) {
jaasConfFile.delete();
}
}
private void initStore() throws Exception {
System.setProperty(USER_STORE_FILE_NAME, STORE_FILE_NAME);
SecureUserStore.initStorage();
SecureUserStore.putUser(USERNAME, DigestUtil.encrypt(PASSWORD), null);
}
private void initJaasConfigFile() throws Exception {
System.setProperty(JAAS_CONFIG_PROPERTY_NAME, JAAS_CONFIG_FILE_NAME);
File jaasConfFile = new File(JAAS_CONFIG_FILE_NAME);
if (!jaasConfFile.exists()) {
PrintWriter out = null;
try {
out = new PrintWriter(jaasConfFile);
out.println("equinox_console {");
out.println(" org.eclipse.equinox.console.jaas.SecureStorageLoginModule REQUIRED;");
out.println("};");
} finally {
if (out != null) {
out.close();
}
}
}
}
}
Accelerate Your Automation Test Cycles With LambdaTest
Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.