test.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. /*
  2. * Copyright (c) 2020, The SerenityOS developers.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/LexicalPath.h>
  27. #include <AK/NonnullOwnPtr.h>
  28. #include <AK/OwnPtr.h>
  29. #include <LibCore/File.h>
  30. #include <getopt.h>
  31. #include <stdio.h>
  32. #include <sys/stat.h>
  33. #include <unistd.h>
  34. bool g_there_was_an_error = false;
  35. [[noreturn]] static void fatal_error(const char* format, ...)
  36. {
  37. fputs("\033[31m", stderr);
  38. va_list ap;
  39. va_start(ap, format);
  40. vfprintf(stderr, format, ap);
  41. va_end(ap);
  42. fputs("\033[0m\n", stderr);
  43. exit(126);
  44. }
  45. class Condition {
  46. public:
  47. virtual ~Condition() { }
  48. virtual bool check() const = 0;
  49. };
  50. class And : public Condition {
  51. public:
  52. And(NonnullOwnPtr<Condition> lhs, NonnullOwnPtr<Condition> rhs)
  53. : m_lhs(move(lhs))
  54. , m_rhs(move(rhs))
  55. {
  56. }
  57. private:
  58. virtual bool check() const override
  59. {
  60. return m_lhs->check() && m_rhs->check();
  61. }
  62. NonnullOwnPtr<Condition> m_lhs;
  63. NonnullOwnPtr<Condition> m_rhs;
  64. };
  65. class Or : public Condition {
  66. public:
  67. Or(NonnullOwnPtr<Condition> lhs, NonnullOwnPtr<Condition> rhs)
  68. : m_lhs(move(lhs))
  69. , m_rhs(move(rhs))
  70. {
  71. }
  72. private:
  73. virtual bool check() const override
  74. {
  75. return m_lhs->check() || m_rhs->check();
  76. }
  77. NonnullOwnPtr<Condition> m_lhs;
  78. NonnullOwnPtr<Condition> m_rhs;
  79. };
  80. class Not : public Condition {
  81. public:
  82. Not(NonnullOwnPtr<Condition> cond)
  83. : m_cond(move(cond))
  84. {
  85. }
  86. private:
  87. virtual bool check() const override
  88. {
  89. return !m_cond->check();
  90. }
  91. NonnullOwnPtr<Condition> m_cond;
  92. };
  93. class FileIsOfKind : public Condition {
  94. public:
  95. enum Kind {
  96. BlockDevice,
  97. CharacterDevice,
  98. Directory,
  99. FIFO,
  100. Regular,
  101. Socket,
  102. SymbolicLink,
  103. };
  104. FileIsOfKind(StringView path, Kind kind)
  105. : m_path(path)
  106. , m_kind(kind)
  107. {
  108. }
  109. private:
  110. virtual bool check() const override
  111. {
  112. struct stat statbuf;
  113. int rc;
  114. if (m_kind == SymbolicLink)
  115. rc = stat(m_path.characters(), &statbuf);
  116. else
  117. rc = lstat(m_path.characters(), &statbuf);
  118. if (rc < 0) {
  119. if (errno != ENOENT) {
  120. perror(m_path.characters());
  121. g_there_was_an_error = true;
  122. }
  123. return false;
  124. }
  125. switch (m_kind) {
  126. case BlockDevice:
  127. return S_ISBLK(statbuf.st_mode);
  128. case CharacterDevice:
  129. return S_ISCHR(statbuf.st_mode);
  130. case Directory:
  131. return S_ISDIR(statbuf.st_mode);
  132. case FIFO:
  133. return S_ISFIFO(statbuf.st_mode);
  134. case Regular:
  135. return S_ISREG(statbuf.st_mode);
  136. case Socket:
  137. return S_ISSOCK(statbuf.st_mode);
  138. case SymbolicLink:
  139. return S_ISLNK(statbuf.st_mode);
  140. default:
  141. ASSERT_NOT_REACHED();
  142. }
  143. }
  144. String m_path;
  145. Kind m_kind { Regular };
  146. };
  147. class UserHasPermission : public Condition {
  148. public:
  149. enum Permission {
  150. Any,
  151. Read,
  152. Write,
  153. Execute,
  154. };
  155. UserHasPermission(StringView path, Permission kind)
  156. : m_path(path)
  157. , m_kind(kind)
  158. {
  159. }
  160. private:
  161. virtual bool check() const override
  162. {
  163. switch (m_kind) {
  164. case Read:
  165. return access(m_path.characters(), R_OK) == 0;
  166. case Write:
  167. return access(m_path.characters(), W_OK) == 0;
  168. case Execute:
  169. return access(m_path.characters(), X_OK) == 0;
  170. case Any:
  171. return access(m_path.characters(), F_OK) == 0;
  172. default:
  173. ASSERT_NOT_REACHED();
  174. }
  175. }
  176. String m_path;
  177. Permission m_kind { Read };
  178. };
  179. class StringCompare : public Condition {
  180. public:
  181. enum Mode {
  182. Equal,
  183. NotEqual,
  184. };
  185. StringCompare(StringView lhs, StringView rhs, Mode mode)
  186. : m_lhs(move(lhs))
  187. , m_rhs(move(rhs))
  188. , m_mode(mode)
  189. {
  190. }
  191. private:
  192. virtual bool check() const override
  193. {
  194. if (m_mode == Equal)
  195. return m_lhs == m_rhs;
  196. return m_lhs != m_rhs;
  197. }
  198. StringView m_lhs;
  199. StringView m_rhs;
  200. Mode m_mode { Equal };
  201. };
  202. class NumericCompare : public Condition {
  203. public:
  204. enum Mode {
  205. Equal,
  206. Greater,
  207. GreaterOrEqual,
  208. Less,
  209. LessOrEqual,
  210. NotEqual,
  211. };
  212. NumericCompare(String lhs, String rhs, Mode mode)
  213. : m_mode(mode)
  214. {
  215. auto lhs_option = lhs.trim_whitespace().to_int();
  216. auto rhs_option = rhs.trim_whitespace().to_int();
  217. if (!lhs_option.has_value())
  218. fatal_error("expected integer expression: '%s'", lhs.characters());
  219. if (!rhs_option.has_value())
  220. fatal_error("expected integer expression: '%s'", rhs.characters());
  221. m_lhs = lhs_option.value();
  222. m_rhs = rhs_option.value();
  223. }
  224. private:
  225. virtual bool check() const override
  226. {
  227. switch (m_mode) {
  228. case Equal:
  229. return m_lhs == m_rhs;
  230. case Greater:
  231. return m_lhs > m_rhs;
  232. case GreaterOrEqual:
  233. return m_lhs >= m_rhs;
  234. case Less:
  235. return m_lhs < m_rhs;
  236. case LessOrEqual:
  237. return m_lhs <= m_rhs;
  238. case NotEqual:
  239. return m_lhs != m_rhs;
  240. default:
  241. ASSERT_NOT_REACHED();
  242. }
  243. }
  244. int m_lhs { 0 };
  245. int m_rhs { 0 };
  246. Mode m_mode { Equal };
  247. };
  248. class FileCompare : public Condition {
  249. public:
  250. enum Mode {
  251. Same,
  252. ModificationTimestampGreater,
  253. ModificationTimestampLess,
  254. };
  255. FileCompare(String lhs, String rhs, Mode mode)
  256. : m_lhs(move(lhs))
  257. , m_rhs(move(rhs))
  258. , m_mode(mode)
  259. {
  260. }
  261. private:
  262. virtual bool check() const override
  263. {
  264. struct stat statbuf_l;
  265. int rc = stat(m_lhs.characters(), &statbuf_l);
  266. if (rc < 0) {
  267. perror(m_lhs.characters());
  268. g_there_was_an_error = true;
  269. return false;
  270. }
  271. struct stat statbuf_r;
  272. rc = stat(m_rhs.characters(), &statbuf_r);
  273. if (rc < 0) {
  274. perror(m_rhs.characters());
  275. g_there_was_an_error = true;
  276. return false;
  277. }
  278. switch (m_mode) {
  279. case Same:
  280. return statbuf_l.st_dev == statbuf_r.st_dev && statbuf_l.st_ino == statbuf_r.st_ino;
  281. case ModificationTimestampLess:
  282. return statbuf_l.st_mtime < statbuf_r.st_mtime;
  283. case ModificationTimestampGreater:
  284. return statbuf_l.st_mtime > statbuf_r.st_mtime;
  285. default:
  286. ASSERT_NOT_REACHED();
  287. }
  288. }
  289. String m_lhs;
  290. String m_rhs;
  291. Mode m_mode { Same };
  292. };
  293. static OwnPtr<Condition> parse_complex_expression(char* argv[]);
  294. static bool should_treat_expression_as_single_string(const StringView& arg_after)
  295. {
  296. return arg_after.is_null() || arg_after == "-a" || arg_after == "-o";
  297. }
  298. static OwnPtr<Condition> parse_simple_expression(char* argv[])
  299. {
  300. StringView arg = argv[optind];
  301. if (arg.is_null()) {
  302. return nullptr;
  303. }
  304. if (arg == "(") {
  305. optind++;
  306. auto command = parse_complex_expression(argv);
  307. if (command && argv[optind] && StringView(argv[++optind]) == ")")
  308. return command;
  309. fatal_error("Unmatched \033[1m(");
  310. }
  311. if (arg == "!") {
  312. if (should_treat_expression_as_single_string(argv[optind]))
  313. return make<StringCompare>(move(arg), "", StringCompare::NotEqual);
  314. auto command = parse_complex_expression(argv);
  315. if (!command)
  316. fatal_error("Expected an expression after \033[1m!");
  317. return make<Not>(command.release_nonnull());
  318. }
  319. // Try to read a unary op.
  320. if (arg.starts_with('-') && arg.length() == 2) {
  321. optind++;
  322. if (should_treat_expression_as_single_string(argv[optind])) {
  323. --optind;
  324. return make<StringCompare>(move(arg), "", StringCompare::NotEqual);
  325. }
  326. StringView value = argv[optind];
  327. switch (arg[1]) {
  328. case 'b':
  329. return make<FileIsOfKind>(value, FileIsOfKind::BlockDevice);
  330. case 'c':
  331. return make<FileIsOfKind>(value, FileIsOfKind::CharacterDevice);
  332. case 'd':
  333. return make<FileIsOfKind>(value, FileIsOfKind::Directory);
  334. case 'f':
  335. return make<FileIsOfKind>(value, FileIsOfKind::Regular);
  336. case 'h':
  337. case 'L':
  338. return make<FileIsOfKind>(value, FileIsOfKind::SymbolicLink);
  339. case 'p':
  340. return make<FileIsOfKind>(value, FileIsOfKind::FIFO);
  341. case 'S':
  342. return make<FileIsOfKind>(value, FileIsOfKind::Socket);
  343. case 'r':
  344. return make<UserHasPermission>(value, UserHasPermission::Read);
  345. case 'w':
  346. return make<UserHasPermission>(value, UserHasPermission::Write);
  347. case 'x':
  348. return make<UserHasPermission>(value, UserHasPermission::Execute);
  349. case 'e':
  350. return make<UserHasPermission>(value, UserHasPermission::Any);
  351. case 'o':
  352. case 'a':
  353. // '-a' and '-o' are boolean ops, which are part of a complex expression
  354. // so we have nothing to parse, simply return to caller.
  355. --optind;
  356. return nullptr;
  357. case 'n':
  358. return make<StringCompare>("", value, StringCompare::NotEqual);
  359. case 'z':
  360. return make<StringCompare>("", value, StringCompare::Equal);
  361. case 'g':
  362. case 'G':
  363. case 'k':
  364. case 'N':
  365. case 'O':
  366. case 's':
  367. fatal_error("Unsupported operator \033[1m%s", argv[optind]);
  368. default:
  369. break;
  370. }
  371. }
  372. // Try to read a binary op, this is either a <string> op <string>, <integer> op <integer>, or <file> op <file>.
  373. auto lhs = arg;
  374. arg = argv[++optind];
  375. if (arg == "=") {
  376. StringView rhs = argv[++optind];
  377. return make<StringCompare>(lhs, rhs, StringCompare::Equal);
  378. } else if (arg == "!=") {
  379. StringView rhs = argv[++optind];
  380. return make<StringCompare>(lhs, rhs, StringCompare::NotEqual);
  381. } else if (arg == "-eq") {
  382. StringView rhs = argv[++optind];
  383. return make<NumericCompare>(lhs, rhs, NumericCompare::Equal);
  384. } else if (arg == "-ge") {
  385. StringView rhs = argv[++optind];
  386. return make<NumericCompare>(lhs, rhs, NumericCompare::GreaterOrEqual);
  387. } else if (arg == "-gt") {
  388. StringView rhs = argv[++optind];
  389. return make<NumericCompare>(lhs, rhs, NumericCompare::Greater);
  390. } else if (arg == "-le") {
  391. StringView rhs = argv[++optind];
  392. return make<NumericCompare>(lhs, rhs, NumericCompare::LessOrEqual);
  393. } else if (arg == "-lt") {
  394. StringView rhs = argv[++optind];
  395. return make<NumericCompare>(lhs, rhs, NumericCompare::Less);
  396. } else if (arg == "-ne") {
  397. StringView rhs = argv[++optind];
  398. return make<NumericCompare>(lhs, rhs, NumericCompare::NotEqual);
  399. } else if (arg == "-ef") {
  400. StringView rhs = argv[++optind];
  401. return make<FileCompare>(lhs, rhs, FileCompare::Same);
  402. } else if (arg == "-nt") {
  403. StringView rhs = argv[++optind];
  404. return make<FileCompare>(lhs, rhs, FileCompare::ModificationTimestampGreater);
  405. } else if (arg == "-ot") {
  406. StringView rhs = argv[++optind];
  407. return make<FileCompare>(lhs, rhs, FileCompare::ModificationTimestampLess);
  408. } else if (arg == "-o" || arg == "-a") {
  409. // '-a' and '-o' are boolean ops, which are part of a complex expression
  410. // put them back and return with lhs as string compare.
  411. --optind;
  412. return make<StringCompare>("", lhs, StringCompare::NotEqual);
  413. } else {
  414. --optind;
  415. return make<StringCompare>("", lhs, StringCompare::NotEqual);
  416. }
  417. }
  418. static OwnPtr<Condition> parse_complex_expression(char* argv[])
  419. {
  420. auto command = parse_simple_expression(argv);
  421. while (argv[optind] && argv[optind + 1]) {
  422. if (!command && argv[optind])
  423. fatal_error("expected an expression");
  424. StringView arg = argv[++optind];
  425. enum {
  426. AndOp,
  427. OrOp,
  428. } binary_operation { AndOp };
  429. if (arg == "-a") {
  430. optind++;
  431. binary_operation = AndOp;
  432. } else if (arg == "-o") {
  433. optind++;
  434. binary_operation = OrOp;
  435. } else {
  436. // Ooops, looked too far.
  437. optind--;
  438. return command;
  439. }
  440. auto rhs = parse_complex_expression(argv);
  441. if (!rhs)
  442. fatal_error("Missing right-hand side");
  443. if (binary_operation == AndOp)
  444. command = make<And>(command.release_nonnull(), rhs.release_nonnull());
  445. else
  446. command = make<Or>(command.release_nonnull(), rhs.release_nonnull());
  447. }
  448. return command;
  449. }
  450. int main(int argc, char* argv[])
  451. {
  452. if (pledge("stdio rpath", nullptr) < 0) {
  453. perror("pledge");
  454. return 126;
  455. }
  456. if (LexicalPath { argv[0] }.basename() == "[") {
  457. --argc;
  458. if (StringView { argv[argc] } != "]")
  459. fatal_error("test invoked as '[' requires a closing bracket ']'");
  460. argv[argc] = nullptr;
  461. }
  462. // Exit false when no arguments are given.
  463. if (argc == 1)
  464. return 1;
  465. auto condition = parse_complex_expression(argv);
  466. if (optind != argc - 1)
  467. fatal_error("Too many arguments");
  468. auto result = condition ? condition->check() : false;
  469. if (g_there_was_an_error)
  470. return 126;
  471. return result ? 0 : 1;
  472. }