view tests/renice.test @ 1613:96aa7ec74936 draft

Fix yet another sed bug. The s/// command would copy the \ of substitutions before deciding what to do with them (generally overwriting the \ with the new data). When the substitution was A) at the very end of the new string, B) resolved to nothing, it could leave a trailing \ that didn't belong there and didn't get overwritten because the "copy trailing data" part that copies the original string's null terminator already happened before the \ overwrote it. The ghostwheel() function restarts regexes after embedded NUL bytes, but if the string it's passed is _longer_ than the length it's told then it gets confused (and it means we're off the end of our allocation so segfaults are likely). Fix: test for \ first and move the "copy byte" logic into an else case.
author Rob Landley <rob@landley.net>
date Mon, 15 Dec 2014 03:34:55 -0600
parents 8700cbe1cb29
children
line wrap: on
line source

#!/bin/bash

# Copyright 2013 Robin Mittal <robinmittal.it@gmail.com>
# Copyright 2013 Divya Kothari <divya.s.kothari@gmail.com>

[ -f testing.sh ] && . testing.sh

#testing "name" "command" "result" "infile" "stdin"

fun_nice_val() 
{
  for each_proc in $@
  do
    echo `awk '{ print $18 }' /proc/${each_proc}/stat`
  done
}

# creating processes as a test data
yes >/dev/null &
proc1=$!
yes >/dev/null &
proc2=$!
yes >/dev/null &
proc3=$!
yes >/dev/null &
proc4=$!
yes >/dev/null &
proc5=$!

n_val1=`fun_nice_val $proc1`
n_val2=`fun_nice_val $proc2`
n_val3=`fun_nice_val $proc3`
n_val4=`fun_nice_val $proc4`
n_val5=`fun_nice_val $proc5`

# Redirecting errors to /dev/null
arg="2>/dev/null"

for n_v in "-1" "1"
do
  for n_o in "" " -p"
  do
    nice_val1=$((`fun_nice_val $proc1` + $n_v))
    nice_val2=$((`fun_nice_val $proc2` + $n_v))
    nice_val3=$((`fun_nice_val $proc3` + $n_v))
    nice_val4=$((`fun_nice_val $proc4` + $n_v))
    nice_val5=$((`fun_nice_val $proc5` + $n_v))
    testing "renice with -n=$n_v and with$n_o multiple_pids" \
      "renice -n $n_v$n_o $proc1 $proc2 $proc3 $proc4 $proc5 &&
       fun_nice_val $proc1 $proc2 $proc3 $proc4 $proc5" \
      "$nice_val1\n$nice_val2\n$nice_val3\n$nice_val4\n$nice_val5\n" "" ""
  
    nice_val1=$((`fun_nice_val $proc1` + $n_v))
    nice_val2=$((`fun_nice_val $proc2` + $n_v))
    nice_val3=$((`fun_nice_val $proc3` + $n_v))
    nice_val4=$((`fun_nice_val $proc4` + $n_v))
    nice_val5=$((`fun_nice_val $proc5` + $n_v))
    testing "renice with -n=$n_v and with$n_o multiple_pids (some invalid)" \
      "renice -n $n_v$n_o $proc1 $proc2 88888 99999 $proc3 $proc4 $proc5 $arg ||
       fun_nice_val $proc1 $proc2 $proc3 $proc4 $proc5" \
      "$nice_val1\n$nice_val2\n$nice_val3\n$nice_val4\n$nice_val5\n" "" ""
  done
done

# Starting Boundary Test Here .. 
loop_cnt=2
echo -n "TEST: Boundary value test for Id($proc1)..[old_nice_val/new_nice_val]:"
cnt=0
n_val=1
while [ $cnt -gt -1 ]
do
  old_nice_val=`fun_nice_val $proc1`
  new_nice_val=`renice -n $n_val $proc1 >/dev/null 2>&1 && fun_nice_val $proc1`
  echo -n "[$old_nice_val/$new_nice_val],"
  if [ $old_nice_val -eq 39 -a $old_nice_val -eq $new_nice_val ]
  then
    echo -n " [Equals 39,doing -1] "
    n_val="-1"
  elif [ $old_nice_val -eq 0 -a $old_nice_val -eq $new_nice_val ]
  then
    echo -n " [Equals 0,doing +1] "
    n_val="1"
  elif [ $new_nice_val -gt 39 -o $new_nice_val -lt 0 ]
  then
    echo " [Test Fail] "
    echo "FAIL: renice with step 1 ($proc1) (boundary value)"
    cnt="-1"
  elif [ $new_nice_val -eq $n_val1 -a $new_nice_val -eq $(($old_nice_val+1)) ]
  then
    cnt=$(($cnt + 1))
    if [ $cnt -eq $loop_cnt ]
    then
      echo " [Test Pass] "
      echo "PASS: renice with step 1 ($proc1) (boundary value)"
      cnt="-1"
    fi
  else
    dif=`echo $(($new_nice_val-$old_nice_val))`
    dif=`echo ${dif/-}`
    if [ $dif -ne 1 ]
    then
      echo " [Test Fail] "
      echo "FAIL: renice with step 1 ($proc1) (boundary value)"
      cnt="-1"
    fi
  fi
done
# Boundary test End

killall yes >/dev/null 2>&1