win32-service 0.7.2-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,61 @@
1
+ #########################################################################
2
+ # test_win32_daemon.rb
3
+ #
4
+ # Test suite for the Win32::Daemon class. You should run this test via
5
+ # the 'rake test' or 'rake test_daemon' tasks.
6
+ #
7
+ # These tests are rather limited, since the acid test is to install
8
+ # your daemon as a service and see how it behaves.
9
+ #########################################################################
10
+ require 'rubygems'
11
+ gem 'test-unit'
12
+
13
+ require 'win32/daemon'
14
+ require 'test/unit'
15
+ include Win32
16
+
17
+ class TC_Daemon < Test::Unit::TestCase
18
+ def setup
19
+ @daemon = Daemon.new
20
+ end
21
+
22
+ test "version number is set properly" do
23
+ assert_equal('0.7.2', Daemon::VERSION)
24
+ end
25
+
26
+ test "constructor basic functionality" do
27
+ assert_respond_to(Daemon, :new)
28
+ assert_nothing_raised{ Daemon.new }
29
+ end
30
+
31
+ test "constructor does not accept any arguments" do
32
+ assert_raises(ArgumentError){ Daemon.new(1) }
33
+ end
34
+
35
+ test "mainloop basic functionality" do
36
+ assert_respond_to(@daemon, :mainloop)
37
+ end
38
+
39
+ test "state basic functionality" do
40
+ assert_respond_to(@daemon, :state)
41
+ end
42
+
43
+ test "is_running basic functionality" do
44
+ assert_respond_to(@daemon, :running?)
45
+ end
46
+
47
+ test "expected constants are defined" do
48
+ assert_not_nil(Daemon::CONTINUE_PENDING)
49
+ assert_not_nil(Daemon::PAUSE_PENDING)
50
+ assert_not_nil(Daemon::PAUSED)
51
+ assert_not_nil(Daemon::RUNNING)
52
+ assert_not_nil(Daemon::START_PENDING)
53
+ assert_not_nil(Daemon::STOP_PENDING)
54
+ assert_not_nil(Daemon::STOPPED)
55
+ assert_not_nil(Daemon::IDLE)
56
+ end
57
+
58
+ def teardown
59
+ @daemon = nil
60
+ end
61
+ end
@@ -0,0 +1,409 @@
1
+ ##########################################################################
2
+ # test_win32_service.rb
3
+ #
4
+ # Tests for the Win32::Service class.
5
+ ##########################################################################
6
+ require 'rubygems'
7
+ gem 'test-unit'
8
+
9
+ require 'win32/service'
10
+ require 'socket'
11
+ require 'test/unit'
12
+
13
+ class TC_Win32_Service < Test::Unit::TestCase
14
+ def self.startup
15
+ @@host = Socket.gethostname
16
+ @@service_name = 'stisvc'
17
+ end
18
+
19
+ def setup
20
+ @display_name = 'Windows Image Acquisition (WIA)'
21
+ @service_name = 'stisvc'
22
+ @service_stat = nil
23
+ @services = []
24
+ end
25
+
26
+ def start_service(service)
27
+ status = Win32::Service.status(@service_name).current_state
28
+ if status == 'paused'
29
+ Win32::Service.resume(service)
30
+ else
31
+ unless ['running', 'start pending'].include?(status)
32
+ Win32::Service.start(service)
33
+ end
34
+ end
35
+ wait_for_status('running')
36
+ end
37
+
38
+ def stop_service(service)
39
+ status = Win32::Service.status(@service_name).current_state
40
+ unless ['stopped', 'stop pending'].include?(status)
41
+ Win32::Service.stop(service)
42
+ end
43
+ wait_for_status('stopped')
44
+ end
45
+
46
+ # Helper method that waits for a status to change its state since state
47
+ # changes aren't usually instantaneous.
48
+ def wait_for_status(status)
49
+ sleep 0.1 while Win32::Service.status(@service_name).current_state != status
50
+ end
51
+
52
+ test "version number is expected value" do
53
+ assert_equal('0.7.2', Win32::Service::VERSION)
54
+ end
55
+
56
+ test "services basic functionality" do
57
+ assert_respond_to(Win32::Service, :services)
58
+ assert_nothing_raised{ Win32::Service.services }
59
+ assert_nothing_raised{ Win32::Service.services(nil) }
60
+ assert_nothing_raised{ Win32::Service.services(nil, 'network') }
61
+ end
62
+
63
+ test "services method returns an array without a block" do
64
+ assert_nothing_raised{ @services = Win32::Service.services }
65
+ assert_kind_of(Array, @services)
66
+ assert_kind_of(Struct::ServiceInfo, @services[0])
67
+ end
68
+
69
+ test "services method yields service objects when a block is provided" do
70
+ assert_nothing_raised{ Win32::Service.services{ |s| @services << s } }
71
+ assert_kind_of(Array, @services)
72
+ assert_kind_of(Struct::ServiceInfo, @services[0])
73
+ end
74
+
75
+ test "the host argument must be a string or an error is raised" do
76
+ assert_raise(TypeError){ Win32::Service.services(1) }
77
+ end
78
+
79
+ test "the group argument must be a string or an error is raised" do
80
+ assert_raise(TypeError){ Win32::Service.services(nil, 1) }
81
+ end
82
+
83
+ test "the services method only accepts 2 arguments" do
84
+ assert_raise(ArgumentError){ Win32::Service.services(nil, 'network', 1) }
85
+ end
86
+
87
+ test "a valid hostname must be provided or an error is raised" do
88
+ assert_raise(Win32::Service::Error){ Win32::Service.services('bogus') }
89
+ end
90
+
91
+ test "delete method basic functionality" do
92
+ assert_respond_to(Win32::Service, :delete)
93
+ end
94
+
95
+ test "a service name must be provided to the delete method" do
96
+ assert_raise(ArgumentError){ Win32::Service.delete }
97
+ end
98
+
99
+ test "delete method raises an error if a bogus service name is provided" do
100
+ assert_raise(Win32::Service::Error){ Win32::Service.delete('bogus') }
101
+ end
102
+
103
+ test "delete method raises an error if a bogus host name is provided" do
104
+ assert_raise(Win32::Service::Error){ Win32::Service.delete('bogus', 'bogus') }
105
+ end
106
+
107
+ test "delete method only accepts up to two arguments" do
108
+ assert_raise(ArgumentError){ Win32::Service.delete('x', 'y', 'z') }
109
+ end
110
+
111
+ test "pause basic functionality" do
112
+ assert_respond_to(Win32::Service, :pause)
113
+ end
114
+
115
+ test "resume basic functionality" do
116
+ assert_respond_to(Win32::Service, :resume)
117
+ end
118
+
119
+ test "pause and resume work as expected" do
120
+ start_service(@service_name)
121
+
122
+ assert_nothing_raised{ Win32::Service.pause(@service_name) }
123
+ wait_for_status('paused')
124
+
125
+ assert_nothing_raised{ Win32::Service.resume(@service_name) }
126
+ wait_for_status('running')
127
+ end
128
+
129
+ test "pausing an already paused service is harmless" do
130
+ start_service(@service_name)
131
+
132
+ assert_nothing_raised{ Win32::Service.pause(@service_name) }
133
+ wait_for_status('paused')
134
+ assert_nothing_raised{ Win32::Service.pause(@service_name) }
135
+ end
136
+
137
+ test "pause requires a service name as an argument" do
138
+ assert_raise(ArgumentError){ Win32::Service.pause }
139
+ end
140
+
141
+ test "pausing an unrecognized service name raises an error" do
142
+ assert_raise(Win32::Service::Error){ Win32::Service.pause('bogus') }
143
+ end
144
+
145
+ test "pausing a service on an unrecognized host raises an error" do
146
+ assert_raise(Win32::Service::Error){ Win32::Service.pause('W32Time', 'bogus') }
147
+ end
148
+
149
+ test "pause method accepts a maximum of two arguments" do
150
+ assert_raise(ArgumentError){ Win32::Service.pause('x', 'y', 'z') }
151
+ end
152
+
153
+ test "resume method requires a service name" do
154
+ assert_raise(ArgumentError){ Win32::Service.resume }
155
+ end
156
+
157
+ test "resume method with an unrecognized service name raises an error" do
158
+ assert_raise(Win32::Service::Error){ Win32::Service.resume('bogus') }
159
+ end
160
+
161
+ test "resume method with an unrecognized host name raises an error" do
162
+ assert_raise(Win32::Service::Error){ Win32::Service.resume('W32Time', 'bogus') }
163
+ end
164
+
165
+ test "resume method accepts a maximum of two arguments" do
166
+ assert_raise(ArgumentError){ Win32::Service.resume('W32Time', @@host, true) }
167
+ end
168
+
169
+ test "stop method basic functionality" do
170
+ assert_respond_to(Win32::Service, :stop)
171
+ end
172
+
173
+ test "start method basic functionality" do
174
+ assert_respond_to(Win32::Service, :start)
175
+ end
176
+
177
+ test "stop and start methods work as expected" do
178
+ start_service(@service_name)
179
+
180
+ assert_nothing_raised{ Win32::Service.stop(@service_name) }
181
+ wait_for_status('stopped')
182
+
183
+ assert_nothing_raised{ Win32::Service.start(@service_name) }
184
+ wait_for_status('running')
185
+ end
186
+
187
+ test "attempting to stop a stopped service raises an error" do
188
+ start_service(@service_name)
189
+
190
+ assert_nothing_raised{ Win32::Service.stop(@service_name) }
191
+ wait_for_status('stopped')
192
+ assert_raise(Win32::Service::Error){ Win32::Service.stop(@service_name) }
193
+
194
+ assert_nothing_raised{ Win32::Service.start(@service_name) }
195
+ end
196
+
197
+ test "stop method requires a service name" do
198
+ assert_raise(ArgumentError){ Win32::Service.stop }
199
+ end
200
+
201
+ test "stop method raises an error if the service name is unrecognized" do
202
+ assert_raise(Win32::Service::Error){ Win32::Service.stop('bogus') }
203
+ end
204
+
205
+ test "stop method raises an error if the host is unrecognized" do
206
+ assert_raise(Win32::Service::Error){ Win32::Service.stop('W32Time', 'bogus') }
207
+ end
208
+
209
+ test "stop metho accepts a maximum of two arguments" do
210
+ assert_raise(ArgumentError){ Win32::Service.stop('W32Time', @@host, true) }
211
+ end
212
+
213
+ test "start method requires a service name" do
214
+ assert_raise(ArgumentError){ Win32::Service.start }
215
+ end
216
+
217
+ test "attempting to start a running service raises an error" do
218
+ assert_raise(Win32::Service::Error){ Win32::Service.start(@service_name) }
219
+ end
220
+
221
+ test "attempting to start an unrecognized service raises an error" do
222
+ assert_raise(Win32::Service::Error){ Win32::Service.start('bogus') }
223
+ end
224
+
225
+ test "attempting to start a service on an unknown host raises an error" do
226
+ assert_raise(Win32::Service::Error){ Win32::Service.start('bogus', 'bogus') }
227
+ end
228
+
229
+ test "stop requires at least one argument" do
230
+ assert_raise(ArgumentError){ Win32::Service.stop }
231
+ end
232
+
233
+ test "stop raises an error with an unrecognized service name" do
234
+ assert_raise(Win32::Service::Error){ Win32::Service.stop('bogus') }
235
+ end
236
+
237
+ test "stop raises an error with an unrecognized host" do
238
+ assert_raise(Win32::Service::Error){ Win32::Service.stop('W32Time', 'bogus') }
239
+ end
240
+
241
+ test "stop accepts a maximum of 2 arguments" do
242
+ assert_raise(ArgumentError){ Win32::Service.stop('a', 'b', 'c') }
243
+ end
244
+
245
+ test "status basic functionality" do
246
+ assert_respond_to(Win32::Service, :status)
247
+ assert_nothing_raised{ Win32::Service.status(@service_name) }
248
+ assert_kind_of(Struct::ServiceStatus, Win32::Service.status(@service_name))
249
+ end
250
+
251
+ test "get_service_name basic functionality" do
252
+ assert_respond_to(Win32::Service, :get_service_name)
253
+ assert_nothing_raised{ Win32::Service.get_service_name(@display_name) }
254
+ assert_kind_of(String, Win32::Service.get_service_name(@display_name))
255
+ end
256
+
257
+ test "get_service_name returns expected results" do
258
+ assert_equal(@service_name, Win32::Service.get_service_name(@display_name))
259
+ end
260
+
261
+ test "getservicename is an alias for get_service_name" do
262
+ assert_respond_to(Win32::Service, :getservicename)
263
+ assert_alias_method(Win32::Service, :getservicename, :get_service_name)
264
+ end
265
+
266
+ test "get_service_name requires at least one argument" do
267
+ assert_raise(ArgumentError){ Win32::Service.get_service_name }
268
+ end
269
+
270
+ test "get_service_name raises an error if a bogus service name is provided" do
271
+ assert_raise(Win32::Service::Error){ Win32::Service.get_service_name('bogus') }
272
+ end
273
+
274
+ test "get_service_name raises an error if a bogus host is provided" do
275
+ assert_raise(Win32::Service::Error){ Win32::Service.get_service_name('foo', 'bogus') }
276
+ end
277
+
278
+ test "get_service_name accepts a maximum of two arguments" do
279
+ assert_raise(ArgumentError){ Win32::Service.get_service_name('x', 'y', 'z') }
280
+ end
281
+
282
+ test "get_display_name basic functionality" do
283
+ assert_respond_to(Win32::Service, :get_display_name)
284
+ assert_nothing_raised{ Win32::Service.get_display_name(@service_name) }
285
+ assert_kind_of(String, Win32::Service.get_display_name(@service_name))
286
+ end
287
+
288
+ test "get_display_name returns expected results" do
289
+ assert_equal(@display_name, Win32::Service.get_display_name(@service_name))
290
+ end
291
+
292
+ test "getdisplayname is an alias for get_display_name" do
293
+ assert_respond_to(Win32::Service, :getdisplayname)
294
+ assert_alias_method(Win32::Service, :getdisplayname, :get_display_name)
295
+ end
296
+
297
+ test "get_display_name requires at least one argument" do
298
+ assert_raise(ArgumentError){ Win32::Service.get_display_name }
299
+ end
300
+
301
+ test "get_display_name raises an error if the service does not exist" do
302
+ assert_raise(Win32::Service::Error){ Win32::Service.get_display_name('bogus') }
303
+ end
304
+
305
+ test "get_display_name raises an error if a bad host name is provided" do
306
+ assert_raise(Win32::Service::Error){ Win32::Service.get_display_name('W32Time', 'bogus') }
307
+ end
308
+
309
+ test "get_display_name takes a maximum of two arguments" do
310
+ assert_raise(ArgumentError){ Win32::Service.get_display_name('x', 'y', 'z') }
311
+ end
312
+
313
+ test "exists method basic functionality" do
314
+ assert_respond_to(Win32::Service, :exists?)
315
+ assert_nothing_raised{ Win32::Service.exists?('W32Time') }
316
+ end
317
+
318
+ test "exists method returns expected results" do
319
+ assert_true(Win32::Service.exists?('W32Time'))
320
+ assert_false(Win32::Service.exists?('foobar'))
321
+ end
322
+
323
+ test "exists method requires at least one argument or an error is raised" do
324
+ assert_raises(ArgumentError){ Win32::Service.exists? }
325
+ end
326
+
327
+ test "exists method raises an error if a bogus host is passed" do
328
+ assert_raises(Win32::Service::Error){Win32::Service.exists?('foo', 'bogushost') }
329
+ end
330
+
331
+ test "exists method only accepts up to two arguments" do
332
+ assert_raises(ArgumentError){ Win32::Service.exists?('foo', 'bar', 'baz') }
333
+ end
334
+
335
+ test "scm security constants are defined" do
336
+ assert_not_nil(Win32::Service::MANAGER_ALL_ACCESS)
337
+ assert_not_nil(Win32::Service::MANAGER_CREATE_SERVICE)
338
+ assert_not_nil(Win32::Service::MANAGER_CONNECT)
339
+ assert_not_nil(Win32::Service::MANAGER_ENUMERATE_SERVICE)
340
+ assert_not_nil(Win32::Service::MANAGER_LOCK)
341
+ assert_not_nil(Win32::Service::MANAGER_QUERY_LOCK_STATUS)
342
+ end
343
+
344
+ test "service specific constants are defined" do
345
+ assert_not_nil(Win32::Service::ALL_ACCESS)
346
+ assert_not_nil(Win32::Service::CHANGE_CONFIG)
347
+ assert_not_nil(Win32::Service::ENUMERATE_DEPENDENTS)
348
+ assert_not_nil(Win32::Service::INTERROGATE)
349
+ assert_not_nil(Win32::Service::PAUSE_CONTINUE)
350
+ assert_not_nil(Win32::Service::QUERY_CONFIG)
351
+ assert_not_nil(Win32::Service::QUERY_STATUS)
352
+ assert_not_nil(Win32::Service::STOP)
353
+ assert_not_nil(Win32::Service::START)
354
+ assert_not_nil(Win32::Service::USER_DEFINED_CONTROL)
355
+ end
356
+
357
+ test "service type constants are defined" do
358
+ assert_not_nil(Win32::Service::FILE_SYSTEM_DRIVER)
359
+ assert_not_nil(Win32::Service::KERNEL_DRIVER)
360
+ assert_not_nil(Win32::Service::WIN32_OWN_PROCESS)
361
+ assert_not_nil(Win32::Service::WIN32_SHARE_PROCESS)
362
+ assert_not_nil(Win32::Service::INTERACTIVE_PROCESS)
363
+ end
364
+
365
+ test "service start option constants are defined" do
366
+ assert_not_nil(Win32::Service::AUTO_START)
367
+ assert_not_nil(Win32::Service::BOOT_START)
368
+ assert_not_nil(Win32::Service::DEMAND_START)
369
+ assert_not_nil(Win32::Service::DISABLED)
370
+ assert_not_nil(Win32::Service::SYSTEM_START)
371
+ end
372
+
373
+ test "service error control constants are defined" do
374
+ assert_not_nil(Win32::Service::ERROR_IGNORE)
375
+ assert_not_nil(Win32::Service::ERROR_NORMAL)
376
+ assert_not_nil(Win32::Service::ERROR_SEVERE)
377
+ assert_not_nil(Win32::Service::ERROR_CRITICAL)
378
+ end
379
+
380
+ test "service state constants are defined" do
381
+ assert_not_nil(Win32::Service::CONTINUE_PENDING)
382
+ assert_not_nil(Win32::Service::PAUSE_PENDING)
383
+ assert_not_nil(Win32::Service::PAUSED)
384
+ assert_not_nil(Win32::Service::RUNNING)
385
+ assert_not_nil(Win32::Service::START_PENDING)
386
+ assert_not_nil(Win32::Service::STOP_PENDING)
387
+ assert_not_nil(Win32::Service::STOPPED)
388
+ end
389
+
390
+ def teardown
391
+ @display_name = nil
392
+ @service_name = nil
393
+ @service_stat = nil
394
+ @services = nil
395
+ end
396
+
397
+ def self.shutdown
398
+ @@host = nil
399
+ status = Win32::Service.status(@@service_name).current_state
400
+
401
+ if status == 'paused'
402
+ Win32::Service.resume(@@service_name)
403
+ end
404
+
405
+ unless ['running', 'start pending'].include?(status)
406
+ Win32::Service.start(@@service_name)
407
+ end
408
+ end
409
+ end