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
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ org-bouncycastle = "1.83"
org-conscrypt = "2.5.2"
org-junit-jupiter = "5.13.4"
playservices-safetynet = "18.1.0"
pkts-core = "3.0.3"
robolectric = "4.16.1"
robolectric-android = "16-robolectric-13921718"
serialization = "1.10.0"
Expand Down Expand Up @@ -141,6 +142,7 @@ square-okio-fakefilesystem = { module = "com.squareup.okio:okio-fakefilesystem",
testcontainers = { module = "org.testcontainers:testcontainers", version.ref = "testcontainers" }
testcontainers-junit5 = { module = "org.testcontainers:junit-jupiter", version.ref = "testcontainers" }
square-zstd-kmp-okio = { module = "com.squareup.zstd:zstd-kmp-okio", version.ref = "zstd-kmp-okio" }
pkts-core = { module = "io.pkts:pkts-core", version.ref = "pkts-core" }

# Build Logic Dependencies
gradlePlugin-android = { module = "com.android.tools.build:gradle", version.ref = "agp" }
Expand Down
429 changes: 429 additions & 0 deletions mocksocket/api/mocksocket.api

Large diffs are not rendered by default.

38 changes: 38 additions & 0 deletions mocksocket/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import okhttp3.buildsupport.testJavaVersion

plugins {
kotlin("jvm")
id("okhttp.publish-conventions")
id("okhttp.jvm-conventions")
id("okhttp.quality-conventions")
id("okhttp.testing-conventions")
}

project.applyJavaModules("mocksocket")

dependencies {
api(libs.square.okio)
api(libs.kotlinx.coroutines.core)
implementation(libs.pkts.core)

testImplementation(libs.assertk)
testImplementation(libs.junit.jupiter.api)
testImplementation(projects.okhttp)
testRuntimeOnly(libs.junit.jupiter.engine)
}

val testJavaVersion = project.testJavaVersion

if (testJavaVersion >= 11) {
tasks.withType<Test> {
jvmArgs(
"--add-opens=java.base/sun.security.ssl=ALL-UNNAMED",
"--add-opens=java.base/sun.security.util=ALL-UNNAMED",
"--add-opens=java.base/sun.security.provider=ALL-UNNAMED",
)
}
}

kotlin {
explicitApi()
}
128 changes: 128 additions & 0 deletions mocksocket/src/main/kotlin/mockwebserver/socket/NetLogRecorder.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* Copyright (c) 2026 Block, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@file:OptIn(ExperimentalTime::class)

package mockwebserver.socket

import java.io.Closeable
import kotlin.time.ExperimentalTime
import okio.BufferedSink
import okio.FileSystem
import okio.Path
import okio.buffer

public class NetLogRecorder(
file: Path,
fileSystem: FileSystem = FileSystem.SYSTEM,
) : SocketEventListener,
Closeable {
private val writer = fileSystem.sink(file).buffer()
private var isFirstEvent = true
private var closed = false

init {
writer.println("{")
writer.println(" \"constants\": {},")
writer.println(" \"events\": [")
writer.flush()
}

override fun onEvent(event: SocketEvent) {
val time = event.timestamp.toEpochMilliseconds()

val jsonEvent =
when (event) {
is SocketEvent.Connect -> {
"""
{
"phase": 1,
"source": { "id": ${event.socketName.hashCode()}, "type": 10 },
"time": "$time",
"type": 67,
"params": {
"address": "${event.host}:${event.port}"
}
}
""".trimIndent()
}

is SocketEvent.ReadSuccess -> {
// Not recording actual base64 payload to save memory, just counts
"""
{
"phase": 0,
"source": { "id": ${event.socketName.hashCode()}, "type": 10 },
"time": "$time",
"type": 113,
"params": { "byte_count": ${event.byteCount} }
}
""".trimIndent()
}

is SocketEvent.WriteSuccess -> {
"""
{
"phase": 0,
"source": { "id": ${event.socketName.hashCode()}, "type": 10 },
"time": "$time",
"type": 114,
"params": { "byte_count": ${event.byteCount} }
}
""".trimIndent()
}

is SocketEvent.Close -> {
"""
{
"phase": 2,
"source": { "id": ${event.socketName.hashCode()}, "type": 10 },
"time": "$time",
"type": 67
}
""".trimIndent()
}

else -> {
null
}
}

if (jsonEvent != null) {
synchronized(this) {
if (!isFirstEvent) {
writer.println(",")
}
isFirstEvent = false
writer.writeUtf8(jsonEvent.replace("\n", "\n "))
writer.flush()
}
}
}

override fun close() {
if (closed) return
closed = true
if (!isFirstEvent) writer.writeUtf8("\n")
writer.println(" ]")
writer.println("}")
writer.close()
}
}

private fun BufferedSink.println(string: String) {
writeUtf8(string)
writeUtf8("\n")
}
Loading
Loading