You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by rw...@apache.org on 2008/02/26 17:35:00 UTC
svn commit: r631283 - in
/commons/proper/net/branches/NET_2_0/src/test/java/org/apache/commons/net/tftp:
TFTPServerPathTest.java TFTPServerPathTests.java TFTPTest.java TFTPTests.java
Author: rwinston
Date: Tue Feb 26 08:34:59 2008
New Revision: 631283
URL: http://svn.apache.org/viewvc?rev=631283&view=rev
Log:
Rename so tests are picked up
Added:
commons/proper/net/branches/NET_2_0/src/test/java/org/apache/commons/net/tftp/TFTPServerPathTest.java
commons/proper/net/branches/NET_2_0/src/test/java/org/apache/commons/net/tftp/TFTPTest.java
Removed:
commons/proper/net/branches/NET_2_0/src/test/java/org/apache/commons/net/tftp/TFTPServerPathTests.java
commons/proper/net/branches/NET_2_0/src/test/java/org/apache/commons/net/tftp/TFTPTests.java
Added: commons/proper/net/branches/NET_2_0/src/test/java/org/apache/commons/net/tftp/TFTPServerPathTest.java
URL: http://svn.apache.org/viewvc/commons/proper/net/branches/NET_2_0/src/test/java/org/apache/commons/net/tftp/TFTPServerPathTest.java?rev=631283&view=auto
==============================================================================
--- commons/proper/net/branches/NET_2_0/src/test/java/org/apache/commons/net/tftp/TFTPServerPathTest.java (added)
+++ commons/proper/net/branches/NET_2_0/src/test/java/org/apache/commons/net/tftp/TFTPServerPathTest.java Tue Feb 26 08:34:59 2008
@@ -0,0 +1,170 @@
+/*
+ * Copyright 2005 The Apache Software Foundation
+ *
+ * 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 org.apache.commons.net.tftp;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+
+import junit.framework.TestCase;
+
+/**
+ * Some basic tests to ensure that the TFTP Server is honoring its read/write mode, and preventing
+ * files from being read or written from outside of the assigned roots.
+ *
+ * @author <A HREF="mailto:daniel.armbrust.list@gmail.com">Dan Armbrust</A>
+ *
+ */
+public class TFTPServerPathTest extends TestCase
+{
+ String filePrefix = "tftp-";
+ File serverDirectory = new File(System.getProperty("java.io.tmpdir"));
+
+ public void testReadOnly() throws IOException
+ {
+ // Start a read-only server
+ TFTPServer tftpS = new TFTPServer(serverDirectory, serverDirectory, 6900,
+ TFTPServer.GET_ONLY, null, null);
+
+ // Create our TFTP instance to handle the file transfer.
+ TFTPClient tftp = new TFTPClient();
+ tftp.open();
+ tftp.setSoTimeout(2000);
+
+ // make a file to work with.
+ File file = new File(serverDirectory, filePrefix + "source.txt");
+ file.createNewFile();
+
+ // Read the file from the tftp server.
+ File out = new File(serverDirectory, filePrefix + "out");
+
+ // cleanup old failed runs
+ out.delete();
+ assertTrue("Couldn't clear output location", !out.exists());
+
+ FileOutputStream output = new FileOutputStream(out);
+
+ tftp.receiveFile(file.getName(), TFTP.BINARY_MODE, output, "localhost", 6900);
+ output.close();
+
+ assertTrue("file not created", out.exists());
+
+ out.delete();
+
+ FileInputStream fis = new FileInputStream(file);
+ try
+ {
+ tftp.sendFile(out.getName(), TFTP.BINARY_MODE, fis, "localhost", 6900);
+ fail("Server allowed write");
+ }
+ catch (IOException e)
+ {
+ // expected path
+ }
+ fis.close();
+ file.delete();
+ tftpS.shutdown();
+ }
+
+ public void testWriteOnly() throws IOException
+ {
+ // Start a write-only server
+ TFTPServer tftpS = new TFTPServer(serverDirectory, serverDirectory, 6900,
+ TFTPServer.PUT_ONLY, null, null);
+
+ // Create our TFTP instance to handle the file transfer.
+ TFTPClient tftp = new TFTPClient();
+ tftp.open();
+ tftp.setSoTimeout(2000);
+
+ // make a file to work with.
+ File file = new File(serverDirectory, filePrefix + "source.txt");
+ file.createNewFile();
+
+ File out = new File(serverDirectory, filePrefix + "out");
+
+ // cleanup old failed runs
+ out.delete();
+ assertTrue("Couldn't clear output location", !out.exists());
+
+ FileOutputStream output = new FileOutputStream(out);
+
+ try
+ {
+ tftp.receiveFile(file.getName(), TFTP.BINARY_MODE, output, "localhost", 6900);
+ fail("Server allowed read");
+ }
+ catch (IOException e)
+ {
+ // expected path
+ }
+ output.close();
+ out.delete();
+
+ FileInputStream fis = new FileInputStream(file);
+ tftp.sendFile(out.getName(), TFTP.BINARY_MODE, fis, "localhost", 6900);
+
+ fis.close();
+
+ assertTrue("file not created", out.exists());
+
+ // cleanup
+ file.delete();
+ out.delete();
+ tftpS.shutdown();
+ }
+
+ public void testWriteOutsideHome() throws IOException
+ {
+ // Start a server
+ TFTPServer tftpS = new TFTPServer(serverDirectory, serverDirectory, 6900,
+ TFTPServer.GET_AND_PUT, null, null);
+
+ // Create our TFTP instance to handle the file transfer.
+ TFTPClient tftp = new TFTPClient();
+ tftp.open();
+
+ File file = new File(serverDirectory, filePrefix + "source.txt");
+ file.createNewFile();
+
+ assertFalse("test construction error", new File(serverDirectory, "../foo").exists());
+
+ FileInputStream fis = new FileInputStream(file);
+ try
+ {
+ tftp.sendFile("../foo", TFTP.BINARY_MODE, fis, "localhost", 6900);
+ fail("Server allowed write!");
+ }
+ catch (IOException e)
+ {
+ // expected path
+ }
+
+ fis.close();
+
+ assertFalse("file created when it should not have been",
+ new File(serverDirectory, "../foo").exists());
+
+ // cleanup
+ file.delete();
+
+ tftpS.shutdown();
+ }
+
+
+}
Added: commons/proper/net/branches/NET_2_0/src/test/java/org/apache/commons/net/tftp/TFTPTest.java
URL: http://svn.apache.org/viewvc/commons/proper/net/branches/NET_2_0/src/test/java/org/apache/commons/net/tftp/TFTPTest.java?rev=631283&view=auto
==============================================================================
--- commons/proper/net/branches/NET_2_0/src/test/java/org/apache/commons/net/tftp/TFTPTest.java (added)
+++ commons/proper/net/branches/NET_2_0/src/test/java/org/apache/commons/net/tftp/TFTPTest.java Tue Feb 26 08:34:59 2008
@@ -0,0 +1,225 @@
+package org.apache.commons.net.tftp;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import junit.framework.TestCase;
+
+/**
+ * Test the TFTP Server and TFTP Client by creating some files in the system temp folder and then
+ * uploading and downloading them.
+ *
+ * @author <A HREF="mailto:daniel.armbrust.list@gmail.com">Dan Armbrust</A>
+ *
+ */
+public class TFTPTest extends TestCase
+{
+ static TFTPServer tftpS;
+ static File serverDirectory = new File(System.getProperty("java.io.tmpdir"));
+ static String filePrefix = "tftp-";
+ static File[] files = new File[8];
+
+ static int testsLeftToRun = 6;
+
+ // only want to do this once...
+ static
+ {
+ try
+ {
+ files[0] = createFile(new File(serverDirectory, filePrefix + "empty.txt"), 0);
+ files[1] = createFile(new File(serverDirectory, filePrefix + "small.txt"), 1);
+ files[2] = createFile(new File(serverDirectory, filePrefix + "511.txt"), 511);
+ files[3] = createFile(new File(serverDirectory, filePrefix + "512.txt"), 512);
+ files[4] = createFile(new File(serverDirectory, filePrefix + "513.txt"), 513);
+ files[5] = createFile(new File(serverDirectory, filePrefix + "med.txt"), 1000 * 1024);
+ files[6] = createFile(new File(serverDirectory, filePrefix + "big.txt"), 5000 * 1024);
+ files[7] = createFile(new File(serverDirectory, filePrefix + "huge.txt"), 37000 * 1024);
+
+ // Start the server
+ tftpS = new TFTPServer(serverDirectory, serverDirectory, 6900, TFTPServer.GET_AND_PUT,
+ null, null);
+ tftpS.setSocketTimeout(2000);
+ }
+ catch (IOException e)
+ {
+ e.printStackTrace();
+ }
+
+ }
+
+ protected void tearDown() throws Exception
+ {
+ testsLeftToRun--;
+ if (testsLeftToRun <= 0)
+ {
+ if (tftpS != null)
+ {
+ tftpS.shutdown();
+ }
+ for (int i = 0; i < files.length; i++)
+ {
+ files[i].delete();
+ }
+ }
+ super.tearDown();
+ }
+
+ /*
+ * Create a file, size specified in bytes
+ */
+ private static File createFile(File file, int size) throws IOException
+ {
+ OutputStream os = new BufferedOutputStream(new FileOutputStream(file));
+ byte[] temp = "0".getBytes();
+ for (int i = 0; i < size; i++)
+ {
+ os.write(temp);
+ }
+ os.close();
+ return file;
+ }
+
+ public void testTFTPBinaryDownloads() throws Exception
+ {
+ // test with the smaller files
+ for (int i = 0; i < 6; i++)
+ {
+ testDownload(TFTP.BINARY_MODE, files[i]);
+ }
+ }
+
+ public void testASCIIDownloads() throws Exception
+ {
+ // test with the smaller files
+ for (int i = 0; i < 6; i++)
+ {
+ testDownload(TFTP.ASCII_MODE, files[i]);
+ }
+ }
+
+ public void testTFTPBinaryUploads() throws Exception
+ {
+ // test with the smaller files
+ for (int i = 0; i < 6; i++)
+ {
+ testUpload(TFTP.BINARY_MODE, files[i]);
+ }
+ }
+
+ public void testASCIIUploads() throws Exception
+ {
+ // test with the smaller files
+ for (int i = 0; i < 6; i++)
+ {
+ testUpload(TFTP.ASCII_MODE, files[i]);
+ }
+ }
+
+ public void testHugeUploads() throws Exception
+ {
+ for (int i = 5; i < files.length; i++)
+ {
+ testUpload(TFTP.BINARY_MODE, files[i]);
+ }
+ }
+
+ public void testHugeDownloads() throws Exception
+ {
+ // test with the smaller files
+ for (int i = 5; i < files.length; i++)
+ {
+ testDownload(TFTP.BINARY_MODE, files[i]);
+ }
+ }
+
+ private void testDownload(int mode, File file) throws IOException
+ {
+ // Create our TFTP instance to handle the file transfer.
+ TFTPClient tftp = new TFTPClient();
+ tftp.open();
+ tftp.setSoTimeout(2000);
+
+ File out = new File(serverDirectory, filePrefix + "download");
+
+ // cleanup old failed runs
+ out.delete();
+ assertTrue("Couldn't clear output location", !out.exists());
+
+ FileOutputStream output = new FileOutputStream(out);
+
+ tftp.receiveFile(file.getName(), mode, output, "localhost", 6900);
+ output.close();
+
+ assertTrue("file not created", out.exists());
+ assertTrue("files not identical on file " + file, filesIdentical(out, file));
+
+ // delete the downloaded file
+ out.delete();
+ }
+
+ private void testUpload(int mode, File file) throws Exception
+ {
+ // Create our TFTP instance to handle the file transfer.
+ TFTPClient tftp = new TFTPClient();
+ tftp.open();
+ tftp.setSoTimeout(2000);
+
+ File in = new File(serverDirectory, filePrefix + "upload");
+ // cleanup old failed runs
+ in.delete();
+ assertTrue("Couldn't clear output location", !in.exists());
+
+ FileInputStream fis = new FileInputStream(file);
+ tftp.sendFile(in.getName(), mode, fis, "localhost", 6900);
+ fis.close();
+
+ // need to give the server a bit of time to receive our last packet, and
+ // close out its file buffers, etc.
+ Thread.sleep(100);
+ assertTrue("file not created", in.exists());
+ assertTrue("files not identical on file " + file, filesIdentical(file, in));
+
+ in.delete();
+ }
+
+ private boolean filesIdentical(File a, File b) throws IOException
+ {
+ if (!a.exists() || !b.exists())
+ {
+ return false;
+ }
+
+ if (a.length() != b.length())
+ {
+ return false;
+ }
+
+ InputStream fisA = new BufferedInputStream(new FileInputStream(a));
+ InputStream fisB = new BufferedInputStream(new FileInputStream(b));
+
+ int aBit = fisA.read();
+ int bBit = fisB.read();
+
+ while (aBit != -1)
+ {
+ if (aBit != bBit)
+ {
+ fisA.close();
+ fisB.close();
+ return false;
+ }
+ aBit = fisA.read();
+ bBit = fisB.read();
+ }
+
+ fisA.close();
+ fisB.close();
+ return true;
+ }
+}