Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,24 @@ jobs:
timeout-minutes: 5
run: go test ./...

# The core needs no Apple SDK, and swift-nio-http3 needs Swift 6.4 and, on Apple platforms, macOS 27, so it is tested
# on Linux.
swift-core:
name: Swift core tests (macOS)
runs-on: macos-latest
timeout-minutes: 10
name: Swift core tests (Linux)
runs-on: ubuntu-latest
container: swift:6.4.2-noble
timeout-minutes: 20
steps:
- uses: actions/checkout@v7
timeout-minutes: 5
# The SwiftPM build of swift-nio-http3 and its dependencies, which takes minutes from cold.
- name: Cache SwiftPM build
uses: actions/cache@v6
timeout-minutes: 5
with:
path: ios/connect_native_http/.build
key: swift-core-${{ hashFiles('ios/connect_native_http/Package.swift') }}
restore-keys: swift-core-
- uses: actions/setup-go@v7
timeout-minutes: 5
with:
Expand All @@ -96,7 +107,7 @@ jobs:
cat server.log >&2
exit 1
- name: Swift tests
timeout-minutes: 5
timeout-minutes: 15
working-directory: ios/connect_native_http
env:
CONNECT_NATIVE_HTTP_CORE_ONLY: "1"
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,6 @@ ios/connect_native_http/.swiftpm/
# Test server build output and the certificates it writes
test_server/connect-native-http-test-server
test_server/certs/

# SwiftPM: the core package resolves its own dependencies; Flutter resolves the app's.
ios/connect_native_http/Package.resolved
241 changes: 161 additions & 80 deletions README.md

Large diffs are not rendered by default.

