-
Notifications
You must be signed in to change notification settings - Fork 6
/
TerminalClientTest.java
448 lines (381 loc) · 13.3 KB
/
TerminalClientTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
package net.infordata.em;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import com.google.common.base.Charsets;
import com.google.common.io.Resources;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.stream.Collectors;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import net.infordata.em.crt5250.XI5250Field;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestRule;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import us.abstracta.wiresham.Flow;
import us.abstracta.wiresham.VirtualTcpService;
public class TerminalClientTest {
private static final Logger LOG = LoggerFactory.getLogger(TerminalClientTest.class);
private static final long TIMEOUT_MILLIS = 10000;
private static final String SERVICE_HOST = "localhost";
private static final String TERMINAL_TYPE = "IBM-3477-FC";
@Rule
public TestRule watchman = new TestWatcher() {
@Override
public void starting(Description description) {
LOG.debug("Starting {}", description.getMethodName());
}
@Override
public void finished(Description description) {
LOG.debug("Finished {}", description.getMethodName());
}
};
private VirtualTcpService service = new VirtualTcpService();
private TerminalClient client;
private ExceptionWaiter exceptionWaiter;
private ScheduledExecutorService stableTimeoutExecutor = Executors
.newSingleThreadScheduledExecutor();
@Before
public void setup() throws IOException {
service.setSslEnabled(false);
service.setFlow(Flow.fromYml(new File(getResourceFilePath("/login.yml"))));
service.start();
client = new TerminalClient();
client.setTerminalType(TERMINAL_TYPE);
client.setConnectionTimeoutMillis(5000);
exceptionWaiter = new ExceptionWaiter();
client.setExceptionHandler(exceptionWaiter);
client.connect(SERVICE_HOST, service.getPort());
}
private String getResourceFilePath(String resourcePath) {
return getClass().getResource(resourcePath).getFile();
}
@After
public void teardown() throws Exception {
client.disconnect();
service.stop(TIMEOUT_MILLIS);
}
@Test
public void shouldGetUnlockedKeyboardWhenConnect() throws Exception {
awaitKeyboardUnlock();
assertThat(client.isKeyboardLocked()).isFalse();
}
private void awaitKeyboardUnlock() throws InterruptedException, TimeoutException {
new UnlockWaiter(client, stableTimeoutExecutor).await(TIMEOUT_MILLIS);
}
@Test
public void shouldGetWelcomeScreenWhenConnect() throws Exception {
awaitKeyboardUnlock();
assertThat(client.getScreenText())
.isEqualTo(getWelcomeScreen());
}
private String getWelcomeScreen() throws IOException {
return getFileContent("login-welcome-screen.txt");
}
private String getFileContent(String resourceFile) throws IOException {
return Resources.toString(Resources.getResource(resourceFile),
Charsets.UTF_8);
}
@Test
public void shouldGetWelcomeScreenWhenConnectWithSsl() throws Exception {
awaitKeyboardUnlock();
teardown();
service.setSslEnabled(true);
System.setProperty("javax.net.ssl.keyStore", getResourceFilePath("/keystore.jks"));
System.setProperty("javax.net.ssl.keyStorePassword", "changeit");
service.start();
client = new TerminalClient();
client.setTerminalType(TERMINAL_TYPE);
client.setSocketFactory(buildSslContext().getSocketFactory());
client.connect(SERVICE_HOST, service.getPort());
awaitKeyboardUnlock();
assertThat(client.getScreenText())
.isEqualTo(getWelcomeScreen());
}
private SSLContext buildSslContext() throws GeneralSecurityException {
SSLContext sslContext = SSLContext.getInstance("TLS");
TrustManager trustManager = new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
public void checkClientTrusted(
X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(
X509Certificate[] certs, String authType) {
}
};
sslContext.init(null, new TrustManager[]{trustManager},
new SecureRandom());
return sslContext;
}
@Test
public void shouldGetUserMenuScreenWhenLoginByCoord() throws Exception {
awaitKeyboardUnlock();
loginByCoord();
awaitKeyboardUnlock();
assertThat(client.getScreenText())
.isEqualTo(getFileContent("user-menu-screen.txt"));
}
@Test
public void shouldGetUserMenuScreenWhenLoginByLabel() throws Exception {
awaitKeyboardUnlock();
loginByLabel();
awaitKeyboardUnlock();
assertThat(client.getScreenText())
.isEqualTo(getFileContent("user-menu-screen.txt"));
}
private void loginByCoord() {
client.setFieldTextByCoord(6, 53, "TESTUSR");
client.setFieldTextByCoord(7, 53, "TESTPSW");
client.sendKeyEvent(KeyEvent.VK_ENTER, 0);
}
private void loginByLabel() {
client.setFieldTextByLabel("User", "TESTUSR");
client.setFieldTextByLabel("Password", "TESTPSW");
client.sendKeyEvent(KeyEvent.VK_ENTER, 0);
}
@Test
public void shouldGetNotSoundedAlarmWhenWhenConnect() throws Exception {
awaitKeyboardUnlock();
assertThat(client.resetAlarm()).isFalse();
}
@Test
public void shouldGetSoundedAlarmWhenWhenLoginByCoord() throws Exception {
awaitKeyboardUnlock();
loginByCoord();
awaitKeyboardUnlock();
assertThat(client.resetAlarm()).isTrue();
}
@Test
public void shouldGetSoundedAlarmWhenWhenLoginByLabel() throws Exception {
awaitKeyboardUnlock();
loginByLabel();
awaitKeyboardUnlock();
assertThat(client.resetAlarm()).isTrue();
}
@Test
public void shouldGetNotSoundedAlarmWhenWhenLoginAndResetAlarmByCoord() throws Exception {
awaitKeyboardUnlock();
loginByCoord();
awaitKeyboardUnlock();
client.resetAlarm();
assertThat(client.resetAlarm()).isFalse();
}
@Test
public void shouldGetNotSoundedAlarmWhenWhenLoginAndResetAlarmByLabel() throws Exception {
awaitKeyboardUnlock();
loginByLabel();
awaitKeyboardUnlock();
client.resetAlarm();
assertThat(client.resetAlarm()).isFalse();
}
@Test
public void shouldGetFieldPositionWhenGetCursorPositionAfterConnect() throws Exception {
Point fieldPosition = new Point(53, 6);
awaitKeyboardUnlock();
assertThat(client.getCursorPosition()).isEqualTo(Optional.of(fieldPosition));
}
@Test
public void shouldSendExceptionToExceptionHandlerWhenConnectWithInvalidPort() throws Exception {
client.connect(SERVICE_HOST, 1);
exceptionWaiter.awaitException();
}
@Test(expected = IllegalArgumentException.class)
public void shouldThrowIllegalArgumentExceptionWhenSendIncorrectFieldPosition()
throws Exception {
awaitKeyboardUnlock();
client.setFieldTextByCoord(0, 1, "test");
}
@Test(expected = IllegalArgumentException.class)
public void shouldThrowIllegalArgumentExceptionWhenSendIncorrectFieldLabel()
throws Exception {
awaitKeyboardUnlock();
client.setFieldTextByLabel("test", "test");
}
@Test
public void shouldSendCloseToExceptionHandlerWhenServerDown() throws Exception {
awaitKeyboardUnlock();
service.stop(TIMEOUT_MILLIS);
exceptionWaiter.awaitClose();
}
@Test
public void shouldGetConnectionClosedWhenServerDisconnects()
throws Exception {
awaitKeyboardUnlock();
service.stop(TIMEOUT_MILLIS);
exceptionWaiter.awaitClose();
}
@Test
public void shouldGetCorrectFieldsWhenGetFields() throws Exception {
awaitKeyboardUnlock();
List<TestField> actualFields = client.getFields().stream()
.map(TestField::new)
.collect(Collectors.toList());
assertEquals(buildExpectedFields(), actualFields);
}
private List<TestField> buildExpectedFields() {
List<TestField> testFieldList = new ArrayList<>();
int fieldsColumn = 52;
String fieldsText = "\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000";
testFieldList.add(new TestField.Builder(5, fieldsColumn, fieldsText).withMonocase().build());
testFieldList.add(
new TestField.Builder(6, fieldsColumn, fieldsText).withMonocase().withHighIntensity()
.withReverseImage()
.build());
testFieldList.add(new TestField.Builder(7, fieldsColumn, fieldsText).build());
testFieldList.add(new TestField.Builder(8, fieldsColumn, fieldsText).build());
testFieldList.add(new TestField.Builder(9, fieldsColumn, fieldsText).build());
return testFieldList;
}
@Test
public void shouldGetSuccessScreenWhenLoginByTabulator()
throws TimeoutException, InterruptedException, IOException {
awaitKeyboardUnlock();
client.setFieldTextByTabulator(5, "TESTUSR");
client.setFieldTextByTabulator(1, "TESTPSW");
client.sendKeyEvent(KeyEvent.VK_ENTER, 0);
awaitKeyboardUnlock();
assertThat(client.getScreenText())
.isEqualTo(getFileContent("user-menu-screen.txt"));
}
@Test(expected = NoSuchElementException.class)
public void shouldThrowExceptionWhenSetTabFieldAndCurrentPositionLacksField() throws Exception {
awaitKeyboardUnlock();
client.updateCursorPosition("", 1, 1);
client.setFieldTextByTabulator(0, "TESTUSR");
}
public static class TestField {
private static final byte FIELD_FORMAT_MASK = 0x40;
private static final byte MONOCASE_MASK = 0x20;
private static final byte SCREEN_ATTRIBUTE_MASK = 0x20;
private static final byte UNDERSCORE_MASK = 0x04;
private static final byte HIGH_INTENSITY_MASK = 0x02;
private static final byte REVERSE_IMAGE_MASK = 0x01;
private final int row;
private final int column;
private final String text;
private final byte[] ffw;
private final byte[] fcw;
private final int attr;
private TestField(Builder builder) {
row = builder.row;
column = builder.column;
attr = builder.attr;
text = builder.text;
ffw = builder.ffw;
fcw = builder.fcw;
}
private TestField(XI5250Field field) {
column = field.getCol();
row = field.getRow();
attr = field.getAttr();
text = field.getString();
ffw = field.getFFW();
fcw = field.getFCW();
}
@Override
public String toString() {
return "TestField{" +
"row=" + row +
", column=" + column +
", ffw=" + Arrays.toString(ffw) +
", fcw=" + Arrays.toString(fcw) +
", attr=" + attr +
", text='" + text + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
TestField testField = (TestField) o;
return row == testField.row &&
column == testField.column &&
attr == testField.attr &&
Arrays.equals(ffw, testField.ffw) &&
Arrays.equals(fcw, testField.fcw) &&
Objects.equals(text, testField.text);
}
@Override
public int hashCode() {
int result = Objects.hash(row, column, attr, text);
result = 31 * result + Arrays.hashCode(ffw);
result = 31 * result + Arrays.hashCode(fcw);
return result;
}
private static final class Builder {
private final int row;
private final int column;
private final String text;
private final byte[] fcw = {0, 0};
private byte[] ffw = {FIELD_FORMAT_MASK, 0};
private byte attr = SCREEN_ATTRIBUTE_MASK | UNDERSCORE_MASK;
private Builder(int row, int column, String text) {
this.row = row;
this.column = column;
this.text = text;
}
private Builder withMonocase() {
ffw[1] |= MONOCASE_MASK;
return this;
}
private Builder withHighIntensity() {
attr |= HIGH_INTENSITY_MASK;
return this;
}
private Builder withReverseImage() {
attr |= REVERSE_IMAGE_MASK;
return this;
}
private TestField build() {
return new TestField(this);
}
}
}
private static class ExceptionWaiter implements ExceptionHandler {
private CountDownLatch exceptionLatch = new CountDownLatch(1);
private CountDownLatch closeLatch = new CountDownLatch(1);
@Override
public void onException(Throwable ex) {
exceptionLatch.countDown();
}
@Override
public void onConnectionClosed() {
closeLatch.countDown();
}
private void awaitException() throws InterruptedException {
assertThat(exceptionLatch.await(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)).isTrue();
}
private void awaitClose() throws InterruptedException {
assertThat(closeLatch.await(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)).isTrue();
}
}
}