| 1 | #include "cpp/stdlib.h"
 | 
| 2 | 
 | 
| 3 | #include <errno.h>
 | 
| 4 | #include <sys/stat.h>
 | 
| 5 | 
 | 
| 6 | #include "mycpp/gc_builtins.h"
 | 
| 7 | #include "vendor/greatest.h"
 | 
| 8 | 
 | 
| 9 | TEST posix_test() {
 | 
| 10 |   ASSERT_EQ(false, posix::access(StrFromC("nonexistent_ZZ"), R_OK));
 | 
| 11 | 
 | 
| 12 |   BigStr* cwd = posix::getcwd();
 | 
| 13 |   log("getcwd() = %s %d", cwd->data_, len(cwd));
 | 
| 14 | 
 | 
| 15 |   ASSERT(posix::getegid() > 0);
 | 
| 16 |   ASSERT(posix::geteuid() > 0);
 | 
| 17 |   ASSERT(posix::getpid() > 0);
 | 
| 18 |   ASSERT(posix::getppid() > 0);
 | 
| 19 |   ASSERT(posix::getuid() > 0);
 | 
| 20 | 
 | 
| 21 |   int pgid = posix::getpgid(0);
 | 
| 22 |   log("pgid = %d", pgid);
 | 
| 23 | 
 | 
| 24 |   // pgid = posix::getpgid(9999);
 | 
| 25 |   // log("pgid = %d", pgid);
 | 
| 26 | 
 | 
| 27 |   Tuple2<int, int> fds = posix::pipe();
 | 
| 28 |   ASSERT(fds.at0() > 0);
 | 
| 29 |   ASSERT(fds.at1() > 0);
 | 
| 30 | 
 | 
| 31 |   ASSERT_EQ(false, posix::isatty(fds.at0()));
 | 
| 32 | 
 | 
| 33 |   posix::close(fds.at0());
 | 
| 34 | 
 | 
| 35 |   BigStr* message = posix::strerror(EBADF);
 | 
| 36 |   log("strerror");
 | 
| 37 |   print(message);
 | 
| 38 | 
 | 
| 39 |   PASS();
 | 
| 40 | }
 | 
| 41 | 
 | 
| 42 | TEST putenv_test() {
 | 
| 43 |   BigStr* key = StrFromC("KEY");
 | 
| 44 |   BigStr* value = StrFromC("value");
 | 
| 45 | 
 | 
| 46 |   posix::putenv(key, value);
 | 
| 47 |   char* got_value = ::getenv(key->data());
 | 
| 48 |   ASSERT(got_value && str_equals(StrFromC(got_value), value));
 | 
| 49 | 
 | 
| 50 |   PASS();
 | 
| 51 | }
 | 
| 52 | 
 | 
| 53 | TEST open_test() {
 | 
| 54 |   bool caught = false;
 | 
| 55 |   try {
 | 
| 56 |     posix::open(StrFromC("nonexistent_ZZ"), 0, 0);
 | 
| 57 |   } catch (IOError_OSError* e) {
 | 
| 58 |     caught = true;
 | 
| 59 |   }
 | 
| 60 |   ASSERT(caught);
 | 
| 61 | 
 | 
| 62 |   // Can open a directory
 | 
| 63 |   int fd = posix::open(StrFromC("."), 0, 0666);
 | 
| 64 |   ASSERT(fd > 0);
 | 
| 65 | 
 | 
| 66 |   PASS();
 | 
| 67 | }
 | 
| 68 | 
 | 
| 69 | TEST time_test() {
 | 
| 70 |   int ts = time_::time();
 | 
| 71 |   log("ts = %d", ts);
 | 
| 72 |   ASSERT(ts > 0);
 | 
| 73 | 
 | 
| 74 |   BigStr* s = time_::strftime(StrFromC("%Y-%m-%d"), ts);
 | 
| 75 |   print(s);
 | 
| 76 | 
 | 
| 77 |   ASSERT(len(s) > 5);
 | 
| 78 | 
 | 
| 79 |   PASS();
 | 
| 80 | }
 | 
