Repository navigation
Expand file tree
/
Copy pathCreateSessionReKeyEntryTest.java
More file actions
265 lines (224 loc) · 12.1 KB
/
Copy pathCreateSessionReKeyEntryTest.java
File metadata and controls
265 lines (224 loc) · 12.1 KB
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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
package com.github.copilot;
import static org.junit.jupiter.api.Assertions.*;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import org.junit.jupiter.api.Test;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.github.copilot.rpc.CopilotClientOptions;
import com.github.copilot.rpc.PermissionHandler;
import com.github.copilot.rpc.SessionConfig;
/**
* Tests that CopilotClient rejects session.create responses whose sessionId
* differs from the client-supplied one. Re-keying the sessions map at runtime
* is intentionally not supported — the server must honor the client-supplied
* sessionId (or generate one when none is supplied).
*/
class CreateSessionReKeyEntryTest {
private static final ObjectMapper MAPPER = JsonRpcClient.getObjectMapper();
/**
* A connected socket pair where the server replies to "session.create" with a
* configurable sessionId and then replies to "session.options.update" with
* success or failure.
*/
private static final class ReKeyServer implements AutoCloseable {
final Socket clientSocket;
final Socket serverSocket;
final JsonRpcClient rpcClient;
private volatile boolean running = true;
private final Thread replyThread;
/** The sessionId to return in the session.create response. */
private final String returnedSessionId;
/** If true, the session.options.update call will fail. */
private final boolean failOptionsUpdate;
ReKeyServer(String returnedSessionId, boolean failOptionsUpdate) throws Exception {
this.returnedSessionId = returnedSessionId;
this.failOptionsUpdate = failOptionsUpdate;
var loopback = InetAddress.getLoopbackAddress();
try (var ss = new ServerSocket(0, 1, loopback)) {
clientSocket = new Socket(loopback, ss.getLocalPort());
serverSocket = ss.accept();
}
serverSocket.setSoTimeout(5000);
rpcClient = JsonRpcClient.fromSocket(clientSocket);
replyThread = new Thread(() -> {
try {
var in = serverSocket.getInputStream();
var out = serverSocket.getOutputStream();
while (running) {
// Read Content-Length header
var header = new StringBuilder();
int b;
while ((b = in.read()) != -1) {
if (b == '\n' && header.toString().endsWith("\r")) {
break;
}
header.append((char) b);
}
if (b == -1)
break;
// Skip blank line
in.read(); // '\r'
in.read(); // '\n'
String hdr = header.toString().trim();
int colon = hdr.indexOf(':');
int len = Integer.parseInt(hdr.substring(colon + 1).trim());
byte[] body = in.readNBytes(len);
JsonNode msg = MAPPER.readTree(body);
String method = msg.get("method").asText();
long id = msg.get("id").asLong();
if ("session.create".equals(method)) {
// Return a response with the (possibly different) session ID
ObjectNode result = MAPPER.createObjectNode();
result.put("sessionId", returnedSessionId);
String response = MAPPER.writeValueAsString(MAPPER.createObjectNode().put("jsonrpc", "2.0")
.put("id", id).set("result", result));
sendRpcMessage(out, response);
} else if ("session.options.update".equals(method)) {
if (failOptionsUpdate) {
// Send an error response
ObjectNode error = MAPPER.createObjectNode();
error.put("code", -32000);
error.put("message", "simulated options update failure");
String response = MAPPER.writeValueAsString(MAPPER.createObjectNode()
.put("jsonrpc", "2.0").put("id", id).set("error", error));
sendRpcMessage(out, response);
} else {
// Send a success response
String response = MAPPER.writeValueAsString(
MAPPER.createObjectNode().put("jsonrpc", "2.0").put("id", id).set("result",
MAPPER.createObjectNode().put("success", true)));
sendRpcMessage(out, response);
}
} else {
// Generic success for anything else
String response = MAPPER.writeValueAsString(MAPPER.createObjectNode().put("jsonrpc", "2.0")
.put("id", id).set("result", MAPPER.createObjectNode().put("success", true)));
sendRpcMessage(out, response);
}
}
} catch (Exception e) {
if (running) {
// Ignore expected exceptions on shutdown
}
}
});
replyThread.setDaemon(true);
replyThread.start();
}
private static void sendRpcMessage(OutputStream out, String json) throws Exception {
byte[] bytes = json.getBytes(StandardCharsets.UTF_8);
String header = "Content-Length: " + bytes.length + "\r\n\r\n";
out.write(header.getBytes(StandardCharsets.UTF_8));
out.write(bytes);
out.flush();
}
@Override
public void close() throws Exception {
running = false;
rpcClient.close();
clientSocket.close();
serverSocket.close();
replyThread.join(3000);
}
}
@SuppressWarnings("unchecked")
private static Map<String, CopilotSession> getSessionsMap(CopilotClient client) throws Exception {
Field f = CopilotClient.class.getDeclaredField("sessions");
f.setAccessible(true);
return (Map<String, CopilotSession>) f.get(client);
}
private static void injectConnection(CopilotClient client, JsonRpcClient rpc) throws Exception {
// Build a Connection record via the private record constructor
Class<?> connClass = null;
for (Class<?> c : CopilotClient.class.getDeclaredClasses()) {
if (c.getSimpleName().equals("Connection")) {
connClass = c;
break;
}
}
assertNotNull(connClass, "Could not find Connection inner class");
var ctor = connClass.getDeclaredConstructors()[0];
ctor.setAccessible(true);
// Connection(JsonRpcClient rpc, Process process, ServerRpc serverRpc,
// AutoCloseable runtimeHost)
Object connection = ctor.newInstance(rpc, null, null, null);
Field f = CopilotClient.class.getDeclaredField("connectionFuture");
f.setAccessible(true);
f.set(client, CompletableFuture.completedFuture(connection));
}
@Test
void createSession_serverReturnsDifferentSessionId_throwsAndRemovesPreRegisteredEntry() throws Exception {
String clientSessionId = "client-supplied-id";
String serverSessionId = "server-returned-id";
try (var server = new ReKeyServer(serverSessionId, false)) {
var client = new CopilotClient(new CopilotClientOptions().setAutoStart(false));
injectConnection(client, server.rpcClient);
var config = new SessionConfig().setSessionId(clientSessionId)
.setOnPermissionRequest(PermissionHandler.APPROVE_ALL);
ExecutionException ex = assertThrows(ExecutionException.class, () -> client.createSession(config).get());
assertNotNull(ex.getCause());
assertTrue(ex.getCause().getMessage().contains(serverSessionId),
"Error message should mention the server-returned sessionId");
assertTrue(ex.getCause().getMessage().contains(clientSessionId),
"Error message should mention the client-requested sessionId");
Map<String, CopilotSession> sessions = getSessionsMap(client);
assertNull(sessions.get(clientSessionId),
"Pre-registered client-supplied sessionId should be removed after rejection");
assertNull(sessions.get(serverSessionId),
"Server-returned sessionId should never be registered after rejection");
assertTrue(sessions.isEmpty(), "Sessions map should be empty after rejected create");
client.close();
}
}
@Test
void createSession_serverReturnsDifferentSessionIdWithSkipCustomInstructions_throwsAndCleansUp() throws Exception {
String clientSessionId = "client-supplied-id";
String serverSessionId = "server-returned-id";
try (var server = new ReKeyServer(serverSessionId, true)) {
var client = new CopilotClient(new CopilotClientOptions().setAutoStart(false));
injectConnection(client, server.rpcClient);
// Even when skipCustomInstructions would trigger session.options.update,
// the sessionId-mismatch error fires first and short-circuits the flow.
var config = new SessionConfig().setSessionId(clientSessionId)
.setOnPermissionRequest(PermissionHandler.APPROVE_ALL).setSkipCustomInstructions(true);
ExecutionException ex = assertThrows(ExecutionException.class, () -> client.createSession(config).get());
assertNotNull(ex.getCause());
Map<String, CopilotSession> sessions = getSessionsMap(client);
assertNull(sessions.get(clientSessionId),
"Pre-registered client-supplied sessionId should be removed on failure");
assertNull(sessions.get(serverSessionId),
"Server-returned sessionId should never be registered on failure");
assertTrue(sessions.isEmpty(), "Sessions map should be empty after failed create");
client.close();
}
}
@Test
void createSession_serverReturnsSameSessionId_sessionKeptUnderClientId() throws Exception {
String sessionId = "same-id-for-both";
try (var server = new ReKeyServer(sessionId, false)) {
var client = new CopilotClient(new CopilotClientOptions().setAutoStart(false));
injectConnection(client, server.rpcClient);
var config = new SessionConfig().setSessionId(sessionId)
.setOnPermissionRequest(PermissionHandler.APPROVE_ALL);
CopilotSession session = client.createSession(config).get();
Map<String, CopilotSession> sessions = getSessionsMap(client);
// When IDs match, the session stays under the original key
assertSame(session, sessions.get(sessionId),
"Session should remain under original key when server returns same ID");
assertEquals(1, sessions.size(), "Should have exactly one entry in sessions map");
client.close();
}
}
}