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