From 8f35fb6d8d805a5e28eedc4a37895ea6ee0fcc7f Mon Sep 17 00:00:00 2001
From: cyBerta <cyberta@riseup.net>
Date: Thu, 13 Feb 2025 20:54:31 +0100
Subject: [PATCH] ensure kcp configs are serialized and deserialized correctly.
 Adds an test with test json data derived from a java model class, in order to
 ensure the interoperability

---
 client/client_test.go | 25 +++++++++++++++++++++++++
 obfsvpn/kcp.go        | 20 ++++++++++----------
 2 files changed, 35 insertions(+), 10 deletions(-)
 create mode 100644 client/client_test.go

diff --git a/client/client_test.go b/client/client_test.go
new file mode 100644
index 0000000..6f071a6
--- /dev/null
+++ b/client/client_test.go
@@ -0,0 +1,25 @@
+package client
+
+import (
+	"encoding/json"
+	"testing"
+)
+
+// This json was generated from the java model classes
+var expected = `{"hopping_config":{"enabled":false,"hop_jitter":10,"min_hop_seconds":10,"obfs4_certs":["XXXXYYYYZZZZ"],"port_count":0,"port_seed":0,"proxy_addr":"127.0.0.1:8080","remotes":["37.218.4.12"]},"kcp_config":{"mtu":1400,"disable_flow_control":true,"enabled":true,"interval":10,"no_delay":true,"read_buffer":16777216,"receive_window_size":65535,"resend":2,"send_window_size":65535,"write_buffer":16777216},"obfs4_cert":"XXXYYYYZZZZZ","proxy_addr":"127.0.0.1:8080","remote_ip":"37.218.4.12","remote_port":"4431"}`
+
+func TestJsonFormattingEnsureFFIInterop(t *testing.T) {
+	config := Config{}
+	err := json.Unmarshal([]byte(expected), &config)
+	if err != nil {
+		t.Error("unexpected marshalling error: ", err)
+	}
+
+	if !config.KCPConfig.DisableFlowControl {
+		t.Error("DisableFlowControl should be false")
+	}
+
+	if config.KCPConfig.MTU != 1400 {
+		t.Error("Unexpected MTU")
+	}
+}
diff --git a/obfsvpn/kcp.go b/obfsvpn/kcp.go
index 4f0d925..d2e0c71 100644
--- a/obfsvpn/kcp.go
+++ b/obfsvpn/kcp.go
@@ -20,16 +20,16 @@ const (
 
 // https://github.com/skywind3000/kcp/blob/master/README.en.md#protocol-configuration
 type KCPConfig struct {
-	Enabled            bool
-	SendWindowSize     int
-	ReceiveWindowSize  int
-	ReadBuffer         int
-	WriteBuffer        int
-	NoDelay            bool
-	Interval           int
-	Resend             int
-	DisableFlowControl bool
-	MTU                int
+	Enabled            bool `json:"enabled"`
+	SendWindowSize     int  `json:"send_window_size"`
+	ReceiveWindowSize  int  `json:"receive_window_size"`
+	ReadBuffer         int  `json:"read_buffer"`
+	WriteBuffer        int  `json:"write_buffer"`
+	NoDelay            bool `json:"no_delay"`
+	Interval           int  `json:"interval"`
+	Resend             int  `json:"resend"`
+	DisableFlowControl bool `json:"disable_flow_control"`
+	MTU                int  `json:"mtu"`
 }
 
 func DefaultKCPConfig() *KCPConfig {
-- 
GitLab