| 81 | 
 | 
| 82 | // To figure out how we should use stat() for core/completion.py
 | 
| 83 | // The number of seconds should suffice, for another 15 years :-P
 | 
| 84 | TEST mtime_demo() {
 | 
| 85 |   struct stat statbuf;
 | 
| 86 |   if (stat("README.md", &statbuf) < 0) {
 | 
| 87 |     ASSERT(false);
 | 
| 88 |   }
 | 
| 89 | 
 | 
| 90 |   // POSIX API
 | 
| 91 |   long mtime = statbuf.st_mtime;
 | 
| 92 |   log("mtime        = %10ld", mtime);
 | 
| 93 | 
 | 
| 94 |   // More precision
 | 
| 95 |   long secs = statbuf.st_mtim.tv_sec;
 | 
| 96 |   log("mtim.tv_sec  = %10ld", secs);
 | 
| 97 | 
 | 
| 98 |   long ns = statbuf.st_mtim.tv_nsec;
 | 
| 99 |   log("mtim.tv_nsec = %10ld", ns);
 | 
| 100 | 
 | 
| 101 |   BigStr* s = time_::strftime(StrFromC("%Y-%m-%d"), secs);
 | 
| 102 |   print(s);
 | 
| 103 | 
 | 
| 104 |   log("INT_MAX      = %10d", INT_MAX);
 | 
| 105 |   log("diff         = %10d", INT_MAX - statbuf.st_mtime);
 | 
| 106 | 
 | 
| 107 |   s = time_::strftime(StrFromC("%Y-%m-%d"), INT_MAX);
 | 
| 108 |   print(s);
 | 
| 109 | 
 | 
| 110 |   PASS();
 | 
| 111 | }
 | 
| 112 | 
 | 
| 113 | TEST listdir_test() {
 | 
| 114 |   List<BigStr*>* contents = posix::listdir(StrFromC("/"));
 | 
| 115 |   // This should be universally true on any working Unix right...?
 | 
| 116 |   ASSERT(len(contents) > 0);
 | 
| 117 | 
 | 
| 118 |   int ec = -1;
 | 
| 119 |   try {
 | 
| 120 |     posix::listdir(StrFromC("nonexistent_ZZ"));
 | 
| 121 |   } catch (IOError_OSError* e) {
 | 
| 122 |     ec = e->errno_;
 | 
| 123 |   }
 | 
| 124 |   ASSERT(ec == ENOENT);
 | 
| 125 | 
 | 
| 126 |   PASS();
 | 
| 127 | }
 | 
| 128 | 
 | 
| 129 | TEST for_test_coverage() {
 | 
| 130 |   time_::sleep(0);
 | 
| 131 | 
 | 
| 132 |   // I guess this has side effects
 | 
| 133 |   time_::tzset();
 | 
| 134 | 
 | 
| 135 |   PASS();
 | 
| 136 | }
 | 
| 137 | 
 | 
| 138 | GREATEST_MAIN_DEFS();
 | 
| 139 | 
 | 
| 140 | int main(int argc, char** argv) {
 | 
| 141 |   gHeap.Init();
 | 
| 142 | 
 | 
| 143 |   GREATEST_MAIN_BEGIN();
 | 
| 144 | 
 | 
| 145 |   RUN_TEST(posix_test);
 | 
| 146 |   RUN_TEST(putenv_test);
 | 
| 147 |   RUN_TEST(open_test);
 | 
| 148 |   RUN_TEST(time_test);
 | 
| 149 |   RUN_TEST(mtime_demo);
 | 
| 150 |   RUN_TEST(listdir_test);
 | 
| 151 | 
 | 
| 152 |   RUN_TEST(for_test_coverage);
 | 
| 153 | 
 | 
| 154 |   gHeap.CleanProcessExit();
 | 
| 155 | 
 | 
| 156 |   GREATEST_MAIN_END();
 | 
| 157 |   return 0;
 | 
| 158 | }
 |