#!/bin/sh
#
# rawhide - find files using pretty C expressions
# https://raf.org/rawhide
# https://github.com/raforg/rawhide
# https://codeberg.org/raforg/rawhide
#
# Copyright (C) 1990 Ken Stauffer, 2022 raf <raf@raf.org>
#
# 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 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <https://www.gnu.org/licenses/>.
#
# 20221011 raf <raf@raf.org>

. tests/.common

label="path buf size handling"

mkdir $d/0123456789
mkdir $d/0123456789/0123456789
touch $d/0123456789/0123456789/0123456789

test_pathbufsize()
{
	max="$1"
	cmd="$2"
	expected_stdout="$3"
	expected_stderr="$4"
	expected_rc="$5"
	desc="$6"

	max="`expr $max - 1`" # Convert buf size to max length
	test_rawhide "RAWHIDE_TEST_PATHLEN_MAX=$max $cmd" "$expected_stdout" "$expected_stderr" "$expected_rc" "$desc"
}

# Where buf size checks are:
# 1 - rawhide_search   - initial fpath
# 2 - rawhide_search   - appending trailing /
# 3 - rawhide_traverse - appending d_name
# 4 - rawhide_traverse - appending trailing /
#
# For 3 and 4, fpath grows as needed (in case we cross a filesystem boundary)

# This requires a path buf size of at least 44 to succeed without forcing
# the path buf to grow
# tests/.t03/0123456789/0123456789/0123456789

ln -s rawhide.conf rc # Avoid errors about rawhide.conf.d being too long
rh="./rh -Nn -f rc"
test_pathbufsize 10 "$rh $d" ""                                                                                   "./rh: path is too long: $d\n" 1 "too big - [10] initial path"
test_pathbufsize 11 "$rh $d" "$d\n$d/0123456789\n$d/0123456789/0123456789\n$d/0123456789/0123456789/0123456789\n" ""                             0 "too big - [11] appending / to initial path - so buffer grows (4)"
test_pathbufsize 12 "$rh $d" "$d\n$d/0123456789\n$d/0123456789/0123456789\n$d/0123456789/0123456789/0123456789\n" ""                             0 "too big - [12] appending d_name to initial path - so buffer grows (3)"
test_pathbufsize 21 "$rh $d" "$d\n$d/0123456789\n$d/0123456789/0123456789\n$d/0123456789/0123456789/0123456789\n" ""                             0 "too big - [21] appending d_name to initial path - so buffer grows (3)"
test_pathbufsize 22 "$rh $d" "$d\n$d/0123456789\n$d/0123456789/0123456789\n$d/0123456789/0123456789/0123456789\n" ""                             0 "too big - [22] appending / to subpath - so buffer grows (4)"
test_pathbufsize 23 "$rh $d" "$d\n$d/0123456789\n$d/0123456789/0123456789\n$d/0123456789/0123456789/0123456789\n" ""                             0 "too big - [23] appending d_name to subpath - so buffer grows (3)"
test_pathbufsize 32 "$rh $d" "$d\n$d/0123456789\n$d/0123456789/0123456789\n$d/0123456789/0123456789/0123456789\n" ""                             0 "too big - [32] appending d_name to subpath - so buffer grows (3)"
test_pathbufsize 33 "$rh $d" "$d\n$d/0123456789\n$d/0123456789/0123456789\n$d/0123456789/0123456789/0123456789\n" ""                             0 "too big - [33] appending / to subsubpath - so buffer grows (4)"
test_pathbufsize 34 "$rh $d" "$d\n$d/0123456789\n$d/0123456789/0123456789\n$d/0123456789/0123456789/0123456789\n" ""                             0 "too big - [34] appending d_name to subsubpath - so buffer grows (3)"
test_pathbufsize 43 "$rh $d" "$d\n$d/0123456789\n$d/0123456789/0123456789\n$d/0123456789/0123456789/0123456789\n" ""                             0 "too big - [43] appending d_name to subsubpath - so buffer grows (3)"
test_pathbufsize 44 "$rh $d" "$d\n$d/0123456789\n$d/0123456789/0123456789\n$d/0123456789/0123456789/0123456789\n" ""                             0 "[44] max"
test_pathbufsize 99 "$rh $d" "$d\n$d/0123456789\n$d/0123456789/0123456789\n$d/0123456789/0123456789/0123456789\n" ""                             0 "[99] small"
rm rc

finish

exit $errors

# vi:set ts=4 sw=4:
