|
| 1 | +/* Copyright (c) 2013, 2017, Oracle and/or its affiliates. All rights reserved. |
| 2 | +
|
| 3 | + This program is free software; you can redistribute it and/or modify |
| 4 | + it under the terms of the GNU General Public License as published by |
| 5 | + the Free Software Foundation; version 2 of the License. |
| 6 | +
|
| 7 | + This program is distributed in the hope that it will be useful, |
| 8 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | + GNU General Public License for more details. |
| 11 | +
|
| 12 | + You should have received a copy of the GNU General Public License |
| 13 | + along with this program; if not, write to the Free Software |
| 14 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 15 | + |
| 16 | +#include "my_config.h" |
| 17 | + |
| 18 | +#include <errno.h> |
| 19 | +#include <sys/types.h> |
| 20 | +#include <sys/stat.h> |
| 21 | +#include <fcntl.h> |
| 22 | + |
| 23 | +#include <gmock/gmock.h> |
| 24 | +#include <gtest/gtest.h> |
| 25 | +#include <stddef.h> |
| 26 | +#ifdef HAVE_UNISTD_H |
| 27 | +#include <unistd.h> |
| 28 | +#endif |
| 29 | + |
| 30 | +#include "my_sys.h" |
| 31 | + |
| 32 | +#ifndef _WIN32 |
| 33 | + |
| 34 | +namespace mysys_my_freopen_unittest { |
| 35 | +FILE *null_file= NULL; |
| 36 | + |
| 37 | +class MysysMyFreopenTest : public ::testing::Test |
| 38 | +{ |
| 39 | +public: |
| 40 | + FILE *stream; |
| 41 | + char name[32]; |
| 42 | + MysysMyFreopenTest() : |
| 43 | + stream(NULL) |
| 44 | + { |
| 45 | + strncpy(name, "MyFreopen_XXXXXX", 32); |
| 46 | + } |
| 47 | + virtual void SetUp() |
| 48 | + { |
| 49 | + int fd= mkstemp(name); |
| 50 | + stream= fdopen(fd, "a"); |
| 51 | + } |
| 52 | + virtual void TearDown() |
| 53 | + { |
| 54 | + if (stream != NULL) |
| 55 | + { |
| 56 | + fclose(stream); |
| 57 | + unlink(name); |
| 58 | + } |
| 59 | + } |
| 60 | +}; |
| 61 | + |
| 62 | + |
| 63 | +// Test case demonstates that freopen is not atomic |
| 64 | +TEST_F(MysysMyFreopenTest, FreopenFailure) |
| 65 | +{ |
| 66 | + EXPECT_EQ(null_file, freopen("/", "a", stream)); |
| 67 | + const char *txt= "This text should end up in old stream file"; |
| 68 | + EXPECT_EQ(EOF, fputs(txt, stream)); |
| 69 | + EXPECT_NE(0, ferror(stream)); |
| 70 | + EXPECT_EQ(0, fflush(stream)); |
| 71 | + |
| 72 | + char buf[64]= "nada"; |
| 73 | + FILE *instream= fopen(name, "r"); |
| 74 | + EXPECT_NE(null_file, instream); |
| 75 | + EXPECT_EQ(static_cast<char*>(NULL), fgets(buf, 64, instream)); |
| 76 | + EXPECT_EQ(0, ferror(instream)); |
| 77 | + EXPECT_NE(0, feof(instream)); |
| 78 | + EXPECT_STREQ("nada", buf); |
| 79 | + EXPECT_EQ(0, fclose(instream)); |
| 80 | +} |
| 81 | + |
| 82 | +// Positive test case for the new version of my_freopen |
| 83 | +TEST_F(MysysMyFreopenTest, MyFreopenOK) |
| 84 | +{ |
| 85 | + char fname[32] = "MyFreopenOK_XXXXXX"; |
| 86 | + int fd= mkstemp(fname); |
| 87 | + |
| 88 | + EXPECT_EQ(stream, my_freopen(fname, "a", stream)); |
| 89 | + const char *txt= "This text should end up in fname"; |
| 90 | + int txtlen= strlen(txt); |
| 91 | + EXPECT_EQ(32, txtlen); |
| 92 | + EXPECT_LT(0, fputs(txt, stream)); |
| 93 | + EXPECT_EQ(0, fflush(stream)); |
| 94 | + |
| 95 | + char buf[64]; |
| 96 | + FILE *instream= fdopen(fd, "r"); |
| 97 | + EXPECT_NE(null_file, instream); |
| 98 | + EXPECT_EQ(buf, fgets(buf, 64, instream)); |
| 99 | + EXPECT_STREQ(txt, buf); |
| 100 | + EXPECT_EQ(0, fclose(instream)); |
| 101 | + EXPECT_EQ(0, unlink(fname)); |
| 102 | +} |
| 103 | + |
| 104 | + |
| 105 | +// Negative test case for my_reopen. Shows that even if my_reopen |
| 106 | +// fails, it is still possible to write to the stream. |
| 107 | +TEST_F(MysysMyFreopenTest, MyFreopenFailure) |
| 108 | +{ |
| 109 | + EXPECT_EQ(null_file, my_freopen("/", "a", stream)); |
| 110 | + const char *txt= "This text should end up in old stream file"; |
| 111 | + EXPECT_LT(0, fputs(txt, stream)); |
| 112 | + EXPECT_EQ(0, fflush(stream)); |
| 113 | + |
| 114 | + char buf[64]; |
| 115 | + FILE *instream= fopen(name, "r"); |
| 116 | + EXPECT_NE(null_file, instream); |
| 117 | + EXPECT_EQ(buf, fgets(buf, 64, instream)); |
| 118 | + EXPECT_STREQ(txt, buf); |
| 119 | + EXPECT_EQ(0, fclose(instream)); |
| 120 | +} |
| 121 | +} |
| 122 | +#endif |
0 commit comments