18c2ecf20Sopenharmony_ci#!/bin/sh
28c2ecf20Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0-only
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci# common_tests - Shell script commonly used by pstore test scripts
58c2ecf20Sopenharmony_ci#
68c2ecf20Sopenharmony_ci# Copyright (C) Hitachi Ltd., 2015
78c2ecf20Sopenharmony_ci#  Written by Hiraku Toyooka <hiraku.toyooka.gu@hitachi.com>
88c2ecf20Sopenharmony_ci#
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci# Utilities
118c2ecf20Sopenharmony_cierrexit() { # message
128c2ecf20Sopenharmony_ci    echo "Error: $1" 1>&2
138c2ecf20Sopenharmony_ci    exit 1
148c2ecf20Sopenharmony_ci}
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ciabsdir() { # file_path
178c2ecf20Sopenharmony_ci    (cd `dirname $1`; pwd)
188c2ecf20Sopenharmony_ci}
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_cishow_result() { # result_value
218c2ecf20Sopenharmony_ci    if [ $1 -eq 0 ]; then
228c2ecf20Sopenharmony_ci	prlog "ok"
238c2ecf20Sopenharmony_ci    else
248c2ecf20Sopenharmony_ci	prlog "FAIL"
258c2ecf20Sopenharmony_ci	rc=1
268c2ecf20Sopenharmony_ci    fi
278c2ecf20Sopenharmony_ci}
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_cicheck_files_exist() { # type of pstorefs file
308c2ecf20Sopenharmony_ci    if [ -e ${1}-${backend}-0 ]; then
318c2ecf20Sopenharmony_ci	prlog "ok"
328c2ecf20Sopenharmony_ci	for f in `ls ${1}-${backend}-*`; do
338c2ecf20Sopenharmony_ci            prlog -e "\t${f}"
348c2ecf20Sopenharmony_ci	done
358c2ecf20Sopenharmony_ci    else
368c2ecf20Sopenharmony_ci	prlog "FAIL"
378c2ecf20Sopenharmony_ci	rc=1
388c2ecf20Sopenharmony_ci    fi
398c2ecf20Sopenharmony_ci}
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_cioperate_files() { # tested value, files, operation
428c2ecf20Sopenharmony_ci    if [ $1 -eq 0 ]; then
438c2ecf20Sopenharmony_ci	prlog
448c2ecf20Sopenharmony_ci	for f in $2; do
458c2ecf20Sopenharmony_ci	    prlog -ne "\t${f} ... "
468c2ecf20Sopenharmony_ci	    # execute operation
478c2ecf20Sopenharmony_ci	    $3 $f
488c2ecf20Sopenharmony_ci	    show_result $?
498c2ecf20Sopenharmony_ci	done
508c2ecf20Sopenharmony_ci    else
518c2ecf20Sopenharmony_ci	prlog " ... FAIL"
528c2ecf20Sopenharmony_ci	rc=1
538c2ecf20Sopenharmony_ci    fi
548c2ecf20Sopenharmony_ci}
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci# Parameters
578c2ecf20Sopenharmony_ciTEST_STRING_PATTERN="Testing pstore: uuid="
588c2ecf20Sopenharmony_ciUUID=`cat /proc/sys/kernel/random/uuid`
598c2ecf20Sopenharmony_ciTOP_DIR=`absdir $0`
608c2ecf20Sopenharmony_ciLOG_DIR=$TOP_DIR/logs/`date +%Y%m%d-%H%M%S`_${UUID}/
618c2ecf20Sopenharmony_ciREBOOT_FLAG=$TOP_DIR/reboot_flag
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci# Preparing logs
648c2ecf20Sopenharmony_ciLOG_FILE=$LOG_DIR/`basename $0`.log
658c2ecf20Sopenharmony_cimkdir -p $LOG_DIR || errexit "Failed to make a log directory: $LOG_DIR"
668c2ecf20Sopenharmony_cidate > $LOG_FILE
678c2ecf20Sopenharmony_ciprlog() { # messages
688c2ecf20Sopenharmony_ci    /bin/echo "$@" | tee -a $LOG_FILE
698c2ecf20Sopenharmony_ci}
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci# Starting tests
728c2ecf20Sopenharmony_circ=0
738c2ecf20Sopenharmony_ciprlog "=== Pstore unit tests (`basename $0`) ==="
748c2ecf20Sopenharmony_ciprlog "UUID="$UUID
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ciprlog -n "Checking pstore backend is registered ... "
778c2ecf20Sopenharmony_cibackend=`cat /sys/module/pstore/parameters/backend`
788c2ecf20Sopenharmony_cishow_result $?
798c2ecf20Sopenharmony_ciprlog -e "\tbackend=${backend}"
808c2ecf20Sopenharmony_ciprlog -e "\tcmdline=`cat /proc/cmdline`"
818c2ecf20Sopenharmony_ciif [ $rc -ne 0 ]; then
828c2ecf20Sopenharmony_ci    exit 1
838c2ecf20Sopenharmony_cifi
84