generate-testcases.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #!/bin/bash
  2. # This is a script to generate “awkward” files and directories that the
  3. # testing scripts use as integration test cases.
  4. #
  5. # Tests like these verify that exa is doing the right thing at every step,
  6. # from command-line parsing to colourising the output properly -- especially
  7. # on multiple or weird platforms!
  8. #
  9. # Examples of the things it generates are:
  10. # - files with newlines in their name
  11. # - files with invalid UTF-8 in their name
  12. # - directories you aren’t allowed to open
  13. # - files with users and groups that don’t exist
  14. # - directories you aren’t allowed to read the xattrs for
  15. ## -- configuration --
  16. # Directory that the files should be generated in.
  17. DIR=testcases
  18. # You! Yes, you, the name of the user running this script.
  19. YOU=`whoami`
  20. # Someone with *higher* privileges than yourself, such as root.
  21. ROOT=root
  22. # A UID that doesn’t map to any user on the system.
  23. INVALID_UID=666
  24. # A GID that doesn’t map to any group on the system.
  25. INVALID_GID=616
  26. # Get confirmation from the user before running.
  27. echo "This script will generate files into the $DIR directory."
  28. echo "It requires sudo for the '$ROOT' user."
  29. echo "You may want to edit this file before running it."
  30. read -r -p "Continue? [y/N] " response
  31. if [[ ! $response =~ ^([yY][eE][sS]|[yY])$ ]]
  32. then
  33. exit 2
  34. fi
  35. # First things first, don’t try to overwrite the testcases if they already
  36. # exist. It’s safer to just start again from scratch.
  37. if [[ -e "$DIR" ]]
  38. then
  39. echo "'$DIR' already exists - aborting" >&2
  40. echo "(you'll probably have to sudo rm it.)" >&2
  41. exit 2
  42. fi
  43. # Abort if anything goes wrong past this point!
  44. abort() { echo 'Hit an error - aborting' >&2; exit 1; }
  45. trap 'abort' ERR
  46. # List commands as they are run
  47. set -x
  48. # Let’s go!
  49. mkdir "$DIR"
  50. ## -- links --
  51. mkdir "$DIR/links"
  52. ln -s / "$DIR/links/root"
  53. ln -s /usr "$DIR/links/usr"
  54. ln -s nowhere "$DIR/links/broken"
  55. ## -- users and groups --
  56. mkdir "$DIR/passwd"
  57. # sudo is needed for these because we technically aren’t a member of the
  58. # groups (because they don’t exist), and chown and chgrp are smart enough to
  59. # disallow it!
  60. touch "$DIR/passwd/unknown-uid"
  61. sudo -u "$ROOT" chown $INVALID_UID "$DIR/passwd/unknown-uid"
  62. touch "$DIR/passwd/unknown-gid"
  63. sudo -u "$ROOT" chgrp $INVALID_GID "$DIR/passwd/unknown-gid"
  64. ## -- permissions --
  65. mkdir "$DIR/permissions"
  66. touch "$DIR/permissions/all-permissions"
  67. chmod 777 "$DIR/permissions/all-permissions"
  68. touch "$DIR/permissions/no-permissions"
  69. chmod 000 "$DIR/permissions/no-permissions"
  70. mkdir "$DIR/permissions/forbidden-directory"
  71. chmod 000 "$DIR/permissions/forbidden-directory"
  72. ## -- extended attributes --
  73. # These tests are optional but the presence of the *directory* is used
  74. # elsewhere! Yes I know this is a bad practice.
  75. mkdir "$DIR/attributes"
  76. if hash xattr; then
  77. touch "$DIR/attributes/none"
  78. touch "$DIR/attributes/one"
  79. xattr -w greeting hello "$DIR/attributes/one"
  80. touch "$DIR/attributes/two"
  81. xattr -w greeting hello "$DIR/attributes/two"
  82. xattr -w another_greeting hi "$DIR/attributes/two"
  83. touch "$DIR/attributes/forbidden"
  84. xattr -w greeting hello "$DIR/attributes/forbidden"
  85. chmod +a "$YOU deny readextattr" "$DIR/attributes/forbidden"
  86. mkdir "$DIR/attributes/dirs"
  87. mkdir "$DIR/attributes/dirs/empty-with-attribute"
  88. xattr -w greeting hello "$DIR/attributes/dirs/empty-with-attribute"
  89. mkdir "$DIR/attributes/dirs/full-with-attribute"
  90. touch "$DIR/attributes/dirs/full-with-attribute/file"
  91. xattr -w greeting hello "$DIR/attributes/dirs/full-with-attribute"
  92. mkdir "$DIR/attributes/dirs/full-but-forbidden"
  93. touch "$DIR/attributes/dirs/full-but-forbidden/file"
  94. xattr -w greeting hello "$DIR/attributes/dirs/full-but-forbidden"
  95. chmod 000 "$DIR/attributes/dirs/full-but-forbidden"
  96. chmod +a "$YOU deny readextattr" "$DIR/attributes/dirs/full-but-forbidden"
  97. else
  98. echo "Skipping xattr tests"
  99. fi