@@ -230,47 +230,53 @@ public void TestCreateNoWindowProperty(bool value)
230
230
public void TestUserCredentialsPropertiesOnWindows ( )
231
231
{
232
232
string username = "test" , password = "PassWord123!!" ;
233
-
234
- if ( Interop . NetUserAdd ( username , password ) )
233
+ try
234
+ {
235
+ Interop . NetUserAdd ( username , password ) ;
236
+ }
237
+ catch ( Exception exc )
235
238
{
236
- Process p = CreateProcessInfinite ( ) ;
239
+ Console . Error . WriteLine ( "TestUserCredentialsPropertiesOnWindows: NetUserAdd failed: {0}" , exc . Message ) ;
240
+ return ; // test is irrelevant if we can't add a user
241
+ }
242
+
243
+ Process p = CreateProcessInfinite ( ) ;
237
244
238
- p . StartInfo . LoadUserProfile = true ;
239
- p . StartInfo . UserName = username ;
240
- p . StartInfo . Password = GetSecureString ( password ) ;
245
+ p . StartInfo . LoadUserProfile = true ;
246
+ p . StartInfo . UserName = username ;
247
+ p . StartInfo . Password = GetSecureString ( password ) ;
241
248
242
- SafeProcessHandle handle = null ;
243
- try
249
+ SafeProcessHandle handle = null ;
250
+ try
251
+ {
252
+ p . Start ( ) ;
253
+ if ( Interop . OpenProcessToken ( p . SafeHandle , 0x8u , out handle ) )
244
254
{
245
- p . Start ( ) ;
246
- if ( Interop . OpenProcessToken ( p . SafeHandle , 0x8u , out handle ) )
255
+ SecurityIdentifier sid ;
256
+ if ( Interop . ProcessTokenToSid ( handle , out sid ) )
247
257
{
248
- SecurityIdentifier sid ;
249
- if ( Interop . ProcessTokenToSid ( handle , out sid ) )
250
- {
251
- string actualUserName = sid . Translate ( typeof ( NTAccount ) ) . ToString ( ) ;
252
- int indexOfDomain = actualUserName . IndexOf ( '\\ ' ) ;
253
- if ( indexOfDomain != - 1 )
254
- actualUserName = actualUserName . Substring ( indexOfDomain + 1 ) ;
255
-
256
- bool isProfileLoaded = GetNamesOfUserProfiles ( ) . Any ( profile => profile . Equals ( username ) ) ;
257
-
258
- Assert . Equal ( username , actualUserName ) ;
259
- Assert . True ( isProfileLoaded ) ;
260
- }
258
+ string actualUserName = sid . Translate ( typeof ( NTAccount ) ) . ToString ( ) ;
259
+ int indexOfDomain = actualUserName . IndexOf ( '\\ ' ) ;
260
+ if ( indexOfDomain != - 1 )
261
+ actualUserName = actualUserName . Substring ( indexOfDomain + 1 ) ;
262
+
263
+ bool isProfileLoaded = GetNamesOfUserProfiles ( ) . Any ( profile => profile . Equals ( username ) ) ;
264
+
265
+ Assert . Equal ( username , actualUserName ) ;
266
+ Assert . True ( isProfileLoaded ) ;
261
267
}
262
268
}
263
- finally
264
- {
265
- if ( handle != null )
266
- handle . Dispose ( ) ;
269
+ }
270
+ finally
271
+ {
272
+ if ( handle != null )
273
+ handle . Dispose ( ) ;
267
274
268
- if ( ! p . HasExited )
269
- p . Kill ( ) ;
275
+ if ( ! p . HasExited )
276
+ p . Kill ( ) ;
270
277
271
- Interop . NetUserDel ( null , username ) ;
272
- Assert . True ( p . WaitForExit ( WaitInMS ) ) ;
273
- }
278
+ Interop . NetUserDel ( null , username ) ;
279
+ Assert . True ( p . WaitForExit ( WaitInMS ) ) ;
274
280
}
275
281
}
276
282
0 commit comments