diff options
author | David Howells <dhowells@redhat.com> | 2013-11-21 23:29:16 +0000 |
---|---|---|
committer | David Howells <dhowells@redhat.com> | 2013-11-21 23:29:16 +0000 |
commit | bd9de979c3b3929892f3e96bc79248fd8c3a291d (patch) | |
tree | 3adab07a9ef96bcc452f209526cc575b77818d0c | |
parent | ee7069af0231c87458e7ec445ae8036fe167c3ba (diff) | |
download | keyutils-bd9de979c3b3929892f3e96bc79248fd8c3a291d.tar.gz |
TEST: Fix version number handling
Fix the shell function version_less_than and associates in the toolbox to
handle -rcN in version numbers correctly so that kernel versions can be
compared.
Signed-off-by: David Howells <dhowells@redhat.com>
-rw-r--r-- | tests/toolbox.inc.sh | 69 | ||||
-rw-r--r-- | tests/vercmp.sh | 31 | ||||
-rw-r--r-- | tests/version.inc.sh | 98 |
3 files changed, 134 insertions, 64 deletions
diff --git a/tests/toolbox.inc.sh b/tests/toolbox.inc.sh index a013f3e..3f6dcac 100644 --- a/tests/toolbox.inc.sh +++ b/tests/toolbox.inc.sh @@ -10,6 +10,10 @@ # ############################################################################### +# Find the relative path from pwd to the directory holding this file +includes=${BASH_SOURCE[0]} +includes=${includes%/*}/ + echo === $OUTPUTFILE === endian=`file -L /proc/$$/exe` @@ -80,70 +84,7 @@ function toolbox_report_result() fi } -############################################################################### -# -# compare version numbers to see if the first is less (older) than the second -# -############################################################################### -function version_less_than () -{ - a=$1 - b=$2 - - if [ "$a" = "$b" ] - then - return 1 - fi - - # grab the leaders - x=${a%%-*} - y=${b%%-*} - - if [ "$x" = "$a" -o "$y" = "$b" ] - then - if [ "$x" = "$y" ] - then - [ "$x" = "$a" ] - else - __version_less_than_dot "$x" "$y" - fi - elif [ "$x" = "$y" ] - then - less_than "${a#*-}" "${b#*-}" - else - __version_less_than_dot "$x" "$y" - fi -} - -function __version_less_than_dot () -{ - a=$1 - b=$2 - - if [ "$a" = "$b" ] - then - return 1 - fi - - # grab the leaders - x=${a%%.*} - y=${b%%.*} - - if [ "$x" = "$a" -o "$y" = "$b" ] - then - if [ "$x" = "$y" ] - then - [ "$x" = "$a" ] - else - expr "$x" \< "$y" >/dev/null - fi - elif [ "$x" = "$y" ] - then - __version_less_than_dot "${a#*.}" "${b#*.}" - else - expr "$x" \< "$y" >/dev/null - fi -} +. $includes/version.inc.sh ############################################################################### # diff --git a/tests/vercmp.sh b/tests/vercmp.sh new file mode 100644 index 0000000..8310c01 --- /dev/null +++ b/tests/vercmp.sh @@ -0,0 +1,31 @@ +# Commandline-driver tester for version comparison shell functions +# +############################################################################### +# +# Copyright (C) 2013 Red Hat, Inc. All Rights Reserved. +# Written by David Howells (dhowells@redhat.com) +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version +# 2 of the License, or (at your option) any later version. +# +############################################################################### + +. ./version.inc.sh + +# +# Compare versions +# +if [ "$1" = "" -o "$2" = "" ] +then + echo "Missing version parameters" >&2 + exit 2 +fi + +if version_less_than $1 $2 +then + echo "$1 < $2" +else + echo "$1 >= $2" +fi diff --git a/tests/version.inc.sh b/tests/version.inc.sh new file mode 100644 index 0000000..383ba91 --- /dev/null +++ b/tests/version.inc.sh @@ -0,0 +1,98 @@ +# Version comparison shell functions +# +############################################################################### +# +# Copyright (C) 2005, 2013 Red Hat, Inc. All Rights Reserved. +# Written by David Howells (dhowells@redhat.com) +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version +# 2 of the License, or (at your option) any later version. +# +############################################################################### + +############################################################################### +# +# compare version numbers to see if the first is less (older) than the second +# +############################################################################### +function version_less_than () +{ + a=$1 + b=$2 + + if [ "$a" = "$b" ] + then + return 1 + fi + + # grab the leaders + a_version=${a%%-*} a_release=${a#*-} + b_version=${b%%-*} b_release=${b#*-} + + if [ "$a_version" = "$b_version" ] + then + case "$a_release" in + rc[0-9]*) + case "$b_release" in + rc[0-9]*) + __version_less_than_dot "${a_release#rc}" "${b_release#rc}" + return $? + ;; + *) + return 0; + ;; + esac + ;; + esac + + case "$b_release" in + rc[0-9]*) + return 1; + ;; + esac + + if [ "$a_version" = "$a" -o "$b_version" = "$b" ] + then + if [ "$a_version" = "$b_version" ] + then + [ "$a_version" = "$a" ] + else + __version_less_than_dot "$a_version" "$b_version" + fi + fi + else + __version_less_than_dot "$a_version" "$b_version" + fi +} + +function __version_less_than_dot () +{ + a=$1 + b=$2 + + if [ "$a" = "$b" ] + then + return 1 + fi + + # grab the leaders + x=${a%%.*} + y=${b%%.*} + + if [ "$x" = "$a" -o "$y" = "$b" ] + then + if [ "$x" = "$y" ] + then + [ "$x" = "$a" ] + else + expr "$x" \< "$y" >/dev/null + fi + elif [ "$x" = "$y" ] + then + __version_less_than_dot "${a#*.}" "${b#*.}" + else + expr "$x" \< "$y" >/dev/null + fi +} |