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