Skip to content

Commit 0c66a91

Browse files
committed
Add regression test for #346 by adding file name check in LTbasic2
1 parent 074737f commit 0c66a91

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

tests/LTbasic2.c

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* LTbasic2.c -- Lsof Test basic tests 2
33
*
44
* The basic tests measure the finding by liblsof of its own open CWD, open
5-
* executable (when possible).
5+
* executable (when possible) and opened regular file.
66
*
77
* V. Abell
88
* Purdue University
@@ -36,6 +36,9 @@
3636

3737
#include "lsof.h"
3838
#include <stdio.h>
39+
#include <fcntl.h>
40+
#include <string.h>
41+
#include <unistd.h>
3942
#include <sys/stat.h>
4043

4144
int main(int argc, char **argv) {
@@ -47,8 +50,11 @@ int main(int argc, char **argv) {
4750
char buffer[128];
4851
int exec_found = 0; /* executable found in result */
4952
int cwd_found = 0; /* cwd found in result */
53+
int fd_found = 0; /* opened fd found in result */
5054
struct stat exec_stat;
5155
struct stat cwd_stat;
56+
int fd = -1;
57+
int tmpfile_created = 0;
5258
if (stat(argv[0], &exec_stat)) {
5359
fprintf(stderr, "Cannot stat %s, skipping executable check\n", argv[0]);
5460
exec_found = 1;
@@ -57,6 +63,11 @@ int main(int argc, char **argv) {
5763
fprintf(stderr, "Cannot stat '.', skipping cwd check\n");
5864
cwd_found = 1;
5965
}
66+
if ((fd = open("LTbasic2-tmp", O_CREAT, 0644)) < 0) {
67+
fprintf(stderr, "Cannot create 'LTbasic2-tmp' in current directory, "
68+
"skipping fd check\n");
69+
fd_found = 1;
70+
}
6071

6172
ctx = lsof_new();
6273
lsof_select_process(ctx, "LTbasic2", 0);
@@ -82,6 +93,11 @@ int main(int argc, char **argv) {
8293
f->dev == cwd_stat.st_dev && f->inode == cwd_stat.st_ino) {
8394
cwd_found = 1;
8495
}
96+
} else if (f->fd_type == LSOF_FD_NUMERIC) {
97+
/* check if fd matches */
98+
if (f->fd_num == fd && strstr(f->name, "LTbasic2-tmp")) {
99+
fd_found = 1;
100+
}
85101
}
86102
}
87103
}
@@ -95,5 +111,10 @@ int main(int argc, char **argv) {
95111
if (!cwd_found) {
96112
fprintf(stderr, "ERROR!!! current working directory wasn't found.\n");
97113
}
98-
return !(exec_found && cwd_found);
114+
if (!fd_found) {
115+
fprintf(stderr, "ERROR!!! opened regular file wasn't found.\n");
116+
}
117+
/* cleanup created temporary file */
118+
unlink("LTbasic2-tmp");
119+
return !(exec_found && cwd_found && fd_found);
99120
}

0 commit comments

Comments
 (0)