KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > drftpd > master > command > plugins > LoginTest


1 /*
2  * This file is part of DrFTPD, Distributed FTP Daemon.
3  *
4  * DrFTPD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * DrFTPD is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with DrFTPD; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package net.sf.drftpd.master.command.plugins;
19
20 import java.net.InetAddress JavaDoc;
21 import java.net.UnknownHostException JavaDoc;
22 import java.util.Properties JavaDoc;
23
24 import junit.framework.TestCase;
25 import junit.framework.TestSuite;
26 import net.sf.drftpd.DuplicateElementException;
27 import net.sf.drftpd.master.BaseFtpConnection;
28 import net.sf.drftpd.master.ConnectionManager;
29 import net.sf.drftpd.master.FtpReply;
30 import net.sf.drftpd.master.FtpRequest;
31 import net.sf.drftpd.master.config.FtpConfig;
32 import net.sf.drftpd.master.usermanager.User;
33 import net.sf.drftpd.master.usermanager.UserManager;
34
35 import org.apache.log4j.BasicConfigurator;
36 import org.apache.log4j.Logger;
37 import org.drftpd.commands.UnhandledCommandException;
38 import org.drftpd.tests.DummyBaseFtpConnection;
39 import org.drftpd.tests.DummyUser;
40 import org.drftpd.tests.DummyUserManager;
41
42 /**
43  * @author mog
44  * @version $Id: LoginTest.java,v 1.5.2.2 2004/06/27 22:22:49 mog Exp $
45  */

46 public class LoginTest extends TestCase {
47     private DummyUser _user;
48
49     private DummyUserManager _userManager;
50
51     private DummyBaseFtpConnection _conn;
52
53     private Login _login;
54
55     private static final Logger logger = Logger.getLogger(LoginTest.class);
56
57     public LoginTest(String JavaDoc name) {
58         super(name);
59     }
60
61     public static TestSuite suite() {
62         return new TestSuite(LoginTest.class);
63     }
64
65     private void internalSetUp() {
66         _login = (Login) new Login().initialize(null, null);
67         _conn = new DummyBaseFtpConnection(null) {
68             public FtpConfig getConfig() {
69                 return new FC();
70             }
71         };
72         _conn.setConnectionManager(new ConnectionManager() {
73             public FtpReply canLogin(BaseFtpConnection baseconn, User user) {
74                 return null;
75             }
76             public FtpConfig getConfig() {
77                 return new FC();
78             }
79             public UserManager getUserManager() {
80                 return _userManager;
81             }
82         });
83         _userManager = new DummyUserManager();
84         _user = new DummyUser("myuser");
85         _userManager.setUser(_user);
86         _conn.setUserManager(_userManager);
87     }
88
89     public void setUp() {
90         BasicConfigurator.configure();
91     }
92     public static class FC extends FtpConfig {
93         public FC() {
94             Properties JavaDoc cfg = new Properties JavaDoc();
95             cfg.setProperty("bouncer_ip", "10.0.1.1 10.0.0.1");
96             try {
97                 loadConfig1(cfg);
98             } catch (UnknownHostException JavaDoc e) {
99                 throw new RuntimeException JavaDoc(e);
100             }
101         }
102         // public List getBouncerIps() {
103
// try {
104
// return Arrays.asList(
105
// new InetAddress[] {
106
// InetAddress.getByName("10.0.1.1"),
107
// InetAddress.getByName("10.0.0.1")});
108
// } catch (UnknownHostException e) {
109
// throw new RuntimeException(e);
110
// }
111
// }
112
}
113     public void testUSER()
114         throws
115             UnknownHostException JavaDoc,
116             UnhandledCommandException,
117             DuplicateElementException {
118         internalSetUp();
119         _conn.setClientAddress(InetAddress.getByName("127.0.0.1"));
120
121         FtpReply reply;
122         _conn.setRequest(new FtpRequest("USER myuser"));
123         reply = _login.execute(_conn);
124         assertEquals(530, reply.getCode());
125         assertNull(_conn.getUserNull());
126
127         _user.addIPMask("*@1.2.3.4");
128         reply = _login.execute(_conn);
129         assertEquals(530, reply.getCode());
130         assertNull(_conn.getUserNull());
131
132         _user.addIPMask("*@127.0.0.1");
133         reply = _login.execute(_conn);
134         assertEquals(331, reply.getCode());
135         assertNotNull(_conn.getUserNull());
136     }
137
138     public void testIDNT()
139         throws
140             UnhandledCommandException,
141             DuplicateElementException,
142             UnknownHostException JavaDoc {
143         internalSetUp();
144         _conn.setClientAddress(InetAddress.getByName("10.0.0.2"));
145         _conn.setRequest(new FtpRequest("IDNT user@127.0.0.1:localhost"));
146         FtpReply reply;
147         reply = _login.execute(_conn);
148         assertNotNull(reply);
149         assertEquals(530, reply.getCode());
150         assertNull(_login._idntAddress);
151
152         internalSetUp();
153         _conn.setClientAddress(InetAddress.getByName("10.0.0.1"));
154         _conn.setRequest(new FtpRequest("IDNT user@127.0.0.1:localhost"));
155         reply = _login.execute(_conn);
156         assertNull(String.valueOf(reply), reply);
157
158         _conn.setRequest(new FtpRequest("USER myuser"));
159
160         _user.addIPMask("*@127.0.0.0"); //invalid
161
reply = _login.execute(_conn);
162         assertEquals(530, reply.getCode());
163         assertNull(_conn.getUserNull());
164         logger.debug(reply.toString());
165
166         _user.addIPMask("*@127.0.0.1"); //what was given in IDNT cmd
167
reply = _login.execute(_conn);
168         assertEquals(331, reply.getCode());
169         assertEquals(_user, _conn.getUserNull());
170         logger.debug(reply.toString());
171     }
172 }
173
Popular Tags