main
Raw Download raw file
  1package mythic
  2
  3import (
  4	"context"
  5	"testing"
  6)
  7
  8func TestIsBase64(t *testing.T) {
  9	tests := []struct {
 10		name     string
 11		input    string
 12		expected bool
 13	}{
 14		{
 15			name:     "valid base64 binary data",
 16			input:    "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChAI9AHW1nw==", // 1x1 PNG
 17			expected: true,
 18		},
 19		{
 20			name:     "valid base64 text that decodes to text",
 21			input:    "SGVsbG8gV29ybGQ=", // "Hello World" in base64
 22			expected: true,               // Actually returns true because the original is longer than decoded
 23		},
 24		{
 25			name:     "short string",
 26			input:    "hi",
 27			expected: false,
 28		},
 29		{
 30			name:     "invalid base64 characters",
 31			input:    "hello@world!",
 32			expected: false,
 33		},
 34		{
 35			name:     "empty string",
 36			input:    "",
 37			expected: false,
 38		},
 39		{
 40			name:     "valid base64 but short",
 41			input:    "dGVzdA==", // "test" in base64
 42			expected: true,       // Actually returns true because original is longer than decoded
 43		},
 44	}
 45
 46	for _, tt := range tests {
 47		t.Run(tt.name, func(t *testing.T) {
 48			result := isBase64(tt.input)
 49			if result != tt.expected {
 50				t.Errorf("isBase64(%q) = %v, expected %v", tt.input, result, tt.expected)
 51			}
 52		})
 53	}
 54}
 55
 56func TestDecodeResponseText(t *testing.T) {
 57	tests := []struct {
 58		name     string
 59		input    string
 60		expected string
 61	}{
 62		{
 63			name:     "valid base64 text",
 64			input:    "SGVsbG8gV29ybGQ=", // "Hello World"
 65			expected: "Hello World",
 66		},
 67		{
 68			name:     "plain text",
 69			input:    "Hello World",
 70			expected: "Hello World",
 71		},
 72		{
 73			name:     "empty string",
 74			input:    "",
 75			expected: "",
 76		},
 77		{
 78			name:     "binary data base64",
 79			input:    "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChAI9AHW1nw==",
 80			expected: "[Binary data: 58 bytes]", // Actual decoded size
 81		},
 82	}
 83
 84	for _, tt := range tests {
 85		t.Run(tt.name, func(t *testing.T) {
 86			result := decodeResponseText(tt.input)
 87			if result != tt.expected {
 88				t.Errorf("decodeResponseText(%q) = %q, expected %q", tt.input, result, tt.expected)
 89			}
 90		})
 91	}
 92}
 93
 94func TestNewClient(t *testing.T) {
 95	apiURL := "https://example.com/graphql"
 96	token := "test-token"
 97	insecure := true
 98	socksProxy := ""
 99
100	client := NewClient(apiURL, token, insecure, socksProxy)
101
102	if client == nil {
103		t.Fatal("NewClient returned nil")
104	}
105
106	if client.token != token {
107		t.Errorf("Expected token %q, got %q", token, client.token)
108	}
109
110	if client.baseURL != "https://example.com" {
111		t.Errorf("Expected baseURL %q, got %q", "https://example.com", client.baseURL)
112	}
113}
114
115func TestNewClientWithSocksProxy(t *testing.T) {
116	apiURL := "https://example.com/graphql"
117	token := "test-token"
118	insecure := true
119	socksProxy := "127.0.0.1:9050"
120
121	client := NewClient(apiURL, token, insecure, socksProxy)
122
123	if client == nil {
124		t.Fatal("NewClient returned nil")
125	}
126
127	// We can't easily test the SOCKS proxy configuration without making actual network calls,
128	// but we can at least ensure the client was created successfully
129	if client.token != token {
130		t.Errorf("Expected token %q, got %q", token, client.token)
131	}
132}
133
134func TestClientMethodsExist(t *testing.T) {
135	apiURL := "https://example.com/graphql"
136	token := "test-token"
137	client := NewClient(apiURL, token, true, "")
138
139	// Test that the new methods exist and can be called (they'll fail due to no server, but should not panic)
140	// This ensures the GraphQL queries compile and the methods are properly defined
141	if client == nil {
142		t.Fatal("NewClient returned nil")
143	}
144
145	// We can't make actual calls without a server, but we can verify the methods exist
146	// by checking they don't panic when called with a canceled context
147	ctx, cancel := context.WithCancel(context.Background())
148	cancel() // Cancel immediately to avoid network calls
149
150	_, err := client.GetTasksWithResponses(ctx, 1, 10)
151	if err == nil {
152		t.Error("Expected error due to canceled context")
153	}
154
155	_, err = client.GetAllTasksWithResponses(ctx, 10)
156	if err == nil {
157		t.Error("Expected error due to canceled context")
158	}
159}