-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtroublemaker.java
executable file
·208 lines (176 loc) · 6.5 KB
/
troublemaker.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
///usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS info.picocli:picocli:4.7.1
//DEPS org.postgresql:postgresql:42.5.4
import picocli.CommandLine;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.stream.Stream;
@CommandLine.Command(
subcommands = {
troublemaker.Http.class,
troublemaker.HttpWithTls.class,
troublemaker.PickRandom.class,
troublemaker.Listen.class,
troublemaker.OpenFile.class,
troublemaker.DeadLock.class
},
mixinStandardHelpOptions = true)
public class troublemaker {
public static class ResponseBody {
public final List<String> body;
public ResponseBody(List<String> body) {
this.body = body;
}
}
public static void main(String... args) {
System.exit(new CommandLine(troublemaker.class).execute(args));
}
@CommandLine.Command(name = "http")
public static class Http implements Callable<Void> {
private static List<ResponseBody> responses = new ArrayList<>();
@CommandLine.Option(names = "--loop")
boolean loop = false;
@CommandLine.Option(names = "--leak")
boolean leak = false;
@CommandLine.Option(names = "--url")
String url = "http://httpbin.org/status/401";
@Override
public Void call() throws Exception {
do {
HttpResponse<Stream<String>> response =
HttpClient.newHttpClient().send(
HttpRequest.newBuilder()
.GET()
.uri(URI.create(url))
.build(),
HttpResponse.BodyHandlers.ofLines());
if (response.statusCode() != 200) {
throw new IOException("Bad status");
}
List<String> responseBody = response.body().toList();
responseBody.forEach(System.out::println);
if(leak) {
responses.add(new ResponseBody(responseBody));
}
} while (loop);
return null;
}
}
@CommandLine.Command(name = "https")
public static class HttpWithTls implements Callable<Void> {
@CommandLine.Option(names = "--version")
HttpClient.Version version = HttpClient.Version.HTTP_2;
@Override
public Void call() throws Exception {
HttpResponse<Stream<String>> response =
HttpClient.newBuilder()
.version(version)
.build()
.send(HttpRequest.newBuilder()
.GET()
.uri(URI.create("https://httpbin.org/status/401"))
.build(),
HttpResponse.BodyHandlers.ofLines());
if (response.statusCode() != 200) {
throw new IOException("Bad status");
}
response.body().forEach(System.out::println);
return null;
}
}
@CommandLine.Command(name = "pick-random")
public static class PickRandom implements Callable<Void> {
@Override
public Void call() throws Exception {
Random rand = new SecureRandom();
boolean cont = true;
while (cont) {
int i = rand.nextInt();
System.out.println(i);
Thread.sleep(1000);
}
return null;
}
}
@CommandLine.Command(name = "dead-lock")
public static class DeadLock implements Callable<Void> {
@Override
public Void call() throws Exception {
Object lockA = new Object();
Object lockB = new Object();
Thread threadA = new Thread(() -> {
while (true) {
synchronized (lockA) {
try {
Thread.sleep(1000);
synchronized (lockB) {
System.out.println("I win");
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
}, "Player 1");
System.out.println("Starting Player 1");
threadA.start();
Thread threadB = new Thread(() -> {
while (true) {
synchronized (lockB) {
try {
Thread.sleep(1000);
synchronized (lockA) {
System.out.println("No, I win");
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
}, "Player 2");
System.out.println("Starting Player 2");
threadB.start();
threadA.join();
threadB.join();
return null;
}
}
@CommandLine.Command(name = "listen")
public static class Listen implements Callable<Void> {
@CommandLine.Option(names = "--port")
int port = 8080;
@Override
public Void call() throws Exception {
try (ServerSocket socket = new ServerSocket(port)) {
System.out.println("Trying to listen on a port");
socket.accept();
}
return null;
}
}
@CommandLine.Command(name = "open")
public static class OpenFile implements Callable<Void> {
@CommandLine.Option(names = "--path")
String path = "/not/a/file";
@Override
public Void call() throws Exception {
try (InputStream fis = new FileInputStream(path)) {
System.out.println("Opened file " + path);
} catch (Exception e) {
System.out.println("File not found");
}
return null;
}
}
}