Home | History | Annotate | Download | only in linux
      1 // Copyright (c) 2011, Google Inc.
      2 // All rights reserved.
      3 //
      4 // Redistribution and use in source and binary forms, with or without
      5 // modification, are permitted provided that the following conditions are
      6 // met:
      7 //
      8 //     * Redistributions of source code must retain the above copyright
      9 // notice, this list of conditions and the following disclaimer.
     10 //     * Redistributions in binary form must reproduce the above
     11 // copyright notice, this list of conditions and the following disclaimer
     12 // in the documentation and/or other materials provided with the
     13 // distribution.
     14 //     * Neither the name of Google Inc. nor the names of its
     15 // contributors may be used to endorse or promote products derived from
     16 // this software without specific prior written permission.
     17 //
     18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29 
     30 // safe_readlink_unittest.cc: Unit tests for google_breakpad::SafeReadLink.
     31 
     32 #include "breakpad_googletest_includes.h"
     33 #include "common/linux/safe_readlink.h"
     34 
     35 using google_breakpad::SafeReadLink;
     36 
     37 TEST(SafeReadLinkTest, ZeroBufferSize) {
     38   char buffer[1];
     39   EXPECT_FALSE(SafeReadLink("/proc/self/exe", buffer, 0));
     40 }
     41 
     42 TEST(SafeReadLinkTest, BufferSizeTooSmall) {
     43   char buffer[1];
     44   EXPECT_FALSE(SafeReadLink("/proc/self/exe", buffer, 1));
     45 }
     46 
     47 TEST(SafeReadLinkTest, BoundaryBufferSize) {
     48   char buffer[PATH_MAX];
     49   EXPECT_TRUE(SafeReadLink("/proc/self/exe", buffer, sizeof(buffer)));
     50   size_t path_length = strlen(buffer);
     51   EXPECT_LT(0U, path_length);
     52   EXPECT_GT(sizeof(buffer), path_length);
     53 
     54   // Buffer size equals to the expected path length plus 1 for the NULL byte.
     55   char buffer2[PATH_MAX];
     56   EXPECT_TRUE(SafeReadLink("/proc/self/exe", buffer2, path_length + 1));
     57   EXPECT_EQ(path_length, strlen(buffer2));
     58   EXPECT_EQ(0, strncmp(buffer, buffer2, PATH_MAX));
     59 
     60   // Buffer size equals to the expected path length.
     61   EXPECT_FALSE(SafeReadLink("/proc/self/exe", buffer, path_length));
     62 }
     63 
     64 TEST(SafeReadLinkTest, NonexistentPath) {
     65   char buffer[PATH_MAX];
     66   EXPECT_FALSE(SafeReadLink("nonexistent_path", buffer, sizeof(buffer)));
     67 }
     68 
     69 TEST(SafeReadLinkTest, NonSymbolicLinkPath) {
     70   char actual_path[PATH_MAX];
     71   EXPECT_TRUE(SafeReadLink("/proc/self/exe", actual_path, sizeof(actual_path)));
     72 
     73   char buffer[PATH_MAX];
     74   EXPECT_FALSE(SafeReadLink(actual_path, buffer, sizeof(buffer)));
     75 }
     76 
     77 TEST(SafeReadLinkTest, DeduceBufferSizeFromCharArray) {
     78   char buffer[PATH_MAX];
     79   char* buffer_pointer = buffer;
     80   EXPECT_TRUE(SafeReadLink("/proc/self/exe", buffer_pointer, sizeof(buffer)));
     81   size_t path_length = strlen(buffer);
     82 
     83   // Use the template version of SafeReadLink to deduce the buffer size
     84   // from the char array.
     85   char buffer2[PATH_MAX];
     86   EXPECT_TRUE(SafeReadLink("/proc/self/exe", buffer2));
     87   EXPECT_EQ(path_length, strlen(buffer2));
     88   EXPECT_EQ(0, strncmp(buffer, buffer2, PATH_MAX));
     89 }
     90