Skip to content

Commit

Permalink
test create_empty_dbdb
Browse files Browse the repository at this point in the history
  • Loading branch information
calccrypto committed Oct 27, 2023
1 parent ed6c20a commit f63e499
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 14 deletions.
26 changes: 12 additions & 14 deletions src/template_db.c
Original file line number Diff line number Diff line change
Expand Up @@ -284,20 +284,18 @@ int create_empty_dbdb(struct template_db *tdb, refstr_t *dst, uid_t uid, gid_t g

/* if database file already exists, assume it's good */
struct stat st;
if (stat(dbname, &st) != 0) {
const int err = errno;
/* if the parent's database file is not found, create it */
if (err == ENOENT) {
if (copy_template(tdb, dbname, uid, gid) != 0) {
return -1;
}
}
else {
fprintf(stderr, "Issue with index parent directory %s: %s (%d)\n",
dst->data, strerror(err), err);
return -1;
}
if (stat(dbname, &st) == 0) { /* following links */
return -!S_ISREG(st.st_mode);
}

return 0;
const int err = errno;

/* if the parent's database file is not found, create it */
if (err != ENOENT) {
fprintf(stderr, "Error: Empty db.db path '%s': %s (%d)\n",
dst->data, strerror(err), err);
return -1;
}

return copy_template(tdb, dbname, uid, gid);
}
81 changes: 81 additions & 0 deletions test/unit/googletest/template_db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,84 @@ TEST(template_db, bad_inputs) {

ASSERT_EQ(close_template_db(&tdb), 0);
}

TEST(create_empty_dbdb, good) {
struct template_db tdb;
ASSERT_EQ(init_template_db(&tdb), 0);
ASSERT_EQ(create_template(&tdb, create_test_tables, template_name), 0);

// create new directory
char dirname[] = "XXXXXX" ;
ASSERT_NE(mkdtemp(dirname), nullptr);

refstr_t dst = {
.data = dirname,
.len = strlen(dirname),
};

// <dirname>/db.db
char dbname[MAXPATH];
SNFORMAT_S(dbname, sizeof(dbname), 3,
dst.data, dst.len,
"/", (std::size_t) 1,
DBNAME, DBNAME_LEN);

// create file under dirname not called db.db
char filename[MAXPATH];
SNFORMAT_S(filename, sizeof(filename), 3,
dst.data, dst.len,
"/", (std::size_t) 1,
"XXXXXX", 6);
const int fd = mkstemp(filename);
ASSERT_GT(fd, -1);
EXPECT_EQ(close(fd), 0);

// symlink to file is ok
EXPECT_EQ(create_empty_dbdb(&tdb, &dst, -1, -1), 0);
EXPECT_EQ(unlink(dbname), 0);
EXPECT_EQ(unlink(filename), 0);

// symlink to <dirname>
char symlinktarget[MAXPATH];
SNFORMAT_S(symlinktarget, sizeof(symlinktarget), 2,
"../", (std::size_t) 3,
dst.data, dst.len);

// symink to directory causes error
ASSERT_EQ(symlink(symlinktarget, dbname), 0);
EXPECT_EQ(create_empty_dbdb(&tdb, &dst, -1, -1), -1);
EXPECT_EQ(unlink(dbname), 0);

// try to create db with a bad file descriptor
const int tfd = tdb.fd;
tdb.fd = -1;
EXPECT_EQ(create_empty_dbdb(&tdb, &dst, -1, -1), -1);
tdb.fd = tfd;

// create new db
EXPECT_EQ(create_empty_dbdb(&tdb, &dst, -1, -1), 0);

// db already exists, so nothing happens
EXPECT_EQ(create_empty_dbdb(&tdb, &dst, -1, -1), 0);

EXPECT_EQ(remove(dbname), 0);
EXPECT_EQ(remove(dirname), 0);

ASSERT_EQ(close_template_db(&tdb), 0);
}

TEST(create_empty_dbdb, path_is_file) {
char filename[] = "XXXXXX" ;
const int fd = mkstemp(filename);
ASSERT_GT(fd, -1);
EXPECT_EQ(close(fd), 0);

refstr_t dst = {
.data = filename,
.len = strlen(filename),
};

// attempt to create file under file
EXPECT_EQ(create_empty_dbdb(nullptr, &dst, -1, -1), -1);
EXPECT_EQ(remove(filename), 0);
}

0 comments on commit f63e499

Please sign in to comment.