1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.net.tftp;
18
19 import java.io.BufferedInputStream;
20 import java.io.BufferedOutputStream;
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.OutputStream;
27
28 import org.apache.commons.net.tftp.TFTPServer.ServerMode;
29
30 import junit.framework.TestCase;
31
32
33
34
35
36
37
38
39 public class TFTPTest extends TestCase
40 {
41 static TFTPServer tftpS;
42 static File serverDirectory = new File(System.getProperty("java.io.tmpdir"));
43 static String filePrefix = "tftp-";
44 static File[] files = new File[8];
45
46 static int testsLeftToRun = 6;
47
48
49 static
50 {
51 try
52 {
53 files[0] = createFile(new File(serverDirectory, filePrefix + "empty.txt"), 0);
54 files[1] = createFile(new File(serverDirectory, filePrefix + "small.txt"), 1);
55 files[2] = createFile(new File(serverDirectory, filePrefix + "511.txt"), 511);
56 files[3] = createFile(new File(serverDirectory, filePrefix + "512.txt"), 512);
57 files[4] = createFile(new File(serverDirectory, filePrefix + "513.txt"), 513);
58 files[5] = createFile(new File(serverDirectory, filePrefix + "med.txt"), 1000 * 1024);
59 files[6] = createFile(new File(serverDirectory, filePrefix + "big.txt"), 5000 * 1024);
60 files[7] = createFile(new File(serverDirectory, filePrefix + "huge.txt"), 37000 * 1024);
61
62
63 tftpS = new TFTPServer(serverDirectory, serverDirectory, 6900, ServerMode.GET_AND_PUT,
64 null, null);
65 tftpS.setSocketTimeout(2000);
66 }
67 catch (IOException e)
68 {
69 e.printStackTrace();
70 }
71
72 }
73
74 @Override
75 protected void tearDown() throws Exception
76 {
77 testsLeftToRun--;
78 if (testsLeftToRun <= 0)
79 {
80 if (tftpS != null)
81 {
82 tftpS.shutdown();
83 }
84 for (int i = 0; i < files.length; i++)
85 {
86 files[i].delete();
87 }
88 }
89 super.tearDown();
90 }
91
92
93
94
95 private static File createFile(File file, int size) throws IOException
96 {
97 OutputStream os = new BufferedOutputStream(new FileOutputStream(file));
98 byte[] temp = "0".getBytes();
99 for (int i = 0; i < size; i++)
100 {
101 os.write(temp);
102 }
103 os.close();
104 return file;
105 }
106
107 public void testTFTPBinaryDownloads() throws Exception
108 {
109
110 for (int i = 0; i < 6; i++)
111 {
112 testDownload(TFTP.BINARY_MODE, files[i]);
113 }
114 }
115
116 public void testASCIIDownloads() throws Exception
117 {
118
119 for (int i = 0; i < 6; i++)
120 {
121 testDownload(TFTP.ASCII_MODE, files[i]);
122 }
123 }
124
125 public void testTFTPBinaryUploads() throws Exception
126 {
127
128 for (int i = 0; i < 6; i++)
129 {
130 testUpload(TFTP.BINARY_MODE, files[i]);
131 }
132 }
133
134 public void testASCIIUploads() throws Exception
135 {
136
137 for (int i = 0; i < 6; i++)
138 {
139 testUpload(TFTP.ASCII_MODE, files[i]);
140 }
141 }
142
143 public void testHugeUploads() throws Exception
144 {
145 for (int i = 5; i < files.length; i++)
146 {
147 testUpload(TFTP.BINARY_MODE, files[i]);
148 }
149 }
150
151 public void testHugeDownloads() throws Exception
152 {
153
154 for (int i = 5; i < files.length; i++)
155 {
156 testDownload(TFTP.BINARY_MODE, files[i]);
157 }
158 }
159
160 private void testDownload(int mode, File file) throws IOException
161 {
162
163 TFTPClient tftp = new TFTPClient();
164 tftp.open();
165 tftp.setSoTimeout(2000);
166
167 File out = new File(serverDirectory, filePrefix + "download");
168
169
170 out.delete();
171 assertTrue("Couldn't clear output location", !out.exists());
172
173 FileOutputStream output = new FileOutputStream(out);
174
175 tftp.receiveFile(file.getName(), mode, output, "localhost", 6900);
176 output.close();
177
178 assertTrue("file not created", out.exists());
179 assertTrue("files not identical on file " + file, filesIdentical(out, file));
180
181
182 out.delete();
183 }
184
185 private void testUpload(int mode, File file) throws Exception
186 {
187
188 TFTPClient tftp = new TFTPClient();
189 tftp.open();
190 tftp.setSoTimeout(2000);
191
192 File in = new File(serverDirectory, filePrefix + "upload");
193
194 in.delete();
195 assertTrue("Couldn't clear output location", !in.exists());
196
197 FileInputStream fis = new FileInputStream(file);
198 tftp.sendFile(in.getName(), mode, fis, "localhost", 6900);
199 fis.close();
200
201
202
203 Thread.sleep(100);
204 assertTrue("file not created", in.exists());
205 assertTrue("files not identical on file " + file, filesIdentical(file, in));
206
207 in.delete();
208 }
209
210 private boolean filesIdentical(File a, File b) throws IOException
211 {
212 if (!a.exists() || !b.exists())
213 {
214 return false;
215 }
216
217 if (a.length() != b.length())
218 {
219 return false;
220 }
221
222 InputStream fisA = new BufferedInputStream(new FileInputStream(a));
223 InputStream fisB = new BufferedInputStream(new FileInputStream(b));
224
225 int aBit = fisA.read();
226 int bBit = fisB.read();
227
228 while (aBit != -1)
229 {
230 if (aBit != bBit)
231 {
232 fisA.close();
233 fisB.close();
234 return false;
235 }
236 aBit = fisA.read();
237 bBit = fisB.read();
238 }
239
240 fisA.close();
241 fisB.close();
242 return true;
243 }
244 }