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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
#
# Author:: Adam Edwards (<adamed@chef.io>)
# Copyright:: Copyright (c) 2015 Chef Software, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
require "spec_helper"
require "chef/util/windows/logon_session"
describe ::Chef::Util::Windows::LogonSession do
before do
stub_const("Chef::ReservedNames::Win32::API::Security", Class.new)
stub_const("Chef::ReservedNames::Win32::API::Security::LOGON32_LOGON_NEW_CREDENTIALS", 314)
stub_const("Chef::ReservedNames::Win32::API::Security::LOGON32_PROVIDER_DEFAULT", 159)
stub_const("Chef::ReservedNames::Win32::API::System", Class.new )
end
let(:session) { ::Chef::Util::Windows::LogonSession.new(session_user, password, session_domain, authentication) }
let(:authentication) { :remote }
shared_examples_for "it received syntactically invalid credentials" do
it "does not raises an exception when it is initialized" do
expect { session }.to raise_error(ArgumentError)
end
end
shared_examples_for "it received an incorrect username and password combination" do
before do
expect(Chef::ReservedNames::Win32::API::Security).to receive(:LogonUserW).and_return(false)
end
it "raises a Chef::Exceptions::Win32APIError exception when the open method is called" do
expect { session.open }.to raise_error(Chef::Exceptions::Win32APIError)
expect(session).not_to receive(:close)
expect(Chef::ReservedNames::Win32::API::System).not_to receive(:CloseHandle)
end
end
shared_examples_for "it received valid credentials" do
it "does not raise an exception when the open method is called" do
expect(Chef::ReservedNames::Win32::API::Security).to receive(:LogonUserW).and_return(true)
expect { session.open }.not_to raise_error
end
end
shared_examples_for "the session is not open" do
it "does not raise an exception when #open is called" do
expect(Chef::ReservedNames::Win32::API::Security).to receive(:LogonUserW).and_return(true)
expect { session.open }.not_to raise_error
end
it "raises an exception if #close is called" do
expect { session.close }.to raise_error(RuntimeError)
end
it "raises an exception if #restore_user_context is called" do
expect { session.restore_user_context }.to raise_error(RuntimeError)
end
end
shared_examples_for "the session is open" do
before do
allow(Chef::ReservedNames::Win32::API::System).to receive(:CloseHandle)
end
it "does not result in an exception when #restore_user_context is called" do
expect { session.restore_user_context }.not_to raise_error
end
it "does not result in an exception when #close is called" do
expect { session.close }.not_to raise_error
end
it "does close the operating system handle when #close is called" do
expect(Chef::ReservedNames::Win32::API::System).not_to receive(:CloseHandle)
expect { session.restore_user_context }.not_to raise_error
end
end
context "when the session is initialized with a nil user" do
context "when the password, and domain are all nil" do
let(:session_user) { nil }
let(:session_domain) { nil }
let(:password) { nil }
it_behaves_like "it received syntactically invalid credentials"
end
context "when the password is non-nil password, and the domain is nil" do
let(:session_user) { nil }
let(:password) { "ponies" }
let(:session_domain) { nil }
it_behaves_like "it received syntactically invalid credentials"
end
context "when the password is nil and the domain is non-nil" do
let(:session_user) { nil }
let(:password) { nil }
let(:session_domain) { "fairyland" }
it_behaves_like "it received syntactically invalid credentials"
end
context "when the password and domain are non-nil" do
let(:session_user) { nil }
let(:password) { "ponies" }
let(:session_domain) { "fairyland" }
it_behaves_like "it received syntactically invalid credentials"
end
end
context "when the session is initialized with a valid user" do
let(:session_user) { "chalena" }
context "when the password is nil" do
let(:password) { nil }
context "when the domain is non-nil" do
let(:session_domain) { "fairyland" }
it_behaves_like "it received syntactically invalid credentials"
end
context "when the domain is nil" do
context "when the domain is non-nil" do
let(:session_domain) { nil }
it_behaves_like "it received syntactically invalid credentials"
end
end
end
context "when a syntactically valid username and password are supplied" do
context "when the password is non-nil and the domain is nil" do
let(:password) { "ponies" }
let(:session_domain) { nil }
it "does not raise an exception if it is initialized with a non-nil username, non-nil password, and a nil domain" do
expect { session }.not_to raise_error
end
it_behaves_like "it received valid credentials"
it_behaves_like "it received an incorrect username and password combination"
end
context "when the password and domain are non-nil" do
let(:password) { "ponies" }
let(:session_domain) { "fairyland" }
it "does not raise an exception if it is initialized with a non-nil username, non-nil password, and non-nil domain" do
expect { session }.not_to raise_error
end
it_behaves_like "it received valid credentials"
it_behaves_like "it received an incorrect username and password combination"
end
context "when the #open method has not been called" do
let(:password) { "ponies" }
let(:session_domain) { "fairyland" }
it_behaves_like "the session is not open"
end
context "when the session was opened" do
let(:password) { "ponies" }
let(:session_domain) { "fairyland" }
before do
expect(Chef::ReservedNames::Win32::API::Security).to receive(:LogonUserW).and_return(true)
expect { session.open }.not_to raise_error
end
it "raises an exception if #open is called" do
expect { session.open }.to raise_error(RuntimeError)
end
context "when the session was opened and then closed with the #close method" do
before do
expect(Chef::ReservedNames::Win32::API::System).to receive(:CloseHandle)
expect { session.close }.not_to raise_error
end
it_behaves_like "the session is not open"
end
it "can be closed and close the operating system handle" do
expect(Chef::ReservedNames::Win32::API::System).to receive(:CloseHandle)
expect { session.close }.not_to raise_error
end
it "can impersonate the user" do
expect(Chef::ReservedNames::Win32::API::Security).to receive(:ImpersonateLoggedOnUser).and_return(true)
expect { session.set_user_context }.not_to raise_error
end
context "when #set_user_context fails due to low resources causing a failure to impersonate" do
before do
expect(Chef::ReservedNames::Win32::API::Security).to receive(:ImpersonateLoggedOnUser).and_return(false)
end
it "raises an exception when #set_user_context fails because impersonation failed" do
expect { session.set_user_context }.to raise_error(Chef::Exceptions::Win32APIError)
end
context "when calling subsequent methods" do
before do
expect { session.set_user_context }.to raise_error(Chef::Exceptions::Win32APIError)
expect(Chef::ReservedNames::Win32::API::Security).not_to receive(:RevertToSelf)
end
it_behaves_like "the session is open"
end
end
context "when #set_user_context successfully impersonates the user" do
before do
expect(Chef::ReservedNames::Win32::API::Security).to receive(:ImpersonateLoggedOnUser).and_return(true)
expect { session.set_user_context }.not_to raise_error
end
context "when attempting to impersonate while already impersonating" do
it "raises an error if the #set_user_context is called again" do
expect { session.set_user_context }.to raise_error(RuntimeError)
end
end
describe "the impersonation will be reverted" do
before do
expect(Chef::ReservedNames::Win32::API::Security).to receive(:RevertToSelf).and_return(true)
end
it_behaves_like "the session is open"
end
context "when the attempt to revert impersonation fails" do
before do
expect(Chef::ReservedNames::Win32::API::Security).to receive(:RevertToSelf).and_return(false)
end
it "raises an exception when #restore_user_context is called" do
expect { session.restore_user_context }.to raise_error(Chef::Exceptions::Win32APIError)
end
it "raises an exception when #close is called and impersonation fails" do
expect { session.close }.to raise_error(Chef::Exceptions::Win32APIError)
end
context "when calling methods after revert fails in #restore_user_context" do
before do
expect { session.restore_user_context }.to raise_error(Chef::Exceptions::Win32APIError)
end
context "when revert continues to fail" do
before do
expect(Chef::ReservedNames::Win32::API::Security).to receive(:RevertToSelf).and_return(false)
end
it "raises an exception when #close is called and impersonation fails" do
expect { session.close }.to raise_error(Chef::Exceptions::Win32APIError)
end
end
context "when revert stops failing and succeeds" do
before do
expect(Chef::ReservedNames::Win32::API::Security).to receive(:RevertToSelf).and_return(true)
end
it "does not raise an exception when #restore_user_context is called" do
expect { session.restore_user_context }.not_to raise_error
end
it "does not raise an exception when #close is called" do
expect(Chef::ReservedNames::Win32::API::System).to receive(:CloseHandle)
expect { session.close }.not_to raise_error
end
end
end
end
end
end
end
end
end
|