~james/core

core/spindle/agentproto/protocol_test.go -rw-r--r-- 1.6 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 16 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package agentproto

import (
	"bytes"
	"encoding/binary"
	"testing"

	agentv1 "tangled.org/core/spindle/agentproto/gen"
)

func TestDecoderRejectsOversizedMessage(t *testing.T) {
	var tooLarge bytes.Buffer
	var header [4]byte
	binary.BigEndian.PutUint32(header[:], MaxMessageBytes+1)
	tooLarge.Write(header[:])

	_, err := NewDecoder(&tooLarge).Decode()
	if err == nil {
		t.Fatal("expected oversized message error")
	}
}

func TestValidation(t *testing.T) {
	// 1. Valid message (exactly one of the payload fields is set)
	validMsg := &Message{
		Id: "test-1",
		Hello: &agentv1.Hello{
			ProtocolVersion: 1,
			AgentVersion:    "1.0",
		},
	}
	if err := validator.Validate(validMsg); err != nil {
		t.Fatalf("expected valid message to pass validation, got: %v", err)
	}

	// 2. Invalid message: zero payloads set
	invalidZeroMsg := &Message{
		Id: "test-2",
	}
	if err := validator.Validate(invalidZeroMsg); err == nil {
		t.Fatal("expected message with zero payloads to fail validation")
	}

	// 3. Invalid message: multiple payloads set
	invalidMultiMsg := &Message{
		Id: "test-3",
		Hello: &agentv1.Hello{
			ProtocolVersion: 1,
		},
		Init: &agentv1.Init{
			JobId: "job-1",
		},
	}
	if err := validator.Validate(invalidMultiMsg); err == nil {
		t.Fatal("expected message with multiple payloads to fail validation")
	}
}

func TestValidateProtocolVersion(t *testing.T) {
	if err := ValidateProtocolVersion(ProtocolVersion); err != nil {
		t.Fatalf("current protocol rejected: %v", err)
	}
	if err := ValidateProtocolVersion(ProtocolVersion + 1); err == nil {
		t.Fatal("mismatched protocol accepted")
	}
}