net-smb 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/test/test_net_smb.rb CHANGED
@@ -1,21 +1,326 @@
1
+ ## vim:fileencoding=utf-8
2
+ ## -*- encoding: utf-8 -*-
3
+
1
4
  require 'net/smb'
2
5
  require 'test/unit'
6
+ require 'etc'
3
7
 
4
8
  module Net
5
9
 
6
10
  class SMBTest < Test::Unit::TestCase
7
11
  def setup
12
+ @test_dir = ENV['TEST_DIR'] = "#{Dir.getwd}/test"
13
+ @username = Etc.getpwuid(Process.uid)['name']
14
+ @password = 'password'
15
+
16
+ @smb_conf = ENV['TEST_SMB_CONF'] ||= @test_dir + "/etc/smb.conf"
17
+ @smbd = ENV['TEST_SMBD'] ||= "smbd"
18
+ @pdbedit = ENV['TEST_PDBEDIT'] ||= "pdbedit"
19
+ @smbstatus = ENV['TEST_SMBSTATUS'] ||= "smbstatus"
20
+ @samba_debug_level = ENV['TEST_SAMBA_DEBUGLEVEL'] ||= "10"
21
+ @samba_log_dir = ENV['TEST_SAMBA_LOG_DIR'] ||= @test_dir + "/log"
22
+ @samba_var_dir = ENV['TEST_SAMBA_VAR_DIR'] ||= @samba_log_dir + "/var"
23
+ @share_dir = ENV['TEST_SHARE_DIR'] ||= @samba_log_dir + "/share"
24
+
25
+ @share_private = "smb://localhost/private"
26
+ @share_public = "smb://localhost/public"
27
+ @dir_noexist = "dir.noexist"
28
+ @dir_writeable = "dir.writeable"
29
+ @dir_writeable_m = "ディレクトリ.writeable"
30
+ @dirs_writeable = [@dir_writeable, @dir_writeable_m]
31
+ @dir_readable = "dir.readable"
32
+ @dirs_readable = [@dir_readable]
33
+ @dir_noaccess = "dir.noaccess"
34
+ @dirs_noaccess = [@dir_noaccess]
35
+ @dirs = @dirs_readable + @dirs_writeable + @dirs_noaccess
36
+ @file_noexist = "file.noexist"
37
+ @file_writeable = "file.writeable"
38
+ @file_writeable_m = "ファイル.writeable"
39
+ @files_writeable = [@file_writeable, @file_writeable_m]
40
+ @file_readable = "file.readable"
41
+ @files_readable = [@file_readable]
42
+ @file_noaccess = "file.noaccess"
43
+ @files_noaccess = [@file_noaccess]
44
+ @file_large = "file.large"
45
+ @files_misc = [@file_large]
46
+ @files = @files_readable + @files_writeable + @files_noaccess + @files_misc
47
+
48
+ ENV['SMB_CONF_PATH'] = nil;
49
+ ENV['LIBSMB_PROG'] ||= @test_dir + "/bin/smbd.wrapper"
50
+
51
+ ## Rotate log directory
52
+ if File.exist?(@samba_log_dir + '.9')
53
+ system('/bin/rm', '-rf', @samba_log_dir + '.9');
54
+ end
55
+ if File.exist?(@samba_log_dir)
56
+ File.rename(@samba_log_dir, @samba_log_dir + '.0')
57
+ end
58
+ 9.downto(1) do |i|
59
+ logdir_a = @samba_log_dir + '.' + (i-1).to_s
60
+ logdir_b = @samba_log_dir + '.' + i.to_s
61
+ if File.exist?(logdir_a)
62
+ File.rename(logdir_a, logdir_b)
63
+ end
64
+ end
65
+
66
+ Dir.mkdir(@samba_log_dir, 0750)
67
+ Dir.mkdir(@samba_var_dir, 0750)
68
+ Dir.mkdir(@share_dir, 0750)
69
+ @dirs_readable.each do |dname|
70
+ Dir.mkdir(@share_dir + '/' + dname, 0550)
71
+ end
72
+ @dirs_writeable.each do |dname|
73
+ Dir.mkdir(@share_dir + '/' + dname, 0750)
74
+ end
75
+ @dirs_noaccess.each do |dname|
76
+ Dir.mkdir(@share_dir + '/' + dname, 0000)
77
+ end
78
+ @files_readable.each do |fname|
79
+ File.open(@share_dir + '/' + fname, "wb", 0440) do |file|
80
+ file.write(fname)
81
+ end
82
+ end
83
+ @files_writeable.each do |fname|
84
+ File.open(@share_dir + '/' + fname, "wb", 0660) do |file|
85
+ file.write(fname)
86
+ end
87
+ end
88
+ @files_noaccess.each do |fname|
89
+ File.open(@share_dir + '/' + fname, "wb", 0000) do |file|
90
+ file.write(fname)
91
+ end
92
+ end
93
+
94
+ File.open(@share_dir + '/' + @file_large, "wb", 0660) do |file|
95
+ random_chars = (0...100).map { rand(256).chr }.join("")
96
+ 100000.times do |n|
97
+ file.write(random_chars)
98
+ end
99
+ end
100
+
101
+ pdbedit_r, pdbedit_w = IO.pipe
102
+ pdbedit_pid = Kernel.spawn(
103
+ @pdbedit, "--configfile", @smb_conf, "--create", "--password-from-stdin", @username,
104
+ :in => pdbedit_r,
105
+ [:out, :err] => [@samba_log_dir + '/pdbedit.log', 'w'],
106
+ )
107
+ pdbedit_r.close
108
+ pdbedit_w.print(@password, "\n")
109
+ pdbedit_w.print(@password, "\n")
110
+ pdbedit_w.close
8
111
  end
