#!/usr/bin/env bash
# SPDX-License-Identifier: BSD-3-Clause
# Copyright 2018-2019, Intel Corporation
#
#
# daxio/TEST2 -- test for daxio utility; move data between two devices

. ../unittest/unittest.sh

require_test_type medium
require_fs_type any
require_dax_devices 2

setup

# must be done after setup, when daxio path is already known
require_binary $DAXIO$EXESUFFIX

LOG=out$UNITTEST_NUM.log
DATALOG=data$UNITTEST_NUM.log

DATA1=$DIR/data1.bin
DATA2=$DIR/data2.bin
DATAOUT1=$DIR/data_out1.bin
DATAOUT2=$DIR/data_out2.bin

# prepare file with test data
dd if=/dev/zero bs=1k count=2 2> prep$UNITTEST_NUM.log | tr '\0' 'x' > $DATA1
dd if=/dev/zero bs=1k count=2 2>> prep$UNITTEST_NUM.log | tr '\0' 'o' >> $DATA1
dd if=/dev/zero bs=1k count=2 2>> prep$UNITTEST_NUM.log | tr '\0' '1' > $DATA2
dd if=/dev/zero bs=1k count=2 2>> prep$UNITTEST_NUM.log | tr '\0' '2' >> $DATA2

# zero device (len in dec)
expect_normal_exit "$DAXIO$EXESUFFIX -z -o ${DEVICE_DAX_PATH[0]} -l 16384 2>$LOG"
expect_normal_exit "$DAXIO$EXESUFFIX -z -o ${DEVICE_DAX_PATH[1]} -l 16384 2>>$LOG"

# check if zeroed
expect_normal_exit "$CMPMAP$EXESUFFIX -z -l 16384 ${DEVICE_DAX_PATH[0]} &>>$LOG"
expect_normal_exit "$CMPMAP$EXESUFFIX -z -l 16384 ${DEVICE_DAX_PATH[1]} &>>$LOG"

# write data from files to Device DAX
expect_normal_exit "$DAXIO$EXESUFFIX -i $DATA1 -o ${DEVICE_DAX_PATH[0]} 2>>$LOG"
expect_normal_exit "$DAXIO$EXESUFFIX -i $DATA2 -o ${DEVICE_DAX_PATH[0]} -s 4096 2>>$LOG"

# move data from one Device DAX to another
expect_normal_exit "$DAXIO$EXESUFFIX -i ${DEVICE_DAX_PATH[0]} -o ${DEVICE_DAX_PATH[1]} -k 512 -s 0 -l 2048 2>>$LOG"
expect_normal_exit "$DAXIO$EXESUFFIX -i ${DEVICE_DAX_PATH[0]} -o ${DEVICE_DAX_PATH[1]} -k 5120 -s 3072 -l 2048 2>>$LOG"
expect_normal_exit "$DAXIO$EXESUFFIX -i ${DEVICE_DAX_PATH[0]} -o ${DEVICE_DAX_PATH[1]} -k 7168 -s 4096 -l 2048 2>>$LOG"

# check content
expect_normal_exit "$DDMAP$EXESUFFIX -i ${DEVICE_DAX_PATH[0]} -b 1 -n 16384 -r > $DATALOG"
expect_normal_exit "$DDMAP$EXESUFFIX -i ${DEVICE_DAX_PATH[1]} -b 1 -n 16384 -r >> $DATALOG"

check

pass
