Add some checks to prevent #13 happen.
3 * see AUTHORS for the list of contributors
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 package test.unit.daemon;
21 import java.io.IOException;
22 import java.lang.reflect.InvocationTargetException;
23 import java.lang.reflect.Method;
24 import java.nio.channels.SocketChannel;
25 import junit.framework.TestCase;
26 import org.sonews.daemon.NNTPConnection;
27 import org.sonews.daemon.command.ArticleCommand;
28 import org.sonews.daemon.command.CapabilitiesCommand;
29 import org.sonews.daemon.command.GroupCommand;
30 import org.sonews.daemon.command.UnsupportedCommand;
33 * Unit test for class NNTPConnection.
34 * @author Christian Lins
37 public class NNTPConnectionTest extends TestCase
40 public void testLineReceived()
41 throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
43 NNTPConnection conn = null;
49 conn = new NNTPConnection(null);
50 fail("Should have raised an IllegalArgumentException");
52 catch(IOException ex) {ex.printStackTrace();}
54 catch(IllegalArgumentException ex){}
58 conn = new NNTPConnection(SocketChannel.open());
67 // Make interesting methods accessible
68 Class clazz = conn.getClass();
69 Method methTryReadLock = clazz.getDeclaredMethod("tryReadLock", null);
70 methTryReadLock.setAccessible(true);
71 Method methLineReceived = clazz.getDeclaredMethod("lineReceived", new byte[0].getClass());
72 methLineReceived.setAccessible(true);
76 // conn.lineReceived(null);
77 methLineReceived.invoke(conn, null);
78 fail("Should have raised an IllegalArgumentException");
80 catch(IllegalArgumentException ex){}
84 // conn.lineReceived(new byte[0]);
85 methLineReceived.invoke(conn, new byte[0]);
86 fail("Should have raised IllegalStateException");
88 catch(InvocationTargetException ex){}
90 boolean tryReadLock = (Boolean)methTryReadLock.invoke(conn, null);
91 assertTrue(tryReadLock);
93 // conn.lineReceived("MODE READER".getBytes());
94 methLineReceived.invoke(conn, "MODE READER".getBytes());
96 // conn.lineReceived("sdkfsdjnfksjfdng ksdf gksjdfngk nskfng ksndfg ".getBytes());
97 methLineReceived.invoke(conn, "sdkfsdjnfksjfdng ksdf ksndfg ".getBytes());
99 // conn.lineReceived(new byte[1024]); // Too long
100 methLineReceived.invoke(conn, new byte[1024]);
102 Method mpcmdl = conn.getClass().getDeclaredMethod("parseCommandLine", String.class);
103 mpcmdl.setAccessible(true);
105 Object result = mpcmdl.invoke(conn, "");
106 assertNotNull(result);
107 assertTrue(result instanceof UnsupportedCommand);
109 result = mpcmdl.invoke(conn, "aRtiCle");
110 assertNotNull(result);
111 assertTrue(result instanceof ArticleCommand);
113 result = mpcmdl.invoke(conn, "capAbilItIEs");
114 assertNotNull(result);
115 assertTrue(result instanceof CapabilitiesCommand);
117 result = mpcmdl.invoke(conn, "grOUp");
118 assertNotNull(result);
119 assertTrue(result instanceof GroupCommand);