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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
-- ChatGPT wrote my unit tests
local yoshi = dofile'yoshi.lua'
local tests = {}
----------------------------------------------------------------
-- Minimal assert functions
----------------------------------------------------------------
local function fail(msg)
error("TEST FAILED: " .. msg, 2)
end
local function assertEqual(a, b, msg)
if a ~= b then fail(msg or string.format("%s ~= %s", tostring(a), tostring(b))) end
end
local function assertNotEqual(a, b, msg)
if a == b then fail(msg or string.format("%s == %s", tostring(a), tostring(b))) end
end
local function assertDeepEqual(a, b, msg)
local function compare(x, y)
if type(x) ~= type(y) then return false end
if type(x) ~= "table" then return x == y end
local k_checked = {}
for k,v in pairs(x) do
if not compare(v, y[k]) then return false end
k_checked[k] = true
end
for k,_ in pairs(y) do
if not k_checked[k] then return false end
end
return true
end
if not compare(a,b) then fail(msg or "Tables not equal") end
end
----------------------------------------------------------------
-- Helpers
----------------------------------------------------------------
-- reset hosts between tests
local function resetHosts()
yoshi.hosts = {}
end
----------------------------------------------------------------
-- Tests
----------------------------------------------------------------
function tests.test_constructor()
local cmd = yoshi.ssh{
HostName = "localhost",
User = "me",
Port = 2222,
SessionType = "none",
DynamicForward = 8080,
LocalForward = 8000,
RemoteCommand = "ls -la",
}
assertEqual(cmd.HostName, "localhost")
assertEqual(cmd.User, "me")
assertEqual(cmd.Port, 2222)
assertEqual(cmd.SessionType, "none")
assertEqual(cmd.DynamicForward, 8080)
assertDeepEqual(cmd.LocalForward, {8000,8000})
assertEqual(cmd.RemoteCommand, "ls -la")
end
function tests.test_proxyjump()
resetHosts()
yoshi.hosts["bastion"] = function()
return yoshi.ssh{HostName="bastion.internal", User="bastionuser"}
end
local cmd = yoshi.ssh{
HostName = "server.internal",
User = "admin",
ProxyJump = "bastion",
}
assertEqual(#cmd.ProxyJumps, 1)
assertEqual(cmd.ProxyJumps[1].host, "bastion.internal")
assertEqual(cmd.ProxyJumps[1].user, "bastionuser")
end
function tests.test_nested_proxyjump()
resetHosts()
yoshi.hosts["jump1"] = function()
return yoshi.ssh{HostName="jump1.host", ProxyJump="jump2"}
end
yoshi.hosts["jump2"] = function()
return yoshi.ssh{HostName="jump2.host", User="jumpuser"}
end
local cmd = yoshi.ssh{
HostName = "target.host",
ProxyJump = "jump1"
}
local hops = cmd.ProxyJumps
assertEqual(#hops, 2)
assertEqual(hops[1].host, "jump2.host")
assertEqual(hops[1].user, "jumpuser")
assertEqual(hops[2].host, "jump1.host")
end
function tests.test_cli_hop_override()
resetHosts()
yoshi.hosts["ganymede"] = function() return yoshi.ssh{HostName="192.168.50.2"} end
yoshi.hosts["io"] = function() return yoshi.ssh{HostName="192.168.50.3", ProxyJump="ganymede"} end
local arg = {"collin@ganymede/io"}
local target = yoshi.getHost("io")
local path = {"collin@ganymede"} -- simulate split_path(arg[1])
local acc = {}
for i = 1, #path do
local hop = yoshi.parse_user_host_port(path[i])
yoshi.collect_proxy_jumps(hop, acc)
end
target.ProxyJumps = acc
assertEqual(#target.ProxyJumps, 1)
assertEqual(target.ProxyJumps[1].user, "collin")
assertEqual(target.ProxyJumps[1].host, "192.168.50.2")
end
function tests.test_toCommand()
resetHosts()
local cmd = yoshi.ssh{
HostName = "localhost",
User = "me",
Port = 2222,
DynamicForward = 8080,
LocalForward = 8000,
SessionType = "none",
RemoteCommand = "ls"
}
local text = cmd:toCommand()
assert(text:match("ssh"))
assert(text:match("-D 8080"))
assert(text:match("-L 8000:127.0.0.1:8000"))
assert(text:match("-N"))
assert(text:match("me@localhost"))
assert(text:match("ls"))
end
function tests.test_LocalForward_tuple()
local cmd = yoshi.ssh{
HostName="localhost",
LocalForward={9000,9001}
}
local text = cmd:toCommand()
assert(text:match("-L 9000:127.0.0.1:9001"))
end
function tests.test_DynamicForward()
local cmd = yoshi.ssh{
HostName="localhost",
DynamicForward=9999
}
local text = cmd:toCommand()
assert(text:match("-D 9999"))
end
function tests.test_RemoteCommand()
local cmd = yoshi.ssh{HostName="localhost", RemoteCommand="echo hello"}
local text = cmd:toCommand()
assert(text:match("echo hello"))
end
----------------------------------------------------------------
-- Run tests
----------------------------------------------------------------
local function main()
tests.test_constructor()
tests.test_proxyjump()
tests.test_nested_proxyjump()
tests.test_cli_hop_override()
tests.test_toCommand()
tests.test_LocalForward_tuple()
tests.test_DynamicForward()
tests.test_RemoteCommand ()
end
|