1 #include <stic.h>
2 
3 #ifndef _WIN32
4 
5 #include <sys/types.h>
6 #include <unistd.h> /* chdir() unlink() */
7 
8 #include <string.h> /* strcpy() */
9 
10 #include "../../src/cfg/config.h"
11 #include "../../src/compat/fs_limits.h"
12 #include "../../src/compat/os.h"
13 #include "../../src/ui/ui.h"
14 #include "../../src/utils/dynarray.h"
15 #include "../../src/utils/fs.h"
16 #include "../../src/utils/str.h"
17 #include "../../src/filelist.h"
18 #include "../../src/fops_misc.h"
19 
20 #include "utils.h"
21 
22 static int get_gids(gid_t *gid1, gid_t *gid2);
23 static int has_more_than_one_group(void);
24 
25 static char *saved_cwd;
26 
SETUP()27 SETUP()
28 {
29 	saved_cwd = save_cwd();
30 
31 	replace_string(&cfg.shell, "/bin/sh");
32 	stats_update_shell_type(cfg.shell);
33 	curr_view = &lwin;
34 }
35 
TEARDOWN()36 TEARDOWN()
37 {
38 	restore_cwd(saved_cwd);
39 }
40 
TEST(file_group_is_changed,IF (has_more_than_one_group))41 TEST(file_group_is_changed, IF(has_more_than_one_group))
42 {
43 	char path[PATH_MAX + 1];
44 	int i;
45 	struct stat s;
46 
47 	gid_t gid1, gid2;
48 	if(get_gids(&gid1, &gid2) != 0)
49 	{
50 		return;
51 	}
52 
53 	assert_int_equal(0, filter_init(&lwin.local_filter.filter, 0));
54 
55 	strcpy(lwin.curr_dir, SANDBOX_PATH);
56 	assert_success(chdir(lwin.curr_dir));
57 
58 	create_empty_dir("dir");
59 	create_empty_file("dir/chown-me");
60 
61 	flist_custom_start(&lwin, "test");
62 	make_abs_path(path, sizeof(path), SANDBOX_PATH, "dir/chown-me", saved_cwd);
63 	flist_custom_add(&lwin, path);
64 	assert_true(flist_custom_finish(&lwin, CV_REGULAR, 0) == 0);
65 
66 	mark_selection_or_current(curr_view);
67 	fops_chown(0, 1, 0, gid1);
68 	assert_success(os_stat("dir/chown-me", &s));
69 	assert_true(s.st_gid == gid1);
70 
71 	mark_selection_or_current(curr_view);
72 	fops_chown(0, 1, 0, gid2);
73 	assert_success(os_stat("dir/chown-me", &s));
74 	assert_true(s.st_gid == gid2);
75 
76 	for(i = 0; i < lwin.list_rows; ++i)
77 	{
78 		fentry_free(&lwin, &lwin.dir_entry[i]);
79 	}
80 	dynarray_free(lwin.dir_entry);
81 
82 	filter_dispose(&lwin.local_filter.filter);
83 
84 	assert_success(unlink("dir/chown-me"));
85 	assert_success(rmdir("dir"));
86 }
87 
88 static int
get_gids(gid_t * gid1,gid_t * gid2)89 get_gids(gid_t *gid1, gid_t *gid2)
90 {
91 	const int size = getgroups(0, NULL);
92 	if(size > 0)
93 	{
94 		gid_t list[size];
95 		if(getgroups(size, &list[0]) != size)
96 		{
97 			return 1;
98 		}
99 		*gid1 = list[0];
100 		*gid2 = list[1];
101 		return 0;
102 	}
103 	else
104 	{
105 		return 1;
106 	}
107 }
108 
109 static int
has_more_than_one_group(void)110 has_more_than_one_group(void)
111 {
112 	return (getgroups(0, NULL) >= 2);
113 }
114 
115 #endif
116 
117 /* vim: set tabstop=2 softtabstop=2 shiftwidth=2 noexpandtab cinoptions-=(0 : */
118 /* vim: set cinoptions+=t0 filetype=c : */
119