xref: /netbsd-src/external/bsd/ntp/dist/sntp/tests/fileHandlingTest.c (revision cdfa2a7ef92791ba9db70a584a1d904730e6fb46)
1*cdfa2a7eSchristos /*	$NetBSD: fileHandlingTest.c,v 1.2 2020/05/25 20:47:35 christos Exp $	*/
2067f5680Schristos 
3a6f3f22fSchristos 
4a6f3f22fSchristos #include "config.h"
5a6f3f22fSchristos #include "stdlib.h"
6a6f3f22fSchristos #include "sntptest.h"
7a6f3f22fSchristos 
8ae49d4a4Schristos #include "fileHandlingTest.h" /* required because of the h.in thingy */
9a6f3f22fSchristos 
10a6f3f22fSchristos #include <string.h>
11a6f3f22fSchristos #include <unistd.h>
12a6f3f22fSchristos 
13a6f3f22fSchristos const char *
CreatePath(const char * filename,enum DirectoryType argument)14ae49d4a4Schristos CreatePath(
15ae49d4a4Schristos 	const char *		filename,
16ae49d4a4Schristos 	enum DirectoryType 	argument
17ae49d4a4Schristos 	)
18ae49d4a4Schristos {
19a6f3f22fSchristos 	const char 	srcdir[] = SRCDIR_DEF;//"@abs_srcdir@/data/";
20ae49d4a4Schristos 	size_t		plen = sizeof(srcdir) + strlen(filename) + 1;
21ae49d4a4Schristos 	char * 		path = emalloc(plen);
22ae49d4a4Schristos 	ssize_t		retc;
23a6f3f22fSchristos 
24ae49d4a4Schristos 	UNUSED_ARG(argument);
25a6f3f22fSchristos 
26ae49d4a4Schristos 	retc = snprintf(path, plen, "%s%s", srcdir, filename);
27ae49d4a4Schristos 	if (retc <= 0 || (size_t)retc >= plen)
28ae49d4a4Schristos 		exit(1);
29a6f3f22fSchristos 	return path;
30a6f3f22fSchristos }
31a6f3f22fSchristos 
32a6f3f22fSchristos 
33ae49d4a4Schristos void
DestroyPath(const char * pathname)34ae49d4a4Schristos DestroyPath(
35ae49d4a4Schristos 	const char *	pathname
36ae49d4a4Schristos 	)
37ae49d4a4Schristos {
38ae49d4a4Schristos 	/* use a union to get terminally rid of the 'const' attribute */
39ae49d4a4Schristos 	union {
40ae49d4a4Schristos 		const char *ccp;
41ae49d4a4Schristos 		void       *vp;
42ae49d4a4Schristos 	} any;
43ae49d4a4Schristos 
44ae49d4a4Schristos 	any.ccp = pathname;
45ae49d4a4Schristos 	free(any.vp);
46ae49d4a4Schristos }
47ae49d4a4Schristos 
48ae49d4a4Schristos 
49a6f3f22fSchristos int
GetFileSize(FILE * file)50ae49d4a4Schristos GetFileSize(
51ae49d4a4Schristos 	FILE *	file
52ae49d4a4Schristos 	)
53ae49d4a4Schristos {
54a6f3f22fSchristos 	fseek(file, 0L, SEEK_END);
55a6f3f22fSchristos 	int length = ftell(file);
56a6f3f22fSchristos 	fseek(file, 0L, SEEK_SET);
57a6f3f22fSchristos 
58a6f3f22fSchristos 	return length;
59a6f3f22fSchristos }
60a6f3f22fSchristos 
61a6f3f22fSchristos 
62a6f3f22fSchristos bool
CompareFileContent(FILE * expected,FILE * actual)63ae49d4a4Schristos CompareFileContent(
64ae49d4a4Schristos 	FILE *	expected,
65ae49d4a4Schristos 	FILE *	actual
66ae49d4a4Schristos 	)
67ae49d4a4Schristos {
68a6f3f22fSchristos 	int currentLine = 1;
69a6f3f22fSchristos 
70a6f3f22fSchristos 	char actualLine[1024];
71a6f3f22fSchristos 	char expectedLine[1024];
72a6f3f22fSchristos 	size_t lenAct = sizeof actualLine;
73a6f3f22fSchristos 	size_t lenExp = sizeof expectedLine;
74a6f3f22fSchristos 
75a6f3f22fSchristos 	while (  ( (fgets(actualLine, lenAct, actual)) != NULL)
76a6f3f22fSchristos 	      && ( (fgets(expectedLine, lenExp, expected)) != NULL )
77a6f3f22fSchristos 	      ) {
78a6f3f22fSchristos 
79a6f3f22fSchristos 
80a6f3f22fSchristos 		if( strcmp(actualLine,expectedLine) !=0 ){
81a6f3f22fSchristos 			printf("Comparision failed on line %d",currentLine);
82a6f3f22fSchristos 			return FALSE;
83a6f3f22fSchristos 		}
84a6f3f22fSchristos 
85a6f3f22fSchristos 		currentLine++;
86a6f3f22fSchristos 	}
87a6f3f22fSchristos 
88a6f3f22fSchristos 	return TRUE;
89a6f3f22fSchristos }
90a6f3f22fSchristos 
91a6f3f22fSchristos 
92a6f3f22fSchristos void
ClearFile(const char * filename)93ae49d4a4Schristos ClearFile(
94ae49d4a4Schristos 	const char * filename
95ae49d4a4Schristos 	)
96ae49d4a4Schristos {
97a6f3f22fSchristos 	if (!truncate(filename, 0))
98a6f3f22fSchristos 		exit(1);
99a6f3f22fSchristos }
100