-
Notifications
You must be signed in to change notification settings - Fork 25.3k
Display security auto-configuration with fancy unicode #82740
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
ff73d15
ace158f
8d56819
1a674d4
f796b57
b0ab407
4951602
089e552
29b9f79
d98599c
5ab6a29
7148258
04853a3
e61226a
4686054
7e2085f
aa73106
d0aeb74
3d74a79
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,9 @@ | |
|
||
package org.elasticsearch.io.ansi; | ||
|
||
import org.elasticsearch.bootstrap.ConsoleLoader; | ||
import org.elasticsearch.test.ESTestCase; | ||
import org.fusesource.jansi.Ansi; | ||
import org.fusesource.jansi.AnsiColors; | ||
import org.fusesource.jansi.AnsiMode; | ||
import org.fusesource.jansi.AnsiPrintStream; | ||
|
@@ -17,9 +19,12 @@ | |
import org.fusesource.jansi.io.AnsiProcessor; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.nio.charset.Charset; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
import static org.hamcrest.Matchers.is; | ||
import static org.hamcrest.Matchers.notNullValue; | ||
import static org.hamcrest.Matchers.nullValue; | ||
|
||
public class AnsiConsoleLoaderTests extends ESTestCase { | ||
|
||
|
@@ -30,35 +35,68 @@ public class AnsiConsoleLoaderTests extends ESTestCase { | |
|
||
private static final AnsiOutputStream.IoRunnable NO_OP_RUNNABLE = () -> {}; | ||
|
||
private static final Charset[] charsets = new Charset[] { | ||
StandardCharsets.US_ASCII, | ||
StandardCharsets.ISO_8859_1, | ||
StandardCharsets.UTF_8, | ||
StandardCharsets.UTF_16, | ||
StandardCharsets.UTF_16LE, | ||
StandardCharsets.UTF_16BE }; | ||
|
||
public void testNullOutputIsNotConsole() { | ||
assertThat(AnsiConsoleLoader.isValidConsole(null), is(false)); | ||
assertThat(AnsiConsoleLoader.newConsole(null), nullValue()); | ||
} | ||
|
||
public void testRedirectedOutputIsNotConsole() { | ||
try (AnsiPrintStream ansiPrintStream = buildStream(AnsiType.Redirected, randomIntBetween(80, 120))) { | ||
assertThat(AnsiConsoleLoader.isValidConsole(ansiPrintStream), is(false)); | ||
assertThat(AnsiConsoleLoader.newConsole(ansiPrintStream), nullValue()); | ||
} | ||
} | ||
|
||
public void testUnsupportedTerminalIsNotConsole() { | ||
try (AnsiPrintStream ansiPrintStream = buildStream(AnsiType.Unsupported, randomIntBetween(80, 120))) { | ||
assertThat(AnsiConsoleLoader.isValidConsole(ansiPrintStream), is(false)); | ||
assertThat(AnsiConsoleLoader.newConsole(ansiPrintStream), nullValue()); | ||
} | ||
} | ||
|
||
public void testZeroWidthTerminalIsNotConsole() { | ||
try (AnsiPrintStream ansiPrintStream = buildStream(randomFrom(SUPPORTED_TERMINAL_TYPES), 0)) { | ||
assertThat(AnsiConsoleLoader.isValidConsole(ansiPrintStream), is(false)); | ||
assertThat(AnsiConsoleLoader.newConsole(ansiPrintStream), nullValue()); | ||
} | ||
} | ||
|
||
public void testStandardTerminalIsConsole() { | ||
int width = randomIntBetween(40, 260); | ||
try (AnsiPrintStream ansiPrintStream = buildStream(randomFrom(SUPPORTED_TERMINAL_TYPES), width)) { | ||
ConsoleLoader.Console console = AnsiConsoleLoader.newConsole(ansiPrintStream); | ||
assertThat(console, notNullValue()); | ||
assertThat(console.width().get(), is(width)); | ||
} | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a test we can write that would fail if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, I don't have good ideas. The method would fail if the classes in the JANSI lib change structure, following an upgrade, but we can't simulate that in a test. We do not have tests asserting the auto-configuration message to the terminal. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We assert the message to the terminal in ArchiveGenerateInitialCredentialsTests for some simple things but we need to adjust and fix these tests ( currently muted ) and we can add additional cases for sure There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good, I missed those! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TBC, when those tests are unmuted, I believe we can assert there that the displayed information contains unicode chars (on some platforms, eg not Windows), which also implies that reflection worked. |
||
public void testConsoleCharset() { | ||
Charset charset = randomFrom(charsets); | ||
try (AnsiPrintStream ansiPrintStream = buildStream(randomFrom(SUPPORTED_TERMINAL_TYPES), randomIntBetween(40, 260), charset)) { | ||
ConsoleLoader.Console console = AnsiConsoleLoader.newConsole(ansiPrintStream); | ||
assertThat(console, notNullValue()); | ||
assertThat(console.charset(), is(charset)); | ||
} | ||
} | ||
|
||
public void testDisableANSI() { | ||
Ansi.setEnabled(false); | ||
try (AnsiPrintStream ansiPrintStream = buildStream(randomFrom(SUPPORTED_TERMINAL_TYPES), randomIntBetween(40, 260))) { | ||
assertThat(AnsiConsoleLoader.isValidConsole(ansiPrintStream), is(true)); | ||
ConsoleLoader.Console console = AnsiConsoleLoader.newConsole(ansiPrintStream); | ||
assertThat(console, notNullValue()); | ||
assertThat(console.ansiEnabled(), is(false)); | ||
} | ||
} | ||
|
||
private AnsiPrintStream buildStream(AnsiType type, int width) { | ||
return buildStream(type, width, randomFrom(charsets)); | ||
} | ||
|
||
private AnsiPrintStream buildStream(AnsiType type, int width, Charset cs) { | ||
final ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
final AnsiOutputStream ansiOutputStream = new AnsiOutputStream( | ||
baos, | ||
|
@@ -67,12 +105,11 @@ private AnsiPrintStream buildStream(AnsiType type, int width) { | |
new AnsiProcessor(baos), | ||
type, | ||
randomFrom(AnsiColors.values()), | ||
randomFrom(StandardCharsets.UTF_8, StandardCharsets.US_ASCII, StandardCharsets.UTF_16, StandardCharsets.ISO_8859_1), | ||
cs, | ||
NO_OP_RUNNABLE, | ||
NO_OP_RUNNABLE, | ||
randomBoolean() | ||
); | ||
return new AnsiPrintStream(ansiOutputStream, randomBoolean()); | ||
} | ||
|
||
} |
Uh oh!
There was an error while loading. Please reload this page.