Skip to content
Snippets Groups Projects
getKSA_key_test.go 7.06 KiB
Newer Older
  • Learn to ignore specific revisions
  • package integration_tests
    
    import (
    	"bytes"
    	"encoding/json"
    	"fmt"
    
    	"strings"
    
    
    	"io"
    	"net/http"
    	"os"
    	"testing"
    	"time"
    
    	integration_test_utils "code.fbi.h-da.de/danet/quant/integration-tests/code/integrationTestUtils"
    	"github.com/stretchr/testify/assert"
    )
    
    
    type Update struct {
    	Path  string
    	Value string
    	JSON  string
    }
    
    
    // For log file.
    type LogFile struct {
    	Source string            `json:"source"`
    	Body   PushKSAKeyRequest `json:"body"`
    }
    type PushKSAKeyRequest struct {
    	RequestID string   `json:"request_ID"`
    	ProcessID string   `json:"process_ID"`
    	KSAKeys   []KSAKey `json:"ksa_keys"`
    }
    
    type KSAKey struct {
    	KeyID string `json:"key_ID"`
    	Key   string `json:"key"`
    }
    
    // For request.
    type KeyProperties struct {
    	Number    int `json:"number"`
    	KeyLength int `json:"key_length"`
    	Timeout   int `json:"timeout"`
    	TTL       int `json:"TTL"`
    }
    
    type RequestData struct {
    	ReceivingCKMSID string        `json:"receiving_CKMS_ID"`
    	RequestID       string        `json:"request_ID"`
    	KeyProperties   KeyProperties `json:"key_properties"`
    }
    
    func TestGetKSAKey(t *testing.T) { //nolint:gocyclo
    	kms2URL := "127.0.0.1:7031"
    	kms2URL_ENV := os.Getenv("INTEGRATION_TEST_KMS2_URL")
    	if kms2URL_ENV != "" {
    		kms2URL = kms2URL_ENV
    	}
    	kms1URL := "127.0.0.1:7030"
    	kms1URL_ENV := os.Getenv("INTEGRATION_TEST_KMS1_URL")
    	if kms1URL_ENV != "" {
    		kms1URL = kms1URL_ENV
    	}
    	kms1AkmsURL := "127.0.0.1:9696"
    	kms1AkmsURL_ENV := os.Getenv("INTEGRATION_TEST_KMS1_AKMS_URL")
    	if kms1AkmsURL_ENV != "" {
    		kms1AkmsURL = kms1AkmsURL_ENV
    	}
    	logFileURL := "127.0.0.1:4444"
    	logFileURL_ENV := os.Getenv("INTEGRATION_TEST_LOG_FILE1_URL")
    	if logFileURL_ENV != "" {
    		logFileURL = logFileURL_ENV
    	}
    	logFileURL2 := "127.0.0.1:4445"
    	logFileURL_ENV2 := os.Getenv("INTEGRATION_TEST_LOG_FILE2_URL")
    	if logFileURL_ENV2 != "" {
    		logFileURL2 = logFileURL_ENV2
    	}
    
    	url := fmt.Sprintf("http://%s/api/v1/keys/ksa_key_req", kms1AkmsURL)
    	data := RequestData{
    		ReceivingCKMSID: "5e41c291-6121-4335-84f6-41e04b8bdaa2",
    		RequestID:       "request_ID-1234",
    		KeyProperties: KeyProperties{
    			Number:    1,
    			KeyLength: 256,
    			Timeout:   20,
    			TTL:       24,
    		},
    	}
    
    	jsonData, err := json.Marshal(data)
    	if err != nil {
    		fmt.Println(err)
    		return
    	}
    
    
    	go func() {
    		resp, err := http.Post(url, "application/json", bytes.NewBuffer(jsonData))
    		if err != nil {
    			t.Errorf("Error making HTTP request: %s", err)
    			return
    		}
    		defer resp.Body.Close() //nolint:errcheck
    
    		if resp.StatusCode != http.StatusNoContent {
    			t.Errorf("Expected status code 204 No Content, but got %d", resp.StatusCode)
    		}
    	}()
    
    	time.Sleep(time.Duration(2) * time.Second)
    
    	output, err := integration_test_utils.GnmicCommand(kms1URL, "get", "--path", "create-route-requests")
    	if err != nil {
    		t.Errorf("Error getting create-route-requests: %s; %s", err, output)
    	}
    
    	output = strings.Split(output, "\"kms-path-id\": \"")[1]
    	output = strings.Split(output, "\",")[0]
    
    	path := fmt.Sprintf("key-routing-sessions/routing-sessions[path-id=%s]", output)
    
    	config01 := []Update{
    		{
    			Path: path,
    			JSON: fmt.Sprintf(`{"path-id": "%s"}`, output),
    		},
    		{
    			Path: fmt.Sprint(path, "/prev-hop"),
    			JSON: fmt.Sprintf(`{"node-id": "%s"}`, "5e41c291-6121-4335-84f6-41e04b8bdaa2"),
    		},
    		{
    			Path: fmt.Sprint(path, "/prev-hop"),
    			JSON: fmt.Sprintf(`{"hostname": "%s"}`, "kms_2"),
    		},
    		{
    			Path: fmt.Sprint(path, "/prev-hop"),
    			JSON: fmt.Sprintf(`{"port": %d}`, 50910),
    		},
    		{
    			Path: fmt.Sprint(path, "/initiating-kms-address"),
    			JSON: fmt.Sprintf(`{"node-id": "%s"}`, "5e41c291-6121-4335-84f6-41e04b8bdaa2"),
    		},
    		{
    			Path: fmt.Sprint(path, "/initiating-kms-address"),
    			JSON: fmt.Sprintf(`{"hostname": "%s"}`, "kms_2"),
    		},
    		{
    			Path: fmt.Sprint(path, "/initiating-kms-address"),
    			JSON: fmt.Sprintf(`{"port": %d}`, 50910),
    		},
    	}
    
    	config02 := []Update{
    		{
    			Path: path,
    			JSON: fmt.Sprintf(`{"path-id": "%s"}`, output),
    		},
    		{
    			Path: fmt.Sprint(path, "/next-hop"),
    			JSON: fmt.Sprintf(`{"node-id": "%s"}`, "0ff33c82-7fe1-482b-a0ca-67565806ee4b"),
    		},
    		{
    			Path: fmt.Sprint(path, "/next-hop"),
    			JSON: fmt.Sprintf(`{"hostname": "%s"}`, "kms_1"),
    		},
    		{
    			Path: fmt.Sprint(path, "/next-hop"),
    			JSON: fmt.Sprintf(`{"port": %d}`, 50910),
    		},
    		{
    			Path: fmt.Sprint(path, "/initiating-kms-address"),
    			JSON: fmt.Sprintf(`{"node-id": "%s"}`, "0ff33c82-7fe1-482b-a0ca-67565806ee4b"),
    		},
    		{
    			Path: fmt.Sprint(path, "/initiating-kms-address"),
    			JSON: fmt.Sprintf(`{"hostname": "%s"}`, "kms_1"),
    		},
    		{
    			Path: fmt.Sprint(path, "/initiating-kms-address"),
    			JSON: fmt.Sprintf(`{"port": %d}`, 50910),
    		},
    	}
    
    	argsKMS1 := []string{"set"}
    	for _, update := range config01 {
    		argsKMS1 = append(argsKMS1, "--update-path", update.Path, "--update-value", update.JSON)
    	}
    
    	argsKMS2 := []string{"set"}
    	for _, update := range config02 {
    		argsKMS2 = append(argsKMS2, "--update-path", update.Path, "--update-value", update.JSON)
    	}
    
    	output, err = integration_test_utils.GnmicCommand(kms1URL, argsKMS1...)
    	if err != nil {
    		t.Errorf("Error setting routing-session: %s; %s", err, output)
    	}
    
    	output, err = integration_test_utils.GnmicCommand(kms2URL, argsKMS2...)
    	if err != nil {
    		t.Errorf("Error setting routing-session: %s; %s", err, output)
    	}
    
    	time.Sleep(time.Duration(2) * time.Second)
    
    
    	// Get logfile of akms
    
    	resp, err := http.Get("http://" + logFileURL + "/debug/get_log_file")
    
    	if err != nil {
    		t.Errorf("Error making HTTP request: %s", err)
    		return
    	}
    	defer resp.Body.Close() //nolint:errcheck
    
    	body, err := io.ReadAll(resp.Body)
    	if err != nil {
    		t.Errorf("Error reading response body: %s", err)
    		return
    	}
    
    	var logFile LogFile
    	err = json.Unmarshal(body, &logFile)
    	if err != nil {
    		t.Errorf("Error parsing body into PushKSAKeyRequest: %s", err)
    		return
    	}
    	assert.NotNil(t, logFile.Source)
    	assert.Equal(t, "request_ID-1234", logFile.Body.RequestID)
    	assert.NotNil(t, logFile.Body.ProcessID)
    	assert.Equal(t, 1, len(logFile.Body.KSAKeys))
    	assert.NotNil(t, logFile.Body.KSAKeys[0].KeyID)
    	assert.NotNil(t, logFile.Body.KSAKeys[0].Key)
    
    	resp, err = http.Get("http://" + logFileURL2 + "/debug/get_log_file")
    	if err != nil {
    		t.Errorf("Error making HTTP request: %s", err)
    		return
    	}
    	defer resp.Body.Close() //nolint:errcheck
    
    	body, err = io.ReadAll(resp.Body)
    	if err != nil {
    		t.Errorf("Error reading response body: %s", err)
    		return
    	}
    	var logFile2 LogFile
    
    	err = json.Unmarshal(body, &logFile2)
    	if err != nil {
    		t.Errorf("Error parsing body into PushKSAKeyRequest: %s", err)
    		return
    	}
    	assert.NotNil(t, logFile2.Source)
    	assert.Equal(t, "request_ID-1234", logFile2.Body.RequestID)
    	assert.NotNil(t, logFile2.Body.ProcessID)
    	assert.Equal(t, 1, len(logFile2.Body.KSAKeys))
    	assert.NotNil(t, logFile2.Body.KSAKeys[0].KeyID)
    	assert.NotNil(t, logFile2.Body.KSAKeys[0].Key)
    
    	// Check that both log files are identical except for the source.
    	assert.NotEqual(t, logFile.Source, logFile2.Source)
    	assert.Equal(t, logFile.Body.RequestID, logFile2.Body.RequestID)
    	assert.Equal(t, logFile.Body.ProcessID, logFile2.Body.ProcessID)
    	assert.Equal(t, logFile.Body.KSAKeys[0].KeyID, logFile2.Body.KSAKeys[0].KeyID)
    	assert.Equal(t, logFile.Body.KSAKeys[0].Key, logFile2.Body.KSAKeys[0].Key)
    }