/**
* Copyright (C) 2010-2017 Structr GmbH
*
* This file is part of Structr <http://structr.org>.
*
* Structr is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* Structr 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Structr. If not, see <http://www.gnu.org/licenses/>.
*/
package org.structr.web.common;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import java.io.IOException;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertNotNull;
import static junit.framework.TestCase.fail;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.structr.common.error.FrameworkException;
import org.structr.core.entity.AbstractUser;
import org.structr.core.graph.Tx;
import org.structr.core.property.PropertyMap;
import org.structr.dynamic.File;
import org.structr.web.entity.AbstractFile;
import org.structr.web.entity.FileBase;
import org.structr.web.entity.Folder;
import org.structr.web.entity.User;
/**
* Common class for FTP tests
*
*
*/
public abstract class SSHTest extends StructrUiTest {
private static final Logger logger = LoggerFactory.getLogger(SSHTest.class.getName());
protected User ftpUser;
protected User createFTPUser(final String username, final String password) throws FrameworkException {
PropertyMap props = new PropertyMap();
props.put(AbstractUser.name, username);
props.put(AbstractUser.password, password);
return (User)createTestNodes(User.class, 1, props).get(0);
}
protected Folder createFTPDirectory(final String path, final String name) throws FrameworkException {
PropertyMap props = new PropertyMap();
props.put(Folder.name, name);
props.put(Folder.owner, ftpUser);
Folder dir = (Folder)createTestNodes(Folder.class, 1, props).get(0);
if (StringUtils.isNotBlank(path)) {
AbstractFile parent = FileHelper.getFileByAbsolutePath(securityContext, path);
if (parent != null && parent instanceof Folder) {
Folder parentFolder = (Folder)parent;
dir.setProperty(AbstractFile.parent, parentFolder);
}
}
logger.info("FTP directory {} created successfully.", dir);
return dir;
}
protected FileBase createFTPFile(final String path, final String name) throws FrameworkException {
PropertyMap props = new PropertyMap();
props.put(File.name, name);
props.put(File.size, 0L);
props.put(File.owner, ftpUser);
FileBase file = (FileBase)createTestNodes(File.class, 1, props).get(0);
if (StringUtils.isNotBlank(path)) {
AbstractFile parent = FileHelper.getFileByAbsolutePath(securityContext, path);
if (parent != null && parent instanceof Folder) {
Folder parentFolder = (Folder)parent;
file.setProperty(AbstractFile.parent, parentFolder);
}
}
logger.info("FTP file {} created successfully.", file);
return file;
}
protected void assertEmptyDirectory(final FTPClient ftp) {
FTPFile[] dirs = null;
try {
dirs = ftp.listDirectories();
} catch (IOException ex) {
logger.error("Error in FTP test", ex);
fail("Unexpected exception: " + ex.getMessage());
}
assertNotNull(dirs);
assertEquals(0, dirs.length);
}
/**
* Creates an FTP client, a backend user and logs this user in.
*
* @param username
* @return
*/
protected ChannelSftp setupSftpClient(final String username, final String password) {
try (final Tx tx = app.tx()) {
ftpUser = createFTPUser(username, password);
tx.success();
} catch (FrameworkException fex) {
logger.error("Unable to create SFTP user", fex);
}
JSch jsch = new JSch();
try {
final Session session = jsch.getSession(username, host, sshPort);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(password);
session.connect(1000);
final Channel channel = session.openChannel("sftp");
channel.connect(1000);
return (ChannelSftp)channel;
} catch (JSchException ex) {
ex.printStackTrace();
}
return null;
}
protected void disconnect(final FTPClient ftp) {
try {
ftp.disconnect();
} catch (IOException ex) {
logger.error("Error while disconnecting from FTP server", ex);
fail("Unexpected exception: " + ex.getMessage());
}
}
}