9
112
 
10
113
  def teardown
11
114
  end
12
115
 
13
- def test_all
14
- smbcctx = Net::SMBCCTX.new
15
- p smbcctx.debug
16
- smbcctx.debug = 5
17
- p smbcctx.debug
116
+ def smbstatus
117
+ smbstatus_r, smbstatus_w = IO.pipe
118
+ smbstatus_pid = Kernel.spawn(
119
+ @smbstatus, "--configfile", @smb_conf, "--shares",
120
+ :out => smbstatus_w,
121
+ :err => [@samba_log_dir + '/smbstatus.log', 'w+'],
122
+ )
123
+ smbstatus_w.close
124
+ smbstatus_r.readline
125
+ smbstatus_r.readline
126
+ smbstatus_r.readline
127
+
128
+ return smbstatus_r
18
129
  end
130
+
131
+ def test_auth
132
+ smb = Net::SMB.new
133
+ smb.on_auth {|server, share|
134
+ [@username, @password]
135
+ }
136
+ assert_nothing_raised do
137
+ smbdir = smb.opendir(@share_private)
138
+ smbdir.close
139
+ end
140
+
141
+ smb = Net::SMB.new
142
+ smb.on_auth {|server, share|
143
+ [@username, 'invalid-password']
144
+ }
145
+ assert_raise(Errno::EPERM) do
146
+ smb.opendir(@share_private)
147
+ end
148
+
149
+ smb = Net::SMB.new
150
+ smb.on_auth {|server, share|
151
+ ['invalid-user', @password]
152
+ }
153
+ assert_raise(Errno::EACCES) do
154
+ smb.opendir(@share_private)
155
+ end
156
+
157
+ smb = Net::SMB.new
158
+ smb.on_auth {|server, share|
159
+ 'blah-blah'
160
+ }
161
+ assert_raise(TypeError) do
162
+ smb.opendir(@share_private)
163
+ end
164
+
165
+ smb = Net::SMB.new
166
+ smb.on_auth {|server, share|
167
+ [@username]
168
+ }
169
+ assert_raise(ArgumentError) do
170
+ smb.opendir(@share_private)
171
+ end
172
+ end
173
+
174
+ def test_dir_open_close
175
+ smb = Net::SMB.new
176
+ smb.on_auth {|server, share|
177
+ [@username, @password]
178
+ }
179
+
180
+ smbdir = smb.opendir(@share_public)
181
+ assert_equal(smb.object_id, smbdir.smb.object_id)
182
+ assert_equal(@share_public, smbdir.url)
183
+ smbdir.close
184
+ assert_raise(IOError) do
185
+ smbdir.close
186
+ end
187
+
188
+ assert_raise(Errno::ENOENT) do
189
+ smbdir = smb.opendir(@share_public + '/' + @dir_noexist)
190
+ end
191
+ assert_raise(Errno::EACCES) do
192
+ smbdir = smb.opendir(@share_public + '/' + @dir_noaccess)
193
+ end
194
+ ## Errno::ENOENT is not expected, but Samba 3.5 and 3.6 has a bug:
195
+ ## https://bugzilla.samba.org/show_bug.cgi?id=9021
196
+ assert_raise(Errno::ENOTDIR, Errno::ENOENT) do
197
+ smbdir = smb.opendir(@share_public + '/' + @file_writeable)
198
+ end
199
+ end ## test_dir_open_close
200
+
201
+ def test_dir_read
202
+ dents_all = [".", "..", *@dirs, *@files]
203
+
204
+ smb = Net::SMB.new
205
+ smb.on_auth {|server, share|
206
+ [@username, @password]
207
+ }
208
+
209
+ smbdir = smb.opendir(@share_private)
210
+ smbdir_pos = [smbdir.pos]
211
+ dents = dents_all.clone
212
+ while fname = smbdir.read
213
+ smbdir_pos << smbdir.pos
214
+ assert_equal(fname, dents.delete(fname), "Unexpected directory entry: #{fname}")
215
+ end
216
+ assert_empty(dents)
217
+
218
+ smbdir.rewind
219
+ assert_equal(smbdir_pos.shift, smbdir.tell)
220
+ dents = dents_all.clone
221
+ while fname = smbdir.read
222
+ assert_equal(smbdir_pos.shift, smbdir.tell)
223
+ assert_equal(fname, dents.delete(fname), "Unexpected directory entry: #{fname}")
224
+ end
225
+ assert_empty(dents)
226
+
227
+ smbdir.close
228
+
229
+ smb.opendir(@share_public) do |smbdir|
230
+ dents = dents_all.clone
231
+ while fname = smbdir.read
232
+ assert_equal(fname, dents.delete(fname), "Unexpected directory entry: #{fname}")
233
+ end
234
+ assert_empty(dents)
235
+ end
236
+
237
+ smbdir = smb.opendir(@share_private)
238
+ dents = dents_all.clone
239
+ smbdir.each do |fname|
240
+ assert(dents.delete(fname) != nil)
241
+ end
242
+ assert_empty(dents)
243
+
244
+ smbdir = smb.opendir(@share_private)
245
+ dents = dents_all.clone
246
+ smbdir_enum = smbdir.each
247
+ dents.size.times do |n|
248
+ fname = smbdir_enum.next
249
+ assert_equal(fname, dents.delete(fname), "Unexpected directory entry: #{fname}")
250
+ end
251
+ assert_raise(StopIteration) do
252
+ smbdir_enum.next
253
+ end
254
+ assert_empty(dents)
255
+ end ## test_dir_read
256
+
257
+ def test_file_open_read_close
258
+ smb = Net::SMB.new
259
+ smb.on_auth {|server, share|
260
+ [@username, @password]
261
+ }
262
+
263
+ @files_readable.each do |filename|
264
+ url = @share_public + '/' + filename
265
+ smbfile = smb.open(url)
266
+
267
+ assert_equal(url, smbfile.url)
268
+
269
+ assert_raise(ArgumentError) do
270
+ smbfile.read(-1)
271
+ end
272
+
273
+ assert_equal(@file_readable, smbfile.read)
274
+
275
+ smbfile.close
276
+
277
+ assert_raise(IOError) do
278
+ smbfile.close
279
+ end
280
+ end
281
+ end ## test_file_open_read_close
282
+
283
+ def test_file_read
284
+ smb = Net::SMB.new
285
+ smb.on_auth {|server, share|
286
+ [@username, @password]
287
+ }
288
+
289
+ file = File.open(@share_dir + '/' + @file_large)
290
+ smbfile = smb.open(@share_public + '/' + @file_large)
291
+
292
+ buffer_size = 8192
293
+ 1.upto(buffer_size * 4) do |read_size|
294
+ file.rewind
295
+ smbfile.rewind
296
+ (buffer_size / read_size * 3).times do |n|
297
+ file_data = file.read(read_size)
298
+ smbfile_data = smbfile.read(read_size)
299
+ assert_equal(file_data, smbfile_data, "read_size #{read_size}, n=#{n}")
300
+ end
301
+ end
302
+
303
+ smbfile.close
304
+ end ## test_file_read
305
+
306
+ def test_file_read_eof
307
+ smb = Net::SMB.new
308
+ smb.on_auth {|server, share|
309
+ [@username, @password]
310
+ }
311
+
312
+ smbfile = smb.open(@share_public + '/' + @file_readable)
313
+
314
+ assert(smbfile.eof? != true)
315
+ smbfile.read
316
+ assert(smbfile.eof? == true)
317
+
318
+ assert_equal("", smbfile.read)
319
+ assert_equal("", smbfile.read(0))
320
+ assert_nil(smbfile.read(1))
321
+
322
+ smbfile.close
323
+ end ## test_file_read_eof
19
324
  end
