001: /****************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one *
003: * or more contributor license agreements. See the NOTICE file *
004: * distributed with this work for additional information *
005: * regarding copyright ownership. The ASF licenses this file *
006: * to you under the Apache License, Version 2.0 (the *
007: * "License"); you may not use this file except in compliance *
008: * with the License. You may obtain a copy of the License at *
009: * *
010: * http://www.apache.org/licenses/LICENSE-2.0 *
011: * *
012: * Unless required by applicable law or agreed to in writing, *
013: * software distributed under the License is distributed on an *
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
015: * KIND, either express or implied. See the License for the *
016: * specific language governing permissions and limitations *
017: * under the License. *
018: ****************************************************************/package org.apache.james.remotemanager;
019:
020: import junit.framework.TestCase;
021: import org.apache.avalon.cornerstone.services.sockets.SocketManager;
022: import org.apache.avalon.cornerstone.services.threads.ThreadManager;
023: import org.apache.commons.net.telnet.TelnetClient;
024: import org.apache.james.services.JamesConnectionManager;
025: import org.apache.james.services.MailServer;
026: import org.apache.james.services.UsersRepository;
027: import org.apache.james.services.UsersStore;
028: import org.apache.james.test.mock.avalon.MockLogger;
029: import org.apache.james.test.mock.avalon.MockServiceManager;
030: import org.apache.james.test.mock.avalon.MockSocketManager;
031: import org.apache.james.test.mock.avalon.MockThreadManager;
032: import org.apache.james.test.mock.james.MockMailServer;
033: import org.apache.james.test.mock.james.MockUsersStore;
034: import org.apache.james.test.util.Util;
035: import org.apache.james.userrepository.MockUsersRepository;
036: import org.apache.james.util.connection.SimpleConnectionManager;
037:
038: import java.io.BufferedReader;
039: import java.io.IOException;
040: import java.io.InputStreamReader;
041: import java.io.OutputStreamWriter;
042: import java.util.ArrayList;
043: import java.util.Arrays;
044: import java.util.Iterator;
045: import java.util.List;
046:
047: /**
048: * Tests the org.apache.james.remotemanager.RemoteManager
049: * TODO: impl missing command tests for:
050: * USER
051: * SHUTDOWN (hard to test, because it does shutdown the whole (testing) JVM
052: */
053:
054: public class RemoteManagerTest extends TestCase {
055:
056: public static final String LINE_SEPARATOR = System.getProperties()
057: .getProperty("line.separator");
058:
059: protected int m_remoteManagerListenerPort = Util
060: .getNonPrivilegedPort();
061: protected RemoteManager m_remoteManager;
062: protected RemoteManagerTestConfiguration m_testConfiguration;
063: protected String m_host = "127.0.0.1";
064: protected BufferedReader m_reader;
065: protected OutputStreamWriter m_writer;
066: protected TelnetClient m_telnetClient;
067: private MockUsersRepository m_mockUsersRepository;
068:
069: protected void setUp() throws Exception {
070: m_remoteManager = new RemoteManager();
071: m_remoteManager.enableLogging(new MockLogger());
072:
073: m_remoteManager.service(setUpServiceManager());
074: m_testConfiguration = new RemoteManagerTestConfiguration(
075: m_remoteManagerListenerPort);
076: }
077:
078: protected void finishSetUp(
079: RemoteManagerTestConfiguration testConfiguration) {
080: testConfiguration.init();
081: try {
082: m_remoteManager.configure(testConfiguration);
083: m_remoteManager.initialize();
084: } catch (Exception e) {
085: throw new RuntimeException(e);
086: }
087: }
088:
089: protected void login() throws IOException {
090: login(m_testConfiguration.getLoginName(), m_testConfiguration
091: .getLoginPassword());
092: }
093:
094: protected void login(String name, String password)
095: throws IOException {
096: sendCommand(name);
097: sendCommand(password);
098:
099: List answers = readAnswer();
100: String last = getLastLine(answers);
101: assertTrue(last.startsWith("Welcome"));
102: }
103:
104: protected String getLastLine(List list) {
105: if (list == null || list.isEmpty())
106: return null;
107: return (String) list.get(list.size() - 1);
108: }
109:
110: protected List readAnswer() {
111: try {
112: while (!m_reader.ready()) {
113: ;
114: }
115: } catch (IOException e) {
116: return null;
117: }
118:
119: StringBuffer stringBuffer = new StringBuffer();
120: char[] charBuffer = new char[100];
121: List allAnswerLines = new ArrayList();
122: try {
123: int readCount;
124: while ((m_reader.ready() && (readCount = m_reader
125: .read(charBuffer)) > 0)) {
126: stringBuffer.append(charBuffer, 0, readCount);
127: }
128: } catch (IOException e) {
129: fail("reading remote manager answer failed");
130: }
131:
132: allAnswerLines.addAll(Arrays.asList(stringBuffer.toString()
133: .split(LINE_SEPARATOR)));
134: if ("".equals(getLastLine(allAnswerLines)))
135: allAnswerLines.remove(allAnswerLines.size() - 1);
136: return allAnswerLines;
137: }
138:
139: protected void sendCommand(String command) throws IOException {
140: m_writer.write(command + LINE_SEPARATOR);
141: m_writer.flush();
142: }
143:
144: protected void connect() throws IOException {
145: m_telnetClient = new TelnetClient();
146: m_telnetClient.connect(m_host, m_remoteManagerListenerPort);
147:
148: m_reader = new BufferedReader(new InputStreamReader(
149: m_telnetClient.getInputStream()));
150: m_writer = new OutputStreamWriter(m_telnetClient
151: .getOutputStream());
152: }
153:
154: private MockServiceManager setUpServiceManager() {
155: MockServiceManager serviceManager = new MockServiceManager();
156: SimpleConnectionManager connectionManager = new SimpleConnectionManager();
157: connectionManager.enableLogging(new MockLogger());
158: serviceManager.put(JamesConnectionManager.ROLE,
159: connectionManager);
160: MockMailServer mailServer = new MockMailServer();
161: serviceManager.put(MailServer.ROLE, mailServer);
162: m_mockUsersRepository = mailServer.getUsersRepository();
163: serviceManager.put(UsersRepository.ROLE, m_mockUsersRepository);
164: serviceManager.put(UsersStore.ROLE, new MockUsersStore(
165: m_mockUsersRepository));
166: serviceManager.put(SocketManager.ROLE, new MockSocketManager(
167: m_remoteManagerListenerPort));
168: serviceManager.put(ThreadManager.ROLE, new MockThreadManager());
169: return serviceManager;
170: }
171:
172: public void testLogin() throws IOException {
173: finishSetUp(m_testConfiguration);
174: connect();
175:
176: login();
177: }
178:
179: public void testWrongLoginUser() throws IOException {
180: finishSetUp(m_testConfiguration);
181: connect();
182:
183: sendCommand("sindbad");
184: sendCommand(m_testConfiguration.getLoginPassword());
185:
186: List answers = readAnswer();
187: String last = getLastLine(answers);
188: assertTrue(last.startsWith("Login id:")); // login failed, getting new login prompt
189: }
190:
191: public void testWrongLoginPassword() throws IOException {
192: finishSetUp(m_testConfiguration);
193: connect();
194:
195: sendCommand(m_testConfiguration.getLoginName());
196: sendCommand("getmethru");
197:
198: List answers = readAnswer();
199: String last = getLastLine(answers);
200: assertTrue(last.startsWith("Login id:")); // login failed, getting new login prompt
201: }
202:
203: public void testUserCount() throws IOException {
204: finishSetUp(m_testConfiguration);
205: connect();
206: login();
207:
208: sendCommand("countusers");
209: assertTrue(getLastLine(readAnswer()).endsWith(" 0")); // no user yet
210:
211: sendCommand("adduser testCount1 testCount");
212: assertTrue(getLastLine(readAnswer()).endsWith(" added")); // success
213:
214: sendCommand("countusers");
215: assertTrue(getLastLine(readAnswer()).endsWith(" 1")); // 1 total
216:
217: sendCommand("adduser testCount2 testCount");
218: assertTrue(getLastLine(readAnswer()).endsWith(" added")); // success
219:
220: sendCommand("countusers");
221: assertTrue(getLastLine(readAnswer()).endsWith(" 2")); // 2 total
222:
223: m_mockUsersRepository.removeUser("testCount1");
224:
225: sendCommand("countusers");
226: assertTrue(getLastLine(readAnswer()).endsWith(" 1")); // 1 total
227: }
228:
229: public void testAddUserAndVerify() throws IOException {
230: finishSetUp(m_testConfiguration);
231: connect();
232: login();
233:
234: sendCommand("adduser testAdd test");
235: assertTrue(getLastLine(readAnswer()).endsWith(" added")); // success
236:
237: sendCommand("verify testNotAdded");
238: assertTrue(getLastLine(readAnswer())
239: .endsWith(" does not exist"));
240:
241: sendCommand("verify testAdd");
242: assertTrue(getLastLine(readAnswer()).endsWith(" exists"));
243:
244: sendCommand("deluser testAdd");
245: readAnswer();
246:
247: sendCommand("verify testAdd");
248: assertTrue(getLastLine(readAnswer())
249: .endsWith(" does not exist"));
250: }
251:
252: public void testDelUser() throws IOException {
253: finishSetUp(m_testConfiguration);
254: connect();
255: login();
256:
257: sendCommand("adduser testDel test");
258: assertTrue(getLastLine(readAnswer()).endsWith(" added")); // success
259:
260: sendCommand("deluser testNotDeletable");
261: assertTrue(getLastLine(readAnswer()).endsWith(" doesn't exist"));
262:
263: sendCommand("verify testDel");
264: assertTrue(getLastLine(readAnswer()).endsWith(" exists"));
265:
266: sendCommand("deluser testDel");
267: assertTrue(getLastLine(readAnswer()).endsWith(" deleted"));
268:
269: sendCommand("verify testDel");
270: assertTrue(getLastLine(readAnswer())
271: .endsWith(" does not exist"));
272: }
273:
274: public void testQuit() throws IOException {
275: finishSetUp(m_testConfiguration);
276: connect();
277: login();
278:
279: sendCommand("help");
280: try {
281: Thread.sleep(100);
282: } catch (InterruptedException e) {
283: ; // ignore
284: }
285: assertTrue("command line is effective", readAnswer().size() > 0);
286:
287: sendCommand("quit");
288: try {
289: Thread.sleep(100);
290: } catch (InterruptedException e) {
291: ; // ignore
292: }
293: readAnswer();
294:
295: sendCommand("help");
296: try {
297: Thread.sleep(100);
298: } catch (InterruptedException e) {
299: ; // ignore
300: }
301: assertNull("connection is closed", m_reader.readLine());
302: }
303:
304: public void testListUsers() throws IOException {
305: finishSetUp(m_testConfiguration);
306: connect();
307: login();
308:
309: String[] users = new String[] { "ccc", "aaa", "dddd", "bbbbb" };
310:
311: for (int i = 0; i < users.length; i++) {
312: String user = users[i];
313: sendCommand("adduser " + user + " test");
314: }
315:
316: try {
317: Thread.sleep(100);
318: } catch (InterruptedException e) {
319: ; // ignore
320: }
321: readAnswer();
322:
323: sendCommand("listusers");
324: List list = readAnswer();
325:
326: assertEquals("user count line", "Existing accounts "
327: + users.length, list.get(0));
328:
329: List readUserNames = new ArrayList();
330: for (Iterator iterator = list.iterator(); iterator.hasNext();) {
331: String answerLine = (String) iterator.next();
332: if (!answerLine.startsWith("user: "))
333: continue;
334: readUserNames.add(answerLine.substring(6));
335: }
336: assertEquals("user count", users.length, readUserNames.size());
337:
338: for (int i = 0; i < users.length; i++) {
339: String user = users[i];
340: assertTrue("name found", readUserNames.contains(user));
341: }
342: }
343:
344: public void testCommandCaseInsensitive() throws IOException {
345: finishSetUp(m_testConfiguration);
346: connect();
347: login();
348:
349: sendCommand("adduser testDel test");
350: assertTrue(getLastLine(readAnswer()).endsWith(" added")); // success
351:
352: sendCommand("verify testDel");
353: assertTrue(getLastLine(readAnswer()).endsWith(" exists"));
354:
355: sendCommand("VERIFY testDel");
356: assertTrue(getLastLine(readAnswer()).endsWith(" exists"));
357:
358: sendCommand("vErIfY testDel");
359: assertTrue(getLastLine(readAnswer()).endsWith(" exists"));
360: }
361:
362: public void testParameterCaseSensitive() throws IOException {
363: finishSetUp(m_testConfiguration);
364: connect();
365: login();
366:
367: sendCommand("adduser testDel test");
368: assertTrue(getLastLine(readAnswer()).endsWith(" added")); // success
369:
370: sendCommand("verify testDel");
371: assertTrue(getLastLine(readAnswer()).endsWith(" exists"));
372:
373: sendCommand("verify TESTDEL");
374: assertTrue(getLastLine(readAnswer())
375: .endsWith(" does not exist"));
376:
377: sendCommand("verify testdel");
378: assertTrue(getLastLine(readAnswer())
379: .endsWith(" does not exist"));
380: }
381:
382: public void testAlias() throws IOException {
383: m_mockUsersRepository.setForceUseJamesUser();
384: finishSetUp(m_testConfiguration);
385: connect();
386: login();
387:
388: // do some tests when parameter users don't exist
389: sendCommand("setalias testNonExist1 testNonExist2");
390: assertTrue(getLastLine(readAnswer()).equals(
391: "No such user testNonExist1"));
392:
393: sendCommand("adduser testAlias1 test");
394: assertTrue(getLastLine(readAnswer()).endsWith(" added"));
395:
396: sendCommand("showalias testAlias1");
397: assertTrue(getLastLine(readAnswer()).equals(
398: "User testAlias1 does not currently have an alias"));
399:
400: sendCommand("setalias testAlias1 testNonExist2");
401: assertTrue(getLastLine(readAnswer()).equals(
402: "Alias unknown to server - create that user first."));
403:
404: sendCommand("setalias testNonExist1 testAlias");
405: assertTrue(getLastLine(readAnswer()).equals(
406: "No such user testNonExist1"));
407:
408: sendCommand("adduser testAlias2 test");
409: assertTrue(getLastLine(readAnswer()).endsWith(" added"));
410:
411: // regular alias
412: sendCommand("setalias testAlias1 testAlias2");
413: assertTrue(getLastLine(readAnswer()).equals(
414: "Alias for testAlias1 set to:testAlias2"));
415:
416: //TODO: is this correct? even primitive circular aliasing allowed!
417: sendCommand("setalias testAlias2 testAlias1");
418: assertTrue(getLastLine(readAnswer()).equals(
419: "Alias for testAlias2 set to:testAlias1"));
420:
421: // did first one persist?
422: sendCommand("showalias testAlias1");
423: assertTrue(getLastLine(readAnswer()).equals(
424: "Current alias for testAlias1 is: testAlias2"));
425:
426: //TODO: is this correct? setting self as alias!
427: sendCommand("setalias testAlias1 testAlias1");
428: assertTrue(getLastLine(readAnswer()).equals(
429: "Alias for testAlias1 set to:testAlias1"));
430:
431: sendCommand("adduser testAlias3 test");
432: assertTrue(getLastLine(readAnswer()).endsWith(" added"));
433:
434: // re-set, simply overwrites
435: sendCommand("setalias testAlias1 testAlias3");
436: assertTrue(getLastLine(readAnswer()).equals(
437: "Alias for testAlias1 set to:testAlias3"));
438:
439: // check overwrite
440: sendCommand("showalias testAlias1");
441: assertTrue(getLastLine(readAnswer()).equals(
442: "Current alias for testAlias1 is: testAlias3"));
443:
444: // retreat
445: sendCommand("unsetalias testAlias1");
446: assertTrue(getLastLine(readAnswer()).equals(
447: "Alias for testAlias1 unset"));
448:
449: // check removed alias
450: sendCommand("showalias testAlias1");
451: assertTrue(getLastLine(readAnswer()).equals(
452: "User testAlias1 does not currently have an alias"));
453:
454: }
455:
456: public void testForward() throws IOException {
457: m_mockUsersRepository.setForceUseJamesUser();
458: finishSetUp(m_testConfiguration);
459: connect();
460: login();
461:
462: // do some tests when parameter users don't exist
463: sendCommand("setforwarding testNonExist1 testForward1@locahost");
464: assertTrue(getLastLine(readAnswer()).equals(
465: "No such user testNonExist1"));
466:
467: sendCommand("adduser testForwardUser test");
468: assertTrue(getLastLine(readAnswer()).endsWith(" added"));
469:
470: sendCommand("showforwarding testForwardUser");
471: assertTrue(getLastLine(readAnswer())
472: .equals(
473: "User testForwardUser is not currently being forwarded"));
474:
475: sendCommand("setforwarding testForwardUser testForward1@locahost");
476: assertTrue(getLastLine(readAnswer())
477: .equals(
478: "Forwarding destination for testForwardUser set to:testForward1@locahost"));
479:
480: // did it persist?
481: sendCommand("showforwarding testForwardUser");
482: assertTrue(getLastLine(readAnswer())
483: .equals(
484: "Current forwarding destination for testForwardUser is: testForward1@locahost"));
485:
486: // re-set, simply overwrites
487: sendCommand("setforwarding testForwardUser testForward2@locahost");
488: assertTrue(getLastLine(readAnswer())
489: .equals(
490: "Forwarding destination for testForwardUser set to:testForward2@locahost"));
491:
492: // check overwrite
493: sendCommand("showforwarding testForwardUser");
494: assertTrue(getLastLine(readAnswer())
495: .equals(
496: "Current forwarding destination for testForwardUser is: testForward2@locahost"));
497:
498: // retreat
499: sendCommand("unsetforwarding testForwardUser");
500: assertTrue(getLastLine(readAnswer()).equals(
501: "Forward for testForwardUser unset"));
502:
503: // check removed forward
504: sendCommand("showforwarding testForwardUser");
505: assertTrue(getLastLine(readAnswer())
506: .equals(
507: "User testForwardUser is not currently being forwarded"));
508:
509: }
510:
511: public void testSetPassword() throws IOException {
512: finishSetUp(m_testConfiguration);
513: connect();
514: login();
515:
516: sendCommand("adduser testPwdUser pwd1");
517: assertTrue(getLastLine(readAnswer()).endsWith(" added"));
518:
519: assertTrue("initial password", m_mockUsersRepository.test(
520: "testPwdUser", "pwd1"));
521:
522: sendCommand("setpassword testPwdUser ");
523: assertTrue("password changed to empty", m_mockUsersRepository
524: .test("testPwdUser", "pwd1"));
525: readAnswer(); // ignore
526:
527: // change pwd
528: sendCommand("setpassword testPwdUser pwd2");
529: assertTrue("password not changed to pwd2",
530: m_mockUsersRepository.test("testPwdUser", "pwd2"));
531: readAnswer(); // ignore
532:
533: // assure case sensitivity
534: sendCommand("setpassword testPwdUser pWD2");
535: assertFalse("password not changed to pWD2",
536: m_mockUsersRepository.test("testPwdUser", "pwd2"));
537: assertTrue("password not changed to pWD2",
538: m_mockUsersRepository.test("testPwdUser", "pWD2"));
539: readAnswer(); // ignore
540:
541: }
542: }
|