-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_test.go
More file actions
94 lines (76 loc) · 1.7 KB
/
client_test.go
File metadata and controls
94 lines (76 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package gomsf
import (
"context"
"os"
"strconv"
"testing"
)
func getTestClient(t *testing.T) *Client {
t.Helper()
if os.Getenv("RUN_MSF_INTEGRATION") != "1" {
t.Skip("set RUN_MSF_INTEGRATION=1 to run live Metasploit RPC tests")
}
password := os.Getenv("MSF_PASSWORD")
if password == "" {
password = "testpass123"
}
username := os.Getenv("MSF_USERNAME")
if username == "" {
username = "msf"
}
host := os.Getenv("MSF_HOST")
if host == "" {
host = "127.0.0.1"
}
port := 55553
if p := os.Getenv("MSF_PORT"); p != "" {
parsed, err := strconv.Atoi(p)
if err != nil {
t.Fatalf("Failed to parse port: %v", err)
}
port = parsed
}
ssl := os.Getenv("MSF_SSL") != "false"
client, err := NewClient(password,
WithHost(host),
WithPort(port),
WithSSL(ssl),
WithUsername(username),
)
if err != nil {
t.Fatalf("Failed to connect to msfrpcd: %v", err)
}
return client
}
func TestNewClient(t *testing.T) {
client := getTestClient(t)
defer client.Logout(context.Background())
if !client.IsAuthenticated() {
t.Error("Expected client to be authenticated")
}
if client.Token() == "" {
t.Error("Expected token to be set")
}
}
func TestClient_Call_Unauthenticated(t *testing.T) {
c := &Client{
host: "127.0.0.1",
port: 55553,
uri: "/api/",
ssl: false,
}
_, err := c.Call(context.Background(), CoreVersion)
if err != ErrNotAuthenticated {
t.Errorf("Expected ErrNotAuthenticated, got: %v", err)
}
}
func TestClient_Logout(t *testing.T) {
client := getTestClient(t)
err := client.Logout(context.Background())
if err != nil {
t.Errorf("Logout failed: %v", err)
}
if client.IsAuthenticated() {
t.Error("Expected client to be unauthenticated after logout")
}
}