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