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.File;
20 import java.io.FileInputStream;
21 import java.io.FileOutputStream;
22 import java.io.IOException;
23
24 import org.apache.commons.net.tftp.TFTPServer.ServerMode;
25
26 import junit.framework.TestCase;
27
28
29
30
31
32
33
34
35 public class TFTPServerPathTest extends TestCase
36 {
37 String filePrefix = "tftp-";
38 File serverDirectory = new File(System.getProperty("java.io.tmpdir"));
39
40 public void testReadOnly() throws IOException
41 {
42
43 TFTPServer tftpS = new TFTPServer(serverDirectory, serverDirectory, 6900,
44 ServerMode.GET_ONLY, null, null);
45
46
47 TFTPClient tftp = new TFTPClient();
48 tftp.open();
49 tftp.setSoTimeout(2000);
50
51
52 File file = new File(serverDirectory, filePrefix + "source.txt");
53 file.createNewFile();
54
55
56 File out = new File(serverDirectory, filePrefix + "out");
57
58
59 out.delete();
60 assertTrue("Couldn't clear output location", !out.exists());
61
62 FileOutputStream output = new FileOutputStream(out);
63
64 tftp.receiveFile(file.getName(), TFTP.BINARY_MODE, output, "localhost", 6900);
65 output.close();
66
67 assertTrue("file not created", out.exists());
68
69 out.delete();
70
71 FileInputStream fis = new FileInputStream(file);
72 try
73 {
74 tftp.sendFile(out.getName(), TFTP.BINARY_MODE, fis, "localhost", 6900);
75 fail("Server allowed write");
76 }
77 catch (IOException e)
78 {
79
80 }
81 fis.close();
82 file.delete();
83 tftpS.shutdown();
84 }
85
86 public void testWriteOnly() throws IOException
87 {
88
89 TFTPServer tftpS = new TFTPServer(serverDirectory, serverDirectory, 6900,
90 ServerMode.PUT_ONLY, null, null);
91
92
93 TFTPClient tftp = new TFTPClient();
94 tftp.open();
95 tftp.setSoTimeout(2000);
96
97
98 File file = new File(serverDirectory, filePrefix + "source.txt");
99 file.createNewFile();
100
101 File out = new File(serverDirectory, filePrefix + "out");
102
103
104 out.delete();
105 assertTrue("Couldn't clear output location", !out.exists());
106
107 FileOutputStream output = new FileOutputStream(out);
108
109 try
110 {
111 tftp.receiveFile(file.getName(), TFTP.BINARY_MODE, output, "localhost", 6900);
112 fail("Server allowed read");
113 }
114 catch (IOException e)
115 {
116
117 }
118 output.close();
119 out.delete();
120
121 FileInputStream fis = new FileInputStream(file);
122 tftp.sendFile(out.getName(), TFTP.BINARY_MODE, fis, "localhost", 6900);
123
124 fis.close();
125
126 assertTrue("file not created", out.exists());
127
128
129 file.delete();
130 out.delete();
131 tftpS.shutdown();
132 }
133
134 public void testWriteOutsideHome() throws IOException
135 {
136
137 TFTPServer tftpS = new TFTPServer(serverDirectory, serverDirectory, 6900,
138 ServerMode.GET_AND_PUT, null, null);
139
140
141 TFTPClient tftp = new TFTPClient();
142 tftp.open();
143
144 File file = new File(serverDirectory, filePrefix + "source.txt");
145 file.createNewFile();
146
147 assertFalse("test construction error", new File(serverDirectory, "../foo").exists());
148
149 FileInputStream fis = new FileInputStream(file);
150 try
151 {
152 tftp.sendFile("../foo", TFTP.BINARY_MODE, fis, "localhost", 6900);
153 fail("Server allowed write!");
154 }
155 catch (IOException e)
156 {
157
158 }
159
160 fis.close();
161
162 assertFalse("file created when it should not have been",
163 new File(serverDirectory, "../foo").exists());
164
165
166 file.delete();
167
168 tftpS.shutdown();
169 }
170
171
172 }