summaryrefslogtreecommitdiff
path: root/source4/torture/local/util_file.c
blob: 5c5047de45d525f46633c53abfe2a87e65d754b5 (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
/* 
   Unix SMB/CIFS implementation.

   util_file testing

   Copyright (C) Jelmer Vernooij 2005
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"
#include "system/filesys.h"

#define TEST_FILENAME "utilfile.test"
#define TEST_LINE1 "This is list line 1..."
#define TEST_LINE2 ".. and this is line 2"
#define TEST_LINE3 "and end of the file"

#define TEST_DATA TEST_LINE1 "\n" TEST_LINE2 "\n" TEST_LINE3

static BOOL test_file_load_save(TALLOC_CTX *mem_ctx)
{
	BOOL ret;
	size_t len;
	char *data;
	
	ret = file_save(TEST_FILENAME, TEST_DATA, strlen(TEST_DATA));
	if (!ret)
		return False;

	data = file_load(TEST_FILENAME, &len, mem_ctx);
	if (!data) 
		return False;

	if (len != strlen(TEST_DATA))
		return False;
	
	if (memcmp(data, TEST_DATA, len) != 0)
		return False;

	unlink(TEST_FILENAME);

	return True;
}

static BOOL test_afdgets(TALLOC_CTX *mem_ctx)
{
	int fd;
	char *line;
	
	if (!file_save(TEST_FILENAME, (const void *)TEST_DATA, strlen(TEST_DATA)))
		return False;

	fd = open(TEST_FILENAME, O_RDONLY);
	
	if (fd == -1) 
		return False;

	line = afdgets(fd, mem_ctx, 8);
	if (strcmp(line, TEST_LINE1) != 0) 
		return False;

	line = afdgets(fd, mem_ctx, 8);
	if (strcmp(line, TEST_LINE2) != 0) 
		return False;

	line = afdgets(fd, mem_ctx, 8);
	if (strcmp(line, TEST_LINE3) != 0) 
		return False;

	close(fd);

	unlink(TEST_FILENAME);

	return True;
}

BOOL torture_local_util_file(void) 
{
	BOOL ret = True;
	TALLOC_CTX *mem_ctx = talloc_init("test_util_file");

	ret &= test_file_load_save(mem_ctx);
	ret &= test_afdgets(mem_ctx);

	talloc_free(mem_ctx);

	return ret;
}