test-fstab-util.c revision 07630cea1f3a845c09309f197ac7c4f11edd3b62
2N/A/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2N/A
2N/A/***
2N/A This file is part of systemd.
2N/A
2N/A Copyright 2015 Zbigniew Jędrzejewski-Szmek
2N/A
2N/A systemd is free software; you can redistribute it and/or modify it
2N/A under the terms of the GNU Lesser General Public License as published by
2N/A the Free Software Foundation; either version 2.1 of the License, or
2N/A (at your option) any later version.
2N/A
2N/A systemd is distributed in the hope that it will be useful, but
2N/A WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2N/A Lesser General Public License for more details.
2N/A
2N/A You should have received a copy of the GNU Lesser General Public License
2N/A along with systemd; If not, see <http://www.gnu.org/licenses/>.
2N/A***/
2N/A
2N/A#include "fstab-util.h"
2N/A#include "log.h"
2N/A#include "string-util.h"
2N/A#include "util.h"
2N/A
2N/A/*
2N/Aint fstab_filter_options(const char *opts, const char *names,
2N/A const char **namefound, char **value, char **filtered);
2N/A*/
2N/A
2N/Astatic void do_fstab_filter_options(const char *opts,
2N/A const char *remove,
2N/A int r_expected,
2N/A const char *name_expected,
2N/A const char *value_expected,
2N/A const char *filtered_expected) {
2N/A
2N/A int r;
2N/A const char *name;
2N/A _cleanup_free_ char *value, *filtered;
2N/A
2N/A r = fstab_filter_options(opts, remove, &name, &value, &filtered);
2N/A log_info("\"%s\" → %d, \"%s\", \"%s\", \"%s\", expected %d, \"%s\", \"%s\", \"%s\"",
2N/A opts, r, name, value, filtered,
2N/A r_expected, name_expected, value_expected, filtered_expected ?: opts);
2N/A assert_se(r == r_expected);
2N/A assert_se(streq_ptr(name, name_expected));
2N/A assert_se(streq_ptr(value, value_expected));
2N/A assert_se(streq_ptr(filtered, filtered_expected ?: opts));
2N/A
2N/A /* also test the malloc-less mode */
2N/A r = fstab_filter_options(opts, remove, &name, NULL, NULL);
2N/A log_info("\"%s\" → %d, \"%s\", expected %d, \"%s\"",
2N/A opts, r, name,
2N/A r_expected, name_expected);
2N/A assert_se(r == r_expected);
2N/A assert_se(streq_ptr(name, name_expected));
2N/A}
2N/A
2N/Astatic void test_fstab_filter_options(void) {
2N/A do_fstab_filter_options("opt=0", "opt\0x-opt\0", 1, "opt", "0", "");
2N/A do_fstab_filter_options("opt=0", "x-opt\0opt\0", 1, "opt", "0", "");
2N/A do_fstab_filter_options("opt", "opt\0x-opt\0", 1, "opt", NULL, "");
2N/A do_fstab_filter_options("opt", "x-opt\0opt\0", 1, "opt", NULL, "");
2N/A do_fstab_filter_options("x-opt", "x-opt\0opt\0", 1, "x-opt", NULL, "");
2N/A
2N/A do_fstab_filter_options("opt=0,other", "opt\0x-opt\0", 1, "opt", "0", "other");
2N/A do_fstab_filter_options("opt=0,other", "x-opt\0opt\0", 1, "opt", "0", "other");
2N/A do_fstab_filter_options("opt,other", "opt\0x-opt\0", 1, "opt", NULL, "other");
2N/A do_fstab_filter_options("opt,other", "x-opt\0opt\0", 1, "opt", NULL, "other");
2N/A do_fstab_filter_options("x-opt,other", "opt\0x-opt\0", 1, "x-opt", NULL, "other");
2N/A
2N/A do_fstab_filter_options("opto=0,other", "opt\0x-opt\0", 0, NULL, NULL, NULL);
2N/A do_fstab_filter_options("opto,other", "opt\0x-opt\0", 0, NULL, NULL, NULL);
2N/A do_fstab_filter_options("x-opto,other", "opt\0x-opt\0", 0, NULL, NULL, NULL);
2N/A
2N/A do_fstab_filter_options("first,opt=0", "opt\0x-opt\0", 1, "opt", "0", "first");
2N/A do_fstab_filter_options("first=1,opt=0", "opt\0x-opt\0", 1, "opt", "0", "first=1");
2N/A do_fstab_filter_options("first,opt=", "opt\0x-opt\0", 1, "opt", "", "first");
2N/A do_fstab_filter_options("first=1,opt", "opt\0x-opt\0", 1, "opt", NULL, "first=1");
2N/A do_fstab_filter_options("first=1,x-opt", "opt\0x-opt\0", 1, "x-opt", NULL, "first=1");
2N/A
2N/A do_fstab_filter_options("first,opt=0,last=1", "opt\0x-opt\0", 1, "opt", "0", "first,last=1");
2N/A do_fstab_filter_options("first=1,opt=0,last=2", "x-opt\0opt\0", 1, "opt", "0", "first=1,last=2");
2N/A do_fstab_filter_options("first,opt,last", "opt\0", 1, "opt", NULL, "first,last");
2N/A do_fstab_filter_options("first=1,opt,last", "x-opt\0opt\0", 1, "opt", NULL, "first=1,last");
2N/A do_fstab_filter_options("first=,opt,last", "opt\0noopt\0", 1, "opt", NULL, "first=,last");
2N/A
2N/A /* check repeated options */
2N/A do_fstab_filter_options("first,opt=0,noopt=1,last=1", "opt\0noopt\0", 1, "noopt", "1", "first,last=1");
2N/A do_fstab_filter_options("first=1,opt=0,last=2,opt=1", "opt\0", 1, "opt", "1", "first=1,last=2");
2N/A do_fstab_filter_options("x-opt=0,x-opt=1", "opt\0x-opt\0", 1, "x-opt", "1", "");
2N/A do_fstab_filter_options("opt=0,x-opt=1", "opt\0x-opt\0", 1, "x-opt", "1", "");
2N/A
2N/A /* check that semicolons are not misinterpreted */
2N/A do_fstab_filter_options("opt=0;", "opt\0", 1, "opt", "0;", "");
2N/A do_fstab_filter_options("opt;=0", "x-opt\0opt\0noopt\0x-noopt\0", 0, NULL, NULL, NULL);
2N/A do_fstab_filter_options("opt;", "opt\0x-opt\0", 0, NULL, NULL, NULL);
2N/A
2N/A /* check that spaces are not misinterpreted */
2N/A do_fstab_filter_options("opt=0 ", "opt\0", 1, "opt", "0 ", "");
2N/A do_fstab_filter_options("opt =0", "x-opt\0opt\0noopt\0x-noopt\0", 0, NULL, NULL, NULL);
2N/A do_fstab_filter_options(" opt ", "opt\0x-opt\0", 0, NULL, NULL, NULL);
2N/A
2N/A /* check function will NULL args */
2N/A do_fstab_filter_options(NULL, "opt\0", 0, NULL, NULL, "");
2N/A do_fstab_filter_options("", "opt\0", 0, NULL, NULL, "");
2N/A}
static void test_fstab_find_pri(void) {
int pri = -1;
assert_se(fstab_find_pri("pri", &pri) == 0);
assert_se(pri == -1);
assert_se(fstab_find_pri("pri=11", &pri) == 1);
assert_se(pri == 11);
assert_se(fstab_find_pri("opt,pri=12,opt", &pri) == 1);
assert_se(pri == 12);
assert_se(fstab_find_pri("opt,opt,pri=12,pri=13", &pri) == 1);
assert_se(pri == 13);
}
static void test_fstab_yes_no_option(void) {
assert_se(fstab_test_yes_no_option("nofail,fail,nofail", "nofail\0fail\0") == true);
assert_se(fstab_test_yes_no_option("nofail,nofail,fail", "nofail\0fail\0") == false);
assert_se(fstab_test_yes_no_option("abc,cde,afail", "nofail\0fail\0") == false);
assert_se(fstab_test_yes_no_option("nofail,fail=0,nofail=0", "nofail\0fail\0") == true);
assert_se(fstab_test_yes_no_option("nofail,nofail=0,fail=0", "nofail\0fail\0") == false);
}
int main(void) {
test_fstab_filter_options();
test_fstab_find_pri();
test_fstab_yes_no_option();
}