1 /*******************************************************************************
2  * Copyright (c) 2011, 2017 SAP AG and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *    Lazar Kirchev, SAP AG - initial contribution
13  ******************************************************************************/
14 
15 package org.eclipse.equinox.console.telnet;
16 
17 import org.apache.felix.service.command.CommandProcessor;
18 import org.apache.felix.service.command.CommandSession;
19 import org.easymock.EasyMock;
20 import org.eclipse.equinox.console.common.ConsoleInputStream;
21 import org.junit.Assert;
22 import org.junit.Test;
23 
24 import java.io.OutputStream;
25 import java.io.PrintStream;
26 import java.net.ConnectException;
27 import java.net.Socket;
28 import java.util.ArrayList;
29 import java.util.List;
30 
31 import static org.easymock.EasyMock.*;
32 
33 public class TelnetServerTests {
34 
35 	private static final String HOST = "localhost";
36 	private static final int PORT = 38888;
37 	private static final long WAIT_TIME = 5000;
38 	private static final int TEST_CONTENT = 100;
39 
40 	@Test
testTelnetServer()41 	public void testTelnetServer() throws Exception {
42 		try (CommandSession session = EasyMock.createMock(CommandSession.class)) {
43 			session.put((String) EasyMock.anyObject(), EasyMock.anyObject());
44 			EasyMock.expectLastCall().times(3);
45 			EasyMock.expect(session.execute((String) EasyMock.anyObject())).andReturn(new Object());
46 			session.close();
47 			EasyMock.expectLastCall();
48 			EasyMock.replay(session);
49 
50 			CommandProcessor processor = EasyMock.createMock(CommandProcessor.class);
51 			EasyMock.expect(processor.createSession((ConsoleInputStream) EasyMock.anyObject(),
52 					(PrintStream) EasyMock.anyObject(), (PrintStream) EasyMock.anyObject())).andReturn(session);
53 			EasyMock.replay(processor);
54 
55 			List<CommandProcessor> processors = new ArrayList<>();
56 			processors.add(processor);
57 			TelnetServer telnetServer = new TelnetServer(null, processors, HOST, PORT);
58 			telnetServer.start();
59 
60 			try (Socket socketClient = new Socket("localhost", PORT);) {
61 
62 				OutputStream outClient = socketClient.getOutputStream();
63 				outClient.write(TEST_CONTENT);
64 				outClient.write('\n');
65 				outClient.flush();
66 				// wait for the accept thread to finish execution
67 				try {
68 					Thread.sleep(WAIT_TIME);
69 				} catch (InterruptedException ie) {
70 					// do nothing
71 				}
72 				verify();
73 			} catch (ConnectException e) {
74 				Assert.fail("Telnet port not open");
75 			} finally {
76 				telnetServer.stopTelnetServer();
77 			}
78 		}
79 	}
80 
81 	@Test
testTelnetServerWithoutHost()82 	public void testTelnetServerWithoutHost() throws Exception {
83 		try (CommandSession session = EasyMock.createMock(CommandSession.class)) {
84 			session.put((String) EasyMock.anyObject(), EasyMock.anyObject());
85 			EasyMock.expectLastCall().times(4);
86 			EasyMock.expect(session.execute((String) EasyMock.anyObject())).andReturn(new Object());
87 			session.close();
88 			EasyMock.expectLastCall();
89 			EasyMock.replay(session);
90 
91 			CommandProcessor processor = EasyMock.createMock(CommandProcessor.class);
92 			EasyMock.expect(processor.createSession((ConsoleInputStream) EasyMock.anyObject(),
93 					(PrintStream) EasyMock.anyObject(), (PrintStream) EasyMock.anyObject())).andReturn(session);
94 			EasyMock.replay(processor);
95 
96 			List<CommandProcessor> processors = new ArrayList<>();
97 			processors.add(processor);
98 			TelnetServer telnetServer = new TelnetServer(null, processors, null, PORT);
99 			telnetServer.start();
100 
101 			try (Socket socketClient = new Socket("localhost", PORT);) {
102 				OutputStream outClient = socketClient.getOutputStream();
103 				outClient.write(TEST_CONTENT);
104 				outClient.write('\n');
105 				outClient.flush();
106 
107 				// wait for the accept thread to finish execution
108 				try {
109 					Thread.sleep(WAIT_TIME);
110 				} catch (InterruptedException ie) {
111 					// do nothing
112 				}
113 			} catch (ConnectException e) {
114 				Assert.fail("Telnet port not open");
115 			} finally {
116 				telnetServer.stopTelnetServer();
117 			}
118 		}
119 
120 	}
121 
122 }
123