001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.taskdefs.optional.ssh;
020:
021: import junit.framework.TestCase;
022:
023: import java.io.*;
024: import java.util.List;
025: import java.util.ArrayList;
026: import java.util.Iterator;
027:
028: import org.apache.tools.ant.Project;
029: import org.apache.tools.ant.taskdefs.condition.FilesMatch;
030: import org.apache.tools.ant.types.FileSet;
031: import org.apache.tools.ant.types.selectors.FilenameSelector;
032:
033: /**
034: * This is a unit test for the Scp task in Ant. It must be
035: * configured with command line options in order for it to work.
036: * Here are the options:
037: *
038: * scp.tmp This is a local path to a temporary
039: * directory for this task to use.
040: * scp.host This is the remote location of the form:
041: * "user:password@host:/path/to/directory"
042: * scp.port The port of the listening SSH service.
043: * Defaults to 22. (optional)
044: * scp.known.hosts The file containing the public keys of known
045: * hosts. Must be a SSH2 version file, but
046: * supports RSA and DSA keys. If it is not present
047: * this task setTrust() to true. (optional)
048: */
049: public class ScpTest extends TestCase {
050:
051: private File tempDir = new File(System.getProperty("scp.tmp"));
052: private String sshHostUri = System.getProperty("scp.host");
053: private int port = Integer.parseInt(System.getProperty("scp.port",
054: "22"));
055: private String knownHosts = System.getProperty("scp.known.hosts");
056:
057: private List cleanUpList = new ArrayList();
058:
059: public ScpTest(String testname) {
060: super (testname);
061: }
062:
063: protected void setUp() {
064: cleanUpList.clear();
065: }
066:
067: protected void tearDown() {
068: for (Iterator i = cleanUpList.iterator(); i.hasNext();) {
069: File file = (File) i.next();
070: file.delete();
071: }
072: }
073:
074: public void testSingleFileUploadAndDownload() throws IOException {
075: File uploadFile = createTemporaryFile();
076:
077: Scp scpTask = createTask();
078: scpTask.setFile(uploadFile.getPath());
079: scpTask.setTodir(sshHostUri);
080: scpTask.execute();
081:
082: File testFile = new File(tempDir.getPath() + File.separator
083: + "download-testSingleFileUploadAndDownload.test");
084: addCleanup(testFile);
085: assertTrue("Assert that the testFile does not exist.",
086: !testFile.exists());
087:
088: scpTask.setFile(sshHostUri + "/" + uploadFile.getName());
089: scpTask.setTodir(testFile.getPath());
090: scpTask.execute();
091:
092: assertTrue("Assert that the testFile exists.", testFile
093: .exists());
094: compareFiles(uploadFile, testFile);
095: }
096:
097: public void testMultiUploadAndDownload() throws IOException {
098: List uploadList = new ArrayList();
099: for (int i = 0; i < 5; i++) {
100: uploadList.add(createTemporaryFile());
101: }
102:
103: Scp scp = createTask();
104: FilenameSelector selector = new FilenameSelector();
105: selector.setName("scp*");
106: FileSet fileset = new FileSet();
107: fileset.setDir(tempDir);
108: fileset.addFilename(selector);
109: scp.addFileset(fileset);
110: scp.setTodir(sshHostUri);
111: scp.execute();
112:
113: File multi = new File(tempDir, "multi");
114: multi.mkdir();
115: addCleanup(multi);
116:
117: Scp scp2 = createTask();
118: scp2.setFile(sshHostUri + "/scp*");
119: scp2.setTodir(multi.getPath());
120: scp2.execute();
121:
122: FilesMatch match = new FilesMatch();
123: for (Iterator i = uploadList.iterator(); i.hasNext();) {
124: File f = (File) i.next();
125: match.setFile1(f);
126: File f2 = new File(multi, f.getName());
127: match.setFile2(f2);
128: assertTrue("Assert file '" + f.getPath() + "' and file '"
129: + f2.getPath() + "'", match.eval());
130: }
131: }
132:
133: public void addCleanup(File file) {
134: cleanUpList.add(file);
135: }
136:
137: private void compareFiles(File src, File dest) {
138: FilesMatch match = new FilesMatch();
139: match.setFile1(src);
140: match.setFile2(dest);
141:
142: assertTrue("Assert files are equal.", match.eval());
143: }
144:
145: private File createTemporaryFile() throws IOException {
146: File uploadFile;
147: uploadFile = File.createTempFile("scp", "test", tempDir);
148: FileWriter writer = new FileWriter(uploadFile);
149: writer.write("Can you hear me now?\n");
150: writer.close();
151: addCleanup(uploadFile);
152: return uploadFile;
153: }
154:
155: private Scp createTask() {
156: Scp scp = new Scp();
157: Project p = new Project();
158: p.init();
159: scp.setProject(p);
160: if (knownHosts != null) {
161: scp.setKnownhosts(knownHosts);
162: } else {
163: scp.setTrust(true);
164: }
165: scp.setPort(port);
166: return scp;
167: }
168: }
|