NUM_ERRS=0 if [ -z "$CLIENTBIN" ]; then echo "Meld client is not set" exit 1 fi TEST_BIN=/tmp/meld_bin TEST_CONF=/tmp/meld_test.conf TEST_CONF_DIR=/tmp/meld_test_dir # Cleanup Functionality to reset state cleanup() { rm -rf "$TEST_BIN" 2> /dev/null rm "$TEST_CONF" 2> /dev/null rm -rf "$TEST_CONF_DIR" 2> /dev/null } setup() { $CLIENTBIN $TEST_BIN init --force &> /dev/null || return 1 echo "opt1=false" > $TEST_CONF mkdir $TEST_CONF_DIR cp $TEST_CONF $TEST_CONF_DIR return 0 } # Test simple config push push_config() { $CLIENTBIN $TEST_BIN push $TEST_CONF &> /dev/null || return 1 OWNER=$(printf '%s' $TEST_CONF | sha512sum | cut -d ' ' -f1) CONTENT_HASH=$(sha512sum $TEST_CONF | cut -d ' ' -f1) # Check new dir in blobs created if [ ! -d "$TEST_BIN/blobs/$OWNER" ]; then echo "Failed to create correct blob dir" >&2 return 1 fi # Check file got placed in blob dir if [ ! -f "$TEST_BIN/blobs/$OWNER/1" ]; then echo "Failed to place v1 (1) in blob dir" >&2 return 1 fi # Check SQLite DB if [ ! "$CONTENT_HASH" = "$(sqlite3 "$TEST_BIN/meld.db" "SELECT id FROM versions WHERE owner='$OWNER';")" ]; then echo "Could not find config in the versions table" >&2 return 1 elif [ ! "1" = "$(sqlite3 "$TEST_BIN/meld.db" "SELECT ver FROM versions WHERE OWNER='$OWNER' ORDER BY ver DESC LIMIT 1;")" ]; then echo "Version not set in versions table" >&2 return 1 elif [ ! "$OWNER" = "$(sqlite3 "$TEST_BIN/meld.db" "SELECT id FROM configs WHERE id='$OWNER';")" ]; then echo "Could not find config in the configs table" >&2 return 1 fi return 0 } # Test simple config dir push push_config_dir() { $CLIENTBIN $TEST_BIN push $TEST_CONF_DIR &> /dev/null || return 1 FILE_OWNER=$(printf '%s' $TEST_CONF | sha512sum | cut -d ' ' -f1) DIR_OWNER=$(printf '%s' $TEST_CONF_DIR | sha512sum | cut -d ' ' -f1) # Check new dir in blobs created if [ ! -d "$TEST_BIN/blobs/$FILE_OWNER" ]; then echo "Dir: Failed to create correct blob dir" >&2 return 1 fi # Check version dir got placed in blob dir if [ ! -f "$TEST_BIN/blobs/$FILE_OWNER/1" ]; then echo "Dir: Failed to place v1 (1) in blob dir" >&2 return 1 fi # Check map file got placed in maps dir if [ ! -f "$TEST_BIN/maps/$DIR_OWNER-1" ]; then echo "Dir: Failed to place map file in maps dir" >&2 return 1 fi # Check SQLite DB if [ ! "DIR" = "$(sqlite3 "$TEST_BIN/meld.db" "SELECT id FROM versions WHERE OWNER='$DIR_OWNER';")" ]; then echo "Could not find config in the versions table" >&2 return 1 elif [ ! "1" = "$(sqlite3 "$TEST_BIN/meld.db" "SELECT ver FROM versions WHERE OWNER='$FILE_OWNER' ORDER BY ver DESC LIMIT 1;")" ]; then echo "Version not set in versions table" >&2 return 1 elif [ ! "$FILE_OWNER" = "$(sqlite3 "$TEST_BIN/meld.db" "SELECT id FROM configs WHERE id='$FILE_OWNER';")" ]; then echo "Could not find config in the configs table" >&2 return 1 fi return 0 } # Test simple config push with subset (this also tests the update of a config) push_config_subset() { $CLIENTBIN $TEST_BIN push -s "suite" $TEST_CONF 2>&1 || return 1 OWNER=$(printf '%s' $TEST_CONF | sha512sum | cut -d ' ' -f1) CONTENT_HASH=$(sha512sum $TEST_CONF | cut -d ' ' -f1) # Check SQLite DB if [ ! "suite" = "$(sqlite3 "$TEST_BIN/meld.db" "SELECT subset FROM configs WHERE id='$OWNER';")" ]; then echo "Could not find subset in the configs table" >&2 return 1 fi return 0 } # Test simple config push with family (this also tests the update of a config) push_config_family() { $CLIENTBIN $TEST_BIN push -f "suite" $TEST_CONF 2>&1 || return 1 OWNER=$(printf '%s' $TEST_CONF | sha512sum | cut -d ' ' -f1) CONTENT_HASH=$(sha512sum $TEST_CONF | cut -d ' ' -f1) # Check SQLite DB if [ ! "suite" = "$(sqlite3 "$TEST_BIN/meld.db" "SELECT family FROM configs WHERE id='$OWNER';")" ]; then echo "Could not find family in the configs table" >&2 return 1 fi return 0 } # Test simple config push push_config_update() { # update the config file echo "opt1=true" > $TEST_CONF $CLIENTBIN $TEST_BIN push $TEST_CONF &> /dev/null || return 1 OWNER=$(printf '%s' $TEST_CONF | sha512sum | cut -d ' ' -f1) CONTENT_HASH=$(sha512sum $TEST_CONF | cut -d ' ' -f1) # Ensure v1 still exists if [ ! -f "$TEST_BIN/blobs/$OWNER/1" ]; then echo "Failed to find the v1 of config" >&2 return 1 fi # Check v2 got placed in blob dir if [ ! -f "$TEST_BIN/blobs/$OWNER/2" ]; then echo "Failed to place v2 (2) in blob dir" >&2 return 1 fi # Check SQLite DB if [ ! "$CONTENT_HASH" = "$(sqlite3 "$TEST_BIN/meld.db" "SELECT id FROM versions WHERE OWNER='$OWNER' and ver=2;")" ]; then echo "Could not find update in the versions table" >&2 return 1 elif [ ! "2" = "$(sqlite3 "$TEST_BIN/meld.db" "SELECT ver FROM versions WHERE OWNER='$OWNER' ORDER BY ver DESC LIMIT 1;")" ]; then echo "Version not updated in versions table" >&2 return 1 elif [ ! "$OWNER" = "$(sqlite3 "$TEST_BIN/meld.db" "SELECT id FROM configs WHERE id='$OWNER';")" ]; then echo "Update not in the configs table" >&2 return 1 fi return 0 } # Test simple config dir push update (test force) push_config_dir_update() { $CLIENTBIN $TEST_BIN push $TEST_CONF_DIR &> /dev/null || return 1 FILE_OWNER=$(printf '%s' $TEST_CONF | sha512sum | cut -d ' ' -f1) DIR_OWNER=$(printf '%s' $TEST_CONF_DIR | sha512sum | cut -d ' ' -f1) # Check new dir in blobs created if [ ! -d "$TEST_BIN/blobs/$FILE_OWNER" ]; then echo "Dir Update: Failed to create correct blob dir" >&2 return 1 fi # Check version dir got placed in blob dir if [ ! -f "$TEST_BIN/blobs/$FILE_OWNER/2" ]; then echo "Dir Update: Failed to place v2 (2) in blob dir" >&2 return 1 fi # Check SQLite DB if [ ! "DIR" = "$(sqlite3 "$TEST_BIN/meld.db" "SELECT id FROM versions WHERE OWNER='$DIR_OWNER' ORDER BY ver DESC LIMIT 1;")" ]; then echo "Could not find config in the versions table" >&2 return 1 elif [ ! "2" = "$(sqlite3 "$TEST_BIN/meld.db" "SELECT ver FROM versions WHERE OWNER='$FILE_OWNER' ORDER BY ver DESC LIMIT 1;")" ]; then echo "Version not set in versions table" >&2 return 1 elif [ ! "$FILE_OWNER" = "$(sqlite3 "$TEST_BIN/meld.db" "SELECT id FROM configs WHERE id='$FILE_OWNER';")" ]; then echo "Could not find config in the configs table" >&2 return 1 elif [ ! "1" = "$(sqlite3 "$TEST_BIN/meld.db" "SELECT ver FROM maps WHERE id='$DIR_OWNER';")" ]; then echo "Could not find map version in the maps table" >&2 return 1 fi return 0 } # Test Setup cleanup setup || exit 1 # Tested Functions push_config || NUM_ERRS=$(( NUM_ERRS + $? )) push_config_dir || NUM_ERRS=$(( NUM_ERRS + $? )) push_config_subset || NUM_ERRS=$(( NUM_ERRS + $? )) push_config_family || NUM_ERRS=$(( NUM_ERRS + $? )) push_config_update || NUM_ERRS=$(( NUM_ERRS + $? )) push_config_dir_update || NUM_ERRS=$(( NUM_ERRS + $? )) #Test Teardown #cleanup if [ $NUM_ERRS -eq 0 ]; then echo "[+] SUCCESS" else echo "[-] FAILURE" >&2 fi exit $NUM_ERRS