- boot/ (Java/Maven, JNPF 7.0) — 移除旧的 boot/.git,源码直接纳入仓库
- web/ (Vue 3 + Vite + Ant Design Vue, pnpm)
- docs/, scard/, nax/
- refs/INDEX.md + refs/国标/(其余 refs/* 开源子仓库不纳入)
- 已忽略: connector_data/, jweb/, log/, web/{dist,node_modules,.pnpm}, boot/*/target/, .DS_Store, .idea, .vscode
148 lines
3.7 KiB
Go
148 lines
3.7 KiB
Go
package state
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestNewStateMachine(t *testing.T) {
|
|
sm := NewStateMachine("test-device-001")
|
|
if sm == nil {
|
|
t.Fatal("StateMachine should not be nil")
|
|
}
|
|
if sm.GetState() != StateTPMInit {
|
|
t.Errorf("Initial state should be TPM_INIT, got %s", sm.GetState())
|
|
}
|
|
if sm.GetDeviceID() != "test-device-001" {
|
|
t.Errorf("DeviceID mismatch: got %s", sm.GetDeviceID())
|
|
}
|
|
}
|
|
|
|
func TestStateTransitions(t *testing.T) {
|
|
sm := NewStateMachine("test-device")
|
|
|
|
// Valid forward transitions
|
|
testCases := []struct {
|
|
from ConnectorState
|
|
to ConnectorState
|
|
wantErr bool
|
|
}{
|
|
{StateTPMInit, StateKeyInit, false},
|
|
{StateKeyInit, StateAuthRegister, false},
|
|
{StateAuthRegister, StateEasyTierJoin, false},
|
|
{StateEasyTierJoin, StateK3sJoin, false},
|
|
{StateK3sJoin, StateRunning, false},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
err := sm.TransitionTo(tc.to)
|
|
if tc.wantErr && err == nil {
|
|
t.Errorf("TransitionTo(%s) should fail", tc.to)
|
|
}
|
|
if !tc.wantErr && err != nil {
|
|
t.Errorf("TransitionTo(%s) failed: %v", tc.to, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestInvalidBackwardTransition(t *testing.T) {
|
|
sm := NewStateMachine("test-device")
|
|
sm.TransitionTo(StateKeyInit)
|
|
|
|
err := sm.TransitionTo(StateTPMInit)
|
|
if err == nil {
|
|
t.Error("Backward transition should fail")
|
|
}
|
|
}
|
|
|
|
func TestInvalidSameStateTransition(t *testing.T) {
|
|
sm := NewStateMachine("test-device")
|
|
|
|
err := sm.TransitionTo(StateTPMInit)
|
|
if err == nil {
|
|
t.Error("Same state transition should fail")
|
|
}
|
|
}
|
|
|
|
func TestTransitionCallback(t *testing.T) {
|
|
sm := NewStateMachine("test-device")
|
|
transitions := []struct {
|
|
from ConnectorState
|
|
to ConnectorState
|
|
}{}
|
|
|
|
sm.SetTransitionCallback(func(from, to ConnectorState) {
|
|
transitions = append(transitions, struct {
|
|
from ConnectorState
|
|
to ConnectorState
|
|
}{from, to})
|
|
})
|
|
|
|
sm.TransitionTo(StateKeyInit)
|
|
sm.TransitionTo(StateAuthRegister)
|
|
sm.TransitionTo(StateRunning)
|
|
|
|
if len(transitions) != 3 {
|
|
t.Errorf("Expected 3 transitions, got %d", len(transitions))
|
|
}
|
|
|
|
if transitions[0].from != StateTPMInit || transitions[0].to != StateKeyInit {
|
|
t.Errorf("First transition mismatch: %s -> %s", transitions[0].from, transitions[0].to)
|
|
}
|
|
if transitions[1].from != StateKeyInit || transitions[1].to != StateAuthRegister {
|
|
t.Errorf("Second transition mismatch: %s -> %s", transitions[1].from, transitions[1].to)
|
|
}
|
|
if transitions[2].from != StateAuthRegister || transitions[2].to != StateRunning {
|
|
t.Errorf("Third transition mismatch: %s -> %s", transitions[2].from, transitions[2].to)
|
|
}
|
|
}
|
|
|
|
func TestStateString(t *testing.T) {
|
|
tests := []struct {
|
|
state ConnectorState
|
|
expected string
|
|
}{
|
|
{StateTPMInit, "TPM_INIT"},
|
|
{StateKeyInit, "KEY_INIT"},
|
|
{StateAuthRegister, "AUTH_REGISTER"},
|
|
{StateEasyTierJoin, "EASYTIER_JOIN"},
|
|
{StateK3sJoin, "K3S_JOIN"},
|
|
{StateRunning, "RUNNING"},
|
|
{ConnectorState(99), "UNKNOWN"},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
if got := tt.state.String(); got != tt.expected {
|
|
t.Errorf("State.String() for %d = %q, want %q", tt.state, got, tt.expected)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestFullStartupSequence(t *testing.T) {
|
|
sm := NewStateMachine("device-full-sequence")
|
|
sequence := []ConnectorState{
|
|
StateTPMInit,
|
|
StateKeyInit,
|
|
StateAuthRegister,
|
|
StateEasyTierJoin,
|
|
StateK3sJoin,
|
|
StateRunning,
|
|
}
|
|
|
|
for i, expectedState := range sequence {
|
|
current := sm.GetState()
|
|
if current != expectedState {
|
|
t.Errorf("Step %d: expected state %s, got %s", i, expectedState, current)
|
|
}
|
|
|
|
if i < len(sequence)-1 {
|
|
nextState := sequence[i+1]
|
|
if err := sm.TransitionTo(nextState); err != nil {
|
|
t.Errorf("Step %d: failed to transition from %s to %s: %v", i, current, nextState, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
if sm.GetState() != StateRunning {
|
|
t.Errorf("Final state should be RUNNING, got %s", sm.GetState())
|
|
}
|
|
} |