Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/ocpp-stop-closes-opening-socket.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"ftw": patch
---

Stopping the OCPP server now also closes a charger connection that was still opening. Before, that connection could stay open on the stopped server. Core stops the server only when it shuts down, so chargers on a running box are not affected.
58 changes: 9 additions & 49 deletions go/internal/ocpp/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"testing"
"time"

ocpp16 "github.com/lorenzodonini/ocpp-go/ocpp1.6"
"github.com/lorenzodonini/ocpp-go/ws"

"github.com/srcfl/ftw/go/internal/telemetry"
Expand Down Expand Up @@ -263,33 +262,21 @@ func TestSchemeFollowsTLS(t *testing.T) {
// End to end over a real connection: the library must actually consult both
// gates, in the order that makes the identity binding effective.
func TestPerChargerCredentialOverTheWire(t *testing.T) {
port := freePort(t)
cfg := &Config{
srv := startOCPP(t, Config{
Enabled: true,
Bind: "127.0.0.1",
Port: port,
HeartbeatIntervalS: 60,
Username: "ftw",
Password: "shared-secret",
ChargerSecrets: map[string]string{"garage": "garage-only-secret"},
ApprovedIDs: []string{"garage"},
}
srv, err := Start(context.Background(), cfg, telemetry.NewStore())
if err != nil {
t.Fatalf("start: %v", err)
}
t.Cleanup(srv.Stop)
waitForListener(t, port)
}, telemetry.NewStore(), false)

connect := func(t *testing.T, id, user, pass string) error {
t.Helper()
client := ws.NewClient()
client.SetBasicAuth(user, pass)
cp := ocpp16.NewChargePoint(id, nil, client)
err := cp.Start(fmt.Sprintf("ws://127.0.0.1:%d", port))
if err == nil {
stopAfterServer(t, srv, cp.Stop, cp.IsConnected)
}
_, _, err := dialChargePoint16(t, wsURL(srv.cfg.Port), id, client, nil)
return err
}

Expand All @@ -313,11 +300,9 @@ func TestPerChargerCredentialOverTheWire(t *testing.T) {
// bind in checkClient is what stops the swap.
func TestClientCertCannotClaimAnotherIdentity(t *testing.T) {
bundle := issueMTLSBundle(t)
port := freePort(t)
cfg := &Config{
srv := startOCPP(t, Config{
Enabled: true,
Bind: "127.0.0.1",
Port: port,
HeartbeatIntervalS: 60,
Username: "ftw",
Password: "shared-secret",
Expand All @@ -326,13 +311,7 @@ func TestClientCertCannotClaimAnotherIdentity(t *testing.T) {
KeyFile: bundle.serverKeyFile,
ClientCAFile: bundle.clientCAFile,
},
}
srv, err := Start(context.Background(), cfg, telemetry.NewStore())
if err != nil {
t.Fatalf("start: %v", err)
}
t.Cleanup(srv.Stop)
waitForListener(t, port)
}, telemetry.NewStore(), false)

connect := func(t *testing.T, id string, clientCert tls.Certificate) error {
t.Helper()
Expand All @@ -344,13 +323,10 @@ func TestClientCertCannotClaimAnotherIdentity(t *testing.T) {
}
client := ws.NewTLSClient(tlsCfg)
client.SetBasicAuth("ftw", "shared-secret")
cp := ocpp16.NewChargePoint(id, nil, client)
err := cp.Start(fmt.Sprintf("wss://127.0.0.1:%d", port))
if err == nil {
// Drop the socket before the next attempt so a refused swap
// cannot be mistaken for the library's duplicate-id reject.
cp.Stop()
}
_, stop, err := dialChargePoint16(t, fmt.Sprintf("wss://127.0.0.1:%d", srv.cfg.Port), id, client, nil)
// Drop the socket before the next attempt so a refused swap cannot
// be mistaken for the library's duplicate-id reject.
stop()
return err
}

Expand Down Expand Up @@ -515,19 +491,3 @@ func tlsCert(issued issuedCert) tls.Certificate {
Leaf: issued.cert,
}
}

// waitForListener blocks until the port accepts, so a client never races the
// listener goroutine.
func waitForListener(t *testing.T, port int) {
t.Helper()
deadline := time.Now().Add(2 * time.Second)
for time.Now().Before(deadline) {
c, err := net.DialTimeout("tcp", fmt.Sprintf("127.0.0.1:%d", port), 100*time.Millisecond)
if err == nil {
c.Close()
return
}
time.Sleep(20 * time.Millisecond)
}
t.Fatalf("server did not bind on port %d within deadline", port)
}
12 changes: 2 additions & 10 deletions go/internal/ocpp/capabilities_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ocpp

import (
"fmt"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -100,13 +99,10 @@ func waitSteerable(t *testing.T, srv *Server, id string) ChargerView {

func connectCapabilityCP(t *testing.T, port int, id string, cp *capabilityCP) ocpp16.ChargePoint {
t.Helper()
client := ocpp16.NewChargePoint(id, nil, nil)
client.SetCoreHandler(cp)
if err := client.Start(fmt.Sprintf("ws://127.0.0.1:%d", port)); err != nil {
client, _, err := dialChargePoint16(t, wsURL(port), id, nil, func(c ocpp16.ChargePoint) { c.SetCoreHandler(cp) })
if err != nil {
t.Fatalf("connect: %v", err)
}
var once sync.Once
t.Cleanup(func() { once.Do(client.Stop) })
if _, err := client.BootNotification("Dawn", "Charge Amps"); err != nil {
t.Fatalf("boot: %v", err)
}
Expand All @@ -117,7 +113,6 @@ func connectCapabilityCP(t *testing.T, port int, id string, cp *capabilityCP) oc
// stops asking once it has an answer.
func TestCapabilityProbeRecordsSmartCharging(t *testing.T) {
port, srv := startServer(t, telemetry.NewStore(), "steerable-cp")
defer srv.Stop()

cp := &capabilityCP{profiles: "Core,FirmwareManagement,SmartCharging"}
connectCapabilityCP(t, port, "steerable-cp", cp)
Expand Down Expand Up @@ -146,7 +141,6 @@ func TestCapabilityProbeRecordsSmartCharging(t *testing.T) {
// the reconnect is exactly when the charger should be asked again.
func TestCapabilityProbeMarkerClearedOnDisconnect(t *testing.T) {
port, srv := startServer(t, telemetry.NewStore(), "flappy")
defer srv.Stop()

h := srv.Handler()
// Simulate "probe fired, no answer yet" without a live charge point.
Expand All @@ -169,7 +163,6 @@ func TestCapabilityProbeMarkerClearedOnDisconnect(t *testing.T) {
// warns rather than the code blocking, so control is still attempted.
func TestCapabilityProbeRecordsTelemetryOnly(t *testing.T) {
port, srv := startServer(t, telemetry.NewStore(), "meter-only")
defer srv.Stop()

connectCapabilityCP(t, port, "meter-only", &capabilityCP{profiles: "Core,FirmwareManagement"})

Expand All @@ -187,7 +180,6 @@ func TestCapabilityProbeRecordsTelemetryOnly(t *testing.T) {
// claiming the charger cannot be steered.
func TestCapabilityProbeUnknownWhenKeyMissing(t *testing.T) {
port, srv := startServer(t, telemetry.NewStore(), "quiet-cp")
defer srv.Stop()

cp := &capabilityCP{omitKey: true}
connectCapabilityCP(t, port, "quiet-cp", cp)
Expand Down
Loading
Loading