forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestmod_mmapstorage_crc.c
145 lines (117 loc) · 3.86 KB
/
testmod_mmapstorage_crc.c
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
/**
* @file
*
* @brief Tests for mmapstorage_crc plugin variant
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*
*/
#define _XOPEN_SOURCE 600
/* -- Imports --------------------------------------------------------------------------------------------------------------------------- */
#include <stdio.h> // fopen()
#include <sys/mman.h> // mmap()
#include <sys/stat.h> // stat(), chmod()
#include <tests_plugin.h>
#include "internal.h"
#include "mmapstorage.h"
/* -- Macros ---------------------------------------------------------------------------------------------------------------------------- */
#define TEST_ROOT_KEY "user:/tests/mmapstorage_crc"
/* -- Functions ------------------------------------------------------------------------------------------------------------------------- */
static void test_mmap_crc_no_checksum (const char * tmpFile)
{
// regression test: write mmap file without checksum (=0L), then read with checksum
{
Key * parentKey = keyNew (TEST_ROOT_KEY, KEY_VALUE, tmpFile, KEY_END);
KeySet * conf = ksNew (0, KS_END);
PLUGIN_OPEN ("mmapstorage");
KeySet * ks = ksNew (0, KS_END);
succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful");
keyDel (parentKey);
ksDel (ks);
PLUGIN_CLOSE ();
}
// the following fails if the internal CRC on/off flag was not set
{
Key * parentKey = keyNew (TEST_ROOT_KEY, KEY_VALUE, tmpFile, KEY_END);
KeySet * conf = ksNew (0, KS_END);
PLUGIN_OPEN ("mmapstorage_crc");
KeySet * ks = ksNew (0, KS_END);
succeed_if (plugin->kdbGet (plugin, ks, parentKey) == 1, "kdbGet was not successful");
keyDel (parentKey);
ksDel (ks);
PLUGIN_CLOSE ();
}
}
static void test_mmap_crc_wrong_checksum (const char * tmpFile)
{
// first write a mmap file
{
Key * parentKey = keyNew (TEST_ROOT_KEY, KEY_VALUE, tmpFile, KEY_END);
KeySet * conf = ksNew (0, KS_END);
PLUGIN_OPEN ("mmapstorage_crc");
KeySet * ks = ksNew (0, KS_END);
succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful");
keyDel (parentKey);
ksDel (ks);
PLUGIN_CLOSE ();
}
// set wrong checksum in mmap header
FILE * fp;
if ((fp = fopen (tmpFile, "r+")) == 0)
{
yield_error ("fopen() error");
}
struct stat sbuf;
if (stat (tmpFile, &sbuf) == -1)
{
yield_error ("stat() error");
}
int fd = fileno (fp);
char * mappedRegion = mmap (0, sbuf.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (mappedRegion == MAP_FAILED)
{
ELEKTRA_LOG_WARNING ("error mapping file %s\nmmapSize: " ELEKTRA_STAT_ST_SIZE_F, tmpFile, sbuf.st_size);
yield_error ("mmap() error");
return;
}
if (fp)
{
fclose (fp);
}
MmapHeader * mmapHeader = (MmapHeader *) mappedRegion;
mmapHeader->checksum++;
if (msync ((void *) mappedRegion, sbuf.st_size, MS_SYNC) != 0)
{
yield_error ("msync() error");
return;
}
if (munmap (mappedRegion, sbuf.st_size) != 0)
{
yield_error ("munmap() error");
return;
}
// wrong checksum should be detected now
{
// we expect an error here
Key * parentKey = keyNew (TEST_ROOT_KEY, KEY_VALUE, tmpFile, KEY_END);
KeySet * conf = ksNew (0, KS_END);
PLUGIN_OPEN ("mmapstorage_crc");
KeySet * ks = ksNew (0, KS_END);
succeed_if (plugin->kdbGet (plugin, ks, parentKey) == ELEKTRA_PLUGIN_STATUS_ERROR, "kdbGet did not detect wrong checksum");
keyDel (parentKey);
ksDel (ks);
PLUGIN_CLOSE ();
}
}
/* -- Main ------------------------------------------------------------------------------------------------------------------------------ */
int main (int argc, char ** argv)
{
printf ("MMAPSTORAGE CRC TESTS\n");
printf ("==================\n\n");
init (argc, argv);
const char * tmpFile = elektraFilename ();
test_mmap_crc_no_checksum (tmpFile);
test_mmap_crc_wrong_checksum (tmpFile);
printf ("\ntestmod_mmapstorage_crc RESULTS: %d test(s) done. %d error(s).\n", nbTest, nbError);
return nbError;
}