Skip to content

Commit

Permalink
[DirectSockets] Add self-contained WPTs for the Direct Sockets API
Browse files Browse the repository at this point in the history
Bug: 338475101
Change-Id: I4441e18da6590937dad8edd4933d7ba46e08c10c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5513762
Commit-Queue: Andrew Rayskiy <[email protected]>
Reviewed-by: Weizhong Xia <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1296893}
  • Loading branch information
GrapeGreen authored and chromium-wpt-export-bot committed May 6, 2024
1 parent fb37d2f commit 34dfef8
Show file tree
Hide file tree
Showing 11 changed files with 196 additions and 141 deletions.
4 changes: 1 addition & 3 deletions direct-sockets/META.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
spec: https://github.com/WICG/direct-sockets/blob/main/docs/explainer.md
suggested_reviewers:
- ewilligers
- mgiuca
- phoglenix
- greengrape
38 changes: 25 additions & 13 deletions direct-sockets/disabled-by-permissions-policy.https.sub.html
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Sockets test: Can be disabled by permissions policy</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
</head>
<body>
<script src="disabled-by-permissions-policy.js"></script>
</body>
</html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>

'use strict';

test(() => {
assert_throws_dom("NotAllowedError", () => {
new TCPSocket("127.0.0.1", 53);
}, "constructor should throw");
}, "TCPSocket disabled by permissions-policy");

test(() => {
assert_throws_dom("NotAllowedError", () => {
new UDPSocket({ remoteAddress: "127.0.0.1", remotePort: 53 });
}, "constructor should throw");
}, "UDPSocket disabled by permissions-policy");

test(() => {
assert_throws_dom("NotAllowedError", () => {
new TCPServerSocket("127.0.0.1");
}, "constructor should throw");
}, "TCPServerSocket disabled by permissions-policy");

</script>
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Cross-Origin-Opener-Policy: same-origin
Cross-Origin-Embedder-Policy: require-corp
Permissions-Policy: direct-sockets=()
Permissions-Policy: direct-sockets=()
8 changes: 0 additions & 8 deletions direct-sockets/disabled-by-permissions-policy.js

This file was deleted.

21 changes: 0 additions & 21 deletions direct-sockets/open-securecontext.http.html

This file was deleted.

68 changes: 68 additions & 0 deletions direct-sockets/tcp_socket.https.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/test-only-api.js"></script>
<script>
'use strict';

promise_test(async () => {
const kPacket = "I'm a netcat. Meow-meow!"

const serverSocket = new TCPServerSocket("127.0.0.1");
const { localPort } = await serverSocket.opened;

const clientSocket = new TCPSocket("127.0.0.1", localPort);
const acceptedSocket = await (async () => {
const { readable } = await serverSocket.opened;
const reader = readable.getReader();
const { value: acceptedSocket, done } = await reader.read();
assert_false(done);
reader.releaseLock();
return acceptedSocket;
})();

const encoder = new TextEncoder();
const decoder = new TextDecoder();

const readPacket = async socket => {
const { readable } = await socket.opened;
const reader = readable.getReader();
let result = "";
while (result.length < kPacket.length) {
const { value, done } = await reader.read();
assert_false(done);
result += decoder.decode(value);
}
assert_equals(result, kPacket);
reader.releaseLock();
};

const sendPacket = async socket => {
const { writable } = await socket.opened;
const writer = writable.getWriter();
await writer.ready;
await writer.write(encoder.encode(kPacket));
writer.releaseLock();
};

const acceptedSocketEcho = (async () => {
await readPacket(acceptedSocket);
await sendPacket(acceptedSocket);
})();

const clientSocketSend = (async () => {
await sendPacket(clientSocket);
})();

const clientSocketReceive = (async () => {
await readPacket(clientSocket);
})();

await clientSocketReceive;

await clientSocket.close();
await acceptedSocket.close();
await serverSocket.close();

}, 'TCPSocket exchanges packets with TCPServerSocket');
</script>
3 changes: 3 additions & 0 deletions direct-sockets/tcp_socket.https.html.headers
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Cross-Origin-Opener-Policy: same-origin
Cross-Origin-Embedder-Policy: require-corp
Permissions-Policy: direct-sockets=(self)
95 changes: 95 additions & 0 deletions direct-sockets/udp_socket.https.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/test-only-api.js"></script>
<script>
'use strict';

