You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
57 lines
1.9 KiB
57 lines
1.9 KiB
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"testing"
|
|
"time"
|
|
|
|
"tera-clone/services/tera-api/internal/domain"
|
|
)
|
|
|
|
func TestMessageContainsSourceIP(t *testing.T) {
|
|
for _, message := range []string{
|
|
"Welcome\nConnected from: 192.168.0.10\nSerial channel: CH01",
|
|
"Welcome Connected from: 2001:db8::10 Serial channel: CH48",
|
|
} {
|
|
if !messageContainsSourceIP(message) {
|
|
t.Fatalf("valid source not detected in %q", message)
|
|
}
|
|
}
|
|
if messageContainsSourceIP("Connected from: workstation Serial channel: CH01") {
|
|
t.Fatal("non-IP source was accepted")
|
|
}
|
|
}
|
|
|
|
func TestValidateSessionLogsCoversEachRequestedChannelAndSession(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) {
|
|
channel := request.URL.Query().Get("channel")
|
|
if request.URL.Query().Get("event_type") != "session" {
|
|
http.Error(response, "missing session filter", http.StatusBadRequest)
|
|
return
|
|
}
|
|
sessionID := map[string]string{"CH01": "direct-CH01-1", "CH48": "direct-CH48-1"}[channel]
|
|
_ = json.NewEncoder(response).Encode([]domain.PortLogEvent{{
|
|
Timestamp: time.Now().UTC(), Channel: channel, EventType: "session",
|
|
Actor: "admin/" + sessionID,
|
|
Message: "Welcome to TERA CORA-X48 PRO K Connected from: 192.168.0.10 Serial channel: " + channel + " User: admin Access: viewer Session: " + sessionID,
|
|
}})
|
|
}))
|
|
defer server.Close()
|
|
base, err := url.Parse(server.URL)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
api := &client{http: server.Client(), base: base, token: "test"}
|
|
connections := []loadConnection{
|
|
{session: domain.DirectSession{Channel: "CH01", SessionID: "direct-CH01-1"}},
|
|
{session: domain.DirectSession{Channel: "CH48", SessionID: "direct-CH48-1"}},
|
|
}
|
|
matched, err := validateSessionLogs(context.Background(), api, connections)
|
|
if err != nil || matched != len(connections) {
|
|
t.Fatalf("matched=%d err=%v", matched, err)
|
|
}
|
|
}
|
|
|