Fix obviously wrong condition in an "if" statement.
[wine] / dlls / advapi32 / tests / security.c
1 /*
2  * Unit tests for security functions
3  *
4  * Copyright (c) 2004 Mike McCormack
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <stdio.h>
22
23 #include "wine/test.h"
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winerror.h"
27 #include "aclapi.h"
28 #include "winnt.h"
29
30 typedef BOOL (WINAPI *fnConvertSidToStringSidA)( PSID pSid, LPSTR *str );
31 typedef BOOL (WINAPI *fnConvertStringSidToSidA)( LPCSTR str, PSID pSid );
32
33 fnConvertSidToStringSidA pConvertSidToStringSidA;
34 fnConvertStringSidToSidA pConvertStringSidToSidA;
35
36 struct sidRef
37 {
38     SID_IDENTIFIER_AUTHORITY auth;
39     const char *refStr;
40 };
41
42 void test_sid()
43 {
44     struct sidRef refs[] = {
45      { { {0x00,0x00,0x33,0x44,0x55,0x66} }, "S-1-860116326-1" },
46      { { {0x00,0x00,0x01,0x02,0x03,0x04} }, "S-1-16909060-1"  },
47      { { {0x00,0x00,0x00,0x01,0x02,0x03} }, "S-1-66051-1"     },
48      { { {0x00,0x00,0x00,0x00,0x01,0x02} }, "S-1-258-1"       },
49      { { {0x00,0x00,0x00,0x00,0x00,0x02} }, "S-1-2-1"         },
50      { { {0x00,0x00,0x00,0x00,0x00,0x0c} }, "S-1-12-1"        },
51     };
52     const char noSubAuthStr[] = "S-1-5";
53     HMODULE hmod = GetModuleHandle("advapi32.dll");
54     unsigned int i;
55     PSID psid = NULL;
56     BOOL r;
57     LPSTR str = NULL;
58
59     pConvertSidToStringSidA = (fnConvertSidToStringSidA)
60                     GetProcAddress( hmod, "ConvertSidToStringSidA" );
61     if( !pConvertSidToStringSidA )
62         return;
63     pConvertStringSidToSidA = (fnConvertStringSidToSidA)
64                     GetProcAddress( hmod, "ConvertStringSidToSidA" );
65     if( !pConvertStringSidToSidA )
66         return;
67
68     r = pConvertStringSidToSidA( NULL, NULL );
69     ok( !r, "expected failure with NULL parameters\n" );
70     if( GetLastError() == ERROR_CALL_NOT_IMPLEMENTED )
71         return;
72     ok( GetLastError() == ERROR_INVALID_PARAMETER,
73      "expected GetLastError() is ERROR_INVALID_PARAMETER, got %ld\n",
74      GetLastError() );
75
76     r = pConvertStringSidToSidA( refs[0].refStr, NULL );
77     ok( !r && GetLastError() == ERROR_INVALID_PARAMETER,
78      "expected GetLastError() is ERROR_INVALID_PARAMETER, got %ld\n",
79      GetLastError() );
80
81     r = pConvertStringSidToSidA( NULL, &str );
82     ok( !r && GetLastError() == ERROR_INVALID_PARAMETER,
83      "expected GetLastError() is ERROR_INVALID_PARAMETER, got %ld\n",
84      GetLastError() );
85
86     r = pConvertStringSidToSidA( noSubAuthStr, &psid );
87     ok( !r,
88      "expected failure with no sub authorities\n" );
89     ok( GetLastError() == ERROR_INVALID_SID,
90      "expected GetLastError() is ERROR_INVALID_SID, got %ld\n",
91      GetLastError() );
92
93     for( i = 0; i < sizeof(refs) / sizeof(refs[0]); i++ )
94     {
95         PISID pisid;
96
97         r = AllocateAndInitializeSid( &refs[i].auth, 1,1,0,0,0,0,0,0,0,
98          &psid );
99         ok( r, "failed to allocate sid\n" );
100         r = pConvertSidToStringSidA( psid, &str );
101         ok( r, "failed to convert sid\n" );
102         ok( !strcmp( str, refs[i].refStr ),
103          "incorrect sid, expected %s, got %s\n", refs[i].refStr, str );
104         if( str )
105             LocalFree( str );
106         if( psid )
107             FreeSid( psid );
108
109         r = pConvertStringSidToSidA( refs[i].refStr, &psid );
110         ok( r, "failed to parse sid string\n" );
111         pisid = (PISID)psid;
112         ok( pisid &&
113          !memcmp( pisid->IdentifierAuthority.Value, refs[i].auth.Value,
114          sizeof(refs[i].auth) ),
115          "string sid %s didn't parse to expected value\n"
116          "(got 0x%04x%08lx, expected 0x%04x%08lx)\n",
117          refs[i].refStr,
118          MAKEWORD( pisid->IdentifierAuthority.Value[1],
119          pisid->IdentifierAuthority.Value[0] ),
120          MAKELONG( MAKEWORD( pisid->IdentifierAuthority.Value[5],
121          pisid->IdentifierAuthority.Value[4] ),
122          MAKEWORD( pisid->IdentifierAuthority.Value[3],
123          pisid->IdentifierAuthority.Value[2] ) ),
124          MAKEWORD( refs[i].auth.Value[1], refs[i].auth.Value[0] ),
125          MAKELONG( MAKEWORD( refs[i].auth.Value[5], refs[i].auth.Value[4] ),
126          MAKEWORD( refs[i].auth.Value[3], refs[i].auth.Value[2] ) ) );
127         if( psid )
128             LocalFree( psid );
129     }
130 }
131
132 void test_trustee()
133 {
134     TRUSTEE trustee;
135     PSID psid;
136     DWORD r;
137     LPSTR str = "2jjj";
138
139     SID_IDENTIFIER_AUTHORITY auth = { {0x11,0x22,0,0,0, 0} };
140
141     r = AllocateAndInitializeSid( &auth, 1, 42, 0,0,0,0,0,0,0,&psid );
142     ok( r, "failed to init SID\n" );
143
144     memset( &trustee, 0xff, sizeof trustee );
145     BuildTrusteeWithSidA( &trustee, psid );
146
147     ok( trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
148     ok( trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, 
149         "MultipleTrusteeOperation wrong\n");
150     ok( trustee.TrusteeForm == TRUSTEE_IS_SID, "TrusteeForm wrong\n");
151     ok( trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
152     ok( trustee.ptstrName == (LPSTR) psid, "ptstrName wrong\n" );
153     FreeSid( psid );
154
155     /* test BuildTrusteeWithNameA */
156     memset( &trustee, 0xff, sizeof trustee );
157     BuildTrusteeWithNameA( &trustee, str );
158
159     ok( trustee.pMultipleTrustee == NULL, "pMultipleTrustee wrong\n");
160     ok( trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE, 
161         "MultipleTrusteeOperation wrong\n");
162     ok( trustee.TrusteeForm == TRUSTEE_IS_NAME, "TrusteeForm wrong\n");
163     ok( trustee.TrusteeType == TRUSTEE_IS_UNKNOWN, "TrusteeType wrong\n");
164     ok( trustee.ptstrName == str, "ptstrName wrong\n" );
165 }
166  
167 START_TEST(security)
168 {
169     test_sid();
170     test_trustee();
171 }