20
325
 
21
326
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: net-smb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,9 +9,9 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-02 00:00:00.000000000 Z
12
+ date: 2012-07-13 00:00:00.000000000 Z
13
13
  dependencies: []
14
- description: SMB/CIFS client
14
+ description: SMB/CIFS client (Samba libsmbclient binding)
15
15
  email:
16
16
  - fumiyas@osstech.co.jp
17
17
  executables: []
@@ -21,11 +21,19 @@ extra_rdoc_files: []
21
21
  files:
22
22
  - .gitignore
23
23
  - Gemfile
24
+ - Makefile
24
25
  - Rakefile
26
+ - Rakefile.local.example
27
+ - ext/net/smb/dlinklist.h
25
28
  - ext/net/smb/extconf.rb
29
+ - ext/net/smb/rb_smb.h
26
30
  - ext/net/smb/smb.c
31
+ - ext/net/smb/smbdir.c
32
+ - ext/net/smb/smbfile.c
27
33
  - lib/net/smb/version.rb
28
34
  - net-smb.gemspec
35
+ - test/bin/smbd.wrapper
36
+ - test/etc/smb.conf
29
37
  - test/test_net_smb.rb
30
38
  homepage: https://github.com/fumiyas/ruby-net-smb
31
39
  licenses: []
@@ -47,9 +55,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
47
55
  version: '0'
48
56
  requirements: []
49
57
  rubyforge_project:
50
- rubygems_version: 1.8.11
58
+ rubygems_version: 1.8.23
51
59
  signing_key:
52
60
  specification_version: 3
53
- summary: SMB/CIFS client
61
+ summary: SMB/CIFS client (Samba libsmbclient binding)
54
62
  test_files:
63
+ - test/bin/smbd.wrapper
64
+ - test/etc/smb.conf
55
65
  - test/test_net_smb.rb