summaryrefslogtreecommitdiff
path: root/test/win32/testlibwin32.cpp
blob: fb735c566db64b99256590fbdc0f8057e80d83f8 (plain)
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// win32test.cpp : Defines the entry point for the console application.
//

//#include <windows.h>
#include "Box.h"

#ifdef WIN32

#include <assert.h>
#include <AccCtrl.h>
#include <Aclapi.h>

#include "../../bin/bbackupd/BackupDaemon.h"
#include "BoxPortsAndFiles.h"
#include "emu.h"

int main(int argc, char* argv[])
{
	// ACL tests
	char* exename = getenv("WINDIR");

	PSID psidOwner;
	PSID psidGroup;
	PACL pDacl;
	PSECURITY_DESCRIPTOR pSecurityDesc;

	DWORD result = GetNamedSecurityInfo(
		exename, // pObjectName
		SE_FILE_OBJECT, // ObjectType
		DACL_SECURITY_INFORMATION | // SecurityInfo
		GROUP_SECURITY_INFORMATION |
		OWNER_SECURITY_INFORMATION,
		&psidOwner, // ppsidOwner,
		&psidGroup, // ppsidGroup,
		&pDacl,     // ppDacl,
		NULL,       // ppSacl,
		&pSecurityDesc // ppSecurityDescriptor
	);
	if (result != ERROR_SUCCESS)
	{
		printf("Error getting security info for '%s': error %d",
			exename, result);
	}
	assert(result == ERROR_SUCCESS);

	char namebuf[1024];
	char domainbuf[1024];
	SID_NAME_USE nametype;
	DWORD namelen = sizeof(namebuf);
	DWORD domainlen = sizeof(domainbuf);

	assert(LookupAccountSid(NULL, psidOwner, namebuf, &namelen,
		domainbuf, &domainlen, &nametype));

	printf("Owner:\n");
	printf("User name:   %s\n", namebuf);
	printf("Domain name: %s\n", domainbuf);
	printf("Name type:   %d\n", nametype);
	printf("\n");

	namelen = sizeof(namebuf);
	domainlen = sizeof(domainbuf);

	assert(LookupAccountSid(NULL, psidGroup, namebuf, &namelen,
		domainbuf, &domainlen, &nametype));

	printf("Group:\n");
	printf("User name:   %s\n", namebuf);
	printf("Domain name: %s\n", domainbuf);
	printf("Name type:   %d\n", nametype);
	printf("\n");

	ULONG numEntries;
	PEXPLICIT_ACCESS pEntries;
	result = GetExplicitEntriesFromAcl
	(
		pDacl,       // pAcl
		&numEntries, // pcCountOfExplicitEntries,
		&pEntries    // pListOfExplicitEntries
	);
	assert(result == ERROR_SUCCESS);

	printf("Found %lu explicit DACL entries for '%s'\n\n",
		(unsigned long)numEntries, exename);

	for (ULONG i = 0; i < numEntries; i++)
	{
		EXPLICIT_ACCESS* pEntry = &(pEntries[i]);
		printf("DACL entry %lu:\n", (unsigned long)i);

		DWORD perms = pEntry->grfAccessPermissions;
		printf("  Access permissions: ", perms);

		#define PRINT_PERM(name) \
		if (perms & name) \
		{ \
			printf(#name " "); \
			perms &= ~name; \
		}

		PRINT_PERM(FILE_ADD_FILE);
		PRINT_PERM(FILE_ADD_SUBDIRECTORY);
		PRINT_PERM(FILE_ALL_ACCESS);
		PRINT_PERM(FILE_APPEND_DATA);
		PRINT_PERM(FILE_CREATE_PIPE_INSTANCE);
		PRINT_PERM(FILE_DELETE_CHILD);
		PRINT_PERM(FILE_EXECUTE);
		PRINT_PERM(FILE_LIST_DIRECTORY);
		PRINT_PERM(FILE_READ_ATTRIBUTES);
		PRINT_PERM(FILE_READ_DATA);
		PRINT_PERM(FILE_READ_EA);
		PRINT_PERM(FILE_TRAVERSE);
		PRINT_PERM(FILE_WRITE_ATTRIBUTES);
		PRINT_PERM(FILE_WRITE_DATA);
		PRINT_PERM(FILE_WRITE_EA);
		PRINT_PERM(STANDARD_RIGHTS_READ);
		PRINT_PERM(STANDARD_RIGHTS_WRITE);
		PRINT_PERM(SYNCHRONIZE);
		PRINT_PERM(DELETE);
		PRINT_PERM(READ_CONTROL);
		PRINT_PERM(WRITE_DAC);
		PRINT_PERM(WRITE_OWNER);
		PRINT_PERM(MAXIMUM_ALLOWED);
		PRINT_PERM(GENERIC_ALL);
		PRINT_PERM(GENERIC_EXECUTE);
		PRINT_PERM(GENERIC_WRITE);
		PRINT_PERM(GENERIC_READ);
		printf("\n");

		if (perms)
		{
			printf("  Bits left over: %08x\n", perms);
		}
		assert(!perms);

		printf("  Access mode: ");
		switch(pEntry->grfAccessMode)
		{
		case NOT_USED_ACCESS:
			printf("NOT_USED_ACCESS\n"); break;
		case GRANT_ACCESS:
			printf("GRANT_ACCESS\n"); break;
		case DENY_ACCESS:
			printf("DENY_ACCESS\n"); break;
		case REVOKE_ACCESS:
			printf("REVOKE_ACCESS\n"); break;
		case SET_AUDIT_SUCCESS:
			printf("SET_AUDIT_SUCCESS\n"); break;
		case SET_AUDIT_FAILURE:
			printf("SET_AUDIT_FAILURE\n"); break;
		default:
			printf("Unknown (%08x)\n", pEntry->grfAccessMode);
		}

		printf("  Trustee: ");
		assert(pEntry->Trustee.pMultipleTrustee == NULL);
		assert(pEntry->Trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE);
		switch(pEntry->Trustee.TrusteeForm)
		{
		case TRUSTEE_IS_SID:
			{
				PSID trusteeSid = (PSID)(pEntry->Trustee.ptstrName);

				namelen = sizeof(namebuf);
				domainlen = sizeof(domainbuf);

				assert(LookupAccountSid(NULL, trusteeSid, namebuf, &namelen,
					domainbuf, &domainlen, &nametype));

				printf("SID of %s\\%s (%d)\n", domainbuf, namebuf, nametype);
			}
			break;
		case TRUSTEE_IS_NAME:
			printf("Name\n"); break;
		case TRUSTEE_BAD_FORM:
			printf("Bad form\n"); assert(0);
		case TRUSTEE_IS_OBJECTS_AND_SID:
			printf("Objects and SID\n"); break;
		case TRUSTEE_IS_OBJECTS_AND_NAME:
			printf("Objects and name\n"); break;
		default:
			printf("Unknown form\n"); assert(0);
		}

		printf("  Trustee type: ");
		switch(pEntry->Trustee.TrusteeType)
		{
		case TRUSTEE_IS_UNKNOWN:
			printf("Unknown type.\n"); break;
		case TRUSTEE_IS_USER:
			printf("User\n"); break;
		case TRUSTEE_IS_GROUP:
			printf("Group\n"); break;
		case TRUSTEE_IS_DOMAIN:
			printf("Domain\n"); break;
		case TRUSTEE_IS_ALIAS:
			printf("Alias\n"); break;
		case TRUSTEE_IS_WELL_KNOWN_GROUP:
			printf("Well-known group\n"); break;
		case TRUSTEE_IS_DELETED:
			printf("Deleted account\n"); break;
		case TRUSTEE_IS_INVALID:
			printf("Invalid trustee type\n"); break;
		case TRUSTEE_IS_COMPUTER:
			printf("Computer\n"); break;
		default:
			printf("Unknown type %d\n", pEntry->Trustee.TrusteeType); 
			assert(0);
		}

		printf("\n");
	}

	assert(LocalFree((HLOCAL)pEntries) == 0);
	assert(LocalFree((HLOCAL)pSecurityDesc) == 0);

	chdir("c:\\tmp");
	openfile("test", O_CREAT, 0);
	struct stat ourfs;
	//test our opendir, readdir and closedir
	//functions
	DIR *ourDir = opendir("C:");

	if ( ourDir != NULL )
	{
		struct dirent *info;
		do
		{
			info = readdir(ourDir);
			if (info) printf("File/Dir name is : %s\r\n", info->d_name);
		}
		while (info != NULL);

		closedir(ourDir);
	}
	
	std::string diry("C:\\Projects\\boxbuild\\testfiles\\");
	ourDir = opendir(diry.c_str());
	if ( ourDir != NULL )
	{
		struct dirent *info;
		do
		{
			info = readdir(ourDir);
			if (info == NULL) break;
			std::string file(diry + info->d_name);
			stat(file.c_str(), &ourfs);
			if (info) printf("File/Dir name is : %s\r\n", info->d_name);
		}
		while ( info != NULL );

		closedir(ourDir);

	}

	stat("c:\\windows", &ourfs);
	stat("c:\\autoexec.bat", &ourfs);
	printf("Finished dir read\n");

	//test our getopt function
	char * test_argv[] = 
	{
		"foobar.exe",
		"-qwc",
		"-",
		"-c",
		"fgfgfg",
		"-f",
		"-l",
		"hello",
		"-",
		"force-sync",
		NULL
	};
	int test_argc;
	for (test_argc = 0; test_argv[test_argc]; test_argc++) { }
	const char* opts = "qwc:l:";

	assert(getopt(test_argc, test_argv, opts) == 'q');
	assert(getopt(test_argc, test_argv, opts) == 'w');
	assert(getopt(test_argc, test_argv, opts) == 'c');
	assert(strcmp(optarg, "-") == 0);
	assert(getopt(test_argc, test_argv, opts) == 'c');
	assert(strcmp(optarg, "fgfgfg") == 0);
	assert(getopt(test_argc, test_argv, opts) == '?');
	assert(optopt == 'f');
	assert(getopt(test_argc, test_argv, opts) == 'l');
	assert(strcmp(optarg, "hello") == 0);
	assert(getopt(test_argc, test_argv, opts) == -1);
	// assert(optopt == 0); // no more options
	assert(strcmp(test_argv[optind], "-") == 0);
	assert(strcmp(test_argv[optind+1], "force-sync") == 0);
	//end of getopt test
	
	//now test our statfs funct
	stat("c:\\cert.cer", &ourfs);

	char *timee;
	
	timee = ctime(&ourfs.st_mtime);

	if (S_ISREG(ourfs.st_mode))
	{
		printf("is a normal file\n");
	}
	else
	{
		printf("is a directory?\n");
		exit(1);
	}

	lstat(getenv("WINDIR"), &ourfs);

	if ( S_ISDIR(ourfs.st_mode))
	{
		printf("is a directory\n");
	}
	else
	{
		printf("is a file?\n");
		exit(1);
	}

	//test the syslog functions
	openlog("Box Backup", 0,0);
	//the old ones are the best...
	syslog(LOG_ERR, "Hello World");
	syslog(LOG_ERR, "Value of int is: %i", 6);

	closelog();

	/*
	//first off get the path name for the default 
	char buf[MAX_PATH];
	
	GetModuleFileName(NULL, buf, sizeof(buf));
	std::string buffer(buf);
	std::string conf("-c " + buffer.substr(0,(buffer.find("win32test.exe"))) + "bbackupd.conf");
	//std::string conf( "-c " + buffer.substr(0,(buffer.find("bbackupd.exe"))) + "bbackupd.conf");
	*/

	return 0;
}

#endif // WIN32