123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- #include <check.h>
- #include <errno.h>
- #include <stdio.h>
- #include <string.h>
- #include <unistd.h>
-
- #include "asmsh_check.h"
- #include "mmap_parse.h"
-
- #define PARSE_FD_SAMPLE "samples/procfs_pid_maps"
- #define PARSE_FD_SAMPLE_SM "samples/procfs_pid_maps_sm"
-
- typedef struct {
- child_mmap_t map;
- char *line;
- } mmap_linecheck_t;
-
- START_TEST (test_parse_line)
- {
- child_mmap_t res;
- size_t i;
-
- mmap_linecheck_t lcheck[] = {
- {{(void*)0x123456, (void*)0x654321,
- MAP_PRIVATE, 0, 0, 0, "[stack]"},
- "123456-654321 ---p 00000000 0:0 0 [stack]"},
- {{(void*)0, (void*)0x1000,
- PROT_READ | PROT_WRITE | PROT_EXEC | MAP_SHARED,
- 1, (13<<8)+12, 123, "[stack]"},
- "0-01000 rwxs 00000001 13:12 123 [stack]"},
- {{(void*)0x35b1800000, (void*)0x35b1820000,
- PROT_READ | PROT_EXEC | MAP_PRIVATE, 0x1f000, (8<<8)+2, 135522, "/usr/lib64/ld-2.15.so"},
- "35b1800000-35b1820000 r-xp 0001f000 08:02 135522 /usr/lib64/ld-2.15.so"},
- };
-
- for(i=0; i<sizeof(lcheck) / sizeof(mmap_linecheck_t); i++)
- {
- ck_assert_int_eq(child_mmap_parseline(lcheck[i].line, &res), 0);
- ck_assert_ptr_eq(lcheck[i].map.start, res.start);
- ck_assert_ptr_eq(lcheck[i].map.stop, res.stop);
- ck_assert_int_eq(lcheck[i].map.perm, res.perm);
- ck_assert(lcheck[i].map.offset == res.offset);
- ck_assert(lcheck[i].map.device == res.device);
- ck_assert(lcheck[i].map.inode == res.inode);
- ck_assert_str_eq(lcheck[i].map.pathname, res.pathname);
- }
- }
- END_TEST
-
- START_TEST (test_parse_fd)
- {
- child_mmap_l maps;
- int fd, ret;
-
- child_mmap_t checks[] = {
- {(void*)0x400000, (void*)0x452000,
- PROT_READ | PROT_EXEC | MAP_PRIVATE, 0, (8<<8)+2, 173521,
- "/usr/bin/dbus-daemon"},
- {(void*)0x651000, (void*)0x652000,
- PROT_READ | MAP_PRIVATE, 0x51000, (8<<8)+2, 173521,
- "/usr/bin/dbus-daemon"},
- {(void*)0x652000, (void*)0x655000,
- PROT_READ | PROT_WRITE | MAP_PRIVATE, 0x52000, (8<<8)+2, 173521,
- "/usr/bin/dbus-daemon"},
- {(void*)0xe03000, (void*)0xe24000,
- PROT_READ | PROT_WRITE | MAP_PRIVATE, 0, 0, 0,
- "[heap]"},
- {(void*)0xe24000, (void*)0x11f7000,
- PROT_READ | PROT_WRITE | MAP_SHARED, 0, 0, 0,
- "[heap]"},
- {(void*)0x35b1a20000, (void*)0x35b1a21000,
- PROT_READ | PROT_WRITE | MAP_PRIVATE, 0x20000, (13<<8)+12, 135522,
- "/usr/lib64/ld-2.15.so"},
- {(void*)0x35b1a21000, (void*)0x35b1a22000,
- PROT_READ | PROT_WRITE | MAP_PRIVATE, 0, 0, 0,
- ""},
- };
-
- child_mmap_init(&maps);
- fd = open(PARSE_FD_SAMPLE, O_RDONLY);
- if(fd < 0)
- {
- perror("Unable to open sample");
- ck_abort_msg("Unable to open sample");
- }
-
- ret = child_mmap_get_fd(fd, &maps);
- ck_assert_int_eq(ret, 0);
-
- int i;
- for(i=0; i<sizeof(checks)/sizeof(*checks);i++)
- {
- dprintf(2, "line %d\n", i);
- child_mmap_t *check, *res;
- check = &checks[i];
- res = &maps.maps[i];
- ck_assert_ptr_eq(check->start, res->start);
- ck_assert_ptr_eq(check->stop, res->stop);
- ck_assert_int_eq(check->perm, res->perm);
- ck_assert_int_eq(check->offset, res->offset);
- ck_assert_mem_eq(&check->device, &res->device, sizeof(check->device));
- ck_assert_mem_eq(&check->inode, &res->inode, sizeof(check->inode));
- ck_assert_str_eq(check->pathname, res->pathname);
- }
- ck_assert_int_eq(i, maps.size);
-
- close(fd);
- }
- END_TEST
-
- START_TEST(test_mmap_get_invalid)
- {
- child_mmap_l maps;
- int ret = child_mmap_get(-1, &maps);
- ck_assert_int_eq(ret, -1);
- }
- END_TEST
-
- START_TEST(test_mmap_get_self)
- {
- child_mmap_l maps;
- pid_t spid = getpid();
- child_mmap_init(&maps);
- int ret = child_mmap_get(spid, &maps);
-
- ck_assert_int_eq(ret, 0);
-
- }
- END_TEST
-
- START_TEST(test_shrink_map)
- {
- child_mmap_l maps;
- int ret, fd;
-
- fd = open(PARSE_FD_SAMPLE, O_RDONLY);
- if(fd < 0)
- {
- perror("Unable to open sample");
- ck_abort_msg("Unable to open sample");
- }
-
- child_mmap_init(&maps);
- ret = child_mmap_get_fd(fd, &maps);
-
- ck_assert_int_eq(ret, 0);
-
- close(fd);
-
- fd = open(PARSE_FD_SAMPLE_SM, O_RDONLY);
- if(fd < 0)
- {
- perror("Unable to open sample");
- ck_abort_msg("Unable to open sample");
- }
-
- ret = child_mmap_get_fd(fd, &maps);
-
- ck_assert_int_eq(ret, 0);
-
- close(fd);
- }
-
- ASMSH_CHECK_START("/proc/[pid]/map file parser", "parsing tests")
- ASMSH_ADD_TEST(test_parse_line);
- ASMSH_ADD_TEST(test_parse_fd);
- ASMSH_ADD_TEST(test_mmap_get_invalid);
- ASMSH_ADD_TEST(test_mmap_get_self);
- ASMSH_ADD_TEST(test_shrink_map);
- ASMSH_CHECK_END
|