Skip to content

Commit 6fda539

Browse files
committed
more tests
gufi_query -Q without -I test parallel_cpr directory to file dupdir now returns err lstat might reset errno to existing directory clear trie user data
1 parent 51bab8c commit 6fda539

File tree

9 files changed

+52
-34
lines changed

9 files changed

+52
-34
lines changed

src/gufi_query/validate_inputs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ int validate_inputs(struct input *in) {
135135
/* -Q requires -I */
136136
if (sll_get_size(&in->external_attach)) {
137137
if (!in->sql.init.len) {
138-
fprintf(stderr, "External databases require template files attached with -I [%s]\n", in->sql.init.data);
138+
fprintf(stderr, "External databases require template files attached with -I\n");
139139
return -1;
140140
}
141141
}

src/parallel_cpr.c

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ static int cpr_file(QPTPool_t *ctx, const size_t id, void *data, void *args) {
146146

147147
const int dst_fd = open(dst.data, O_CREAT | O_WRONLY, st.st_mode);
148148
if (dst_fd < 0) {
149-
print_error_and_goto("Could not opent file", dst.data, free_name);
149+
print_error_and_goto("Could not open file", dst.data, free_name);
150150
}
151151

152152
if (copyfd(src_fd, 0, dst_fd, 0, st.st_size) != st.st_size) {
@@ -291,31 +291,16 @@ static int cpr_dir(QPTPool_t *ctx, const size_t id, void *data, void *args) {
291291
}
292292

293293
static int setup(const refstr_t *dst) {
294-
const mode_t mode = umask(0);
295-
const uid_t uid = geteuid();
296-
const uid_t gid = getegid();
297-
298-
umask(mode); /* reset umask */
299-
300-
/* should copy because mkpath modifies the string */
301-
char *path = malloc(dst->len + 1);
302-
SNFORMAT_S(path, dst->len + 1, 1,
303-
dst->data, dst->len);
304-
305-
int rc = mkpath(path, ~mode & 0777, uid, gid);
306-
const int err = errno;
307-
308-
free(path);
294+
struct stat st;
295+
st.st_mode = umask(0);
296+
st.st_uid = geteuid();
297+
st.st_gid = getegid();
309298

310-
if (rc != 0) {
311-
if (err != EEXIST) {
312-
return err;
313-
}
299+
umask(st.st_mode); /* reset umask */
314300

315-
rc = 0;
316-
}
301+
st.st_mode = ~st.st_mode & 0777;
317302

318-
return rc;
303+
return dupdir(dst->data, &st);
319304
}
320305

321306
static void sub_help(void) {
@@ -339,7 +324,10 @@ int main(int argc, char * argv[]) {
339324
INSTALL_STR(&in.nameto, argv[argc - 1]);
340325
}
341326

342-
if (setup(&in.nameto) != 0) {
327+
int rc = setup(&in.nameto);
328+
if (rc != 0) {
329+
fprintf(stderr, "Error: Cannot copy to \"%s\": %s (%d)\n",
330+
in.nameto.data, strerror(rc), rc);
343331
input_fini(&in);
344332
return EXIT_FAILURE;
345333
}
@@ -365,8 +353,6 @@ int main(int argc, char * argv[]) {
365353
return EXIT_FAILURE;
366354
}
367355

368-
int rc = 0;
369-
370356
argc--;
371357
for(int i = idx; i < argc; i++) {
372358
const char *path = argv[i];

src/utils.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -285,12 +285,12 @@ int dupdir(const char *path, struct stat *stat) {
285285
}
286286
else if (err == EEXIST) {
287287
struct stat st;
288-
if ((lstat(copy, &st) != 0) || !S_ISDIR(st.st_mode)) {
289-
return 1;
288+
if ((lstat(copy, &st) != 0) || !S_ISDIR(st.st_mode)) {
289+
return err;
290290
}
291291
}
292-
else if (err != EEXIST) {
293-
return 1;
292+
else {
293+
return err;
294294
}
295295
}
296296
chmod(copy, stat->st_mode);

test/regression/gufi_query.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,10 @@ Error: Missing -I
244244
$ gufi_query -O outdb -K "create;" "prefix"
245245
Error: Missing -I
246246

247+
# Missing -I (external databases)
248+
$ gufi_query -Q "external" "user" "data" "base" "prefix"
249+
External databases require template files attached with -I
250+
247251
# Missing -J (aggregate, stdout)
248252
$ gufi_query -I "create" -S "insert;" -E "insert;" -K "create;" -G "query;" "prefix"
249253
Error: Missing -J

test/regression/gufi_query.sh.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ run_no_sort "${GUFI_QUERY} -o outfile -K \
127127
echo "# Missing -I (aggregating, outdb)"
128128
run_no_sort "${GUFI_QUERY} -O outdb -K \"create;\" \"${INDEXROOT}\""
129129

130+
echo "# Missing -I (external databases)"
131+
run_no_sort "${GUFI_QUERY} -Q \"external\" \"user\" \"data\" \"base\" \"${INDEXROOT}\""
132+
130133
echo "# Missing -J (aggregate, stdout)"
131134
run_no_sort "${GUFI_QUERY} -I \"create\" -S \"insert;\" -E \"insert;\" -K \"create;\" -G \"query;\" \"${INDEXROOT}\""
132135

test/regression/parallel_cpr.expected

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ prefix/1KB
110110

111111
$ rm -rf "copy"
112112

113+
$ parallel_cpr "prefix" "copy"
114+
115+
$ rm -rf "copy"
116+
113117
$ parallel_cpr "prefix" "prefix"
114118
Not copying into itself: "prefix" -> "prefix"
115119

@@ -122,3 +126,6 @@ Error: Cannot lstat "badtrace": No such file or directory (2)
122126
$ parallel_cpr /dev/null "copy"
123127
Not copying "/dev/null"
124128

129+
$ parallel_cpr "prefix" "prefix/old_file"
130+
Error: Cannot copy to "prefix/old_file": File exists (17)
131+

test/regression/parallel_cpr.sh.in

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,12 @@ run_no_sort "getfattr -d -m \".*\" -- \"${CPR_ROOT}/leaf_file1\""
125125
run_no_sort "getfattr -d -m \".*\" -- \"${CPR_ROOT}/leaf_file2\""
126126
run_no_sort "readlink \"${CPR_ROOT}/file_symlink\""
127127

128+
run_no_sort "rm -rf \"${CPR_ROOT}\""
129+
130+
# copy to existing directory
131+
mkdir "${CPR_ROOT}"
132+
run_no_sort "${PARALLEL_CPR} \"${SRCDIR}\" \"${CPR_ROOT}\""
133+
128134
# errors
129135

130136
run_no_sort "rm -rf \"${CPR_ROOT}\""
@@ -139,6 +145,9 @@ run_no_sort "${PARALLEL_CPR} \"${BADTRACE}\" \"${CPR_ROOT}\""
139145

140146
# non-directory/link/file source
141147
run_no_sort "${PARALLEL_CPR} /dev/null \"${CPR_ROOT}\""
148+
149+
# directory to file
150+
run_no_sort "${PARALLEL_CPR} \"${SRCDIR}\" \"${SRCDIR}/old_file\""
142151
set -e
143152
) | tee "${OUTPUT}"
144153

test/unit/googletest/trie.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,11 @@ TEST(trie, insert_empty) {
9595
trie_t *root = trie_alloc();
9696
ASSERT_NE(root, nullptr);
9797

98-
trie_insert(root, buf, 0, nullptr, nullptr);
98+
trie_insert(root, buf, 0, nullptr, [](void *) {});
99+
EXPECT_EQ(trie_search(root, buf, 0, nullptr), 1);
99100

101+
// replace previous value, running user free fucntion
102+
trie_insert(root, buf, 0, nullptr, nullptr);
100103
EXPECT_EQ(trie_search(root, buf, 0, nullptr), 1);
101104

102105
trie_free(root);
@@ -140,13 +143,19 @@ TEST(trie, delete) {
140143
{
141144
/* insert long string and sub string */
142145
trie_insert(root, str, str_len, nullptr, nullptr);
143-
trie_insert(root, str, sub_len, nullptr, nullptr);
146+
trie_insert(root, str, sub_len, nullptr, [](void *) {});
144147
EXPECT_EQ(trie_search(root, str, str_len, nullptr), 1);
145148
EXPECT_EQ(trie_search(root, str, sub_len, nullptr), 1);
146149

147150
/* shorter string is deleted, but the longer string remains */
148151
EXPECT_EQ(trie_delete(root, str, sub_len), 0);
149152
EXPECT_NE(root, nullptr);
153+
154+
/* insert and remove sub string again without a free user function */
155+
trie_insert(root, str, sub_len, nullptr, nullptr);
156+
EXPECT_EQ(trie_search(root, str, sub_len, nullptr), 1);
157+
EXPECT_EQ(trie_delete(root, str, sub_len), 0);
158+
150159
EXPECT_EQ(trie_search(root, str, str_len, nullptr), 1);
151160
EXPECT_EQ(trie_search(root, str, sub_len, nullptr), 0);
152161
}

test/unit/googletest/utils.cpp.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ TEST(dupdir, fileasdir) {
514514
child_stat.st_uid = geteuid();
515515
child_stat.st_gid = getegid();
516516

517-
EXPECT_EQ(dupdir(child, &child_stat), 1);
517+
EXPECT_NE(dupdir(child, &child_stat), 0);
518518
EXPECT_EQ(remove(parent), 0);
519519
}
520520

0 commit comments

Comments
 (0)