/
ftpserver_test.rb
244 lines (191 loc) · 8.03 KB
/
ftpserver_test.rb
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#! /usr/bin/env rspec
ENV["Y2DIR"] = File.expand_path("../../src", __FILE__)
require "yast"
# stub module to prevent its Import
# Useful for modules from different yast packages, to avoid build dependencies
def stub_module(name)
Yast.const_set name.to_sym, Class.new { def self.fake_method; end }
end
stub_module("Users")
Yast.import "FtpServer"
VS_CONFIG_PATH = Yast::Path.new(".vsftpd")
PURE_CONFIG_PATH = Yast::Path.new(".pure-ftpd")
def mock_config(config_path, map)
allow(Yast::SCR).to receive(:Dir).with(config_path).and_return(map.keys)
map.each_pair do |key, value|
allow(Yast::SCR).to receive(:Read).with(config_path + key).and_return(value)
end
end
VS_SETTINGS = {
"write_enable" => "NO",
"dirmessage_enable" => "YES",
"nopriv_user" => "ftpsecure",
"local_enable" => "YES",
"anonymous_enable" => "YES",
"anon_world_readable_only" => "YES",
"syslog_enable" => "YES",
"connect_from_port_20" => "YES",
"ascii_upload_enable" => "YES",
"pam_service_name" => "vsftpd",
"listen" => "NO",
"listen_ipv6" => "YES",
"ssl_enable" => "NO",
"pasv_min_port" => "30000",
"pasv_max_port" => "30100"
}
PURE_SETTINGS = {
"AllowAnonymousFXP" => "no",
"AllowDotFiles" => "yes",
"AllowUserFXP" => "no",
"AnonymousCanCreateDirs" => "no",
"AnonymousCantUpload" => "yes",
"AnonymousOnly" => "yes",
"AntiWarez" => "yes",
"AutoRename" => "yes",
"BrokenClientsCompatibility" => "no",
"ChrootEveryone" => "yes",
"CustomerProof" => "yes",
"Daemonize" => "no",
"DisplayDotFiles" => "yes",
"DontResolve" => "yes",
"LimitRecursion" => "10000 8",
"MaxClientsNumber" => "10",
"MaxClientsPerIP" => "3",
"MaxDiskUsage" => "99",
"MaxIdleTime" => "15",
"MaxLoad" => "4",
"MinUID" => "40",
"NoAnonymous" => "no",
"NoRename" => "yes",
"PAMAuthentication" => "yes",
"PassivePortRange" => "30000 30100",
"ProhibitDotFilesRead" => "no",
"ProhibitDotFilesWrite" => "yes",
"SyslogFacility" => "ftp",
"VerboseLog" => "no"
}
describe "Yast::FtpServer" do
describe ".Modified" do
it "returns false if no modification happens" do
expect(Yast::FtpServer.Modified).to eq false
end
end
describe ".GetPassivePortRangeBoundaries" do
it "can read boundaries when separated by colon" do
Yast::FtpServer.PURE_SETTINGS = { "PassivePortRange" => "1024:4201" }
expected_boundaries = ["1024", "4201"]
expect(Yast::FtpServer.GetPassivePortRangeBoundaries).to eq expected_boundaries
end
it "can read boundaries when separated by whitespace" do
Yast::FtpServer.PURE_SETTINGS = { "PassivePortRange" => "1024 \t 4201" }
expected_boundaries = ["1024", "4201"]
expect(Yast::FtpServer.GetPassivePortRangeBoundaries).to eq expected_boundaries
end
it "returns nil if boundaries is spearated by invalid delimeter" do
Yast::FtpServer.PURE_SETTINGS = { "PassivePortRange" => "1024::4201" }
expect(Yast::FtpServer.GetPassivePortRangeBoundaries).to eq nil
end
end
describe ".ValueUI" do
context "'VerboseLogging' when getting vsftpd settings" do
before do
Yast::FtpServer.vsftpd_edit = true
end
it "returns default value 'YES' if 'log_ftp_protocol' missing in config file" do
mock_config(VS_CONFIG_PATH, VS_SETTINGS)
Yast::FtpServer.ReadVSFTPDSettings()
expect(Yast::FtpServer.ValueUI("VerboseLogging", false)).to eql "YES"
end
it "returns value from config file 'NO' if 'log_ftp_protocol = NO' in config file" do
mock_config(VS_CONFIG_PATH, VS_SETTINGS.merge("log_ftp_protocol" => "NO"))
Yast::FtpServer.ReadVSFTPDSettings()
expect(Yast::FtpServer.ValueUI("VerboseLogging", false)).to eql "NO"
end
it "returns value from config file 'YES' if 'log_ftp_protocol = YES' in config file" do
mock_config(VS_CONFIG_PATH, VS_SETTINGS.merge("log_ftp_protocol" => "YES"))
Yast::FtpServer.ReadVSFTPDSettings()
expect(Yast::FtpServer.ValueUI("VerboseLogging", false)).to eql "YES"
end
end
context "'VerboseLogging' when getting pure-ftpd settings" do
before do
Yast::FtpServer.vsftpd_edit = false
end
it "returns default value 'NO' if config file empty" do
mock_config(PURE_CONFIG_PATH, {})
Yast::FtpServer.ReadPUREFTPDSettings()
expect(Yast::FtpServer.ValueUI("VerboseLogging", false)).to eql "NO"
end
it "returns value from config file 'NO' if 'VerboseLog = no' in config file" do
mock_config(PURE_CONFIG_PATH, PURE_SETTINGS)
Yast::FtpServer.ReadPUREFTPDSettings()
expect(Yast::FtpServer.ValueUI("VerboseLogging", false)).to eql "NO"
end
it "returns value from config file 'YES' if 'VerboseLog = yes' in config file" do
mock_config(PURE_CONFIG_PATH, PURE_SETTINGS.merge("VerboseLog" => "YES"))
Yast::FtpServer.ReadPUREFTPDSettings()
expect(Yast::FtpServer.ValueUI("VerboseLogging", false)).to eql "YES"
end
end
context "using vsftpd" do
before do
Yast::FtpServer.vsftpd_edit = true
# Init values to a known (and invalid) state
mock_config(VS_CONFIG_PATH,
VS_SETTINGS.merge("listen" => "", "listen_ipv6" => ""))
end
context "configured to use xinetd" do
before do
Yast::FtpServer.EDIT_SETTINGS["StartDaemon"] = "2"
Yast::FtpServer.EDIT_SETTINGS["StartXinetd"] = "YES"
end
it "disables vsftpd's standalone mode when writing StartXinetd" do
allow(Yast::Service).to receive(:Enabled).and_return false
Yast::FtpServer.ValueUI("StartXinetd", true)
expect(Yast::FtpServer.VS_SETTINGS["listen"]).to eq("NO")
expect(Yast::FtpServer.VS_SETTINGS["listen_ipv6"]).to eq("NO")
end
it "disables vsftpd service when writing StartXinetd" do
allow(Yast::Service).to receive(:Enabled).with("pure-ftpd").and_return false
allow(Yast::Service).to receive(:Enabled).with("vsftpd").and_return true
expect(Yast::Service).to receive(:Disable).with("vsftpd")
Yast::FtpServer.ValueUI("StartXinetd", true)
end
end
context "configured to run at boot" do
before do
Yast::FtpServer.EDIT_SETTINGS["StartDaemon"] = "1"
end
it "enables vsftpd's standalone mode when writing StartXinetd" do
allow(Yast::Service).to receive(:Disable)
allow(Yast::Service).to receive(:Enable)
Yast::FtpServer.ValueUI("StartXinetd", true)
expect(Yast::FtpServer.VS_SETTINGS["listen"]).to eq("YES")
expect(Yast::FtpServer.VS_SETTINGS["listen_ipv6"]).to be_nil
end
it "configures ftp services when writing StartXinetd" do
expect(Yast::Service).to receive(:Disable).with("pure-ftpd")
expect(Yast::Service).to receive(:Enable).with("vsftpd")
Yast::FtpServer.ValueUI("StartXinetd", true)
end
end
context "configured to run manually" do
before do
Yast::FtpServer.EDIT_SETTINGS["StartDaemon"] = "0"
end
it "enables vsftpd's standalone mode when writing StartXinetd" do
allow(Yast::Service).to receive(:Disable)
allow(Yast::Service).to receive(:Enable)
Yast::FtpServer.ValueUI("StartXinetd", true)
expect(Yast::FtpServer.VS_SETTINGS["listen"]).to eq("YES")
expect(Yast::FtpServer.VS_SETTINGS["listen_ipv6"]).to be_nil
end
it "disables ftp services when writing StartXinetd" do
expect(Yast::Service).to receive(:Disable).with("pure-ftpd")
expect(Yast::Service).to receive(:Disable).with("vsftpd")
Yast::FtpServer.ValueUI("StartXinetd", true)
end
end
end
end
end