promise_test(async () => {
const kRequiredDatagrams = 150;
const kRequiredBytes =
kRequiredDatagrams * (kRequiredDatagrams + 1) / 2;

const boundSocket = new UDPSocket({ localAddress: "127.0.0.1" });
const { localPort: boundLocalPort } = await boundSocket.opened;

const connectedSocket = new UDPSocket({
remoteAddress: "127.0.0.1",
remotePort: boundLocalPort,
});

const {
localAddress: clientAddress,
localPort: clientPort
} = await connectedSocket.opened;

const boundEchoLoop = (async() => {
let bytesEchoed = 0;

const { readable, writable } = await boundSocket.opened;
const reader = readable.getReader();
const writer = writable.getWriter();

while (bytesEchoed < kRequiredBytes) {
const { value: { data, remoteAddress, remotePort }, done } = await reader.read();
assert_false(done);
assert_equals(remoteAddress, clientAddress);
assert_equals(remotePort, clientPort);
for (let index = 0; index < data.length; index++) {
assert_equals(data[index], bytesEchoed % 256);
bytesEchoed++;
}
await writer.write({ data, remoteAddress, remotePort });
}

assert_equals(bytesEchoed, kRequiredBytes);
reader.releaseLock();
writer.releaseLock();
})();

const connectedSendLoop = (async () => {
let bytesWritten = 0;
let chunkLength = 0;

const { writable } = await connectedSocket.opened;
const writer = writable.getWriter();

while (bytesWritten < kRequiredBytes) {
chunkLength = Math.min(chunkLength + 1, kRequiredBytes - bytesWritten);
let chunk = new Uint8Array(chunkLength);
for (let index = 0; index < chunkLength; index++) {
chunk[index] = bytesWritten % 256;
bytesWritten++;
}
await writer.ready;
await writer.write({ data: chunk });
}
assert_equals(bytesWritten, kRequiredBytes);
writer.releaseLock();
})();

const connectedReadLoop = (async () => {
let bytesRead = 0;

const { readable } = await connectedSocket.opened;
const reader = readable.getReader();

while (bytesRead < kRequiredBytes) {
const { value: { data }, done } = await reader.read();
assert_false(done);
for (let index = 0; index < data.length; index++) {
assert_equals(data[index], bytesRead % 256);
bytesRead++;
}
}
assert_equals(bytesRead, kRequiredBytes);

reader.releaseLock();
})();

await connectedReadLoop;

await boundSocket.close();
await connectedSocket.close();
}, "UDPSocket (connected) exchanges datagrams with UDPSocket (bound)");
</script>
3 changes: 3 additions & 0 deletions direct-sockets/udp_socket.https.html.headers
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Cross-Origin-Opener-Policy: same-origin
Cross-Origin-Embedder-Policy: require-corp
Permissions-Policy: direct-sockets=(self)
1 change: 0 additions & 1 deletion lint.ignore
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,6 @@ MISSING DEPENDENCY: resources/chromium/fake-serial.js
MISSING DEPENDENCY: resources/chromium/fake-hid.js
MISSING DEPENDENCY: resources/chromium/mock-battery-monitor.js
MISSING DEPENDENCY: resources/chromium/mock-barcodedetection.js
MISSING DEPENDENCY: resources/chromium/mock-direct-sockets.js
MISSING DEPENDENCY: resources/chromium/mock-facedetection.js
MISSING DEPENDENCY: resources/chromium/mock-idle-detection.js
MISSING DEPENDENCY: resources/chromium/mock-imagecapture.js
Expand Down
94 changes: 0 additions & 94 deletions resources/chromium/mock-direct-sockets.js

This file was deleted.

0 comments on commit 34dfef8

Please sign in to comment.