<commit_before>#!/bin/bash

dbDriver=postgres

: ${TESTCASE="aff_modify"}
FABRIC_CA="$GOPATH/src/github.com/hyperledger/fabric-ca"
SCRIPTDIR="$FABRIC_CA/scripts/fvt"
. $SCRIPTDIR/fabric-ca_utils
TESTDIR=/tmp/$TESTCASE
RC=0
NUMROLES=8

# defaults
declare -A defaultValues
defaultValues=([Maxenrollments]=2147483647 [Affiliation]='.' [Type]="user" [Passwd]="<PASSWORD>")

function tableCount() {
   driver="$1"
   tableType="$2"
   shift 2
   $SCRIPTDIR/fabric-ca_setup.sh -L -d $driver -D | sed -n "/$tableType:/,/^.*: *$/p" | sed '1,3d;$d' |
      awk -v s="$*" '
         BEGIN { n=split(s, terms) }
         {
            for (i in terms)
               if (match(tolower($0), tolower(terms[i]))) val[terms[i]]++
         }
         END { for (i in terms) {
                  printf terms[i]":"
                  print val[terms[i]] ? val[terms[i]] : "0"
               }
         }'
}

function displayRunningTotal() {
   tableCount $dbDriver Affiliations "planet0 planet1 planet2 planetx"
}

function verifyTotals() {
   totals="$(displayRunningTotal | sort | awk -F':' '{printf $2" "}')"
   expected="$(echo $@ | sed 's/ \+/ /')"
   test "${totals%% }" = "${expected:-"0"}" || return 1
}

function genAffYaml() {
   export FABRIC_CA_CLIENT_HOME=$TESTDIR/admin
   local Planet=(0 1 2)
   local Landmass=(0 1)
   local Country=(0 1)
   local Province=(0 1 2)
   local Locale=(0 1)
   local City=(0 1 2)
   local Hood=(0 1 2 3)
   echo "affiliations:"
   indent="${indent}  "
   echo "${indent}org1:"
   echo "${indent}  - department1"
   echo "${indent}  - department2"
   for P in ${Planet[@]}; do
     echo "${indent}Planet$P:"
     indent="${indent}  "
     for L in ${Landmass[@]}; do
       echo "${indent}Landmass$L:"
       indent="${indent}  "
        for C in ${Country[@]}; do
         echo "${indent}Country$C:"
         indent="${indent}  "
         for R in ${Province[@]}; do
            echo "${indent}Province$R:"
            indent="${indent}  "
           for O in ${Locale[@]}; do
             echo "${indent}Locale$O:"
             indent="${indent}  "
             for I in ${City[@]}; do
               echo "${indent}City$I:"
               indent="${indent}  "
               for H in ${Hood[@]}; do
                 echo "${indent}- Hood$H"
               done
               indent="${indent#  }"
             done
             indent="${indent#  }"
           done
           indent="${indent#  }"
         done
         indent="${indent#  }"
       done
       indent="${indent#  }"
     done
     indent="${indent#  }"
   done
   echo "${indent}org2:"
   echo "${indent}  - department1"
   echo "${indent}  - department2"
}

function setupServerEnv() {
   $SCRIPTDIR/fabric-ca_setup.sh -d $dbDriver -I -S -X -n1 -D -x $TESTDIR > $TESTDIR/server.log 2>&1
   enroll
   # Ensure affiliations cannot be deleted if --cfg.affiliations.allowremove not configured
   $FABRIC_CA_CLIENTEXEC affiliation remove org1 $URI -H $TESTDIR/admin/ 2>&1|
      grep 'Authorization failure' ||
         ErrorMsg "should not be able to delete 'org1', or wrong error msg"
   $SCRIPTDIR/fabric-ca_setup.sh -K

   # Generate a large affinity tree for testing;
   # this is way faster than adding with the cmd-line client
   genAffYaml >> $CA_CFG_PATH/runFabricCaFvt.yaml
   $SCRIPTDIR/fabric-ca_setup.sh -d $dbDriver -S -X -n1 -D -x $TESTDIR -- \
                    --cfg.affiliations.allowremove > $TESTDIR/server.log 2>&1
   # Sanity check the number of affilitations
   dbEntries=$(tableCount $dbDriver Affiliations ".*"| awk -F':' '{print $2}')
   # discount the summary line displayed in the above command
   let dbEntries--
   serverEntries="$(( $($FABRIC_CA_CLIENTEXEC affiliation list $URI -H $TESTDIR/admin/ | wc -l)  -1))"
   test "$dbEntries" -eq "$serverEntries" || ErrorMsg "Wrong number of affiliations: expected $dbEntries, got $serverEntries"
   displayRunningTotal
   verifyTotals "403 403 403 0" || ErrorMsg "Wrong number of affiliations"
}

