- 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
198 lines
4.4 KiB
Go
198 lines
4.4 KiB
Go
package easytier
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestNewCoordinator(t *testing.T) {
|
|
c := NewCoordinator()
|
|
if c == nil {
|
|
t.Fatal("Coordinator should not be nil")
|
|
}
|
|
if c.nodes == nil {
|
|
t.Error("Nodes map should be initialized")
|
|
}
|
|
if c.networks == nil {
|
|
t.Error("Networks map should be initialized")
|
|
}
|
|
}
|
|
|
|
func TestSetServerAddress(t *testing.T) {
|
|
c := NewCoordinator()
|
|
c.SetServerAddress("10.0.0.1:7777")
|
|
|
|
if c.serverAddr != "10.0.0.1:7777" {
|
|
t.Errorf("ServerAddr mismatch: got %s", c.serverAddr)
|
|
}
|
|
}
|
|
|
|
func TestCreateNetwork(t *testing.T) {
|
|
c := NewCoordinator()
|
|
|
|
if err := c.CreateNetwork("net_001", "Test Network"); err != nil {
|
|
t.Fatalf("CreateNetwork failed: %v", err)
|
|
}
|
|
|
|
// Duplicate should fail
|
|
if err := c.CreateNetwork("net_001", "Test Network"); err == nil {
|
|
t.Error("CreateNetwork should fail for duplicate network")
|
|
}
|
|
}
|
|
|
|
func TestAuthorizeDevice(t *testing.T) {
|
|
c := NewCoordinator()
|
|
|
|
resp, err := c.AuthorizeDevice("net_001", "DEV_test_001")
|
|
if err != nil {
|
|
t.Fatalf("AuthorizeDevice failed: %v", err)
|
|
}
|
|
|
|
if resp.Credential == "" {
|
|
t.Error("Credential should not be empty")
|
|
}
|
|
if resp.NetworkID != "net_001" {
|
|
t.Errorf("NetworkID mismatch: got %s", resp.NetworkID)
|
|
}
|
|
}
|
|
|
|
func TestAuthorizeDeviceCreatesNetwork(t *testing.T) {
|
|
c := NewCoordinator()
|
|
|
|
// Authorize without creating network first - should auto-create
|
|
resp, err := c.AuthorizeDevice("auto_net", "DEV_test_002")
|
|
if err != nil {
|
|
t.Fatalf("AuthorizeDevice failed: %v", err)
|
|
}
|
|
|
|
if resp.NetworkID != "auto_net" {
|
|
t.Errorf("NetworkID mismatch")
|
|
}
|
|
}
|
|
|
|
func TestRevokeDevice(t *testing.T) {
|
|
c := NewCoordinator()
|
|
|
|
// First authorize
|
|
c.AuthorizeDevice("net_001", "DEV_test_001")
|
|
|
|
if err := c.RevokeDevice("DEV_test_001"); err != nil {
|
|
t.Fatalf("RevokeDevice failed: %v", err)
|
|
}
|
|
|
|
// Verify status
|
|
node, _ := c.GetNode("DEV_test_001")
|
|
if node.Status != "revoked" {
|
|
t.Errorf("Status should be revoked, got %s", node.Status)
|
|
}
|
|
|
|
// Revoke non-existent should fail
|
|
if err := c.RevokeDevice("nonexistent"); err == nil {
|
|
t.Error("RevokeDevice should fail for nonexistent device")
|
|
}
|
|
}
|
|
|
|
func TestGetNode(t *testing.T) {
|
|
c := NewCoordinator()
|
|
|
|
c.AuthorizeDevice("net_001", "DEV_test_001")
|
|
|
|
node, err := c.GetNode("DEV_test_001")
|
|
if err != nil {
|
|
t.Fatalf("GetNode failed: %v", err)
|
|
}
|
|
if node.DeviceID != "DEV_test_001" {
|
|
t.Errorf("DeviceID mismatch")
|
|
}
|
|
if node.NetworkID != "net_001" {
|
|
t.Errorf("NetworkID mismatch")
|
|
}
|
|
}
|
|
|
|
func TestGetNodeNotFound(t *testing.T) {
|
|
c := NewCoordinator()
|
|
|
|
_, err := c.GetNode("nonexistent")
|
|
if err == nil {
|
|
t.Error("GetNode should fail for nonexistent node")
|
|
}
|
|
}
|
|
|
|
func TestListNodes(t *testing.T) {
|
|
c := NewCoordinator()
|
|
|
|
// Create different networks
|
|
c.AuthorizeDevice("net_001", "DEV_1")
|
|
c.AuthorizeDevice("net_001", "DEV_2")
|
|
c.AuthorizeDevice("net_002", "DEV_3")
|
|
|
|
nodes := c.ListNodes("net_001")
|
|
if len(nodes) != 2 {
|
|
t.Errorf("Should have 2 nodes in net_001, got %d", len(nodes))
|
|
}
|
|
|
|
nodes = c.ListNodes("net_002")
|
|
if len(nodes) != 1 {
|
|
t.Errorf("Should have 1 node in net_002, got %d", len(nodes))
|
|
}
|
|
}
|
|
|
|
func TestGenerateCommand(t *testing.T) {
|
|
c := NewCoordinator()
|
|
|
|
c.AuthorizeDevice("net_001", "DEV_test_001")
|
|
|
|
cmd := c.GenerateCommand("net_001", "DEV_test_001")
|
|
if cmd == "" {
|
|
t.Error("GenerateCommand should return non-empty string")
|
|
}
|
|
|
|
// Should start with easytier-node (13 chars)
|
|
if len(cmd) < 13 || cmd[:13] != "easytier-node" {
|
|
t.Errorf("Command should start with easytier-node, got: %s", cmd)
|
|
}
|
|
}
|
|
|
|
func TestGenerateCommandNonExistent(t *testing.T) {
|
|
c := NewCoordinator()
|
|
|
|
cmd := c.GenerateCommand("net_001", "nonexistent")
|
|
if cmd != "" {
|
|
t.Error("GenerateCommand should return empty for nonexistent node")
|
|
}
|
|
}
|
|
|
|
func TestGenerateCredential(t *testing.T) {
|
|
c := NewCoordinator()
|
|
|
|
c.AuthorizeDevice("net_001", "DEV_cred_test")
|
|
|
|
node, _ := c.GetNode("DEV_cred_test")
|
|
|
|
// Credential should be in format device_id:base64key
|
|
cred := node.Credential
|
|
if len(cred) < 10 {
|
|
t.Error("Credential should be longer")
|
|
}
|
|
}
|
|
|
|
func TestCoordinatorConcurrentAccess(t *testing.T) {
|
|
c := NewCoordinator()
|
|
|
|
// Simple concurrent authorization test
|
|
done := make(chan bool)
|
|
for i := 0; i < 10; i++ {
|
|
go func(id int) {
|
|
c.AuthorizeDevice("net_001", "DEV_concurrent_"+string(rune('0'+id)))
|
|
done <- true
|
|
}(i)
|
|
}
|
|
|
|
for i := 0; i < 10; i++ {
|
|
<-done
|
|
}
|
|
|
|
nodes := c.ListNodes("net_001")
|
|
if len(nodes) != 10 {
|
|
t.Errorf("Should have 10 nodes, got %d", len(nodes))
|
|
}
|
|
} |