]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/journal/test-mmap-cache.c
build-sys: use glibc's xattr support instead of requiring libattr
[thirdparty/systemd.git] / src / journal / test-mmap-cache.c
CommitLineData
2a2507e6
LP
1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3/***
4 This file is part of systemd.
5
6 Copyright 2012 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
22#include <stdlib.h>
23#include <sys/mman.h>
24#include <unistd.h>
2d5bdf5b 25#include <fcntl.h>
2a2507e6
LP
26
27#include "log.h"
28#include "macro.h"
29#include "util.h"
30#include "mmap-cache.h"
31
32int main(int argc, char *argv[]) {
33 int x, y, z, r;
34 char px[] = "/tmp/testmmapXXXXXXX", py[] = "/tmp/testmmapYXXXXXX", pz[] = "/tmp/testmmapZXXXXXX";
35 MMapCache *m;
36 void *p, *q;
37
38 assert_se(m = mmap_cache_new());
39
2d5bdf5b 40 x = mkostemp_safe(px, O_RDWR|O_CLOEXEC);
2a2507e6
LP
41 assert(x >= 0);
42 unlink(px);
43
2d5bdf5b 44 y = mkostemp_safe(py, O_RDWR|O_CLOEXEC);
2a2507e6
LP
45 assert(y >= 0);
46 unlink(py);
47
2d5bdf5b 48 z = mkostemp_safe(pz, O_RDWR|O_CLOEXEC);
2a2507e6
LP
49 assert(z >= 0);
50 unlink(pz);
51
52 r = mmap_cache_get(m, x, PROT_READ, 0, false, 1, 2, NULL, &p);
53 assert(r >= 0);
54
55 r = mmap_cache_get(m, x, PROT_READ, 0, false, 2, 2, NULL, &q);
56 assert(r >= 0);
57
58 assert((uint8_t*) p + 1 == (uint8_t*) q);
59
60 r = mmap_cache_get(m, x, PROT_READ, 1, false, 3, 2, NULL, &q);
61 assert(r >= 0);
62
63 assert((uint8_t*) p + 2 == (uint8_t*) q);
64
65 r = mmap_cache_get(m, x, PROT_READ, 0, false, 16ULL*1024ULL*1024ULL, 2, NULL, &p);
66 assert(r >= 0);
67
68 r = mmap_cache_get(m, x, PROT_READ, 1, false, 16ULL*1024ULL*1024ULL+1, 2, NULL, &q);
69 assert(r >= 0);
70
71 assert((uint8_t*) p + 1 == (uint8_t*) q);
72
73 mmap_cache_unref(m);
74
03e334a1
LP
75 safe_close(x);
76 safe_close(y);
77 safe_close(z);
2a2507e6
LP
78
79 return 0;
80}