function testAffiliationRefs() {
   # Ensure affiliations can be deleted if no ID's are referencing them
   $FABRIC_CA_CLIENTEXEC affiliation remove org1 $URI -H $TESTDIR/admin/ 2>&1 ||
      ErrorMsg "should be able to delete 'org1'"
   # Ensure affiliations cannot be deleted if ID's are referencing them
   $FABRIC_CA_CLIENTEXEC affiliation remove bank_b $URI -H $TESTDIR/admin/ 2>&1 &&
      ErrorMsg "should not be able to delete 'bank_b'"
}

function testAllowremove() {
   # ensure cfg.identities.allowremove flag is required
   $FABRIC_CA_CLIENTEXEC affiliation remove bank_b --force $URI -H $TESTDIR/admin/ 2>&1 |
      grep 'Authorization failure' ||
         ErrorMsg "should not be able to delete 'bank_b', or wrong error msg"
   # add cfg.identities.allowremove flag
   $SCRIPTDIR/fabric-ca_setup.sh -K
   $SCRIPTDIR/fabric-ca_setup.sh -d $dbDriver -S -X -n1 -D -x $TESTDIR -- \
                    --cfg.affiliations.allowremove --cfg.identities.allowremove > $TESTDIR/server.log 2>&1
   # try again
   $FABRIC_CA_CLIENTEXEC affiliation remove bank_b --force $URI -H $TESTDIR/admin/ 2>&1  || ErrorMsg "should be able to delete 'bank_b'"
   # make sure entries are deleted
   $SCRIPTDIR/fabric-ca_setup.sh -L -d $dbDriver -D | grep bank_b && ErrorMsg "'bank_b' not deleted"
   expected=$((dbEntries - 2))
   dbEntries=$(tableCount $dbDriver Affiliations ".*"| awk -F':' '{print $2}')
   let dbEntries--
   serverEntries="$(( $($FABRIC_CA_CLIENTEXEC affiliation list $URI -H $TESTDIR/admin/ | wc -l)  -1))"
   test "$expected" -eq "$serverEntries" || ErrorMsg "Wrong number of affiliations: expected $expected, got $serverEntries"
   displayRunningTotal
   verifyTotals "403 403 403 0" || ErrorMsg "Wrong number of affiliations"

   # Ensure all children are deleted
   $FABRIC_CA_CLIENTEXEC affiliation remove planet2.landmass1 $URI -H $TESTDIR/admin/ 2>&1  || ErrorMsg "should be able to delete 'planet2.landmass1'"
   # make sure entries are deleted
   $SCRIPTDIR/fabric-ca_setup.sh -L -d $dbDriver -D | grep "planet2.landmass1" && ErrorMsg "'planet2.landmass1' not deleted"
   expected=$((dbEntries - 201))
   dbEntries=$(tableCount $dbDriver Affiliations ".*"| awk -F':' '{print $2}')
   let dbEntries--
   serverEntries="$(( $($FABRIC_CA_CLIENTEXEC affiliation list $URI -H $TESTDIR/admin/ | wc -l)  -1))"
   test "$expected" -eq "$serverEntries" || ErrorMsg "Wrong number of affiliations: expected $expected, got $serverEntries"
   displayRunningTotal
   verifyTotals "403 403 202 0" || ErrorMsg "Wrong number of affiliations"
}

