Fixed some issues found by winapi_check.
[wine] / dlls / netapi32 / tests / access.c
1 /*
2  * Copyright 2002 Andriy Palamarchuk
3  *
4  * Conformance test of the access functions.
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 <wine/test.h>
22 #include <winbase.h>
23 #include <winerror.h>
24 #include <lmaccess.h>
25 #include <lmerr.h>
26 #include <lmapibuf.h>
27
28 WCHAR user_name[UNLEN + 1];
29 WCHAR computer_name[MAX_COMPUTERNAME_LENGTH + 1];
30
31 const WCHAR sAdminUserName[] = {'A','d','m','i','n','i','s','t','r','a','t',
32                                 'o','r',0};
33 const WCHAR sGuestUserName[] = {'G','u','e','s','t',0};
34 const WCHAR sNonexistentUser[] = {'N','o','n','e','x','i','s','t','e','n','t',' ',
35                                 'U','s','e','r',0};
36 const WCHAR sBadNetPath[] = {'\\','\\','B','a',' ',' ','p','a','t','h',0};
37 const WCHAR sInvalidName[] = {'\\',0};
38 const WCHAR sInvalidName2[] = {'\\','\\',0};
39 const WCHAR sEmptyStr[] = { 0 };
40
41
42 void init_access_tests(void)
43 {
44     DWORD dwSize;
45
46     user_name[0] = 0;
47     dwSize = sizeof(user_name);
48     ok(GetUserNameW(user_name, &dwSize), "User Name Retrieved");
49
50     computer_name[0] = 0;
51     dwSize = sizeof(computer_name);
52     ok(GetComputerNameW(computer_name, &dwSize), "Computer Name Retrieved");
53 }
54
55 void run_usergetinfo_tests(void)
56 {
57     PUSER_INFO_0 ui0 = NULL;
58     PUSER_INFO_10 ui10 = NULL;
59     DWORD dwSize;
60
61     /* Level 0 */
62     ok(NetUserGetInfo(NULL, sAdminUserName, 0, (LPBYTE *)&ui0) == NERR_Success,
63        "NetUserGetInfo is successful");
64     ok(!lstrcmpW(sAdminUserName, ui0->usri0_name), "This is really user name");
65     NetApiBufferSize(ui0, &dwSize);
66     ok(dwSize >= (sizeof(USER_INFO_0) +
67                   (lstrlenW(ui0->usri0_name) + 1) * sizeof(WCHAR)),
68        "Is allocated with NetApiBufferAllocate");
69
70     /* Level 10 */
71     ok(NetUserGetInfo(NULL, sAdminUserName, 10, (LPBYTE *)&ui10) == NERR_Success,
72        "NetUserGetInfo is successful");
73     ok(!lstrcmpW(sAdminUserName, ui10->usri10_name), "This is really user name");
74     NetApiBufferSize(ui10, &dwSize);
75     ok(dwSize >= (sizeof(USER_INFO_10) +
76                   (lstrlenW(ui10->usri10_name) + 1 +
77                    lstrlenW(ui10->usri10_comment) + 1 +
78                    lstrlenW(ui10->usri10_usr_comment) + 1 +
79                    lstrlenW(ui10->usri10_full_name) + 1) * sizeof(WCHAR)),
80        "Is allocated with NetApiBufferAllocate");
81
82     NetApiBufferFree(ui0);
83     NetApiBufferFree(ui10);
84
85     /* errors handling */
86     ok(NetUserGetInfo(NULL, sAdminUserName, 10000, (LPBYTE *)&ui0) == ERROR_INVALID_LEVEL,
87        "Invalid Level");
88     ok(NetUserGetInfo(NULL, sNonexistentUser, 0, (LPBYTE *)&ui0) == NERR_UserNotFound,
89        "Invalid User Name");
90     todo_wine {
91         /* FIXME - Currently Wine can't verify whether the network path is good or bad */
92         ok(NetUserGetInfo(sBadNetPath, sAdminUserName, 0, (LPBYTE *)&ui0) == ERROR_BAD_NETPATH,
93            "Bad Network Path");
94     }
95     ok(NetUserGetInfo(sEmptyStr, sAdminUserName, 0, (LPBYTE *)&ui0) == ERROR_BAD_NETPATH,
96        "Bad Network Path");
97     ok(NetUserGetInfo(sInvalidName, sAdminUserName, 0, (LPBYTE *)&ui0) == ERROR_INVALID_NAME,
98        "Invalid Server Name");
99     ok(NetUserGetInfo(sInvalidName2, sAdminUserName, 0, (LPBYTE *)&ui0) == ERROR_INVALID_NAME,
100        "Invalid Server Name");
101 }
102
103 /* checks Level 1 of NetQueryDisplayInformation */
104 void run_querydisplayinformation1_tests(void)
105 {
106     PNET_DISPLAY_USER Buffer, rec;
107     DWORD Result, EntryCount;
108     DWORD i = 0;
109     BOOL hasAdmin = FALSE;
110     BOOL hasGuest = FALSE;
111
112     do
113     {
114         Result = NetQueryDisplayInformation(
115             NULL, 1, i, 1000, MAX_PREFERRED_LENGTH, &EntryCount,
116             (PVOID *)&Buffer);
117
118         ok((Result == ERROR_SUCCESS) || (Result == ERROR_MORE_DATA),
119            "Information Retrieved");
120         rec = Buffer;
121         for(; EntryCount > 0; EntryCount--)
122         {
123             if (!lstrcmpW(rec->usri1_name, sAdminUserName))
124             {
125                 ok(!hasAdmin, "One admin user");
126                 ok(rec->usri1_flags & UF_SCRIPT, "UF_SCRIPT flag is set");
127                 ok(rec->usri1_flags & UF_NORMAL_ACCOUNT, "UF_NORMAL_ACCOUNT flag is set");
128                 hasAdmin = TRUE;
129             }
130             else if (!lstrcmpW(rec->usri1_name, sGuestUserName))
131             {
132                 ok(!hasGuest, "One guest record");
133                 ok(rec->usri1_flags & UF_SCRIPT, "UF_SCRIPT flag is set");
134                 ok(rec->usri1_flags & UF_NORMAL_ACCOUNT, "UF_NORMAL_ACCOUNT flag is set");
135                 hasGuest = TRUE;
136             }
137
138             i = rec->usri1_next_index;
139             rec++;
140         }
141
142         NetApiBufferFree(Buffer);
143     } while (Result == ERROR_MORE_DATA);
144
145     ok(hasAdmin, "Has Administrator account");
146     ok(hasGuest, "Has Guest account");
147 }
148
149 START_TEST(access)
150 {
151     init_access_tests();
152     run_usergetinfo_tests();
153     run_querydisplayinformation1_tests();
154 }