Tidy up /etc/swupd creation

Set /etc/swupd permissions to 0777 to let the default umask policy
handle stripping bits appropriately.
If we can write files to /etc/swupd, do it, making the directory first,
if necessary. If not, bail out cleanly. Only delete the
mirror_contenturl and mirror_versionurl files from the directory, not
the directory itself.
Add tests to verify normal behavior and corner cases.
This commit is contained in:
Brett T. Warden
2018-05-09 16:51:23 -07:00
committed by Matthew Johnson
parent c264c23f61
commit 541acddecd
7 changed files with 138 additions and 24 deletions
+2
View File
@@ -145,6 +145,8 @@ BATS = \
test/functional/completion/basic/test.bats \
test/functional/hashdump/file-hash/test.bats \
test/functional/hashdump/file-hash-no-path-prefix/test.bats \
test/functional/mirror/createdir/test.bats \
test/functional/mirror/createdir-negative/test.bats \
test/functional/search/content-check-negfull-path/test.bats \
test/functional/search/content-check-neglibtest/test.bats \
test/functional/search/content-check-posbin/test.bats \
+44 -24
View File
@@ -52,53 +52,73 @@ static const struct option prog_opts[] = {
static int unset_mirror_url()
{
char *fullpath;
fullpath = mk_full_filename(path_prefix, "/etc/swupd");
char *content_path;
char *version_path;
int ret = 0;
content_path = mk_full_filename(path_prefix, MIRROR_CONTENT_URL_PATH);
version_path = mk_full_filename(path_prefix, MIRROR_VERSION_URL_PATH);
if (fullpath == NULL ||
strcmp(fullpath, "/") == 0 ||
strcmp(fullpath, "/etc") == 0) {
fprintf(stderr, "Invalid mirror configuration path\n");
free_string(&fullpath);
return 1;
if ((ret = swupd_rm(content_path))) {
goto out;
}
int ret = swupd_rm(fullpath);
free_string(&fullpath);
if ((ret = swupd_rm(version_path))) {
goto out;
}
out:
free_string(&content_path);
free_string(&version_path);
return ret;
}
static int write_to_path(char *content, char *path)
{
char *dir, *tmp = NULL;
struct stat dirstat;
string_or_die(&tmp, "%s", path);
dir = dirname(tmp);
/* attempt to make the directory
* ignore EEXIST errors */
int ret = mkdir(dir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
char *cmd;
string_or_die(&cmd, "mkdir -p %s", dir);
ret = system(cmd);
free_string(&cmd);
free_string(&tmp);
if (ret) {
return ret;
/* attempt to make the directory, ok if already exists */
int ret = mkdir(dir, S_IRWXU | S_IRWXG | S_IRWXO);
if (ret && EEXIST != errno) {
fprintf(stderr, "mkdir\n");
perror(dir);
goto out;
}
/* Make sure we have a directory, for better user feedback */
if ((ret = stat(dir, &dirstat))) {
perror(dir);
goto out;
} else if (!S_ISDIR(dirstat.st_mode)) {
fprintf(stderr, "%s: not a directory\n", dir);
ret = 1;
goto out;
}
/* now try to open the file to write */
FILE *fp = NULL;
fp = fopen(path, "w");
if (fp == NULL) {
return 1;
perror(path);
ret = 1;
goto out;
}
/* and write to the file */
ret = fputs(content, fp);
fclose(fp);
if (ret < 0 || ret == EOF) {
return 1;
fprintf(stderr, "%s: write failed\n", path);
}
return 0;
if ((ret = fclose(fp))) {
fprintf(stderr, "fclose\n");
perror(path);
}
out:
free_string(&tmp);
return ret;
}
static int set_mirror_url(char *url)
@@ -0,0 +1,4 @@
REGEXP:^swupd-client mirror .*$
REGEXP:^.*/etc/swupd: not a directory$
Unable to set mirror url
Default version URL not found. Use the -v option instead.
+35
View File
@@ -0,0 +1,35 @@
#!/usr/bin/env bats
load "../../swupdlib"
setup() {
clean_test_dir
sudo mkdir -p "$DIR/target-dir/usr/share/defaults/swupd/"
echo 4 | sudo tee "$DIR/target-dir/usr/share/defaults/swupd/format"
sudo mkdir -p "$DIR/target-dir/etc"
}
teardown() {
sudo rm -rf "$DIR/target-dir/etc/swupd"
sudo rm -rf "$DIR/target-dir/foo"
}
@test "mirror /etc/swupd is a file" {
sudo rm -rf "$DIR/target-dir/etc/swupd"
sudo touch "$DIR/target-dir/etc/swupd"
[[ -f "$DIR/target-dir/etc/swupd" ]] && ! [[ -L "$DIR/target-dir/etc/swupd" ]]
run sudo sh -c "$SWUPD mirror -s http://example.com/swupd-file $SWUPD_OPTS_MIRROR"
check_lines "$output"
}
@test "mirror /etc/swupd is a symlink to a file" {
sudo rm -rf "$DIR/target-dir/etc/swupd"
sudo rm -rf "$DIR/target-dir/foo"
sudo touch "$DIR/target-dir/foo"
sudo ln -s "$DIR/target-dir/foo" "$DIR/target-dir/etc/swupd"
run sudo sh -c "$SWUPD mirror -s http://example.com/swupd-file $SWUPD_OPTS_MIRROR"
check_lines "$output"
sudo rm "$DIR/target-dir/foo"
}
# vi: ft=sh ts=8 sw=2 sts=2 et tw=80
@@ -0,0 +1,5 @@
REGEXP:^swupd-client mirror .*$
Set upstream mirror to http://example.com/swupd-file
Installed version: -1
Version URL: http://example.com/swupd-file
Content URL: http://example.com/swupd-file
+46
View File
@@ -0,0 +1,46 @@
#!/usr/bin/env bats
load "../../swupdlib"
setup() {
clean_test_dir
sudo mkdir -p "$DIR/target-dir/usr/share/defaults/swupd/"
echo 4 | sudo tee "$DIR/target-dir/usr/share/defaults/swupd/format"
sudo mkdir -p "$DIR/target-dir/etc"
}
teardown() {
sudo rm -rf "$DIR/target-dir/etc/swupd"
sudo rm -rf "$DIR/target-dir/foo"
}
@test "mirror /etc/swupd does not exist" {
sudo rm -rf "$DIR/target-dir/etc/swupd"
run sudo sh -c "$SWUPD mirror -s http://example.com/swupd-file $SWUPD_OPTS_MIRROR"
check_lines "$output"
[[ "http://example.com/swupd-file" == $(<$DIR/target-dir/etc/swupd/mirror_contenturl) ]]
[[ "http://example.com/swupd-file" == $(<$DIR/target-dir/etc/swupd/mirror_versionurl) ]]
}
@test "mirror /etc/swupd already exists" {
sudo mkdir "$DIR/target-dir/etc/swupd"
run sudo sh -c "$SWUPD mirror -s http://example.com/swupd-file $SWUPD_OPTS_MIRROR"
check_lines "$output"
[[ "http://example.com/swupd-file" == $(<$DIR/target-dir/etc/swupd/mirror_contenturl) ]]
[[ "http://example.com/swupd-file" == $(<$DIR/target-dir/etc/swupd/mirror_versionurl) ]]
}
@test "mirror /etc/swupd is a symlink to a directory" {
sudo rm -rf "$DIR/target-dir/etc/swupd"
sudo rm -rf "$DIR/target-dir/foo"
sudo mkdir "$DIR/target-dir/foo"
sudo ln -s "$DIR/target-dir/foo" "$DIR/target-dir/etc/swupd"
run sudo sh -c "$SWUPD mirror -s http://example.com/swupd-file $SWUPD_OPTS_MIRROR"
check_lines "$output"
! [[ -L "$DIR/target-dir/etc/swupd" ]]
[[ "http://example.com/swupd-file" == $(<$DIR/target-dir/etc/swupd/mirror_contenturl) ]]
[[ "http://example.com/swupd-file" == $(<$DIR/target-dir/etc/swupd/mirror_versionurl) ]]
sudo rm -rf "$DIR/target-dir/foo"
}
# vi: ft=sh ts=8 sw=2 sts=2 et tw=80
+2
View File
@@ -15,6 +15,8 @@ export SWUPD_OPTS_NO_FMT="-S $STATE_DIR -p $DIR/target-dir -u file://$DIR/web-di
export SWUPD_OPTS_NO_CERT="-S $STATE_DIR -p $DIR/target-dir -F staging -u file://$DIR/web-dir"
export SWUPD_OPTS_MIRROR="-p $DIR/target-dir"
export CERT="$BATS_TEST_DIRNAME/Swupd_Root.pem"
export CERTCONF="$BATS_TEST_DIRNAME/certattributes.cnf"