diff --git a/Makefile.am b/Makefile.am index 940b506c..e6a40a12 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 \ diff --git a/src/mirror.c b/src/mirror.c index 6d64aa91..e2d76c46 100644 --- a/src/mirror.c +++ b/src/mirror.c @@ -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) diff --git a/test/functional/mirror/createdir-negative/lines-checked b/test/functional/mirror/createdir-negative/lines-checked new file mode 100644 index 00000000..37b5c676 --- /dev/null +++ b/test/functional/mirror/createdir-negative/lines-checked @@ -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. diff --git a/test/functional/mirror/createdir-negative/test.bats b/test/functional/mirror/createdir-negative/test.bats new file mode 100755 index 00000000..7d776a53 --- /dev/null +++ b/test/functional/mirror/createdir-negative/test.bats @@ -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 diff --git a/test/functional/mirror/createdir/lines-checked b/test/functional/mirror/createdir/lines-checked new file mode 100644 index 00000000..1035c45b --- /dev/null +++ b/test/functional/mirror/createdir/lines-checked @@ -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 diff --git a/test/functional/mirror/createdir/test.bats b/test/functional/mirror/createdir/test.bats new file mode 100755 index 00000000..86d60d29 --- /dev/null +++ b/test/functional/mirror/createdir/test.bats @@ -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 diff --git a/test/functional/swupdlib.bash b/test/functional/swupdlib.bash index 6559d972..d8094e0b 100644 --- a/test/functional/swupdlib.bash +++ b/test/functional/swupdlib.bash @@ -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"