From 78ee1c72c670a71bfd165448676fc65bff802916 Mon Sep 17 00:00:00 2001 From: gramanas Date: Mon, 19 Nov 2018 19:11:06 +0200 Subject: Add ability to use ck without secret dir --- ck.1 | 20 +++++++++--- src/actions.h | 1 + src/add.c | 15 ++++++--- src/clparser.h | 2 +- src/confparser.c | 12 ++++---- src/confparser.h | 13 ++++---- src/dblayer.c | 21 +++++++++++++ src/dblayer.h | 2 +- src/init.c | 45 ++++++++++++++++++++++----- src/list.c | 2 +- src/queries.c | 32 +++++++++++++++++++ src/queries.h | 8 +++++ test/00_init | 24 --------------- test/01_add | 48 ----------------------------- test/02_list | 27 ---------------- test/03_delete | 55 --------------------------------- test/04_search | 22 ------------- test/05_restore | 46 ---------------------------- test/06_edit | 83 -------------------------------------------------- test/add.sh | 48 +++++++++++++++++++++++++++++ test/delete.sh | 55 +++++++++++++++++++++++++++++++++ test/edit.sh | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++ test/init.sh | 24 +++++++++++++++ test/init_no_secret.sh | 38 +++++++++++++++++++++++ test/list.sh | 27 ++++++++++++++++ test/restore.sh | 46 ++++++++++++++++++++++++++++ test/search.sh | 22 +++++++++++++ 27 files changed, 485 insertions(+), 336 deletions(-) delete mode 100644 test/00_init delete mode 100644 test/01_add delete mode 100644 test/02_list delete mode 100644 test/03_delete delete mode 100644 test/04_search delete mode 100644 test/05_restore delete mode 100644 test/06_edit create mode 100644 test/add.sh create mode 100644 test/delete.sh create mode 100644 test/edit.sh create mode 100644 test/init.sh create mode 100644 test/init_no_secret.sh create mode 100644 test/list.sh create mode 100644 test/restore.sh create mode 100644 test/search.sh diff --git a/ck.1 b/ck.1 index 205270e..9944b3f 100644 --- a/ck.1 +++ b/ck.1 @@ -14,7 +14,8 @@ ck \- manage configuration across the system \" Init .SY ck .B init -.I VERSION_CONTROL_DIR SECRET_DIR +.I VERSION_CONTROL_DIR +.RI [ SECRET_DIR ] .YS \" Add .SY ck @@ -100,7 +101,7 @@ based command line interface. needs a database and an rc file to run. It also needs two directories (stored in the rc file), the .I VERSION_CONRTOL_DIR -and the +and (optionally) the .IR SECRET_DIR . This is where the configurations will end up after they are added to .BR ck . @@ -217,14 +218,24 @@ database .RI ( ckdb ) and initialize it. Create the ck config file .RI ( ckrc ) -and add the directory paths to it. +and add the directory paths to it. If +.I SECRET_DIR +is not passed, the +.B \-s +flag will be disabled in the +.B add +action, and this +.B ck +instance won't be able to +store secret configs. .TP 2 .B USAGE .ns .RS 2 .SY ck .B init -.I VERSION_CONTROL_DIR SECRET_DIR +.I VERSION_CONTROL_DIR +.RI [ SECRET_DIR ] .YS .RE .TP 2 @@ -254,6 +265,7 @@ flag in .EX $ ck init /home/ckuser/configs/vc home/ckuser/configs/sec $ ck i configs/vc configs/sec +$ ck i ~/scripts # no secret dir provided .EE .SS "ADD CONFIG" Add a diff --git a/src/actions.h b/src/actions.h index 1b7ef05..0cf4b8f 100644 --- a/src/actions.h +++ b/src/actions.h @@ -28,6 +28,7 @@ enum AddOptErrors { ADD_NO_ERR = 0, ADD_ERR_WRONG_CONFIG, ADD_ERR_LINK_CONFIG, + ADD_ERR_NO_SECRET, ADD_ERR_WRONG_FLAGS }; typedef enum AddOptErrors AddOptErr; diff --git a/src/add.c b/src/add.c index 574ac73..79227d1 100644 --- a/src/add.c +++ b/src/add.c @@ -204,7 +204,7 @@ static int add_transaction_try(DB *db, const AddOpt * const opt, const char *hom } static int link_config(const AddOpt *opt, const char* newPath) { - hLOG("Linking %s -> %s\n", newPath, opt->confPath); + hLOG("Linking %s -> %s", newPath, opt->confPath); if (util_symlink_file(newPath, opt->confPath) != 0) { ERR("Could not link file."); return -1; @@ -222,7 +222,7 @@ static int move_config(const AddOpt *opt, char *progDir, char *ret) { return -1; } strcpy(ret, newPath); - hLOG("Moving %s -> %s\n", opt->confPath, newPath); + hLOG("Moving %s -> %s", opt->confPath, newPath); if (util_move_file(opt->confPath, newPath) != 0) { ERR("Could not move file."); return -1; @@ -230,7 +230,7 @@ static int move_config(const AddOpt *opt, char *progDir, char *ret) { return 0; } -static AddOpt add_make_options(cklist *args) { +static AddOpt add_make_options(cklist *args, DB *db) { list_rewind(args); /* since we are here, the first two arguments must exist */ AddOpt addOpt = { @@ -254,6 +254,10 @@ static AddOpt add_make_options(cklist *args) { while (list_next(args)) { if (strcmp(list_get(args), "-s") == 0 && addOpt.secret == 0) { + if (!secret_enabled(db)) { + addOpt.err = ADD_ERR_NO_SECRET; + return addOpt; + } addOpt.secret = 1; } else if (strcmp(list_get(args), "-p") == 0 && addOpt.prime == 0) { addOpt.prime = 1; @@ -304,10 +308,13 @@ int run_ADD(UserOpt * opt, Conf *conf) { if (open_DB(&db, opt)) { return -1; } - AddOpt addOpt = add_make_options(opt->args); + AddOpt addOpt = add_make_options(opt->args, &db); switch (addOpt.err) { case ADD_NO_ERR: break; + case ADD_ERR_NO_SECRET: + ERR("Secret is not enabled for this ck instance."); + goto error; case ADD_ERR_LINK_CONFIG: ERR("%s is a link.", addOpt.confPath); goto error; diff --git a/src/clparser.h b/src/clparser.h index 4208a91..afb6260 100644 --- a/src/clparser.h +++ b/src/clparser.h @@ -22,7 +22,7 @@ /* NAME | MIN ACCEPTED ARGS | MAX ACCEPTED ARGS */ #define CK_ACTIONS \ - X(INIT, 2 , 2) \ + X(INIT, 1 , 2) \ X(ADD, 2, 4) \ X(DEL, 1, 2) \ X(EDIT, 1, 5) \ diff --git a/src/confparser.c b/src/confparser.c index 6c01e6d..48f0c71 100644 --- a/src/confparser.c +++ b/src/confparser.c @@ -16,7 +16,7 @@ ERRLOG(configfile); static const char * const CONFIG_NAME = "/ckrc"; void initialize_conf(Conf *c) { -#define X(var, str, name) \ +#define X(var, str, name, optional) \ c->var = NULL; CONFIG_VARIABLES_TABLE #undef X @@ -35,7 +35,7 @@ ConfVar match_variables(char *line, char matched[]) { if (line[0] == '#' || str_is_empty(line)) { return CV_NO_VAL_OR_COMMENT; } -#define X(var, str, name) \ +#define X(var, str, name, optional) \ if (sscanf(line, str, matched) == 1) { \ return CV_##var; \ } @@ -109,7 +109,7 @@ int config_file_parse(Conf *conf, UserOpt *opt) { return -1; } switch(match_variables(line, matched)) { -#define X(var, str, name) \ +#define X(var, str, name, optional) \ case CV_##var: \ conf->var = malloc(strlen(matched)+1); \ strcpy(conf->var, matched); \ @@ -131,8 +131,8 @@ int config_file_parse(Conf *conf, UserOpt *opt) { } /* Could add an optional row that would make the config var * optional. */ -#define X(var, str, name) \ - if (!conf->var) { \ +#define X(var, str, name, optional) \ + if (!optional && !conf->var) { \ ERR("Missing %s", name); \ return -1; \ } @@ -142,7 +142,7 @@ int config_file_parse(Conf *conf, UserOpt *opt) { } void free_conf(Conf *conf) { -#define X(var,str,name) \ +#define X(var, str, name, optional) \ if (conf->var) { \ free(conf->var); \ } diff --git a/src/confparser.h b/src/confparser.h index ebcf0c7..10e7e18 100644 --- a/src/confparser.h +++ b/src/confparser.h @@ -18,15 +18,16 @@ #include "clparser.h" -#define CONFIG_VARIABLES_TABLE \ - X(vc_dir, " version_control_dir = %s ", "Version Control directory") \ - X(scrt_dir, " secret_dir = %s " , "Secret directory") \ - X(home_dir, " home_dir = %s " , "Home directory") +/* name | match str | desc | optional */ +#define CONFIG_VARIABLES_TABLE \ + X(vc_dir, " version_control_dir = %s ", "Version Control directory", 0) \ + X(scrt_dir, " secret_dir = %s " , "Secret directory", 1) \ + X(home_dir, " home_dir = %s " , "Home directory", 0) enum ConfingVariables { CV_NO_VAL_OR_COMMENT, CV_WRONG_VAL, -#define X(var, str, name) CV_##var, +#define X(var, str, name, optional) CV_##var, CONFIG_VARIABLES_TABLE #undef X }; @@ -34,7 +35,7 @@ typedef enum ConfingVariables ConfVar; typedef struct ConfigValues Conf; struct ConfigValues { -#define X(var, str, name) char* var; +#define X(var, str, name, optional) char* var; CONFIG_VARIABLES_TABLE #undef X }; diff --git a/src/dblayer.c b/src/dblayer.c index f13236c..3a8dd4f 100644 --- a/src/dblayer.c +++ b/src/dblayer.c @@ -315,3 +315,24 @@ int get_program_paths(DB *db, cklist *ckl, const char* pName, int bname, int att } return -1; } + +int secret_enabled(DB *db) { + sqlite3_stmt *stmt; + int rc; + + char sql[STR_M] = ""; + dbh_form_query_secret_enabled(sql); + + rc = sqlite3_prepare_v2(db->ptr, sql, -1, &stmt, 0); + if (rc != SQLITE_OK) { + ERR("while preparing secret_enabled sql."); + return -2; + } + int enabled = 1; + while (sqlite3_step(stmt) == SQLITE_ROW) { + enabled = sqlite3_column_int(stmt, 0); + break; + } + sqlite3_finalize(stmt); + return enabled; +} diff --git a/src/dblayer.h b/src/dblayer.h index 08299f1..fb3ab90 100644 --- a/src/dblayer.h +++ b/src/dblayer.h @@ -63,8 +63,8 @@ int get_pid_from_cid(DB *db, int cid); void print_suggested_configs(DB *db, const char *pName); -/* list.c */ int list_get_paths(DB *db, cklist *ckl, int bName, int attr, const char *home); int list_get_programs(DB *db, cklist *ckl); +int secret_enabled(DB *db); #endif /* DBLAYER_H */ diff --git a/src/init.c b/src/init.c index 5f47b33..10f38cb 100644 --- a/src/init.c +++ b/src/init.c @@ -15,15 +15,18 @@ ERRLOG(init); static int init_create_config_file(UserOpt *opt) { char absVCdir[STR_L] = ""; - if (!util_file_exists(list_get_at(opt->args, 0), absVCdir)) { + list_rewind(opt->args); + if (!util_file_exists(list_get(opt->args), absVCdir)) { ERR("Version control directory: %s does not exist.", list_get_at(opt->args, 0)); return 1; } char absSRdir[STR_L] = ""; - if (!util_file_exists(list_get_at(opt->args, 1), absSRdir)) { - ERR("Secret directory: %s does not exist.", list_get_at(opt->args, 1)); - return 1; + if (list_next(opt->args)) { + if (!util_file_exists(list_get_at(opt->args, 1), absSRdir)) { + ERR("Secret directory: %s does not exist.", list_get_at(opt->args, 1)); + return 1; + } } if (!util_file_exists(opt->confDir, NULL)) { @@ -43,10 +46,13 @@ static int init_create_config_file(UserOpt *opt) { strcat(tmp, "\n"); fputs(tmp, f); - strcpy(tmp, "secret_dir = "); - strcat(tmp, absSRdir); - strcat(tmp, "\n"); - fputs(tmp, f); + /* if absSRdir is not equal to "" */ + if (strcmp(absSRdir, "")) { + strcpy(tmp, "secret_dir = "); + strcat(tmp, absSRdir); + strcat(tmp, "\n"); + fputs(tmp, f); + } strcpy(tmp, "home_dir = "); strcat(tmp, getenv("HOME")); @@ -57,6 +63,27 @@ static int init_create_config_file(UserOpt *opt) { return 0; } +static int set_secret_enabled(DB *db, int enabled) { + sqlite3_stmt *stmt; + int rc; + + char sql[STR_M] = ""; + dbh_form_query_set_secret_enabled(sql); + + rc = sqlite3_prepare_v2(db->ptr, sql, -1, &stmt, 0); + if (rc != SQLITE_OK) { + ERR("while preparing set_secret_enabled sql."); + return -2; + } + sqlite3_bind_int(stmt, 1, enabled); + if (sqlite3_step(stmt) != SQLITE_DONE) { + ERR("while excecuting set_secret_enabled sql."); + return -1; + } + sqlite3_finalize(stmt); + return 0; +} + static void init_make_tables(DB *db) { char sql[STR_L] = ""; dbh_form_query_make_tables(sql); @@ -84,6 +111,8 @@ int run_INIT(UserOpt * opt, Conf *conf) { return -1; } init_make_tables(&db); + /* If there are two arguments secret is enabled */ + set_secret_enabled(&db, list_size(opt->args) == 2); sqlite3_close(db.ptr); hLOG("Initialized empty ckdb."); return 0; diff --git a/src/list.c b/src/list.c index 52603b8..0976ac1 100644 --- a/src/list.c +++ b/src/list.c @@ -244,7 +244,7 @@ int run_LIST(UserOpt *opt, Conf *conf) { strcat(tmp, "ck configuration directory path: "); strcat(tmp, opt->confDir); list_add(the_list, tmp); -#define X(var, str, name) \ +#define X(var, str, name, optional) \ strcpy(tmp, ""); \ strcat(tmp, name); \ strcat(tmp, ": "); \ diff --git a/src/queries.c b/src/queries.c index 15e5acb..ddd9250 100644 --- a/src/queries.c +++ b/src/queries.c @@ -40,6 +40,14 @@ void dbh_form_query_make_tables(char *query) { strcat(tmp, COL_REL_CONFIG_ID); strcat(tmp, " INT NOT NULL);"); + strcat(tmp, "CREATE TABLE "); + strcat(tmp, TBL_CTX); + strcat(tmp, "("); + strcat(tmp, COL_CTX_KEY); + strcat(tmp, " TEXT NOT NULL, "); + strcat(tmp, COL_CTX_VAL); + strcat(tmp, " INT NOT NULL);"); + strcpy(query, tmp); } @@ -226,3 +234,27 @@ void dbh_form_query_get_pid_from_cid(char *query) { strcpy(query, tmp); } + +void dbh_form_query_secret_enabled(char *query) { + char tmp[STR_M] = "SELECT "; + strcat(tmp, COL_CTX_VAL); + strcat(tmp, " FROM "); + strcat(tmp, TBL_CTX); + strcat(tmp, " WHERE "); + strcat(tmp, COL_CTX_KEY); + strcat(tmp, " = \""); + strcat(tmp, KEY_CTX_SECRET); + strcat(tmp, "\";"); + + strcpy(query, tmp); +} + +void dbh_form_query_set_secret_enabled(char *query) { + char tmp[STR_L] = "INSERT INTO "; + strcat(tmp, TBL_CTX); + strcat(tmp, " VALUES(\""); + strcat(tmp, KEY_CTX_SECRET); + strcat(tmp, "\", ?);"); + + strcpy(query, tmp); +} diff --git a/src/queries.h b/src/queries.h index 7d5ec47..aaaa862 100644 --- a/src/queries.h +++ b/src/queries.h @@ -24,6 +24,7 @@ #define TBL_PROGRAM "PROGRAM" #define TBL_CONFIG "CONFIG" #define TBL_REL "REL" +#define TBL_CTX "CONTEXT" #define COL_PROGRAM_ID "ID" #define COL_PROGRAM_NAME "NAME" @@ -36,6 +37,11 @@ #define COL_REL_PROGRAM_ID "PID" #define COL_REL_CONFIG_ID "CID" +#define COL_CTX_KEY "KEY" +#define COL_CTX_VAL "VAL" + +#define KEY_CTX_SECRET "secret_enabled" + #define __BEGIN_TRANSACTION__ sqlite3_exec(db->ptr, "BEGIN TRANSACTION;", NULL, NULL, NULL); #define __END_TRANSACTION__ sqlite3_exec(db->ptr, "END TRANSACTION;", NULL, NULL, NULL); @@ -59,4 +65,6 @@ void dbh_form_query_select_from_joined_like(char *query, const char *selection, void dbh_form_query_delete_x_from_y(char *query, const char *x, const char *y); void dbh_form_query_count_program_relations(char *query); void dbh_form_query_get_pid_from_cid(char *query); +void dbh_form_query_secret_enabled(char *query); +void dbh_form_query_set_secret_enabled(char *query); #endif /* DBHELPER_H */ diff --git a/test/00_init b/test/00_init deleted file mode 100644 index a38c81c..0000000 --- a/test/00_init +++ /dev/null @@ -1,24 +0,0 @@ -#!/bin/bash - -running init - -mkdir -p $TEST_LOCATION/vc -mkdir $TEST_LOCATION/sec - -exec $BIN/ck -c $BIN init $TEST_LOCATION/vc $TEST_LOCATION/sec >&${V} & -wait $! - -if [ $? -ne 0 ]; then - err "ck crashed." -fi - -if [ ! -f $BIN/ckrc ]; then - err "Config file not created." -fi - -if [ ! -f $BIN/ckdb ]; then - err "DB file not created." -fi - -clear_tests > /dev/null 2>&1 -echo -e $PASS diff --git a/test/01_add b/test/01_add deleted file mode 100644 index 05cf13f..0000000 --- a/test/01_add +++ /dev/null @@ -1,48 +0,0 @@ -#!/bin/bash - -init add - -# setup test configs -echo "Test 1" > $BIN/test1.conf -echo "Test 2" > $BIN/test2.conf -echo "Test 3" > $BIN/test3.conf - -function run_add { - # add configs to ck - exec $BIN/ck -c $BIN add $1 $BIN/$2 $3 $4 >&${V} & - wait $! - - if [ $? -ne 0 ]; then - err "ck crashed." - fi - - # check db - if [ "$(sqlite3 $BIN/ckdb "select name from program where name = '$1'")" != "$1" ]; then - err "$1 is not in the db." - fi - - if [ "$($BIN/ck -c $BIN ls -p $1)" != "$BIN/$2" ]; then - err "$2 is not in the db." - fi - - # check files - FOLDER=vc - if [[ "$3" = "-s" || "$4" = "-s" ]]; then - FOLDER=sec - fi - - if [ ! -f $TEST_LOCATION/$FOLDER/$1/$2 ]; then - err "$2 No move (add $3 $4)" - fi - - if [ ! -L $BIN/$2 ]; then - err "$2 No symlink (add $3 $4)" - fi -} - -run_add prog1 test1.conf -p -run_add prog2 test2.conf -s -run_add prog3 test3.conf -p -s - -clear_tests -echo -e $PASS diff --git a/test/02_list b/test/02_list deleted file mode 100644 index e330dad..0000000 --- a/test/02_list +++ /dev/null @@ -1,27 +0,0 @@ -#!/bin/bash - -init list - -# add configs to ck -path1=$BIN/test1.conf -path2=$BIN/test2.conf -path3=$BIN/test3.conf - -add_config prog1 $path1 -add_config prog2 $path2 -add_config prog3 $path3 - -for i in $($BIN/ck -c $BIN list paths); do - if [[ "$i" != "$path1" ]] && [[ "$i" != "$path2" ]] && [[ "$i" != "$path3" ]]; then - err "Listing paths" - fi -done - -for i in $($BIN/ck -c $BIN list programs); do - if [[ "$i" != "prog1" ]] && [[ "$i" != "prog2" ]] && [[ "$i" != "prog3" ]]; then - err "Listing programs" - fi -done - -clear_tests -echo -e $PASS diff --git a/test/03_delete b/test/03_delete deleted file mode 100644 index 7e31d66..0000000 --- a/test/03_delete +++ /dev/null @@ -1,55 +0,0 @@ -#!/bin/bash - -init delete - -# setup test configs -path1=$BIN/test1.conf -path2=$BIN/test2.conf -path3=$BIN/test3.conf -path4=$BIN/test4.conf - -add_config prog1 $path1 -add_config prog1 $path2 - -add_config prog2 $path3 -add_config prog2 $path4 - -# delete path1 -exec $BIN/ck -c $BIN del prog1 `basename $path1` >&${V} & -wait $! - -for i in $($BIN/ck -c $BIN list paths); do - if [[ "$i" == "$path1" ]]; then - err "Couldn't delete path." - exit 1 - fi -done - -# delete path2 (also prog1) -exec $BIN/ck -c $BIN del prog1 `basename $path2` >&${V} & -wait $! - -for i in $($BIN/ck -c $BIN list programs); do - if [[ "$i" == "prog1" ]]; then - err "Removing all configs should delete the correspondig program." - fi -done - -# delete prog2 -exec $BIN/ck -c $BIN del prog2 >&${V} & -wait $! - -for i in $($BIN/ck -c $BIN list paths); do - if [[ "$i" == "$path3" ]] || [[ "$i" == "$path4" ]]; then - err "Removing the program should delete all it's configs." - fi -done - -for i in $($BIN/ck -c $BIN list programs); do - if [[ "$i" == "prog1" ]]; then - err "Couldn't remove program." - fi -done - -clear_tests -echo -e $PASS diff --git a/test/04_search b/test/04_search deleted file mode 100644 index a315730..0000000 --- a/test/04_search +++ /dev/null @@ -1,22 +0,0 @@ -#!/bin/bash - -init search - -# setup test configs -path1=$BIN/test1.conf -path2=$BIN/test2.conf -path3=$BIN/test3.conf -path4=$BIN/test4.conf - -add_config prog1 $path1 "search-term" -add_config prog1 $path2 "search-term" -add_config prog2 $path3 "search-term" -add_config prog3 $path4 "search-term" - -# search -if [[ $($BIN/ck -c $BIN search search-term | wc -l) != 4 ]]; then - err "search failed." -fi - -clear_tests -echo -e $PASS diff --git a/test/05_restore b/test/05_restore deleted file mode 100644 index 2d60cb9..0000000 --- a/test/05_restore +++ /dev/null @@ -1,46 +0,0 @@ -#!/bin/bash - -init restore - -# setup test configs -path1=$BIN/test1.conf -path2=$BIN/test2.conf -path3=$BIN/test3.conf -path4=$BIN/test4.conf - -add_config prog1 $path1 -add_config prog1 $path2 - -add_config prog2 $path3 -add_config prog2 $path4 - -# delete prog1 links -rm "$path1" "$path2" - -# restore them -exec $BIN/ck -c $BIN restore -p prog1 >&${V} & -wait $! - -for i in $($BIN/ck -c $BIN list -p prog1); do - if [[ ! -L "$i" ]]; then - err "Couldn't restore path $i" - exit 1 - fi -done - -## delete all links -rm "$path1" "$path2" "$path3" "$path4" - -# restore all -exec $BIN/ck -c $BIN restore all >&${V} & -wait $! - -for i in $($BIN/ck -c $BIN list paths); do - if [[ ! -L "$i" ]]; then - err "Couldn't restore path $i" - exit 1 - fi -done - -clear_tests -echo -e $PASS diff --git a/test/06_edit b/test/06_edit deleted file mode 100644 index 65419b1..0000000 --- a/test/06_edit +++ /dev/null @@ -1,83 +0,0 @@ -#!/bin/bash - -init edit - -# add configs to ck -path1=$BIN/test1.conf -path2=$BIN/test2.conf -path3=$BIN/test3.conf -path4=$BIN/test4.conf -path5=$BIN/test5.conf - -add_config prog1 $path1 "" -p -add_config prog1 $path2 - -add_config prog2 $path3 -add_config prog2 $path4 - -add_config prog3 $path5 - -# edit primary -exec $BIN/ck -c $BIN e prog1 --command ":" > __test_file & -wait $! - -TEST_STR=$(cat __test_file) -EXPECTED_STR="Running: : $(realpath $path1)" - -echo "Expected: $EXPECTED_STR" >&${V} -echo " Actual: $TEST_STR" >&${V} - -if [[ "$TEST_STR" != "$EXPECTED_STR" ]]; then - err "Worng edit." - exit 1; -fi - -# edit specific -exec $BIN/ck -c $BIN e prog1 test2.conf --command ":" > __test_file & -wait $! - -echo "Expected: $EXPECTED_STR" >&${V} -echo " Actual: $TEST_STR" >&${V} - -TEST_STR=$(cat __test_file) -EXPECTED_STR="Running: : $(realpath $path2)" - -if [[ "$TEST_STR" != "$EXPECTED_STR" ]]; then - err "Worng edit." - exit 1; -fi - -# Ambiguous config -exec $BIN/ck -c $BIN e prog2 --command ":" > __test_file & -wait $! - -echo "Expected: $EXPECTED_STR" >&${V} -echo " Actual: $TEST_STR" >&${V} - -TEST_STR=$(head -1 __test_file) -EXPECTED_STR="Ambiguous config. Please type the config name after the program." - -if [[ "$TEST_STR" != "$EXPECTED_STR" ]]; then - err "Worng edit." - exit 1; -fi - -# solo program -exec $BIN/ck -c $BIN e prog3 --command ":" > __test_file & -wait $! - -echo "Expected: $EXPECTED_STR" >&${V} -echo " Actual: $TEST_STR" >&${V} - -TEST_STR=$(cat __test_file) -EXPECTED_STR="Running: : $(realpath $path5)" - -if [[ "$TEST_STR" != "$EXPECTED_STR" ]]; then - err "Worng edit." - exit 1; -fi - -rm __test_file - -clear_tests -echo -e $PASS diff --git a/test/add.sh b/test/add.sh new file mode 100644 index 0000000..05cf13f --- /dev/null +++ b/test/add.sh @@ -0,0 +1,48 @@ +#!/bin/bash + +init add + +# setup test configs +echo "Test 1" > $BIN/test1.conf +echo "Test 2" > $BIN/test2.conf +echo "Test 3" > $BIN/test3.conf + +function run_add { + # add configs to ck + exec $BIN/ck -c $BIN add $1 $BIN/$2 $3 $4 >&${V} & + wait $! + + if [ $? -ne 0 ]; then + err "ck crashed." + fi + + # check db + if [ "$(sqlite3 $BIN/ckdb "select name from program where name = '$1'")" != "$1" ]; then + err "$1 is not in the db." + fi + + if [ "$($BIN/ck -c $BIN ls -p $1)" != "$BIN/$2" ]; then + err "$2 is not in the db." + fi + + # check files + FOLDER=vc + if [[ "$3" = "-s" || "$4" = "-s" ]]; then + FOLDER=sec + fi + + if [ ! -f $TEST_LOCATION/$FOLDER/$1/$2 ]; then + err "$2 No move (add $3 $4)" + fi + + if [ ! -L $BIN/$2 ]; then + err "$2 No symlink (add $3 $4)" + fi +} + +run_add prog1 test1.conf -p +run_add prog2 test2.conf -s +run_add prog3 test3.conf -p -s + +clear_tests +echo -e $PASS diff --git a/test/delete.sh b/test/delete.sh new file mode 100644 index 0000000..7e31d66 --- /dev/null +++ b/test/delete.sh @@ -0,0 +1,55 @@ +#!/bin/bash + +init delete + +# setup test configs +path1=$BIN/test1.conf +path2=$BIN/test2.conf +path3=$BIN/test3.conf +path4=$BIN/test4.conf + +add_config prog1 $path1 +add_config prog1 $path2 + +add_config prog2 $path3 +add_config prog2 $path4 + +# delete path1 +exec $BIN/ck -c $BIN del prog1 `basename $path1` >&${V} & +wait $! + +for i in $($BIN/ck -c $BIN list paths); do + if [[ "$i" == "$path1" ]]; then + err "Couldn't delete path." + exit 1 + fi +done + +# delete path2 (also prog1) +exec $BIN/ck -c $BIN del prog1 `basename $path2` >&${V} & +wait $! + +for i in $($BIN/ck -c $BIN list programs); do + if [[ "$i" == "prog1" ]]; then + err "Removing all configs should delete the correspondig program." + fi +done + +# delete prog2 +exec $BIN/ck -c $BIN del prog2 >&${V} & +wait $! + +for i in $($BIN/ck -c $BIN list paths); do + if [[ "$i" == "$path3" ]] || [[ "$i" == "$path4" ]]; then + err "Removing the program should delete all it's configs." + fi +done + +for i in $($BIN/ck -c $BIN list programs); do + if [[ "$i" == "prog1" ]]; then + err "Couldn't remove program." + fi +done + +clear_tests +echo -e $PASS diff --git a/test/edit.sh b/test/edit.sh new file mode 100644 index 0000000..65419b1 --- /dev/null +++ b/test/edit.sh @@ -0,0 +1,83 @@ +#!/bin/bash + +init edit + +# add configs to ck +path1=$BIN/test1.conf +path2=$BIN/test2.conf +path3=$BIN/test3.conf +path4=$BIN/test4.conf +path5=$BIN/test5.conf + +add_config prog1 $path1 "" -p +add_config prog1 $path2 + +add_config prog2 $path3 +add_config prog2 $path4 + +add_config prog3 $path5 + +# edit primary +exec $BIN/ck -c $BIN e prog1 --command ":" > __test_file & +wait $! + +TEST_STR=$(cat __test_file) +EXPECTED_STR="Running: : $(realpath $path1)" + +echo "Expected: $EXPECTED_STR" >&${V} +echo " Actual: $TEST_STR" >&${V} + +if [[ "$TEST_STR" != "$EXPECTED_STR" ]]; then + err "Worng edit." + exit 1; +fi + +# edit specific +exec $BIN/ck -c $BIN e prog1 test2.conf --command ":" > __test_file & +wait $! + +echo "Expected: $EXPECTED_STR" >&${V} +echo " Actual: $TEST_STR" >&${V} + +TEST_STR=$(cat __test_file) +EXPECTED_STR="Running: : $(realpath $path2)" + +if [[ "$TEST_STR" != "$EXPECTED_STR" ]]; then + err "Worng edit." + exit 1; +fi + +# Ambiguous config +exec $BIN/ck -c $BIN e prog2 --command ":" > __test_file & +wait $! + +echo "Expected: $EXPECTED_STR" >&${V} +echo " Actual: $TEST_STR" >&${V} + +TEST_STR=$(head -1 __test_file) +EXPECTED_STR="Ambiguous config. Please type the config name after the program." + +if [[ "$TEST_STR" != "$EXPECTED_STR" ]]; then + err "Worng edit." + exit 1; +fi + +# solo program +exec $BIN/ck -c $BIN e prog3 --command ":" > __test_file & +wait $! + +echo "Expected: $EXPECTED_STR" >&${V} +echo " Actual: $TEST_STR" >&${V} + +TEST_STR=$(cat __test_file) +EXPECTED_STR="Running: : $(realpath $path5)" + +if [[ "$TEST_STR" != "$EXPECTED_STR" ]]; then + err "Worng edit." + exit 1; +fi + +rm __test_file + +clear_tests +echo -e $PASS diff --git a/test/init.sh b/test/init.sh new file mode 100644 index 0000000..a38c81c --- /dev/null +++ b/test/init.sh @@ -0,0 +1,24 @@ +#!/bin/bash + +running init + +mkdir -p $TEST_LOCATION/vc +mkdir $TEST_LOCATION/sec + +exec $BIN/ck -c $BIN init $TEST_LOCATION/vc $TEST_LOCATION/sec >&${V} & +wait $! + +if [ $? -ne 0 ]; then + err "ck crashed." +fi + +if [ ! -f $BIN/ckrc ]; then + err "Config file not created." +fi + +if [ ! -f $BIN/ckdb ]; then + err "DB file not created." +fi + +clear_tests > /dev/null 2>&1 +echo -e $PASS diff --git a/test/init_no_secret.sh b/test/init_no_secret.sh new file mode 100644 index 0000000..b135377 --- /dev/null +++ b/test/init_no_secret.sh @@ -0,0 +1,38 @@ +running init_no_secret + +mkdir -p $TEST_LOCATION/vc + +exec $BIN/ck -c $BIN init $TEST_LOCATION/vc >&${V} & +wait $! + +if [ $? -ne 0 ]; then + err "ck crashed." +fi + +if [ ! -f $BIN/ckrc ]; then + err "Config file not created." +fi + +if [ "$(sqlite3 $BIN/ckdb "select VAL from CONTEXT where KEY = 'secret_enabled';")" != "0" ]; then + err "secret_enabled should be 0." +fi + +# setup test configs +echo "Test 1" > $BIN/test1.conf + +exec $BIN/ck -c $BIN add prog1 test1.conf -s >&${V} & +wait $! + +if [ $? -eq 0 ]; then + err "Adding secret should fail" +fi + +exec $BIN/ck -c $BIN add prog1 test1.conf >&${V} & +wait $! + +if [ $? -ne 0 ]; then + err "ck crashed." +fi + +clear_tests > /dev/null 2>&1 +echo -e $PASS diff --git a/test/list.sh b/test/list.sh new file mode 100644 index 0000000..e330dad --- /dev/null +++ b/test/list.sh @@ -0,0 +1,27 @@ +#!/bin/bash + +init list + +# add configs to ck +path1=$BIN/test1.conf +path2=$BIN/test2.conf +path3=$BIN/test3.conf + +add_config prog1 $path1 +add_config prog2 $path2 +add_config prog3 $path3 + +for i in $($BIN/ck -c $BIN list paths); do + if [[ "$i" != "$path1" ]] && [[ "$i" != "$path2" ]] && [[ "$i" != "$path3" ]]; then + err "Listing paths" + fi +done + +for i in $($BIN/ck -c $BIN list programs); do + if [[ "$i" != "prog1" ]] && [[ "$i" != "prog2" ]] && [[ "$i" != "prog3" ]]; then + err "Listing programs" + fi +done + +clear_tests +echo -e $PASS diff --git a/test/restore.sh b/test/restore.sh new file mode 100644 index 0000000..2d60cb9 --- /dev/null +++ b/test/restore.sh @@ -0,0 +1,46 @@ +#!/bin/bash + +init restore + +# setup test configs +path1=$BIN/test1.conf +path2=$BIN/test2.conf +path3=$BIN/test3.conf +path4=$BIN/test4.conf + +add_config prog1 $path1 +add_config prog1 $path2 + +add_config prog2 $path3 +add_config prog2 $path4 + +# delete prog1 links +rm "$path1" "$path2" + +# restore them +exec $BIN/ck -c $BIN restore -p prog1 >&${V} & +wait $! + +for i in $($BIN/ck -c $BIN list -p prog1); do + if [[ ! -L "$i" ]]; then + err "Couldn't restore path $i" + exit 1 + fi +done + +## delete all links +rm "$path1" "$path2" "$path3" "$path4" + +# restore all +exec $BIN/ck -c $BIN restore all >&${V} & +wait $! + +for i in $($BIN/ck -c $BIN list paths); do + if [[ ! -L "$i" ]]; then + err "Couldn't restore path $i" + exit 1 + fi +done + +clear_tests +echo -e $PASS diff --git a/test/search.sh b/test/search.sh new file mode 100644 index 0000000..a315730 --- /dev/null +++ b/test/search.sh @@ -0,0 +1,22 @@ +#!/bin/bash + +init search + +# setup test configs +path1=$BIN/test1.conf +path2=$BIN/test2.conf +path3=$BIN/test3.conf +path4=$BIN/test4.conf + +add_config prog1 $path1 "search-term" +add_config prog1 $path2 "search-term" +add_config prog2 $path3 "search-term" +add_config prog3 $path4 "search-term" + +# search +if [[ $($BIN/ck -c $BIN search search-term | wc -l) != 4 ]]; then + err "search failed." +fi + +clear_tests +echo -e $PASS -- cgit v1.2.3