format.bats 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. #!/usr/bin/env bats
  2. @test "Default header" {
  3. run ./bin/gfpsgo
  4. [ "${status:-}" -eq 0 ]
  5. [[ ${lines[0]:?} =~ "USER" ]]
  6. [[ ${lines[0]:?} =~ "PID" ]]
  7. [[ ${lines[0]:?} =~ "PPID" ]]
  8. [[ ${lines[0]:?} =~ "%CPU" ]]
  9. [[ ${lines[0]:?} =~ "ELAPSED" ]]
  10. [[ ${lines[0]:?} =~ "TTY" ]]
  11. [[ ${lines[0]:?} =~ "TIME" ]]
  12. [[ ${lines[0]:?} =~ "COMMAND" ]]
  13. }
  14. @test "%CPU header" {
  15. run ./bin/gfpsgo -format "%C"
  16. [ "${status:-}" -eq 0 ]
  17. [[ ${lines[0]:?} =~ "%CPU" ]]
  18. run ./bin/gfpsgo -format "pcpu"
  19. [ "${status:-}" -eq 0 ]
  20. [[ ${lines[0]:?} =~ "%CPU" ]]
  21. }
  22. @test "GROUP header" {
  23. run ./bin/gfpsgo -format "%G"
  24. [ "${status:-}" -eq 0 ]
  25. [[ ${lines[0]:?} =~ "GROUP" ]]
  26. run ./bin/gfpsgo -format "group"
  27. [ "${status:-}" -eq 0 ]
  28. [[ ${lines[0]:?} =~ "GROUP" ]]
  29. }
  30. @test "PPID header" {
  31. run ./bin/gfpsgo -format "%P"
  32. [ "${status:-}" -eq 0 ]
  33. [[ ${lines[0]:?} =~ "PPID" ]]
  34. run ./bin/gfpsgo -format "ppid"
  35. [ "${status:-}" -eq 0 ]
  36. [[ ${lines[0]:?} =~ "PPID" ]]
  37. }
  38. @test "USER header" {
  39. run ./bin/gfpsgo -format "%U"
  40. [ "${status:-}" -eq 0 ]
  41. [[ ${lines[0]:?} =~ "USER" ]]
  42. run ./bin/gfpsgo -format "user"
  43. [ "${status:-}" -eq 0 ]
  44. [[ ${lines[0]:?} =~ "USER" ]]
  45. }
  46. @test "COMMAND (args) header" {
  47. run ./bin/gfpsgo -format "%a"
  48. [ "${status:-}" -eq 0 ]
  49. [[ ${lines[0]:?} =~ "COMMAND" ]]
  50. run ./bin/gfpsgo -format "args"
  51. [ "${status:-}" -eq 0 ]
  52. [[ ${lines[0]:?} =~ "COMMAND" ]]
  53. }
  54. @test "COMMAND (comm) header" {
  55. run ./bin/gfpsgo -format "%c"
  56. [ "${status:-}" -eq 0 ]
  57. [[ ${lines[0]:?} =~ "COMMAND" ]]
  58. run ./bin/gfpsgo -format "comm"
  59. [ "${status:-}" -eq 0 ]
  60. [[ ${lines[0]:?} =~ "COMMAND" ]]
  61. }
  62. @test "RGROUP header" {
  63. run ./bin/gfpsgo -format "%g"
  64. [ "${status:-}" -eq 0 ]
  65. [[ ${lines[0]:?} =~ "RGROUP" ]]
  66. run ./bin/gfpsgo -format "rgroup"
  67. [ "${status:-}" -eq 0 ]
  68. [[ ${lines[0]:?} =~ "RGROUP" ]]
  69. }
  70. @test "NI" {
  71. run ./bin/gfpsgo -format "%n"
  72. [ "${status:-}" -eq 0 ]
  73. [[ ${lines[0]:?} =~ "NI" ]]
  74. run ./bin/gfpsgo -format "nice"
  75. [ "${status:-}" -eq 0 ]
  76. [[ ${lines[0]:?} =~ "NI" ]]
  77. }
  78. @test "PID header" {
  79. run ./bin/gfpsgo -format "%p"
  80. [ "${status:-}" -eq 0 ]
  81. [[ ${lines[0]:?} =~ "PID" ]]
  82. run ./bin/gfpsgo -format "pid"
  83. [ "${status:-}" -eq 0 ]
  84. [[ ${lines[0]:?} =~ "PID" ]]
  85. }
  86. @test "ELAPSED header" {
  87. run ./bin/gfpsgo -format "%t"
  88. [ "${status:-}" -eq 0 ]
  89. [[ ${lines[0]:?} =~ "ELAPSED" ]]
  90. run ./bin/gfpsgo -format "etime"
  91. [ "${status:-}" -eq 0 ]
  92. [[ ${lines[0]:?} =~ "ELAPSED" ]]
  93. }
  94. @test "RUSER header" {
  95. run ./bin/gfpsgo -format "%u"
  96. [ "${status:-}" -eq 0 ]
  97. [[ ${lines[0]:?} =~ "RUSER" ]]
  98. run ./bin/gfpsgo -format "ruser"
  99. [ "${status:-}" -eq 0 ]
  100. [[ ${lines[0]:?} =~ "RUSER" ]]
  101. }
  102. @test "TIME header" {
  103. run ./bin/gfpsgo -format "%x"
  104. [ "${status:-}" -eq 0 ]
  105. [[ ${lines[0]:?} =~ "TIME" ]]
  106. run ./bin/gfpsgo -format "time"
  107. [ "${status:-}" -eq 0 ]
  108. [[ ${lines[0]:?} =~ "TIME" ]]
  109. }
  110. @test "TTY header" {
  111. run ./bin/gfpsgo -format "%y"
  112. [ "${status:-}" -eq 0 ]
  113. [[ ${lines[0]:?} =~ "TTY" ]]
  114. run ./bin/gfpsgo -format "tty"
  115. [ "${status:-}" -eq 0 ]
  116. [[ ${lines[0]:?} =~ "TTY" ]]
  117. }
  118. @test "VSZ header" {
  119. run ./bin/gfpsgo -format "%z"
  120. [ "${status:-}" -eq 0 ]
  121. [[ ${lines[0]:?} =~ "VSZ" ]]
  122. run ./bin/gfpsgo -format "vsz"
  123. [ "${status:-}" -eq 0 ]
  124. [[ ${lines[0]:?} =~ "VSZ" ]]
  125. }
  126. @test "CAPAMB header" {
  127. run ./bin/gfpsgo -format "capamb"
  128. [ "${status:-}" -eq 0 ]
  129. [[ ${lines[0]:?} =~ "AMBIENT CAPS" ]]
  130. }
  131. @test "CAPINH header" {
  132. run ./bin/gfpsgo -format "capinh"
  133. [ "${status:-}" -eq 0 ]
  134. [[ ${lines[0]:?} =~ "INHERITED CAPS" ]]
  135. }
  136. @test "CAPPRM header" {
  137. run ./bin/gfpsgo -format "capprm"
  138. [ "${status:-}" -eq 0 ]
  139. [[ ${lines[0]:?} =~ "PERMITTED CAPS" ]]
  140. }
  141. @test "CAPEFF header" {
  142. run ./bin/gfpsgo -format "capeff"
  143. [ "${status:-}" -eq 0 ]
  144. [[ ${lines[0]:?} =~ "EFFECTIVE CAPS" ]]
  145. }
  146. @test "CAPBND header" {
  147. run ./bin/gfpsgo -format "capbnd"
  148. [ "${status:-}" -eq 0 ]
  149. [[ ${lines[0]:?} =~ "BOUNDING CAPS" ]]
  150. }
  151. @test "SECCOMP header" {
  152. run ./bin/gfpsgo -format "seccomp"
  153. [ "${status:-}" -eq 0 ]
  154. [[ ${lines[0]:?} =~ "SECCOMP" ]]
  155. }
  156. @test "HPID header" {
  157. run ./bin/gfpsgo -format "hpid"
  158. [ "${status:-}" -eq 0 ]
  159. [[ ${lines[0]:?} =~ "HPID" ]]
  160. # host PIDs are only extracted with `-pid`
  161. [[ ${lines[1]:?} =~ "?" ]]
  162. }
  163. @test "HUSER header" {
  164. run ./bin/gfpsgo -format "huser"
  165. [ "${status:-}" -eq 0 ]
  166. [[ ${lines[0]:?} =~ "HUSER" ]]
  167. # (host users are only extracted with `-pid`)
  168. [[ ${lines[1]:?} =~ "?" ]]
  169. }
  170. @test "HGROUP header" {
  171. run ./bin/gfpsgo -format "hgroup"
  172. [ "${status:-}" -eq 0 ]
  173. [[ ${lines[0]:?} =~ "HGROUP" ]]
  174. # (host groups are only extracted with `-pid`)
  175. [[ ${lines[1]:?} =~ "?" ]]
  176. }
  177. # TODO(jhj): Use getenforcing, make test more robust.
  178. function is_labeling_enabled()
  179. {
  180. if [ -e "/usr/sbin/selinuxenabled" ] && "/usr/sbin/selinuxenabled"; then
  181. printf %s\\n 1
  182. return
  183. fi
  184. printf %s\\n 0
  185. }
  186. @test "LABEL header" {
  187. enabled=$(is_labeling_enabled)
  188. if [[ ${enabled:-} -eq 0 ]]; then
  189. skip "Labeling not enabled."
  190. fi
  191. run ./bin/gfpsgo -format "label"
  192. [ "${status:-}" -eq 0 ]
  193. [[ ${lines[0]:?} =~ "LABEL" ]]
  194. }
  195. @test "RSS header" {
  196. run ./bin/gfpsgo -format "rss"
  197. [ "${status:-}" -eq 0 ]
  198. [[ ${lines[0]:?} =~ "RSS" ]]
  199. }
  200. @test "STATE header" {
  201. run ./bin/gfpsgo -format "state"
  202. [ "${status:-}" -eq 0 ]
  203. [[ ${lines[0]:?} =~ "STATE" ]]
  204. }
  205. @test "STIME header" {
  206. run ./bin/gfpsgo -format "stime"
  207. [ "${status:-}" -eq 0 ]
  208. [[ ${lines[0]:?} =~ "STIME" ]]
  209. }
  210. @test "ALL header" {
  211. run ./bin/gfpsgo -format "pcpu, group, ppid, user, args, comm, rgroup, nice, pid, pgid, etime, ruser, time, tty, vsz, capamb, capinh, capprm, capeff, capbnd, seccomp, hpid, huser, hgroup, rss, state"
  212. [ "${status}" -eq 0 ]
  213. [[ ${lines[0]:?} =~ "%CPU" ]]
  214. [[ ${lines[0]:?} =~ "GROUP" ]]
  215. [[ ${lines[0]:?} =~ "PPID" ]]
  216. [[ ${lines[0]:?} =~ "USER" ]]
  217. [[ ${lines[0]:?} =~ "COMMAND" ]]
  218. [[ ${lines[0]:?} =~ "COMMAND" ]]
  219. [[ ${lines[0]:?} =~ "RGROUP" ]]
  220. [[ ${lines[0]:?} =~ "NI" ]]
  221. [[ ${lines[0]:?} =~ "PID" ]]
  222. [[ ${lines[0]:?} =~ "ELAPSED" ]]
  223. [[ ${lines[0]:?} =~ "RUSER" ]]
  224. [[ ${lines[0]:?} =~ "TIME" ]]
  225. [[ ${lines[0]:?} =~ "TTY" ]]
  226. [[ ${lines[0]:?} =~ "VSZ" ]]
  227. [[ ${lines[0]:?} =~ "AMBIENT CAPS" ]]
  228. [[ ${lines[0]:?} =~ "INHERITED CAPS" ]]
  229. [[ ${lines[0]:?} =~ "PERMITTED CAPS" ]]
  230. [[ ${lines[0]:?} =~ "EFFECTIVE CAPS" ]]
  231. [[ ${lines[0]:?} =~ "BOUNDING CAPS" ]]
  232. [[ ${lines[0]:?} =~ "SECCOMP" ]]
  233. [[ ${lines[0]:?} =~ "HPID" ]]
  234. [[ ${lines[0]:?} =~ "HUSER" ]]
  235. [[ ${lines[0]:?} =~ "HGROUP" ]]
  236. [[ ${lines[0]:?} =~ "RSS" ]]
  237. [[ ${lines[0]:?} =~ "STATE" ]]
  238. }