63 changes: 51 additions & 12 deletions example/integration_test/eliza_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ final class Harness {
String baseUrl, {
Set<Uri> http3Origins = const {},
Duration? readIdleTimeout,
Duration? connectTimeout,
Duration? deadlineStep,
bool evictOnDeadline = false,
bool useHttpGet = false,
Expand All @@ -51,6 +52,7 @@ final class Harness {
NativeHttpConfig(
http3Origins: http3Origins,
readIdleTimeout: readIdleTimeout,
connectTimeout: connectTimeout,
deadlineStep: deadlineStep,
evictOnDeadline: evictOnDeadline,
onResponse: (info) => harness.platformProtocols.add(info.protocol),
Expand Down Expand Up @@ -112,10 +114,10 @@ Future<(StreamController<ConverseRequest>, StreamIterator<ConverseResponse>)> op

/// A client whose pooled connection to the server negotiated [protocol], such as `HTTP/3.0`.
///
/// URLSession races QUIC against TCP for each new connection and keeps whichever connects first, so on a loaded
/// device a connection sometimes lands on HTTP/2 even for an HTTP/3 origin. A client keeps reusing the connection it
/// has, so each attempt uses a fresh client, closing the ones that got another protocol. Each attempt is that
/// client's first call, before any Alt-Svc: reaching HTTP/3 shows the client tried QUIC because of http3Origins.
/// iOS only speaks HTTP/3, so its first attempt gets it. Cronet may race QUIC against TCP for a new connection and
/// keep whichever connects first, and a client keeps reusing the connection it has, so each attempt uses a fresh
/// client, closing the ones that got another protocol. Each attempt is that client's first call, before any Alt-Svc:
/// reaching HTTP/3 shows the client tried QUIC because of http3Origins.
Future<Harness> harnessNegotiating(String protocol) async {
final deadline = DateTime.now().add(const Duration(seconds: 5));
final protocols = <String>[];
Expand All @@ -139,8 +141,8 @@ Future<Harness> harnessNegotiating(String protocol) async {
}
}

/// A client on HTTP/2, with the server's UDP frozen for the rest of the case so that QUIC can't connect: URLSession
/// then falls back to TCP, as on a network that blocks UDP. Android is on HTTP/2 with this server anyway.
/// A client on HTTP/2, with the server's UDP frozen for the rest of the case so that QUIC can't connect, as on a
/// network that blocks UDP. Android is on HTTP/2 with this server anyway. Not on iOS, which has no HTTP/2.
Future<Harness> harnessOverHttp2({
Duration? readIdleTimeout,
Duration? deadlineStep,
Expand Down Expand Up @@ -187,8 +189,8 @@ void main() {

// One call on a throwaway client before any case. Right after the app launches, the simulator and the test server
// on the same CI runner are both slow (a QUIC connection took 66 ms to set up, and the server answered its first
// packet 130 ms after it was sent), so the first call's QUIC attempt loses URLSession's race to TCP. The cases then
// start on a warm app and server. The protocol it got is logged, not checked.
// packet 130 ms after it was sent). The cases then start on a warm app and server. The protocol it got is logged,
// not checked.
//
// A few attempts, each on a fresh client whose call a deadline cancels: on a freshly booted simulator, trustd can
// stall every certificate check for a while (once while it updated its pinning database from a trust data update).
Expand Down Expand Up @@ -311,19 +313,26 @@ void main() {

testCase('a refused connection is reported as never sent', (_) async {
expect(unreachableUrl, isNotEmpty, reason: 'run with --dart-define=UNREACHABLE_URL=...');
final unreachable = await Harness.create(unreachableUrl);
// QUIC ignores the ICMP error a closed UDP port answers with, so on iOS only the connect timeout notices.
final unreachable = await Harness.create(unreachableUrl, connectTimeout: const Duration(seconds: 3));
addTearDown(unreachable.native.close);
final cause = await nativeCause(unreachable.eliza.say(SayRequest(sentence: 'hi')));
expect(cause.failure, NativeHttpFailure.notSent, reason: '$cause');
expect(cause.requestNeverSent, isTrue);
});

testCase('a certificate for another name is rejected as untrusted', (_) async {
testCase('a certificate for another name is rejected, and the request never sent', (_) async {
expect(untrustedUrl, isNotEmpty, reason: 'run with --dart-define=UNTRUSTED_URL=...');
final untrusted = await Harness.create(untrustedUrl);
addTearDown(untrusted.native.close);
final cause = await nativeCause(untrusted.eliza.say(SayRequest(sentence: 'hi')));
expect(cause.failure, NativeHttpFailure.untrusted, reason: '$cause');
// The QUIC stack on iOS reports every failed TLS handshake alike: NSURLErrorSecureConnectionFailed (-1200).
expect(
cause.failure,
io.Platform.isIOS ? NativeHttpFailure.notSent : NativeHttpFailure.untrusted,
reason: '$cause',
);
expect(cause.requestNeverSent, isTrue);
});

testCase('negotiates the expected protocol', (_) async {
Expand Down Expand Up @@ -380,8 +389,13 @@ void main() {
expect(h3.serverProtocols.last, 'HTTP/3.0');
}, timeout: http3CaseTimeout);

// Over HTTP/2 neither platform notices a dead connection (no pings), so a read-idle timeout is what does.
// Over HTTP/2 Cronet doesn't notice a dead connection (no pings), so a read-idle timeout is what does. iOS has no
// HTTP/2.
bool skipUnlessAdmin() {
if (io.Platform.isIOS) {
markTestSkipped('iOS speaks HTTP/3 only');
return true;
}
if (adminUrl.isEmpty) {
markTestSkipped('needs ADMIN_URL');
return true;
Expand Down Expand Up @@ -557,4 +571,29 @@ void main() {
expect(stopwatch.elapsed, lessThan(serverMaxIdle + const Duration(seconds: 2)));
await requests.close();
}, timeout: http3CaseTimeout);

testCase('a datagram session echoes datagrams', (_) async {
final url = Uri.parse('$serverUrl/datagrams/echo');
if (!io.Platform.isIOS) {
expect(() => harness.native.openDatagramSession(url, protocol: 'datagram-echo'), throwsUnsupportedError);
return;
}
final session = await harness.native.openDatagramSession(url, protocol: 'datagram-echo');
expect(session.status, 200);
final echoes = StreamIterator(session.datagrams);
// Datagrams may be lost, even locally; resend until each comes back.
for (final text in ['one', 'two']) {
final payload = Uint8List.fromList(text.codeUnits);
var echoed = false;
for (var attempt = 0; attempt < 5 && !echoed; attempt++) {
await session.send(payload);
echoed = await echoes.moveNext().timeout(const Duration(seconds: 1), onTimeout: () => false);
}
expect(echoed, isTrue, reason: 'no echo of $text');
expect(String.fromCharCodes(echoes.current), text);
}
await session.close();
await session.done;
expect(await echoes.moveNext(), isFalse);
});
}
18 changes: 8 additions & 10 deletions example/integration_test/public_server_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import 'package:integration_test/integration_test.dart';
/// A real ELIZA, such as Connect's demo server.
const publicElizaUrl = String.fromEnvironment('PUBLIC_ELIZA_URL');

/// What Cronet reports for HTTP/3. URLSession reports nothing, so the check only applies on Android.
/// What Cronet reports for HTTP/3. iOS reports `h3`, and speaks nothing else.
const cronetHttp3 = 'quic/1+spdy/3';

void main() {
Expand All @@ -40,10 +40,8 @@ void main() {
});

void expectHttp3() {
if (Platform.isAndroid) {
expect(protocols, isNotEmpty);
expect(protocols, everyElement(cronetHttp3));
}
expect(protocols, isNotEmpty);
expect(protocols, everyElement(Platform.isAndroid ? cronetHttp3 : 'h3'));
}

testWidgets('unary over HTTP/3', (_) async {
Expand Down Expand Up @@ -101,16 +99,16 @@ void main() {
return (client, requests, responses);
}

bool skipUnlessAndroid() {
if (publicElizaUrl.isEmpty || !Platform.isAndroid) {
markTestSkipped('PUBLIC_ELIZA_URL not set, or not Android: URLSession has no idle timeout setting');
bool skipUnlessPublicServer() {
if (publicElizaUrl.isEmpty) {
markTestSkipped('PUBLIC_ELIZA_URL not set');
return true;
}
return false;
}

testWidgets('the client QUIC idle timeout ends a quiet connection', (_) async {
if (skipUnlessAndroid()) {
if (skipUnlessPublicServer()) {
return;
}
final (client, requests, responses) = await quietConverse(quicIdle);
Expand All @@ -123,7 +121,7 @@ void main() {

testWidgets('without the setting the same quiet connection survives', (_) async {
// The control for the previous case: the server keeps a quiet stream open, so the setting is what closed it.
if (skipUnlessAndroid()) {
if (skipUnlessPublicServer()) {
return;
}
final (client, requests, responses) = await quietConverse(null);
Expand Down
6 changes: 3 additions & 3 deletions example/ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
IPHONEOS_DEPLOYMENT_TARGET = 27.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
STRING_CATALOG_GENERATE_SYMBOLS = YES;
Expand Down Expand Up @@ -491,7 +491,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
IPHONEOS_DEPLOYMENT_TARGET = 27.0;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
Expand Down Expand Up @@ -543,7 +543,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
IPHONEOS_DEPLOYMENT_TARGET = 27.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
STRING_CATALOG_GENERATE_SYMBOLS = YES;
Expand Down
52 changes: 42 additions & 10 deletions ios/connect_native_http/Package.swift
Original file line number Diff line number Diff line change
@@ -1,14 +1,37 @@
// swift-tools-version: 5.9
// swift-tools-version: 6.0

import PackageDescription

// Flutter builds this package next to the FlutterFramework package it generates for the app. The Flutter-free core
// and its tests also build on their own, without it:
// and its tests also build on their own, without it, on macOS or Linux:
// CONNECT_NATIVE_HTTP_CORE_ONLY=1 swift test
//
// swift-nio-http3 and swift-nio-quic need Swift 6.4 and, on Apple platforms, iOS or macOS 27.
let coreOnly = Context.environment["CONNECT_NATIVE_HTTP_CORE_ONLY"] != nil

let core = Target.target(name: "ConnectNativeHttpCore")
let coreTests = Target.testTarget(name: "ConnectNativeHttpCoreTests", dependencies: ["ConnectNativeHttpCore"])
let core = Target.target(
name: "ConnectNativeHttpCore",
dependencies: [
.product(name: "NIOCore", package: "swift-nio"),
.product(name: "NIOPosix", package: "swift-nio"),
.product(name: "HTTP3", package: "swift-nio-http3"),
.product(name: "NIOHTTP3", package: "swift-nio-http3"),
.product(name: "NIOQUIC", package: "swift-nio-quic"),
.product(name: "NIOQUICHelpers", package: "swift-nio-quic-helpers"),
.product(name: "HTTPTypes", package: "swift-http-types"),
.product(name: "NIOHTTPTypes", package: "swift-nio-extras"),
.product(name: "Logging", package: "swift-log"),
.product(name: "X509", package: "swift-certificates"),
],
swiftSettings: [.enableExperimentalFeature("AnyAppleOSAvailability")]
)
let coreTests = Target.testTarget(
name: "ConnectNativeHttpCoreTests",
dependencies: [
"ConnectNativeHttpCore",
.product(name: "X509", package: "swift-certificates"),
]
)
let plugin = Target.target(
name: "connect_native_http",
dependencies: [
Expand All @@ -20,14 +43,23 @@ let plugin = Target.target(
let package = Package(
name: "connect_native_http",
platforms: [
.iOS("15.0"),
.macOS("12.0"),
.iOS("27.0"),
.macOS("27.0"),
],
products: coreOnly ? [] : [
.library(name: "connect-native-http", targets: ["connect_native_http"])
],
dependencies: coreOnly ? [] : [
.package(name: "FlutterFramework", path: "../FlutterFramework")
],
targets: coreOnly ? [core, coreTests] : [core, plugin, coreTests]
dependencies: [
.package(url: "https://github.com/apple/swift-nio.git", from: "2.104.0"),
// swift-nio-http3 has no release yet, so it is pinned to a commit.
.package(url: "https://github.com/apple/swift-nio-http3.git", revision: "58ba11baee89b43ca1adeafe6bb976f17c9a86f3"),
.package(url: "https://github.com/apple/swift-nio-quic.git", .upToNextMinor(from: "0.6.0")),
.package(url: "https://github.com/apple/swift-nio-quic-helpers.git", .upToNextMinor(from: "0.1.2")),
.package(url: "https://github.com/apple/swift-http-types.git", from: "1.8.0"),
.package(url: "https://github.com/apple/swift-nio-extras.git", from: "1.22.0"),
.package(url: "https://github.com/apple/swift-log.git", from: "1.12.1"),
.package(url: "https://github.com/apple/swift-certificates.git", from: "1.21.0"),
] + (coreOnly ? [] : [.package(name: "FlutterFramework", path: "../FlutterFramework")]),
targets: coreOnly ? [core, coreTests] : [core, plugin, coreTests],
swiftLanguageModes: [.v5]
)
Loading
Loading