function testAffiliationMgr() {
   # Ensure affiliations can only be updated by authorized users
   enroll admin2 adminpw2
   $FABRIC_CA_CLIENTEXEC affiliation remove org2 $URI -H $TESTDIR/admin2 2>&1 |
      grep "User does not have attribute 'hf.AffiliationMgr'" ||
         ErrorMsg  "Should not be able to delete attributes, or wrong error msg"<commit_msg>[ FAB-8116 ] Fix bad merge causing CI failure<commit_after>#!/bin/bash

dbDriver=postgres

: ${TESTCASE="aff_modify"}
FABRIC_CA="$GOPATH/src/github.com/hyperledger/fabric-ca"
SCRIPTDIR="$FABRIC_CA/scripts/fvt"
. $SCRIPTDIR/fabric-ca_utils
TESTDIR=/tmp/$TESTCASE
RC=0
NUMROLES=8

# defaults
declare -A defaultValues
defaultValues=([Maxenrollments]=2147483647 [Affiliation]='.' [Type]="user" [Passwd]="<PASSWORD>")

function tableCount() {
   driver="$1"
   tableType="$2"
   shift 2
   $SCRIPTDIR/fabric-ca_setup.sh -L -d $driver -D | sed -n "/$tableType:/,/^.*: *$/p" | sed '1,3d;$d' |
      awk -v s="$*" '
         BEGIN { n=split(s, terms) }
         {
            for (i in terms)
               if (match(tolower($0), tolower(terms[i]))) val[terms[i]]++
         }
         END { for (i in terms) {
                  printf terms[i]":"
                  print val[terms[i]] ? val[terms[i]] : "0"
               }
         }'
}

function displayRunningTotal() {
   tableCount $dbDriver Affiliations "planet0 planet1 planet2 planetx"
}

function verifyTotals() {
   totals="$(displayRunningTotal | sort | awk -F':' '{printf $2" "}')"
   expected="$(echo $@ | sed 's/ \+/ /')"
   test "${totals%% }" = "${expected:-"0"}" || return 1
}

function genAffYaml() {
   export FABRIC_CA_CLIENT_HOME=$TESTDIR/admin
   local Planet=(0 1 2)
   local Landmass=(0 1)
   local Country=(0 1)
   local Province=(0 1 2)
   local Locale=(0 1)
   local City=(0 1 2)
   local Hood=(0 1 2 3)
   echo "affiliations:"
   indent="${indent}  "
   echo "${indent}org1:"
   echo "${indent}  - department1"
   echo "${indent}  - department2"
   for P in ${Planet[@]}; do
     echo "${indent}Planet$P:"
     indent="${indent}  "
     for L in ${Landmass[@]}; do
       echo "${indent}Landmass$L:"
       indent="${indent}  "
        for C in ${Country[@]}; do
         echo "${indent}Country$C:"
         indent="${indent}  "
         for R in ${Province[@]}; do
            echo "${indent}Province$R:"
            indent="${indent}  "
           for O in ${Locale[@]}; do
             echo "${indent}Locale$O:"
             indent="${indent}  "
             for I in ${City[@]}; do
               echo "${indent}City$I:"
               indent="${indent}  "
               for H in ${Hood[@]}; do
                 echo "${indent}- Hood$H"
               done
               indent="${indent#  }"
             done
             indent="${indent#  }"
           done
           indent="${indent#  }"
         done
         indent="${indent#  }"
       done
       indent="${indent#  }"
     done
     indent="${indent#  }"
   done
   echo "${indent}org2:"
   echo "${indent}  - department1"
   echo "${indent}  - department2"
}

function setupServerEnv() {
   $SCRIPTDIR/fabric-ca_setup.sh -d $dbDriver -I -S -X -n1 -D -x $TESTDIR > $TESTDIR/server.log 2>&1
   enroll
   # Ensure affiliations cannot be deleted if --cfg.affiliations.allowremove not configured
   $FABRIC_CA_CLIENTEXEC affiliation remove org1 $URI -H $TESTDIR/admin/ 2>&1|
      grep 'Authorization failure' ||
         ErrorMsg "should not be able to delete 'org1', or wrong error msg"
   $SCRIPTDIR/fabric-ca_setup.sh -K

   # Generate a large affinity tree for testing;
   # this is way faster than adding with the cmd-line client
   genAffYaml >> $CA_CFG_PATH/runFabricCaFvt.yaml
   $SCRIPTDIR/fabric-ca_setup.sh -d $dbDriver -S -X -n1 -D -x $TESTDIR -- \
                    --cfg.affiliations.allowremove > $TESTDIR/server.log 2>&1
   # Sanity check the number of affilitations
   dbEntries=$(tableCount $dbDriver Affiliations ".*"| awk -F':' '{print $2}')
   # discount the summary line displayed in the above command
   let dbEntries--
   serverEntries="$(( $($FABRIC_CA_CLIENTEXEC affiliation list $URI -H $TESTDIR/admin/ | wc -l)  -1))"
   test "$dbEntries" -eq "$serverEntries" || ErrorMsg "Wrong number of affiliations: expected $dbEntries, got $serverEntries"
   displayRunningTotal
   verifyTotals "403 403 403 0" || ErrorMsg "Wrong number of affiliations"
}

function testAffiliationRefs() {
   # @TODO all of these should be 400 bad request FAB-7466
   # Ensure affiliations w/ sub-affiliations cannot be deleted w/o --force
   $FABRIC_CA_CLIENTEXEC affiliation remove org1 $URI -H $TESTDIR/admin/ -d 2>&1 |
      grep "401 Unauthorized" ||
         ErrorMsg "should not be able to delete 'org1' w/o force (has sub-affiliations)"
   # Ensure affiliations can be deleted if no ID's are referencing them
   $FABRIC_CA_CLIENTEXEC affiliation remove org1.department1 $URI -H $TESTDIR/admin/ -d 2>&1 ||
         ErrorMsg "should be able to delete org1.department1"
   $FABRIC_CA_CLIENTEXEC affiliation remove org1 --force $URI -H $TESTDIR/admin/ -d 2>&1 ||
         ErrorMsg "should be able to delete org1"
   # Ensure affiliations can be deleted, even if ID's are referencing them, but only w/ --force
   $FABRIC_CA_CLIENTEXEC affiliation remove bank_b $URI -H $TESTDIR/admin/ -d 2>&1 &&
      ErrorMsg "should not be able to delete 'bank_b' with references"
   # Ensure affiliations cannot be deleted if ID's are referencing them,
   # and --cfg.identities.allowremove is not configed, even w/ --force
   $FABRIC_CA_CLIENTEXEC affiliation remove bank_b $URI -H $TESTDIR/admin/ --force -d 2>&1 &&
      ErrorMsg "should be able to delete 'bank_b' without --cfg.identities.allowremove"
}

function testAllowremove() {
   # ensure cfg.identities.allowremove flag is required
   $FABRIC_CA_CLIENTEXEC affiliation remove bank_b --force $URI -H $TESTDIR/admin/ 2>&1 |
      grep 'Authorization failure' ||
         ErrorMsg "should not be able to delete 'bank_b', or wrong error msg"
   # add cfg.identities.allowremove flag
   $SCRIPTDIR/fabric-ca_setup.sh -K
   $SCRIPTDIR/fabric-ca_setup.sh -d $dbDriver -S -X -n1 -D -x $TESTDIR -- \
                    --cfg.affiliations.allowremove --cfg.identities.allowremove > $TESTDIR/server.log 2>&1
   # try again
   $FABRIC_CA_CLIENTEXEC affiliation remove bank_b --force $URI -H $TESTDIR/admin/ 2>&1  || ErrorMsg "should be able to delete 'bank_b'"
   # make sure entries are deleted
   $SCRIPTDIR/fabric-ca_setup.sh -L -d $dbDriver -D | grep bank_b && ErrorMsg "'bank_b' not deleted"
   expected=$((dbEntries - 2))
   dbEntries=$(tableCount $dbDriver Affiliations ".*"| awk -F':' '{print $2}')
   let dbEntries--
   serverEntries="$(( $($FABRIC_CA_CLIENTEXEC affiliation list $URI -H $TESTDIR/admin/ | wc -l)  -1))"
   test "$expected" -eq "$serverEntries" || ErrorMsg "Wrong number of affiliations: expected $expected, got $serverEntries"
   displayRunningTotal
   verifyTotals "403 403 403 0" || ErrorMsg "Wrong number of affiliations"

   # Ensure all children are deleted
   $FABRIC_CA_CLIENTEXEC affiliation remove planet2.landmass1 --force $URI -H $TESTDIR/admin/ 2>&1 ||
      ErrorMsg "should be able to delete 'planet2.landmass1'"
   # make sure entries are deleted
   $SCRIPTDIR/fabric-ca_setup.sh -L -d $dbDriver -D | grep "planet2.landmass1" && ErrorMsg "'planet2.landmass1' not deleted"
   expected=$((dbEntries - 201))
   dbEntries=$(tableCount $dbDriver Affiliations ".*"| awk -F':' '{print $2}')
   let dbEntries--
   serverEntries="$(( $($FABRIC_CA_CLIENTEXEC affiliation list $URI -H $TESTDIR/admin/ | wc -l)  -1))"
   test "$expected" -eq "$serverEntries" || ErrorMsg "Wrong number of affiliations: expected $expected, got $serverEntries"
   displayRunningTotal
   verifyTotals "403 403 202 0" || ErrorMsg "Wrong number of affiliations"
}

function testAffiliationMgr() {
   # Ensure affiliations can only be updated by authorized users
   enroll admin2 adminpw2
   $FABRIC_CA_CLIENTEXEC affiliation remove org2 $URI -H $TESTDIR/admin2 2>&1 |
      grep "User does not have attribute 'hf.AffiliationMgr'" ||
         ErrorMsg  "Should not be able to delete attributes, or wrong error msg"
{'fraction_non_alphanumeric': 0.13742590447639164, 'fraction_numerical': 0.01935000340669074, 'mean_word_length': 2.9372317596566524, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '5986547', 'n_tokens_mistral': 5005, 'n_tokens_neox': 4770, 'n_words': 1406}