Home | History | Annotate | Download | only in tests
      1 /*
      2  * Copyright (C) 2012 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #include <gtest/gtest.h>
     18 
     19 #include <libgen.h>
     20 
     21 #include <errno.h>
     22 
     23 static void TestBasename(const char* in, const char* expected_out) {
     24   char* writable_in = (in != NULL) ? strdup(in) : NULL;
     25   errno = 0;
     26   const char* out = basename(&writable_in[0]);
     27   ASSERT_STREQ(expected_out, out) << in;
     28   ASSERT_EQ(0, errno) << in;
     29   free(writable_in);
     30 }
     31 
     32 static void TestDirname(const char* in, const char* expected_out) {
     33   char* writable_in = (in != NULL) ? strdup(in) : NULL;
     34   errno = 0;
     35   const char* out = dirname(&writable_in[0]);
     36   ASSERT_STREQ(expected_out, out) << in;
     37   ASSERT_EQ(0, errno) << in;
     38   free(writable_in);
     39 }
     40 
     41 TEST(libgen, basename) {
     42   TestBasename(NULL, ".");
     43   TestBasename("", ".");
     44   TestBasename("/usr/lib", "lib");
     45   TestBasename("/usr/", "usr");
     46   TestBasename("usr", "usr");
     47   TestBasename("/", "/");
     48   TestBasename(".", ".");
     49   TestBasename("..", "..");
     50   TestBasename("///", "/");
     51   TestBasename("//usr//lib//", "lib");
     52 }
     53 
     54 TEST(libgen, dirname) {
     55   TestDirname(NULL, ".");
     56   TestDirname("", ".");
     57   TestDirname("/usr/lib", "/usr");
     58   TestDirname("/usr/", "/");
     59   TestDirname("usr", ".");
     60   TestDirname(".", ".");
     61   TestDirname("..", ".");
     62   TestDirname("/", "/");
     63 }
     64 
     65 #if __BIONIC__
     66 
     67 static void TestBasename(const char* in, const char* expected_out, int expected_rc,
     68                          char* buf, size_t buf_size, int expected_errno) {
     69   errno = 0;
     70   int rc = basename_r(in, buf, buf_size);
     71   ASSERT_EQ(expected_rc, rc) << in;
     72   if (rc != -1 && buf != NULL) {
     73     ASSERT_STREQ(expected_out, buf) << in;
     74   }
     75   ASSERT_EQ(expected_errno, errno) << in;
     76 }
     77 
     78 static void TestDirname(const char* in, const char* expected_out, int expected_rc,
     79                         char* buf, size_t buf_size, int expected_errno) {
     80   errno = 0;
     81   int rc = dirname_r(in, buf, buf_size);
     82   ASSERT_EQ(expected_rc, rc) << in;
     83   if (rc != -1 && buf != NULL) {
     84     ASSERT_STREQ(expected_out, buf) << in;
     85   }
     86   ASSERT_EQ(expected_errno, errno) << in;
     87 }
     88 
     89 TEST(libgen, basename_r) {
     90   char buf[256];
     91   TestBasename("", ".",  1, NULL, 0, 0);
     92   TestBasename("", ".", -1, buf, 0, ERANGE);
     93   TestBasename("", ".", -1, buf, 1, ERANGE);
     94   TestBasename("", ".", 1, buf, 2, 0);
     95   TestBasename("", ".", 1, buf, sizeof(buf), 0);
     96   TestBasename("/usr/lib", "lib", 3, buf, sizeof(buf), 0);
     97   TestBasename("/usr/", "usr", 3, buf, sizeof(buf), 0);
     98   TestBasename("usr", "usr", 3, buf, sizeof(buf), 0);
     99   TestBasename("/", "/", 1, buf, sizeof(buf), 0);
    100   TestBasename(".", ".", 1, buf, sizeof(buf), 0);
    101   TestBasename("..", "..", 2, buf, sizeof(buf), 0);
    102 }
    103 
    104 TEST(libgen, dirname_r) {
    105   char buf[256];
    106   TestDirname("", ".",  1, NULL, 0, 0);
    107   TestDirname("", ".", -1, buf, 0, ERANGE);
    108   TestDirname("", ".", -1, buf, 1, ERANGE);
    109   TestDirname("", ".", 1, buf, 2, 0);
    110   TestDirname("/usr/lib", "/usr", 4, buf, sizeof(buf), 0);
    111   TestDirname("/usr/", "/", 1, buf, sizeof(buf), 0);
    112   TestDirname("usr", ".", 1, buf, sizeof(buf), 0);
    113   TestDirname(".", ".", 1, buf, sizeof(buf), 0);
    114   TestDirname("..", ".", 1, buf, sizeof(buf), 0);
    115 }
    116 
    117 #endif
    118