1package protocol
2
3import (
4 "strings"
5 "testing"
6
7 "github.com/bnema/vev/internal/domain"
8)
9
10func TestValidateNavigationInventoryRequest(t *testing.T) {
11 registration, err := domain.NewRemoteRegistration("user@arch", [16]byte{1})
12 if err != nil {
13 t.Fatalf("NewRemoteRegistration: %v", err)
14 }
15 tests := []struct {
16 name string
17 request NavigationInventoryRequest
18 wantErr bool
19 }{
20 {name: "snapshot ok", request: NavigationInventoryRequest{Version: Version, RequestID: 1, Operation: NavigationInventorySnapshot}},
21 {name: "zero request", request: NavigationInventoryRequest{Operation: NavigationInventorySnapshot}, wantErr: true},
22 {name: "bad operation", request: NavigationInventoryRequest{Version: Version, RequestID: 1, Operation: 9}, wantErr: true},
23 {name: "snapshot with keys", request: NavigationInventoryRequest{Version: Version, RequestID: 1, Operation: NavigationInventorySnapshot, SourceKey: "local"}, wantErr: true},
24 {name: "resolve ok", request: NavigationInventoryRequest{Version: Version, RequestID: 2, Operation: NavigationInventoryResolve, SourceKey: "remote-a", EntryKey: "abc", Registration: registration}},
25 {name: "resolve local ok", request: NavigationInventoryRequest{Version: Version, RequestID: 3, Operation: NavigationInventoryResolve, SourceKey: NavigationInventoryLocalSourceKey, EntryKey: "abc"}},
26 {name: "resolve local with registration", request: NavigationInventoryRequest{Version: Version, RequestID: 3, Operation: NavigationInventoryResolve, SourceKey: NavigationInventoryLocalSourceKey, EntryKey: "abc", Registration: registration}, wantErr: true},
27 {name: "resolve missing registration", request: NavigationInventoryRequest{Version: Version, RequestID: 2, Operation: NavigationInventoryResolve, SourceKey: "remote-a", EntryKey: "abc"}, wantErr: true},
28 {name: "resolve bad key", request: NavigationInventoryRequest{Version: Version, RequestID: 2, Operation: NavigationInventoryResolve, SourceKey: "a\x1b", EntryKey: "abc", Registration: registration}, wantErr: true},
29 }
30 for _, tt := range tests {
31 t.Run(tt.name, func(t *testing.T) {
32 err := ValidateNavigationInventoryRequest(tt.request)
33 if (err != nil) != tt.wantErr {
34 t.Fatalf("ValidateNavigationInventoryRequest() = %v, wantErr %t", err, tt.wantErr)
35 }
36 })
37 }
38}
39
40func TestValidateNavigationInventoryResponseUnions(t *testing.T) {
41 group := NavigationInventorySourceGroup{SourceKey: "local", Status: NavigationInventorySourceOK, Entries: []NavigationInventoryEntry{{SourceKey: "local", EntryKey: "a", Name: "one"}}}
42 tests := []struct {
43 name string
44 response NavigationInventoryResponse
45 wantErr bool
46 }{
47 {name: "snapshot ok", response: NavigationInventoryResponse{RequestID: 1, Operation: NavigationInventorySnapshot, Status: NavigationInventoryOK, Groups: []NavigationInventorySourceGroup{group}}},
48 {name: "snapshot with target", response: NavigationInventoryResponse{RequestID: 1, Operation: NavigationInventorySnapshot, Status: NavigationInventoryOK, Groups: []NavigationInventorySourceGroup{group}, Resolved: &AttachTarget{}}, wantErr: true},
49 {name: "duplicate source", response: NavigationInventoryResponse{RequestID: 1, Operation: NavigationInventorySnapshot, Status: NavigationInventoryOK, Groups: []NavigationInventorySourceGroup{group, group}}, wantErr: true},
50 {name: "too many groups", response: NavigationInventoryResponse{RequestID: 1, Operation: NavigationInventorySnapshot, Status: NavigationInventoryOK, Groups: make([]NavigationInventorySourceGroup, NavigationInventoryMaxSourceGroups+1)}, wantErr: true},
51 {name: "control char label", response: NavigationInventoryResponse{RequestID: 1, Operation: NavigationInventorySnapshot, Status: NavigationInventoryOK, Groups: []NavigationInventorySourceGroup{{SourceKey: "local", Status: NavigationInventorySourceOK, Entries: []NavigationInventoryEntry{{SourceKey: "local", EntryKey: "a", Name: "bad\x1b"}}}}}, wantErr: true},
52 {name: "oversize label", response: NavigationInventoryResponse{RequestID: 1, Operation: NavigationInventorySnapshot, Status: NavigationInventoryOK, Groups: []NavigationInventorySourceGroup{{SourceKey: "local", Status: NavigationInventorySourceOK, Entries: []NavigationInventoryEntry{{SourceKey: "local", EntryKey: "a", Name: strings.Repeat("x", NavigationInventoryMaxDisplayBytes+1)}}}}}, wantErr: true},
53 {name: "version mismatch clean", response: NavigationInventoryResponse{RequestID: 1, Operation: NavigationInventorySnapshot, Status: NavigationInventoryVersionMismatch}},
54 {name: "version mismatch with target", response: NavigationInventoryResponse{RequestID: 1, Operation: NavigationInventorySnapshot, Status: NavigationInventoryVersionMismatch, Groups: []NavigationInventorySourceGroup{group}}, wantErr: true},
55 }
56 for _, tt := range tests {
57 t.Run(tt.name, func(t *testing.T) {
58 err := ValidateNavigationInventoryResponse(tt.response)
59 if (err != nil) != tt.wantErr {
60 t.Fatalf("ValidateNavigationInventoryResponse() = %v, wantErr %t", err, tt.wantErr)
61 }
62 })
63 }
64}
65
66func TestNavigationCapabilityInventoryValid(t *testing.T) {
67 if err := ValidateNavigation(IntentAttach, NavigationCapabilityInventory); err != nil {
68 t.Fatalf("ValidateNavigation(inventory) = %v", err)
69 }
70}
71
72// TestValidateNavigationBindsInventoryToTheIntentsThatServeIt pins the shared
73// client/daemon rule: inventory is negotiable on the intents whose attachment
74// owns a relay, and no capability is admissible on the others.
75func TestValidateNavigationBindsInventoryToTheIntentsThatServeIt(t *testing.T) {
76 tests := []struct {
77 name string
78 intent uint8
79 capabilities NavigationCapabilities
80 wantErr bool
81 }{
82 {name: "attach inventory", intent: IntentAttach, capabilities: NavigationCapabilityInventory},
83 {name: "resume inventory", intent: IntentResume, capabilities: NavigationCapabilityInventory},
84 {name: "new inventory", intent: IntentNew, capabilities: NavigationCapabilityInventory},
85 {name: "ephemeral inventory", intent: IntentEphemeral, capabilities: NavigationCapabilityInventory, wantErr: true},
86 {name: "ephemeral clean", intent: IntentEphemeral},
87 {name: "unknown bit", intent: IntentAttach, capabilities: 1 << 7, wantErr: true},
88 }
89 for _, tt := range tests {
90 t.Run(tt.name, func(t *testing.T) {
91 err := ValidateNavigation(tt.intent, tt.capabilities)
92 if (err != nil) != tt.wantErr {
93 t.Fatalf("ValidateNavigation(%d, %d) = %v, wantErr %t", tt.intent, tt.capabilities, err, tt.wantErr)
94 }
95 })
96 }
97}