cantabile_edge, cantabile_modbus, cantabile_eebus 0.1.400: full source

This commit is contained in:
Setti Solutions S.R.L committed 2026-10-30 08:26:45 +02:00
1 parent 343331d234
commit d5fbcacaa4
176 files changed
+32511 -19

No files matched your search

+93
View File
@@ -0,0 +1,93 @@
// Copyright © 2026 Setti Solutions S.R.L, www.setti.md. All rights reserved.
package main
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/sha256"
"crypto/x509"
"encoding/base64"
"encoding/json"
"encoding/pem"
"fmt"
"strings"
"testing"
"time"
)
func gridSigned(t *testing.T, k *ecdsa.PrivateKey, v any) (string, string) {
data, _ := json.Marshal(v)
sum := sha256.Sum256(data)
sig, err := ecdsa.SignASN1(rand.Reader, k, sum[:])
if err != nil {
t.Fatal(err)
}
return base64.StdEncoding.EncodeToString(data), base64.StdEncoding.EncodeToString(sig)
}
// gridArgs: an emergency `building` command as fleet relays a grid order.
func gridArgs(t *testing.T, bundleKey, opKey *ecdsa.PrivateKey, now time.Time, edit func(reg, order map[string]any, args map[string]string)) map[string]string {
der, _ := x509.MarshalPKIXPublicKey(&opKey.PublicKey)
reg := map[string]any{"v": 1, "kind": "grid-operator", "id": "r1", "operator": "netz-berlin", "name": "Stromnetz Berlin",
"area": "DE-BER", "key": string(pem.EncodeToMemory(&pem.Block{Type: "PUBLIC KEY", Bytes: der})),
"issued": now.Unix() - 60, "expires": now.Unix() + 86400}
order := map[string]any{"v": 1, "kind": "grid-order", "id": "o1", "operator": "netz-berlin", "connection": "MALO-1",
"limit_w": 4200.0, "from": now.Unix() - 60, "until": now.Unix() + 3600, "issued": now.Unix() - 60}
args := map[string]string{"v": "1", "id": "c1", "building": "b1", "level": "emergency", "control": "load_limit",
"value": "1400", "from": fmt.Sprint(now.Unix()), "until": fmt.Sprint(now.Unix() + 1800), "connection": "MALO-1"}
if edit != nil {
edit(reg, order, args)
}
args["registration"], args["registration_sig"] = gridSigned(t, bundleKey, reg)
signer := opKey
if forger, ok := order["_signer"].(*ecdsa.PrivateKey); ok {
signer = forger
delete(order, "_signer")
}
args["order"], args["order_sig"] = gridSigned(t, signer, order)
return args
}
func TestGridProof(t *testing.T) {
now := time.Now()
bk, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
ok, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
other, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
a := gridArgs(t, bk, ok, now, nil)
if err := checkGridProof(&bk.PublicKey, a, now); err != nil {
t.Fatalf("valid proof refused: %v", err)
}
if a["proof"] != "verified:netz-berlin:o1" || a["order"] != "" || a["registration"] != "" {
t.Errorf("proof not replaced: %v", a)
}
for name, c := range map[string]struct {
edit func(reg, order map[string]any, args map[string]string)
key *ecdsa.PrivateKey // signs the order
bk *ecdsa.PrivateKey // signs the registration
want string
}{
"registration not Cantabile's": {nil, ok, other, "not Cantabile's"},
"order not the operator's": {func(_, o map[string]any, _ map[string]string) { o["_signer"] = other }, ok, bk, "not signed by"},
"another connection": {func(_, _ map[string]any, a map[string]string) { a["connection"] = "MALO-2" }, ok, bk, "another grid connection"},
"limit above the order's": {func(_, _ map[string]any, a map[string]string) { a["value"] = "5000" }, ok, bk, "above the order"},
"window outside": {func(_, _ map[string]any, a map[string]string) { a["until"] = fmt.Sprint(now.Unix() + 7200) }, ok, bk, "outside the order"},
"order over": {func(_, o map[string]any, _ map[string]string) { o["until"] = now.Unix() - 1 }, ok, bk, "order is over"},
"not a load limit": {func(_, _ map[string]any, a map[string]string) { a["control"] = "thermostat_cap" }, ok, bk, "load_limit"},
"registration expired": {func(r, _ map[string]any, _ map[string]string) { r["expires"] = now.Unix() - 1 }, ok, bk, "expired"},
"order names another operator": {func(_, o map[string]any, _ map[string]string) { o["operator"] = "someone" }, ok, bk, "another operator"},
} {
a := gridArgs(t, c.bk, c.key, now, c.edit)
err := checkGridProof(&bk.PublicKey, a, now)
if err == nil || !strings.Contains(err.Error(), c.want) {
t.Errorf("%s: %v, want %q", name, err, c.want)
}
}
// an emergency without any proof
if err := checkGridProof(&bk.PublicKey, map[string]string{"level": "emergency"}, now); err == nil {
t.Error("no proof accepted")
}
}