test-env-replace.c revision 1f28b2deb4e118cd2d2a5138ccb4cc45841c136d
796b06c21b62d13c9021e2fbd9c58a5c6edb2764Kay Sievers/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
796b06c21b62d13c9021e2fbd9c58a5c6edb2764Kay Sievers
796b06c21b62d13c9021e2fbd9c58a5c6edb2764Kay Sievers/***
796b06c21b62d13c9021e2fbd9c58a5c6edb2764Kay Sievers This file is part of systemd.
796b06c21b62d13c9021e2fbd9c58a5c6edb2764Kay Sievers
cf7ebcea78223b95b2676b97e1d2aad16caa3c86Kay Sievers Copyright 2010 Lennart Poettering
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers systemd is free software; you can redistribute it and/or modify it
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers under the terms of the GNU Lesser General Public License as published by
1d09582ab9d8bc619de3a01f01d614eab44d31c0Kay Sievers the Free Software Foundation; either version 2.1 of the License, or
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers (at your option) any later version.
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers
378cf88f72a9cda84baf703ed24f54c8c539fdcfKay Sievers systemd is distributed in the hope that it will be useful, but
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers WITHOUT ANY WARRANTY; without even the implied warranty of
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers Lesser General Public License for more details.
c35ddc5b69ef1911de39933329eda0b569cae4b9Kay Sievers
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers You should have received a copy of the GNU Lesser General Public License
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers along with systemd; If not, see <http://www.gnu.org/licenses/>.
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers***/
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers#include <unistd.h>
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers#include <string.h>
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers#include "util.h"
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers#include "strv.h"
c35ddc5b69ef1911de39933329eda0b569cae4b9Kay Sievers
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sieversstatic void test_strv_env_merge(void) {
1d09582ab9d8bc619de3a01f01d614eab44d31c0Kay Sievers _cleanup_strv_free_ char **a = NULL, **b = NULL, **r = NULL;
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers a = strv_new("FOO=BAR", "WALDO=WALDO", "WALDO=", "PIEP", "SCHLUMPF=SMURF", NULL);
1d09582ab9d8bc619de3a01f01d614eab44d31c0Kay Sievers b = strv_new("FOO=KKK", "FOO=", "PIEP=", "SCHLUMPF=SMURFF", "NANANANA=YES", NULL);
1d09582ab9d8bc619de3a01f01d614eab44d31c0Kay Sievers
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers r = strv_env_merge(2, a, b);
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers assert(streq(r[0], "FOO="));
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers assert(streq(r[1], "WALDO="));
f9cd22249dbdcebe2ab54eea56c0b32e2a1c2ce5Mantas Mikulėnas assert(streq(r[2], "PIEP"));
f9cd22249dbdcebe2ab54eea56c0b32e2a1c2ce5Mantas Mikulėnas assert(streq(r[3], "SCHLUMPF=SMURFF"));
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers assert(streq(r[4], "PIEP="));
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers assert(streq(r[5], "NANANANA=YES"));
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers assert(strv_length(r) == 6);
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers strv_env_clean(r);
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers assert(streq(r[0], "PIEP"));
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers assert(streq(r[1], "SCHLUMPF=SMURFF"));
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers assert(streq(r[2], "NANANANA=YES"));
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers assert(strv_length(r) == 3);
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers}
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers
378cf88f72a9cda84baf703ed24f54c8c539fdcfKay Sieversstatic void test_replace_env_arg(void) {
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers const char *env[] = {
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers "FOO=BAR BAR",
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers "BAR=waldo",
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers NULL
c35ddc5b69ef1911de39933329eda0b569cae4b9Kay Sievers };
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers const char *line[] = {
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers "FOO$FOO",
09d8f5d757515122d7b726218e249621e564157cKay Sievers "FOO$FOOFOO",
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers "FOO${FOO}$FOO",
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers "FOO${FOO}",
09d8f5d757515122d7b726218e249621e564157cKay Sievers "${FOO}",
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers "$FOO",
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers "$FOO$FOO",
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers "${FOO}${BAR}",
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers "${FOO",
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers NULL
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers };
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers _cleanup_strv_free_ char **r = NULL;
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers r = replace_env_argv((char**) line, (char**) env);
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers assert(streq(r[0], "FOO$FOO"));
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers assert(streq(r[1], "FOO$FOOFOO"));
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers assert(streq(r[2], "FOOBAR BAR$FOO"));
c35ddc5b69ef1911de39933329eda0b569cae4b9Kay Sievers assert(streq(r[3], "FOOBAR BAR"));
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers assert(streq(r[4], "BAR BAR"));
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers assert(streq(r[5], "BAR"));
09d8f5d757515122d7b726218e249621e564157cKay Sievers assert(streq(r[6], "BAR"));
09d8f5d757515122d7b726218e249621e564157cKay Sievers assert(streq(r[7], "BAR BARwaldo"));
09d8f5d757515122d7b726218e249621e564157cKay Sievers assert(streq(r[8], "${FOO"));
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers assert(strv_length(r) == 9);
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers}
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sieversstatic void test_one_normalize(const char *input, const char *output)
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers{
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers _cleanup_free_ char *t;
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers t = normalize_env_assignment(input);
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers assert(streq(t, output));
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers}
c35ddc5b69ef1911de39933329eda0b569cae4b9Kay Sievers
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sieversstatic void test_normalize_env_assignment(void) {
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers test_one_normalize("foo=bar", "foo=bar");
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers test_one_normalize("=bar", "=bar");
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers test_one_normalize("foo=", "foo=");
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers test_one_normalize("=", "=");
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers test_one_normalize("", "");
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers test_one_normalize("a=\"waldo\"", "a=waldo");
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers test_one_normalize("a=\"waldo", "a=\"waldo");
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers test_one_normalize("a=waldo\"", "a=waldo\"");
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers test_one_normalize("a=\'", "a='");
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers test_one_normalize("a=\'\'", "a=");
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers test_one_normalize(" xyz ", "xyz");
f9cd22249dbdcebe2ab54eea56c0b32e2a1c2ce5Mantas Mikulėnas test_one_normalize(" xyz = bar ", "xyz=bar");
f9cd22249dbdcebe2ab54eea56c0b32e2a1c2ce5Mantas Mikulėnas test_one_normalize(" xyz = 'bar ' ", "xyz=bar ");
cf7ebcea78223b95b2676b97e1d2aad16caa3c86Kay Sievers test_one_normalize(" ' xyz' = 'bar ' ", "' xyz'=bar ");
cf7ebcea78223b95b2676b97e1d2aad16caa3c86Kay Sievers}
cf7ebcea78223b95b2676b97e1d2aad16caa3c86Kay Sievers
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sieversint main(int argc, char *argv[]) {
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers test_strv_env_merge();
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers test_replace_env_arg();
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers test_normalize_env_assignment();
1d09582ab9d8bc619de3a01f01d614eab44d31c0Kay Sievers
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers return 0;
8db6dcfd3c3d19d35767f04884a99368f6c64b36Kay Sievers}
378cf88f72a9cda84baf703ed24f54c8c539fdcfKay Sievers