source: box/trunk/test/win32/testlibwin32.cpp @ 710

Revision 710, 8.0 KB checked in by chris, 6 years ago (diff)
  • merge
  • This is my current patch queue. I think that all of these are safe to apply. This is just under half of the pending changes in chris/general (the easy half).
  • Property svn:eol-style set to native
Line 
1// win32test.cpp : Defines the entry point for the console application.
2//
3
4//#include <windows.h>
5#include "Box.h"
6
7#ifdef WIN32
8
9#include <assert.h>
10#include <AccCtrl.h>
11#include <Aclapi.h>
12
13#include "../../bin/bbackupd/BackupDaemon.h"
14#include "BoxPortsAndFiles.h"
15#include "emu.h"
16
17int main(int argc, char* argv[])
18{
19        // ACL tests
20        char* exename = getenv("WINDIR");
21
22        PSID psidOwner;
23        PSID psidGroup;
24        PACL pDacl;
25        PSECURITY_DESCRIPTOR pSecurityDesc;
26
27        DWORD result = GetNamedSecurityInfo(
28                exename, // pObjectName
29                SE_FILE_OBJECT, // ObjectType
30                DACL_SECURITY_INFORMATION | // SecurityInfo
31                GROUP_SECURITY_INFORMATION |
32                OWNER_SECURITY_INFORMATION,
33                &psidOwner, // ppsidOwner,
34                &psidGroup, // ppsidGroup,
35                &pDacl,     // ppDacl,
36                NULL,       // ppSacl,
37                &pSecurityDesc // ppSecurityDescriptor
38        );
39        if (result != ERROR_SUCCESS)
40        {
41                printf("Error getting security info for '%s': error %d",
42                        exename, result);
43        }
44        assert(result == ERROR_SUCCESS);
45
46        char namebuf[1024];
47        char domainbuf[1024];
48        SID_NAME_USE nametype;
49        DWORD namelen = sizeof(namebuf);
50        DWORD domainlen = sizeof(domainbuf);
51
52        assert(LookupAccountSid(NULL, psidOwner, namebuf, &namelen,
53                domainbuf, &domainlen, &nametype));
54
55        printf("Owner:\n");
56        printf("User name:   %s\n", namebuf);
57        printf("Domain name: %s\n", domainbuf);
58        printf("Name type:   %d\n", nametype);
59        printf("\n");
60
61        namelen = sizeof(namebuf);
62        domainlen = sizeof(domainbuf);
63
64        assert(LookupAccountSid(NULL, psidGroup, namebuf, &namelen,
65                domainbuf, &domainlen, &nametype));
66
67        printf("Group:\n");
68        printf("User name:   %s\n", namebuf);
69        printf("Domain name: %s\n", domainbuf);
70        printf("Name type:   %d\n", nametype);
71        printf("\n");
72
73        ULONG numEntries;
74        PEXPLICIT_ACCESS pEntries;
75        result = GetExplicitEntriesFromAcl
76        (
77                pDacl,       // pAcl
78                &numEntries, // pcCountOfExplicitEntries,
79                &pEntries    // pListOfExplicitEntries
80        );
81        assert(result == ERROR_SUCCESS);
82
83        printf("Found %lu explicit DACL entries for '%s'\n\n",
84                (unsigned long)numEntries, exename);
85
86        for (ULONG i = 0; i < numEntries; i++)
87        {
88                EXPLICIT_ACCESS* pEntry = &(pEntries[i]);
89                printf("DACL entry %lu:\n", (unsigned long)i);
90
91                DWORD perms = pEntry->grfAccessPermissions;
92                printf("  Access permissions: ", perms);
93
94                #define PRINT_PERM(name) \
95                if (perms & name) \
96                { \
97                        printf(#name " "); \
98                        perms &= ~name; \
99                }
100
101                PRINT_PERM(FILE_ADD_FILE);
102                PRINT_PERM(FILE_ADD_SUBDIRECTORY);
103                PRINT_PERM(FILE_ALL_ACCESS);
104                PRINT_PERM(FILE_APPEND_DATA);
105                PRINT_PERM(FILE_CREATE_PIPE_INSTANCE);
106                PRINT_PERM(FILE_DELETE_CHILD);
107                PRINT_PERM(FILE_EXECUTE);
108                PRINT_PERM(FILE_LIST_DIRECTORY);
109                PRINT_PERM(FILE_READ_ATTRIBUTES);
110                PRINT_PERM(FILE_READ_DATA);
111                PRINT_PERM(FILE_READ_EA);
112                PRINT_PERM(FILE_TRAVERSE);
113                PRINT_PERM(FILE_WRITE_ATTRIBUTES);
114                PRINT_PERM(FILE_WRITE_DATA);
115                PRINT_PERM(FILE_WRITE_EA);
116                PRINT_PERM(STANDARD_RIGHTS_READ);
117                PRINT_PERM(STANDARD_RIGHTS_WRITE);
118                PRINT_PERM(SYNCHRONIZE);
119                PRINT_PERM(DELETE);
120                PRINT_PERM(READ_CONTROL);
121                PRINT_PERM(WRITE_DAC);
122                PRINT_PERM(WRITE_OWNER);
123                PRINT_PERM(MAXIMUM_ALLOWED);
124                PRINT_PERM(GENERIC_ALL);
125                PRINT_PERM(GENERIC_EXECUTE);
126                PRINT_PERM(GENERIC_WRITE);
127                PRINT_PERM(GENERIC_READ);
128                printf("\n");
129
130                if (perms)
131                {
132                        printf("  Bits left over: %08x\n", perms);
133                }
134                assert(!perms);
135
136                printf("  Access mode: ");
137                switch(pEntry->grfAccessMode)
138                {
139                case NOT_USED_ACCESS:
140                        printf("NOT_USED_ACCESS\n"); break;
141                case GRANT_ACCESS:
142                        printf("GRANT_ACCESS\n"); break;
143                case DENY_ACCESS:
144                        printf("DENY_ACCESS\n"); break;
145                case REVOKE_ACCESS:
146                        printf("REVOKE_ACCESS\n"); break;
147                case SET_AUDIT_SUCCESS:
148                        printf("SET_AUDIT_SUCCESS\n"); break;
149                case SET_AUDIT_FAILURE:
150                        printf("SET_AUDIT_FAILURE\n"); break;
151                default:
152                        printf("Unknown (%08x)\n", pEntry->grfAccessMode);
153                }
154
155                printf("  Trustee: ");
156                assert(pEntry->Trustee.pMultipleTrustee == NULL);
157                assert(pEntry->Trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE);
158                switch(pEntry->Trustee.TrusteeForm)
159                {
160                case TRUSTEE_IS_SID:
161                        {
162                                PSID trusteeSid = (PSID)(pEntry->Trustee.ptstrName);
163
164                                namelen = sizeof(namebuf);
165                                domainlen = sizeof(domainbuf);
166
167                                assert(LookupAccountSid(NULL, trusteeSid, namebuf, &namelen,
168                                        domainbuf, &domainlen, &nametype));
169
170                                printf("SID of %s\\%s (%d)\n", domainbuf, namebuf, nametype);
171                        }
172                        break;
173                case TRUSTEE_IS_NAME:
174                        printf("Name\n"); break;
175                case TRUSTEE_BAD_FORM:
176                        printf("Bad form\n"); assert(0);
177                case TRUSTEE_IS_OBJECTS_AND_SID:
178                        printf("Objects and SID\n"); break;
179                case TRUSTEE_IS_OBJECTS_AND_NAME:
180                        printf("Objects and name\n"); break;
181                default:
182                        printf("Unknown form\n"); assert(0);
183                }
184
185                printf("  Trustee type: ");
186                switch(pEntry->Trustee.TrusteeType)
187                {
188                case TRUSTEE_IS_UNKNOWN:
189                        printf("Unknown type.\n"); break;
190                case TRUSTEE_IS_USER:
191                        printf("User\n"); break;
192                case TRUSTEE_IS_GROUP:
193                        printf("Group\n"); break;
194                case TRUSTEE_IS_DOMAIN:
195                        printf("Domain\n"); break;
196                case TRUSTEE_IS_ALIAS:
197                        printf("Alias\n"); break;
198                case TRUSTEE_IS_WELL_KNOWN_GROUP:
199                        printf("Well-known group\n"); break;
200                case TRUSTEE_IS_DELETED:
201                        printf("Deleted account\n"); break;
202                case TRUSTEE_IS_INVALID:
203                        printf("Invalid trustee type\n"); break;
204                case TRUSTEE_IS_COMPUTER:
205                        printf("Computer\n"); break;
206                default:
207                        printf("Unknown type %d\n", pEntry->Trustee.TrusteeType); 
208                        assert(0);
209                }
210
211                printf("\n");
212        }
213
214        assert(LocalFree((HLOCAL)pEntries) == 0);
215        assert(LocalFree((HLOCAL)pSecurityDesc) == 0);
216
217        chdir("c:\\tmp");
218        openfile("test", O_CREAT, 0);
219        struct stat ourfs;
220        //test our opendir, readdir and closedir
221        //functions
222        DIR *ourDir = opendir("C:");
223
224        if ( ourDir != NULL )
225        {
226                struct dirent *info;
227                do
228                {
229                        info = readdir(ourDir);
230                        if (info) printf("File/Dir name is : %s\r\n", info->d_name);
231                }
232                while (info != NULL);
233
234                closedir(ourDir);
235        }
236       
237        std::string diry("C:\\Projects\\boxbuild\\testfiles\\");
238        ourDir = opendir(diry.c_str());
239        if ( ourDir != NULL )
240        {
241                struct dirent *info;
242                do
243                {
244                        info = readdir(ourDir);
245                        if (info == NULL) break;
246                        std::string file(diry + info->d_name);
247                        stat(file.c_str(), &ourfs);
248                        if (info) printf("File/Dir name is : %s\r\n", info->d_name);
249                }
250                while ( info != NULL );
251
252                closedir(ourDir);
253
254        }
255
256        stat("c:\\windows", &ourfs);
257        stat("c:\\autoexec.bat", &ourfs);
258        printf("Finished dir read\n");
259
260        //test our getopt function
261        char * test_argv[] = 
262        {
263                "foobar.exe",
264                "-qwc",
265                "-",
266                "-c",
267                "fgfgfg",
268                "-f",
269                "-l",
270                "hello",
271                "-",
272                "force-sync",
273                NULL
274        };
275        int test_argc;
276        for (test_argc = 0; test_argv[test_argc]; test_argc++) { }
277        const char* opts = "qwc:l:";
278
279        assert(getopt(test_argc, test_argv, opts) == 'q');
280        assert(getopt(test_argc, test_argv, opts) == 'w');
281        assert(getopt(test_argc, test_argv, opts) == 'c');
282        assert(strcmp(optarg, "-") == 0);
283        assert(getopt(test_argc, test_argv, opts) == 'c');
284        assert(strcmp(optarg, "fgfgfg") == 0);
285        assert(getopt(test_argc, test_argv, opts) == '?');
286        assert(optopt == 'f');
287        assert(getopt(test_argc, test_argv, opts) == 'l');
288        assert(strcmp(optarg, "hello") == 0);
289        assert(getopt(test_argc, test_argv, opts) == -1);
290        // assert(optopt == 0); // no more options
291        assert(strcmp(test_argv[optind], "-") == 0);
292        assert(strcmp(test_argv[optind+1], "force-sync") == 0);
293        //end of getopt test
294       
295        //now test our statfs funct
296        stat("c:\\cert.cer", &ourfs);
297
298        char *timee;
299       
300        timee = ctime(&ourfs.st_mtime);
301
302        if (S_ISREG(ourfs.st_mode))
303        {
304                printf("is a normal file\n");
305        }
306        else
307        {
308                printf("is a directory?\n");
309                exit(1);
310        }
311
312        lstat(getenv("WINDIR"), &ourfs);
313
314        if ( S_ISDIR(ourfs.st_mode))
315        {
316                printf("is a directory\n");
317        }
318        else
319        {
320                printf("is a file?\n");
321                exit(1);
322        }
323
324        //test the syslog functions
325        openlog("Box Backup", 0,0);
326        //the old ones are the best...
327        syslog(LOG_ERR, "Hello World");
328        syslog(LOG_ERR, "Value of int is: %i", 6);
329
330        closelog();
331
332        /*
333        //first off get the path name for the default
334        char buf[MAX_PATH];
335       
336        GetModuleFileName(NULL, buf, sizeof(buf));
337        std::string buffer(buf);
338        std::string conf("-c " + buffer.substr(0,(buffer.find("win32test.exe"))) + "bbackupd.conf");
339        //std::string conf( "-c " + buffer.substr(0,(buffer.find("bbackupd.exe"))) + "bbackupd.conf");
340        */
341
342        return 0;
343}
344
345#endif // WIN32
Note: See TracBrowser for help on using the repository browser.