genericunixprotocols/telnetsrv/test/src/TelnetTest.java
changeset 0 c6b0df440bee
equal deleted inserted replaced
-1:000000000000 0:c6b0df440bee
       
     1 /*
       
     2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 import java.io.IOException;
       
    19 import java.io.InputStream;
       
    20 import java.io.OutputStream;
       
    21 import java.util.regex.Matcher;
       
    22 import java.util.regex.Pattern;
       
    23 
       
    24 import junit.framework.TestCase;
       
    25 
       
    26 import org.apache.commons.net.telnet.TelnetClient;
       
    27 
       
    28 
       
    29 public class TelnetTest extends TestCase {
       
    30 	
       
    31 	/* (non-Javadoc)
       
    32 	 * @see junit.framework.TestCase#setUp()
       
    33 	 */
       
    34 	TelnetClient telnetClient = new TelnetClient();
       
    35 	
       
    36 	InputStream is;
       
    37 	OutputStream os;
       
    38 	byte promptBuffer[] = new byte[11]; //"localhost# "
       
    39 	String prompt = "localhost# ";
       
    40 	
       
    41 	
       
    42 	//FTP&Telnet-Connection-001
       
    43 	//To Establish a connection from remote host to Symbian OS device(H2/H4)
       
    44 	
       
    45 	protected void setUp() throws Exception {
       
    46 		super.setUp();
       
    47 		
       
    48 		String address = System.getProperty("address");
       
    49 		
       
    50 		telnetClient.connect(/*"172.0.0.3"*/address);
       
    51 		
       
    52 		is = telnetClient.getInputStream();
       
    53 		os = telnetClient.getOutputStream();
       
    54 		
       
    55 		is.read(promptBuffer);
       
    56 		
       
    57 		
       
    58 	}
       
    59 	
       
    60 	/* (non-Javadoc)
       
    61 	 * @see junit.framework.TestCase#tearDown()
       
    62 	 */
       
    63 	
       
    64 	//FTP&Telnet-Connection-002
       
    65 	//To Close a connection from remote host to Symbian OS device(H2/H4)
       
    66 	
       
    67 	protected void tearDown() throws Exception {
       
    68 		super.tearDown();
       
    69 		
       
    70 		os.write("exit\r\n".getBytes());
       
    71 		os.flush();
       
    72 	
       
    73 		telnetClient.disconnect();
       
    74 	}
       
    75 	
       
    76 	private String readUntil(InputStream in, String pattern) throws IOException {
       
    77 		StringBuffer sb = new StringBuffer();
       
    78 		while(!(sb.toString().endsWith(prompt)))
       
    79 		{
       
    80 			sb.append((char)in.read());
       
    81 		}
       
    82 		return sb.toString();
       
    83 	}
       
    84 	
       
    85 	public void testAYT() throws IOException, IllegalArgumentException, InterruptedException
       
    86 	{
       
    87 		assertTrue(telnetClient.sendAYT(5000));
       
    88 	}
       
    89 	
       
    90 	public void testCd() throws IOException
       
    91 	{
       
    92 		os.write("cd c:\r\n".getBytes());
       
    93 		os.flush();
       
    94 		is.read(promptBuffer);	//"localhost# "
       
    95 		assertEquals(prompt, new String(promptBuffer));
       
    96 	}
       
    97 	
       
    98 	public void testPwd() throws IOException
       
    99 	{
       
   100 		os.write("pwd\r\n".getBytes());
       
   101 		os.flush();
       
   102 		
       
   103 		String s = readUntil(is,prompt);
       
   104 		s = s.substring(1, s.indexOf(prompt));
       
   105 		
       
   106 		assertEquals(":\\Private\\2000cd0f\\\r\n", s);
       
   107 		
       
   108 	}
       
   109 	
       
   110 	public void testCdPwd() throws IOException
       
   111 	{
       
   112 		
       
   113 		os.write("cd c:\r\n".getBytes());
       
   114 		os.flush();
       
   115 		is.read(promptBuffer);	//"localhost# "
       
   116 		assertEquals(new String(promptBuffer),prompt);
       
   117 		
       
   118 		os.write("pwd\r\n".getBytes());
       
   119 		os.flush();
       
   120 		
       
   121 		String s = readUntil(is,prompt);
       
   122 		s = s.substring(0, s.indexOf(prompt));
       
   123 		
       
   124 		assertEquals("c:\r\n", s);
       
   125 	}
       
   126 	
       
   127 	//DEF117414
       
   128 	//zsh cannot run external commands after running 82 commands
       
   129 	//TODO: change maxIter to 100 when fixed
       
   130 	
       
   131 	public void testPs() throws IOException
       
   132 	{
       
   133 	
       
   134 		int maxIter = 2;
       
   135 		
       
   136 		for(int i=0; i<maxIter; i++)
       
   137 		{
       
   138 
       
   139 		os.write("ps\r\n".getBytes());
       
   140 		os.flush();
       
   141 		
       
   142 		String s = readUntil(is,prompt);
       
   143 		
       
   144 		s = s.substring(0, s.indexOf(prompt));
       
   145 		
       
   146 		System.out.println(s);
       
   147 		
       
   148 		System.out.println("iteration "+(i+1)+" of "+maxIter);
       
   149 		
       
   150 		Pattern p = Pattern.compile(".*ps.EXE\\[2000ee91\\]\\d\\d\\d\\d\r\n$", Pattern.DOTALL | Pattern.CASE_INSENSITIVE); //Pattern.DOTALL => '.' includes end of line
       
   151 		Matcher m = p.matcher(s);
       
   152 		assertTrue(m.matches());
       
   153 		}
       
   154 		
       
   155 	}
       
   156 	
       
   157 	
       
   158 	//DEF116570
       
   159 	//after few successful ps and grep, when you do ps and grep on non exsiting string the emulator hangs
       
   160 	
       
   161 	public void testPsGrepNonExistingString() throws IOException
       
   162 	{
       
   163 		int maxIter = 2;
       
   164 		
       
   165 		for(int i=0; i<maxIter; i++)
       
   166 		{
       
   167 		
       
   168 		os.write("ps | grep nonexisting\r\n".getBytes());
       
   169 		os.flush();
       
   170 		
       
   171 		String s = readUntil(is,prompt);
       
   172 		
       
   173 		s = s.substring(0, s.indexOf(prompt));
       
   174 		
       
   175 		assertEquals(0,s.length());
       
   176 		}
       
   177 	}
       
   178 	
       
   179 	//FTP&Telnet-Remote Execution-001
       
   180 	//To Launch a process on a remote Symbian OS device(H2/H4) from PC
       
   181 	
       
   182 	public void testFTP_Telnet_Remote_Execution_001() throws IOException
       
   183 	{
       
   184 		os.write("testexecute c:\\nonexisting.script\r\n".getBytes());
       
   185 		os.flush();
       
   186 		
       
   187 		String s = readUntil(is,prompt);
       
   188 		
       
   189 		assertEquals(11, s.length());
       
   190 	}
       
   191 	
       
   192 	
       
   193 	//FTP&Telnet-Remote Execution-002
       
   194 	//To pool all the process on the symbian OS device(H2/H4) from PC
       
   195 	
       
   196 	public void testFTP_Telnet_Remote_Execution_002() throws IOException
       
   197 	{
       
   198 		os.write("ps\r\n".getBytes());
       
   199 		os.flush();
       
   200 		
       
   201 		String s = readUntil(is,prompt);
       
   202 		
       
   203 		s = s.substring(0, s.indexOf(prompt));
       
   204 		
       
   205 	//	System.out.println(s);
       
   206 		
       
   207 		Pattern p = Pattern.compile(".*ps.EXE\\[2000ee91\\]\\d\\d\\d\\d\r\n$", Pattern.DOTALL | Pattern.CASE_INSENSITIVE); //Pattern.DOTALL => '.' includes end of line
       
   208 		Matcher m = p.matcher(s);
       
   209 		assertTrue(m.matches());
       
   210 	}
       
   211 	
       
   212 	
       
   213 	//FTP&Telnet-Remote Execution-003
       
   214 	// Kill any process on Symbian OS device(H2/H4) from PC
       
   215 	
       
   216 	public void testFTP_Telnet_Remote_Execution_003() throws IOException
       
   217 	{
       
   218 		os.write("testexecute\r\n".getBytes());
       
   219 		os.flush();
       
   220 		
       
   221 		//this shell will lock, open another one and kill TEF
       
   222 		
       
   223 		TelnetClient tc = new TelnetClient();
       
   224 		tc.connect(telnetClient.getRemoteAddress());
       
   225 		
       
   226 		InputStream is2 = tc.getInputStream();
       
   227 		OutputStream os2 = tc.getOutputStream();
       
   228 		
       
   229 		readUntil(is2, prompt);
       
   230 		
       
   231 		os2.write("ps | grep TestExecute\r\n".getBytes());
       
   232 		os2.flush();
       
   233 		
       
   234 		String s = readUntil(is2, prompt);
       
   235 		
       
   236 		Pattern p = Pattern.compile(".*\\D+(\\d+)\\s[\\s:\\d]+TestExecute Script Engine.*", Pattern.DOTALL | Pattern.CASE_INSENSITIVE); //Pattern.DOTALL => '.' includes end of line
       
   237 		Matcher m = p.matcher(s);
       
   238 		
       
   239 		assertTrue(m.matches());
       
   240 				
       
   241 		String s1 = m.group(1);
       
   242 		
       
   243 		int pid = Integer.parseInt(s1);
       
   244 		
       
   245 		os2.write(("kill "+pid+"\r\n").getBytes());
       
   246 		os2.flush();
       
   247 		readUntil(is2, prompt);
       
   248 		os2.write("bye\r\n".getBytes());
       
   249 		os2.flush();
       
   250 		
       
   251 		//we should be able now to close the other shell
       
   252 		
       
   253 		readUntil(is, prompt);
       
   254 		
       
   255 		
       
   256 	}
       
   257 	
       
   258 	//FTP&Telnet-Remote Execution-004
       
   259 	//To verify the output stream of any Symbian OS process(H2/H4) to PC
       
   260 	public void testFTP_Telnet_Remote_Execution_004() throws IOException
       
   261 	{
       
   262 		
       
   263 		os.write("helloworld $PIPE\r\n".getBytes());
       
   264 		os.flush();
       
   265 		
       
   266 		String s = readUntil(is, prompt);
       
   267 		
       
   268 		assertTrue(s.length()>0); //should print "ABCDEFG...".length() > 0
       
   269 		
       
   270 	}
       
   271 	
       
   272 	//FTP&Telnet-Remote Execution-005
       
   273 	//To verify the Standard Error display on Symbian OS device(H2/H4) to PC
       
   274 	public void testFTP_Telnet_Remote_Execution_005() throws IOException
       
   275 	{
       
   276 		os.write("aaa\r\n".getBytes());
       
   277 		os.flush();
       
   278 		
       
   279 		String s = readUntil(is, prompt);
       
   280 		
       
   281 		assertEquals("zsh: command not found: aaa",s.substring(0, s.lastIndexOf("\r\n"))); 
       
   282 	}
       
   283 	
       
   284 	
       
   285 	//FTP&Telnet-Software Install-001
       
   286 	//Install a SIS file on the Symbian OS device (H2/H4) from PC
       
   287 	public void testFTP_Telnet_Install_001() throws IOException
       
   288 	{
       
   289 		
       
   290 		os.write("install c:\\10210d02.sis\r\n".getBytes());
       
   291 		os.flush();
       
   292 		
       
   293 		String s = readUntil(is, prompt);
       
   294 		
       
   295 		assertEquals("Return Code: 0",s.substring(0, s.lastIndexOf("\r\n"))); 
       
   296 	}
       
   297 	
       
   298 	//FTP&Telnet-Software Install-002
       
   299 	//Uninstall the SIS file on the symbian OS device (H2/H4) from PC
       
   300 	public void testFTP_Telnet_Install_002() throws IOException
       
   301 	{
       
   302 		
       
   303 		os.write("uninstall 10210d02\r\n".getBytes());
       
   304 		os.flush();
       
   305 		
       
   306 		String s = readUntil(is, prompt);
       
   307 		
       
   308 		assertEquals("Return Code: 0",s.substring(0, s.lastIndexOf("\r\n"))); 
       
   309 	}
       
   310 	
       
   311 	
       
   312 	
       
   313 	
       
   314 }