private void testClose(TestContext context, Consumer<Term> closer) throws Exception { Async async = context.async(); termHandler = term -> { term.closeHandler(v -> { async.complete(); }); closer.accept(term); }; startShell(); Session session = createSession("paulo", "secret", false); session.connect(); Channel channel = session.openChannel("shell"); channel.connect(); while (channel.isClosed()) { Thread.sleep(10); } }
@Test public void testCloseImmediatly(TestContext context) throws Exception { testClose(context, Term::close); }
@Test public void testType(TestContext context) throws Exception { Async async = context.async(); termHandler = term -> { context.assertEquals("vt100", term.type()); async.complete(); }; startShell(); Session session = createSession("paulo", "secret", false); session.connect(); Channel channel = session.openChannel("shell"); channel.connect(); }
@Test public void testClose(TestContext context) throws Exception { testClose(context, term -> { vertx.setTimer(10, id -> { term.close(); }); }); }
@Test public void testCloseHandler(TestContext context) throws Exception { Async async = context.async(); termHandler = term -> { term.closeHandler(v -> { async.complete(); }); }; startShell(); Session session = createSession("paulo", "secret", false); session.connect(); Channel channel = session.openChannel("shell"); channel.connect(); channel.disconnect(); session.disconnect(); }
@Test public void testWrite() throws Exception { termHandler = term -> { term.write("hello"); }; startShell(); Session session = createSession("paulo", "secret", false); session.connect(); Channel channel = session.openChannel("shell"); channel.connect(); Reader in = new InputStreamReader(channel.getInputStream()); int count = 5; StringBuilder sb = new StringBuilder(); while (count > 0) { int code = in.read(); if (code == -1) { count = 0; } else { count--; sb.append((char)code); } } assertEquals("hello", sb.toString()); channel.disconnect(); session.disconnect(); }
@Test public void testRead(TestContext context) throws Exception { Async async = context.async(); termHandler = term -> { term.stdinHandler(s -> { context.assertEquals("hello", s); async.complete(); }); }; startShell(); Session session = createSession("paulo", "secret", false); session.connect(); Channel channel = session.openChannel("shell"); channel.connect(); OutputStream out = channel.getOutputStream(); out.write("hello".getBytes()); out.flush(); channel.disconnect(); session.disconnect(); }
@Test public void testKeymapFromFilesystem() throws Exception { URL url = TermServer.class.getResource(SSHTermOptions.DEFAULT_INPUTRC); File f = new File(url.toURI()); termHandler = Term::close; startShell(new SSHTermOptions().setIntputrc(f.getAbsolutePath()).setPort(5000).setHost("localhost").setKeyPairOptions( new JksOptions().setPath("src/test/resources/server-keystore.jks").setPassword("wibble")). setAuthOptions(new ShiroAuthOptions().setType(ShiroAuthRealmType.PROPERTIES).setConfig( new JsonObject().put("properties_path", "classpath:test-auth.properties")))); Session session = createSession("paulo", "secret", false); session.connect(); Channel channel = session.openChannel("shell"); channel.connect(); }
async.complete(); }; startShell(new SSHTermOptions().setPort(5000).setHost("localhost").setKeyPairOptions( new JksOptions().setPath("src/test/resources/server-keystore.jks").setPassword("wibble"))); Session session = createSession("paulo", "anothersecret", false); session.connect(); Channel channel = session.openChannel("shell");
@Test public void testExternalAuthProviderFails(TestContext context) throws Exception { AtomicInteger count = new AtomicInteger(); authProvider = (authInfo, resultHandler) -> { count.incrementAndGet(); resultHandler.handle(Future.failedFuture("not authenticated")); }; termHandler = term -> { context.fail(); }; startShell(new SSHTermOptions().setPort(5000).setHost("localhost").setKeyPairOptions( new JksOptions().setPath("src/test/resources/server-keystore.jks").setPassword("wibble"))); Session session = createSession("paulo", "anothersecret", false); try { session.connect(); context.fail("Was not expected to login"); } catch (JSchException e) { assertEquals("Auth cancel", e.getMessage()); } context.assertEquals(1, count.get()); }
@Test public void testResizeHandler(TestContext context) throws Exception { Async async = context.async(); termHandler = term -> { term.resizehandler(v -> { context.assertEquals(20, term.width()); context.assertEquals(10, term.height()); async.complete(); }); }; startShell(); Session session = createSession("paulo", "secret", false); session.connect(); ChannelShell channel = (ChannelShell) session.openChannel("shell"); channel.connect(); OutputStream out = channel.getOutputStream(); channel.setPtySize(20, 10, 20 * 8, 10 * 8); out.flush(); channel.disconnect(); session.disconnect(); }
@Test public void testDifferentCharset(TestContext context) throws Exception { termHandler = term -> { term.write("\u20AC"); term.close(); }; startShell(new SSHTermOptions().setDefaultCharset("ISO_8859_1").setPort(5000).setHost("localhost").setKeyPairOptions( new JksOptions().setPath("src/test/resources/server-keystore.jks").setPassword("wibble")). setAuthOptions(new ShiroAuthOptions().setType(ShiroAuthRealmType.PROPERTIES).setConfig( new JsonObject().put("properties_path", "classpath:test-auth.properties")))); Session session = createSession("paulo", "secret", false); session.connect(); Channel channel = session.openChannel("shell"); channel.connect(); InputStream in = channel.getInputStream(); int b = in.read(); context.assertEquals(63, b); }