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 #ifndef _GNU_SOURCE
     18   #define _GNU_SOURCE 1
     19 #endif
     20 
     21 #include <string.h>
     22 
     23 #if defined(basename)
     24   #error basename should not be defined at this point
     25 #endif
     26 
     27 static const char* gnu_basename(const char* in) {
     28   return basename(in);
     29 }
     30 
     31 #include <libgen.h>
     32 
     33 #if !defined(basename)
     34   #error basename should be defined at this point
     35 #endif
     36 
     37 static char* posix_basename(char* in) {
     38   return basename(in);
     39 }
     40 
     41 #include <errno.h>
     42 #include <gtest/gtest.h>
     43 
     44 static void __TestGnuBasename(const char* in, const char* expected_out, int line) {
     45   errno = 0;
     46   const char* out = gnu_basename(in);
     47   ASSERT_STREQ(expected_out, out) << "(" << line << "): " << in << std::endl;
     48   ASSERT_EQ(0, errno) << "(" << line << "): " << in << std::endl;
     49 }
     50 
     51 static void __TestPosixBasename(const char* in, const char* expected_out, int line) {
     52   char* writable_in = (in != NULL) ? strdup(in) : NULL;
     53   errno = 0;
     54   const char* out = posix_basename(&writable_in[0]);
     55   ASSERT_STREQ(expected_out, out) << "(" << line << "): " << in << std::endl;
     56   ASSERT_EQ(0, errno) << "(" << line << "): " << in << std::endl;
     57   free(writable_in);
     58 }
     59 
     60 #define TestGnuBasename(in, expected) __TestGnuBasename(in, expected, __LINE__)
     61 #define TestPosixBasename(in, expected) __TestPosixBasename(in, expected, __LINE__)
     62 
     63 TEST(libgen_basename, gnu_basename) {
     64   // GNU's basename doesn't accept NULL
     65   // TestGnuBasename(NULL, ".");
     66   TestGnuBasename("", "");
     67   TestGnuBasename("/usr/lib", "lib");
     68   TestGnuBasename("/system/bin/sh/", "");
     69   TestGnuBasename("/usr/", "");
     70   TestGnuBasename("usr", "usr");
     71   TestGnuBasename("/", "");
     72   TestGnuBasename(".", ".");
     73   TestGnuBasename("..", "..");
     74   TestGnuBasename("///", "");
     75   TestGnuBasename("//usr//lib//", "");
     76 }
     77 
     78 TEST(libgen_basename, posix_basename) {
     79   TestPosixBasename(NULL, ".");
     80   TestPosixBasename("", ".");
     81   TestPosixBasename("/usr/lib", "lib");
     82   TestPosixBasename("/system/bin/sh/", "sh");
     83   TestPosixBasename("/usr/", "usr");
     84   TestPosixBasename("usr", "usr");
     85   TestPosixBasename("/", "/");
     86   TestPosixBasename(".", ".");
     87   TestPosixBasename("..", "..");
     88   TestPosixBasename("///", "/");
     89   TestPosixBasename("//usr//lib//", "lib");
     90 }
     91