<commit_before>#! /bin/bash

# Check that saving preserves mode and ownership; for this test to make
# much sense (if any) the user running it should have at least one
# supplementary group

root=$abs_top_builddir/build/preserve
hosts=$root/etc/hosts

rm -rf $root
mkdir -p $(dirname $hosts)

echo -e '127.0.0.1\tlocalhost' > $hosts

chmod 0600 $hosts
group=$(groups | tr ' ' '\n' | tail -n 1)
chgrp $group $hosts
chcon -t etc_t $hosts

augtool --nostdinc -r $root -I $abs_top_srcdir/lenses > /dev/null <<EOF
set /files/etc/hosts/1/alias alias.example.com
save
EOF
if [ $? != 0 ] ; then
    echo "augtool failed on existing file"
    exit 1
fi

act_group=$(ls -l $hosts | cut -d ' ' -f 4)
act_mode=$(ls -l $hosts | cut -d ' ' -f 1)
act_con=$(ls -Z $hosts | cut -d ' ' -f 5 | cut -d ':' -f 3)
if [ "x$group" != "x$act_group" ] ; then
    echo "Expected group $group but got $act_group"
    exit 1
fi

if [ x-rw------- != "x$act_mode" ] ; then
    echo "Expected mode 0600 but got $act_mode"
    exit 1
fi

if [ xetc_t != "x$act_con" ] ; then
    echo "Expected SELinux type etc_t but got $act_con"
    exit 1
fi

# Check that we create new files without error
rm -rf $root
mkdir -p $(dirname $hosts)
augtool --nostdinc -r $root -I $abs_top_srcdir/lenses > /dev/null <<EOF
set /files/etc/hosts/1/ipaddr 127.0.0.1
set /files/etc/hosts/1/canonical host.example.com
save
EOF
if [ $? != 0 ] ; then
    echo "augtool failed on new file"
    exit 1
fi
<commit_msg>test-preserve.sh: do not check SELinux context on non-SELinux systems<commit_after>#! /bin/bash

# Check that saving preserves mode and ownership; for this test to make
# much sense (if any) the user running it should have at least one
# supplementary group

root=$abs_top_builddir/build/preserve
hosts=$root/etc/hosts

rm -rf $root
mkdir -p $(dirname $hosts)

echo -e '127.0.0.1\tlocalhost' > $hosts

chmod 0600 $hosts
group=$(groups | tr ' ' '\n' | tail -n 1)
chgrp $group $hosts

[ -x /usr/bin/chcon ] && selinux=yes || selinux=no
if [ $selinux = yes ] ; then
  /usr/bin/chcon -t etc_t $hosts > /dev/null 2>/dev/null || selinux=no
fi

augtool --nostdinc -r $root -I $abs_top_srcdir/lenses > /dev/null <<EOF
set /files/etc/hosts/1/alias alias.example.com
save
EOF
if [ $? != 0 ] ; then
    echo "augtool failed on existing file"
    exit 1
fi

act_group=$(ls -l $hosts | cut -d ' ' -f 4)
act_mode=$(ls -l $hosts | cut -d ' ' -f 1)
if [ $selinux = yes ] ; then
  act_con=$(ls -Z $hosts | cut -d ' ' -f 5 | cut -d ':' -f 3)
fi
if [ "x$group" != "x$act_group" ] ; then
    echo "Expected group $group but got $act_group"
    exit 1
fi

if [ x-rw------- != "x$act_mode" ] ; then
    echo "Expected mode 0600 but got $act_mode"
    exit 1
fi

if [ $selinux = yes -a xetc_t != "x$act_con" ] ; then
    echo "Expected SELinux type etc_t but got $act_con"
    exit 1
fi

# Check that we create new files without error
rm -rf $root
mkdir -p $(dirname $hosts)
augtool --nostdinc -r $root -I $abs_top_srcdir/lenses > /dev/null <<EOF
set /files/etc/hosts/1/ipaddr 127.0.0.1
set /files/etc/hosts/1/canonical host.example.com
save
EOF
if [ $? != 0 ] ; then
    echo "augtool failed on new file"
    exit 1
fi

{'fraction_non_alphanumeric': 0.14620995876942594, 'fraction_numerical': 0.02251823660006343, 'mean_word_length': 3.0127226463104324, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3674172', 'n_tokens_mistral': 1295, 'n_tokens_neox': 1200, 'n_words': 416}