Skip to content
Open
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ exclude = [
"sdk/revdist/testdata/fixtures/generate-fixtures",
"sdk/serviceability/testdata/fixtures/generate-fixtures",
"sdk/telemetry/testdata/fixtures/generate-fixtures",
"sdk/geolocation/testdata/fixtures/generate-fixtures",
]
resolver = "2"

Expand Down
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ rust-program-accounts-compat:
.PHONY: sdk-test
sdk-test:
go test ./sdk/borsh-incremental/go/...
go test ./sdk/geolocation/go/...
go test ./sdk/revdist/go/...
$(MAKE) python-test-borsh-incremental
$(MAKE) python-test-revdist
Expand Down Expand Up @@ -207,6 +208,7 @@ generate-fixtures:
cd sdk/revdist/testdata/fixtures/generate-fixtures && cargo run
cd sdk/serviceability/testdata/fixtures/generate-fixtures && cargo run
cd sdk/telemetry/testdata/fixtures/generate-fixtures && cargo run
cd sdk/geolocation/testdata/fixtures/generate-fixtures && cargo run

# -----------------------------------------------------------------------------
# E2E targets
Expand Down
2 changes: 1 addition & 1 deletion controlplane/telemetry/cmd/geoprobe-agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -868,7 +868,7 @@ func (ml *measurementLoop) runCycle() {

rttData := make(map[geoprobe.ProbeAddress]uint64)

if len(ml.targets) > 0 {
if len(ml.targets) > 0
twampResults, err := ml.pinger.MeasureAll(ml.ctx)
if err != nil {
ml.log.Error("Failed to measure TWAMP targets", "error", err)
Expand Down
214 changes: 214 additions & 0 deletions sdk/geolocation/go/fixture_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
package geolocation

import (
"encoding/json"
"os"
"path/filepath"
"runtime"
"strconv"
"testing"

"github.com/gagliardetto/solana-go"
)

// These tests deserialize binary fixtures generated by the Rust fixture generator
// (sdk/geolocation/testdata/fixtures/generate-fixtures) and verify that Go's
// deserialized field values match the expected values from the JSON sidecar files.
//
// Regenerate fixtures:
// cd ../testdata/fixtures/generate-fixtures && cargo run

type fixtureMeta struct {
Name string `json:"name"`
AccountType uint8 `json:"account_type"`
Fields []fieldValue `json:"fields"`
}

type fieldValue struct {
Name string `json:"name"`
Value string `json:"value"`
Type string `json:"typ"`
}

func fixturesDir() string {
_, filename, _, _ := runtime.Caller(0)
return filepath.Join(filepath.Dir(filename), "..", "testdata", "fixtures")
}

func loadFixture(t *testing.T, name string) ([]byte, fixtureMeta) {
t.Helper()
dir := fixturesDir()

binData, err := os.ReadFile(filepath.Join(dir, name+".bin"))
if err != nil {
t.Fatalf("reading %s.bin: %v", name, err)
}

jsonData, err := os.ReadFile(filepath.Join(dir, name+".json"))
if err != nil {
t.Fatalf("reading %s.json: %v", name, err)
}

var meta fixtureMeta
if err := json.Unmarshal(jsonData, &meta); err != nil {
t.Fatalf("parsing %s.json: %v", name, err)
}

return binData, meta
}

func TestFixtureProgramConfig(t *testing.T) {
data, meta := loadFixture(t, "program_config")
cfg, err := DeserializeProgramConfig(data)
if err != nil {
t.Fatalf("DeserializeProgramConfig: %v", err)
}

assertFields(t, meta.Fields, map[string]any{
"AccountType": cfg.AccountType,
"BumpSeed": cfg.BumpSeed,
"Version": cfg.Version,
"MinCompatibleVersion": cfg.MinCompatibleVersion,
})
}

func TestFixtureGeoProbe(t *testing.T) {
data, meta := loadFixture(t, "geo_probe")
probe, err := DeserializeGeoProbe(data)
if err != nil {
t.Fatalf("DeserializeGeoProbe: %v", err)
}

assertFields(t, meta.Fields, map[string]any{
"AccountType": probe.AccountType,
"Owner": probe.Owner,
"ExchangePK": probe.ExchangePK,
"PublicIP": probe.PublicIP,
"LocationOffsetPort": probe.LocationOffsetPort,
"MetricsPublisherPK": probe.MetricsPublisherPK,
"ReferenceCount": probe.ReferenceCount,
"Code": probe.Code,
"ParentDevicesLen": uint32(len(probe.ParentDevices)),
"ParentDevices0": probe.ParentDevices[0],
"ParentDevices1": probe.ParentDevices[1],
"TargetUpdateCount": probe.TargetUpdateCount,
})
}

func TestFixtureGeolocationUser(t *testing.T) {
data, meta := loadFixture(t, "geolocation_user")
user, err := DeserializeGeolocationUser(data)
if err != nil {
t.Fatalf("DeserializeGeolocationUser: %v", err)
}

assertFields(t, meta.Fields, map[string]any{
"AccountType": user.AccountType,
"Owner": user.Owner,
"Code": user.Code,
"TokenAccount": user.TokenAccount,
"PaymentStatus": user.PaymentStatus,
"BillingDiscriminant": user.Billing.Variant,
"BillingRate": user.Billing.FlatPerEpoch.Rate,
"BillingLastDeductionDzEpoch": user.Billing.FlatPerEpoch.LastDeductionDzEpoch,
"Status": user.Status,
"TargetsLen": uint32(len(user.Targets)),

"Target0Type": user.Targets[0].TargetType,
"Target0IP": user.Targets[0].IPAddress,
"Target0LocationOffsetPort": user.Targets[0].LocationOffsetPort,
"Target0TargetPK": user.Targets[0].TargetPK,
"Target0GeoProbePK": user.Targets[0].GeoProbePK,

"Target1Type": user.Targets[1].TargetType,
"Target1IP": user.Targets[1].IPAddress,
"Target1LocationOffsetPort": user.Targets[1].LocationOffsetPort,
"Target1TargetPK": user.Targets[1].TargetPK,
"Target1GeoProbePK": user.Targets[1].GeoProbePK,

"Target2Type": user.Targets[2].TargetType,
"Target2IP": user.Targets[2].IPAddress,
"Target2LocationOffsetPort": user.Targets[2].LocationOffsetPort,
"Target2TargetPK": user.Targets[2].TargetPK,
"Target2GeoProbePK": user.Targets[2].GeoProbePK,

"ResultDestination": user.ResultDestination,
})
}

func assertFields(t *testing.T, expected []fieldValue, got map[string]any) {
t.Helper()
for _, f := range expected {
val, ok := got[f.Name]
if !ok {
t.Errorf("field %s: not found in Go struct map", f.Name)
continue
}
assertField(t, f, val)
}
}

func assertField(t *testing.T, f fieldValue, got any) {
t.Helper()

switch f.Type {
case "u8":
want, _ := strconv.ParseUint(f.Value, 10, 8)
switch v := got.(type) {
case uint8:
assertEq(t, f.Name, uint8(want), v)
case AccountType:
assertEq(t, f.Name, uint8(want), uint8(v))
case GeolocationPaymentStatus:
assertEq(t, f.Name, uint8(want), uint8(v))
case GeolocationUserStatus:
assertEq(t, f.Name, uint8(want), uint8(v))
case GeoLocationTargetType:
assertEq(t, f.Name, uint8(want), uint8(v))
default:
t.Fatalf("field %s: expected u8-like value, got %T", f.Name, got)
}
case "u16":
want, _ := strconv.ParseUint(f.Value, 10, 16)
assertEq(t, f.Name, uint16(want), got)
case "u32":
want, _ := strconv.ParseUint(f.Value, 10, 32)
assertEq(t, f.Name, uint32(want), got)
case "u64":
want, _ := strconv.ParseUint(f.Value, 10, 64)
assertEq(t, f.Name, uint64(want), got)
case "string":
assertEq(t, f.Name, f.Value, got)
case "pubkey":
switch v := got.(type) {
case solana.PublicKey:
assertEq(t, f.Name, f.Value, v.String())
default:
t.Fatalf("field %s: expected pubkey, got %T", f.Name, got)
}
case "ipv4":
switch v := got.(type) {
case [4]uint8:
assertEq(t, f.Name, f.Value, ipv4String(v))
default:
t.Fatalf("field %s: expected ipv4 [4]uint8, got %T", f.Name, got)
}
default:
t.Fatalf("field %s: unknown fixture type %q", f.Name, f.Type)
}
}

func assertEq[T comparable](t *testing.T, name string, want T, got any) {
t.Helper()
v, ok := got.(T)
if !ok {
t.Fatalf("field %s: expected %T, got %T (%v)", name, want, got, got)
}
if v != want {
t.Fatalf("field %s: want %v, got %v", name, want, v)
}
}

func ipv4String(ip [4]uint8) string {
return strconv.Itoa(int(ip[0])) + "." + strconv.Itoa(int(ip[1])) + "." + strconv.Itoa(int(ip[2])) + "." + strconv.Itoa(int(ip[3]))
}
Loading