efivarfs.sh 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #!/bin/bash
  2. efivarfs_mount=/sys/firmware/efi/efivars
  3. test_guid=210be57c-9849-4fc7-a635-e6382d1aec27
  4. check_prereqs()
  5. {
  6. local msg="skip all tests:"
  7. if [ $UID != 0 ]; then
  8. echo $msg must be run as root >&2
  9. exit 0
  10. fi
  11. if ! grep -q "^\S\+ $efivarfs_mount efivarfs" /proc/mounts; then
  12. echo $msg efivarfs is not mounted on $efivarfs_mount >&2
  13. exit 0
  14. fi
  15. }
  16. run_test()
  17. {
  18. local test="$1"
  19. echo "--------------------"
  20. echo "running $test"
  21. echo "--------------------"
  22. if [ "$(type -t $test)" = 'function' ]; then
  23. ( $test )
  24. else
  25. ( ./$test )
  26. fi
  27. if [ $? -ne 0 ]; then
  28. echo " [FAIL]"
  29. rc=1
  30. else
  31. echo " [PASS]"
  32. fi
  33. }
  34. test_create()
  35. {
  36. local attrs='\x07\x00\x00\x00'
  37. local file=$efivarfs_mount/$FUNCNAME-$test_guid
  38. printf "$attrs\x00" > $file
  39. if [ ! -e $file ]; then
  40. echo "$file couldn't be created" >&2
  41. exit 1
  42. fi
  43. if [ $(stat -c %s $file) -ne 5 ]; then
  44. echo "$file has invalid size" >&2
  45. exit 1
  46. fi
  47. }
  48. test_create_empty()
  49. {
  50. local file=$efivarfs_mount/$FUNCNAME-$test_guid
  51. : > $file
  52. if [ ! -e $file ]; then
  53. echo "$file can not be created without writing" >&2
  54. exit 1
  55. fi
  56. }
  57. test_create_read()
  58. {
  59. local file=$efivarfs_mount/$FUNCNAME-$test_guid
  60. ./create-read $file
  61. }
  62. test_delete()
  63. {
  64. local attrs='\x07\x00\x00\x00'
  65. local file=$efivarfs_mount/$FUNCNAME-$test_guid
  66. printf "$attrs\x00" > $file
  67. if [ ! -e $file ]; then
  68. echo "$file couldn't be created" >&2
  69. exit 1
  70. fi
  71. rm $file 2>/dev/null
  72. if [ $? -ne 0 ]; then
  73. chattr -i $file
  74. rm $file
  75. fi
  76. if [ -e $file ]; then
  77. echo "$file couldn't be deleted" >&2
  78. exit 1
  79. fi
  80. }
  81. # test that we can remove a variable by issuing a write with only
  82. # attributes specified
  83. test_zero_size_delete()
  84. {
  85. local attrs='\x07\x00\x00\x00'
  86. local file=$efivarfs_mount/$FUNCNAME-$test_guid
  87. printf "$attrs\x00" > $file
  88. if [ ! -e $file ]; then
  89. echo "$file does not exist" >&2
  90. exit 1
  91. fi
  92. chattr -i $file
  93. printf "$attrs" > $file
  94. if [ -e $file ]; then
  95. echo "$file should have been deleted" >&2
  96. exit 1
  97. fi
  98. }
  99. test_open_unlink()
  100. {
  101. local file=$efivarfs_mount/$FUNCNAME-$test_guid
  102. ./open-unlink $file
  103. }
  104. # test that we can create a range of filenames
  105. test_valid_filenames()
  106. {
  107. local attrs='\x07\x00\x00\x00'
  108. local ret=0
  109. local file_list="abc dump-type0-11-1-1362436005 1234 -"
  110. for f in $file_list; do
  111. local file=$efivarfs_mount/$f-$test_guid
  112. printf "$attrs\x00" > $file
  113. if [ ! -e $file ]; then
  114. echo "$file could not be created" >&2
  115. ret=1
  116. else
  117. rm $file 2>/dev/null
  118. if [ $? -ne 0 ]; then
  119. chattr -i $file
  120. rm $file
  121. fi
  122. fi
  123. done
  124. exit $ret
  125. }
  126. test_invalid_filenames()
  127. {
  128. local attrs='\x07\x00\x00\x00'
  129. local ret=0
  130. local file_list="
  131. -1234-1234-1234-123456789abc
  132. foo
  133. foo-bar
  134. -foo-
  135. foo-barbazba-foob-foob-foob-foobarbazfoo
  136. foo-------------------------------------
  137. -12345678-1234-1234-1234-123456789abc
  138. a-12345678=1234-1234-1234-123456789abc
  139. a-12345678-1234=1234-1234-123456789abc
  140. a-12345678-1234-1234=1234-123456789abc
  141. a-12345678-1234-1234-1234=123456789abc
  142. 1112345678-1234-1234-1234-123456789abc"
  143. for f in $file_list; do
  144. local file=$efivarfs_mount/$f
  145. printf "$attrs\x00" 2>/dev/null > $file
  146. if [ -e $file ]; then
  147. echo "Creating $file should have failed" >&2
  148. rm $file 2>/dev/null
  149. if [ $? -ne 0 ]; then
  150. chattr -i $file
  151. rm $file
  152. fi
  153. ret=1
  154. fi
  155. done
  156. exit $ret
  157. }
  158. check_prereqs
  159. rc=0
  160. run_test test_create
  161. run_test test_create_empty
  162. run_test test_create_read
  163. run_test test_delete
  164. run_test test_zero_size_delete
  165. run_test test_open_unlink
  166. run_test test_valid_filenames
  167. run_test test_invalid_filenames
  168. exit $rc