-
Notifications
You must be signed in to change notification settings - Fork 461
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WFCORE-3187] add-user.sh tool prints unexpected characters if arrow …
…keys are pushed during interactive mode
- Loading branch information
Showing
7 changed files
with
240 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
...gement/src/main/java/org/jboss/as/domain/management/security/adduser/ReadLineHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source. | ||
* Copyright 2017, Red Hat, Inc., and individual contributors | ||
* as indicated by the @author tags. See the copyright.txt file in the | ||
* distribution for a full listing of individual contributors. | ||
* | ||
* This is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU Lesser General Public License as | ||
* published by the Free Software Foundation; either version 2.1 of | ||
* the License, or (at your option) any later version. | ||
* | ||
* This software is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this software; if not, write to the Free | ||
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | ||
* 02110-1301 USA, or see the FSF site: http://www.fsf.org. | ||
*/ | ||
|
||
package org.jboss.as.domain.management.security.adduser; | ||
|
||
import java.util.function.Consumer; | ||
|
||
import org.aesh.readline.Prompt; | ||
import org.aesh.readline.Readline; | ||
import org.aesh.readline.ReadlineBuilder; | ||
import org.aesh.terminal.Connection; | ||
import org.aesh.terminal.tty.Signal; | ||
|
||
/** | ||
* Class to read line from terminal using Aesh-readline library. | ||
* Created by Marek Marusic <mmarusic@redhat.com> on 8/7/17. | ||
*/ | ||
public class ReadLineHandler implements Consumer<Connection> { | ||
private String line; | ||
private Prompt prompt; | ||
|
||
public ReadLineHandler(Prompt prompt) { | ||
this.prompt = prompt; | ||
} | ||
|
||
public String getLine() { | ||
return line; | ||
} | ||
|
||
@Override | ||
public void accept(Connection connection) { | ||
read(connection, ReadlineBuilder.builder().enableHistory(false).build(), prompt); | ||
// lets open the connection to the terminal using this thread | ||
connection.openBlocking(); | ||
} | ||
|
||
protected void read(Connection connection, Readline readline, Prompt prompt) { | ||
// Set the Ctrl-C handler | ||
connection.setSignalHandler(signal -> { | ||
if (signal == Signal.INT) | ||
connection.close(); | ||
}); | ||
|
||
readline.readline(connection, prompt, line -> { | ||
// we specify a simple lambda consumer to read the input thats saved into line. | ||
if (line != null) { | ||
this.line = line; | ||
connection.close(); | ||
} else | ||
read(connection, readline, prompt); | ||
}); | ||
} | ||
} |
134 changes: 134 additions & 0 deletions
134
...nt/src/test/java/org/jboss/as/domain/management/security/adduser/AeshJavaConsoleTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source. | ||
* Copyright 2017, Red Hat, Inc., and individual contributors | ||
* as indicated by the @author tags. See the copyright.txt file in the | ||
* distribution for a full listing of individual contributors. | ||
* | ||
* This is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU Lesser General Public License as | ||
* published by the Free Software Foundation; either version 2.1 of | ||
* the License, or (at your option) any later version. | ||
* | ||
* This software is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this software; if not, write to the Free | ||
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | ||
* 02110-1301 USA, or see the FSF site: http://www.fsf.org. | ||
*/ | ||
|
||
package org.jboss.as.domain.management.security.adduser; | ||
|
||
import org.aesh.readline.terminal.Key; | ||
import org.aesh.readline.tty.terminal.TerminalConnection; | ||
import org.junit.Test; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.PipedInputStream; | ||
import java.io.PipedOutputStream; | ||
import java.nio.charset.Charset; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
/** | ||
* Created by Marek Marusic <mmarusic@redhat.com> on 8/9/17. | ||
*/ | ||
public class AeshJavaConsoleTest { | ||
|
||
@Test | ||
public void testLeftArrow() throws IOException, InterruptedException { | ||
AeshJavaConsoleTestResource aeshJavaConsoleTestResource = new AeshJavaConsoleTestResource(); | ||
aeshJavaConsoleTestResource.start(); | ||
Thread.sleep(100); | ||
|
||
aeshJavaConsoleTestResource.write("Foo"); | ||
aeshJavaConsoleTestResource.write(Key.LEFT); | ||
aeshJavaConsoleTestResource.write("F"); | ||
aeshJavaConsoleTestResource.write(Key.ENTER); | ||
aeshJavaConsoleTestResource.flushWrites(); | ||
|
||
Thread.sleep(100); | ||
assertEquals("FoFo", aeshJavaConsoleTestResource.getLine()); | ||
} | ||
|
||
@Test | ||
public void testUpArrow() throws IOException, InterruptedException { | ||
AeshJavaConsoleTestResource aeshJavaConsoleTestResource = new AeshJavaConsoleTestResource(); | ||
aeshJavaConsoleTestResource.start(); | ||
Thread.sleep(100); | ||
|
||
aeshJavaConsoleTestResource.write("Foo"); | ||
aeshJavaConsoleTestResource.write(Key.UP); | ||
aeshJavaConsoleTestResource.write("F"); | ||
aeshJavaConsoleTestResource.write(Key.ENTER); | ||
aeshJavaConsoleTestResource.flushWrites(); | ||
|
||
Thread.sleep(100); | ||
assertEquals("FooF", aeshJavaConsoleTestResource.getLine()); | ||
} | ||
|
||
@Test | ||
public void testInterruptionSignal() throws IOException, InterruptedException { | ||
AeshJavaConsoleTestResource aeshJavaConsoleTestResource = new AeshJavaConsoleTestResource(); | ||
aeshJavaConsoleTestResource.start(); | ||
Thread.sleep(100); | ||
|
||
aeshJavaConsoleTestResource.write("Foo".getBytes()); | ||
aeshJavaConsoleTestResource.write(Key.CTRL_C.getKeyValuesAsString().getBytes()); | ||
aeshJavaConsoleTestResource.flushWrites(); | ||
|
||
Thread.sleep(100); | ||
assertEquals(false, aeshJavaConsoleTestResource.isAlive()); | ||
assertEquals(Thread.State.TERMINATED, aeshJavaConsoleTestResource.getState()); | ||
} | ||
|
||
public class AeshJavaConsoleTestResource extends Thread { | ||
private PipedOutputStream pipedOutputStream; | ||
private PipedInputStream pipedInputStream; | ||
private ByteArrayOutputStream out; | ||
private String line; | ||
|
||
public AeshJavaConsoleTestResource() throws IOException { | ||
pipedOutputStream = new PipedOutputStream(); | ||
pipedInputStream = new PipedInputStream(pipedOutputStream); | ||
out = new ByteArrayOutputStream(); | ||
} | ||
|
||
public String getLine() { | ||
return line; | ||
} | ||
|
||
public void flushWrites() throws IOException { | ||
pipedOutputStream.flush(); | ||
} | ||
|
||
public void write(String data) throws IOException { | ||
write(data.getBytes()); | ||
} | ||
|
||
public void write(byte[] data) throws IOException { | ||
pipedOutputStream.write(data); | ||
} | ||
|
||
public void write(Key key) throws IOException { | ||
write(key.getKeyValuesAsString().getBytes()); | ||
} | ||
|
||
public void run() { | ||
// Create JavaConsole with custom input, output streams for testing | ||
JavaConsole javaConsole = new JavaConsole() { | ||
@Override | ||
protected void createTerminalConnection(ReadLineHandler readLineHandler) throws IOException { | ||
new TerminalConnection(Charset.defaultCharset(), pipedInputStream, out, readLineHandler); | ||
} | ||
}; | ||
|
||
line = javaConsole.readLine(""); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters