summaryrefslogtreecommitdiff
path: root/tests/stdio_test.cpp
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2012-09-24 17:55:15 -0700
committerElliott Hughes <enh@google.com>2012-09-26 12:02:17 -0700
commit91875dcd6e17b7f3b251efe9b236b905ef414dde (patch)
tree3e1fb66b922496ec295bd63bfea01fe339f7fcc0 /tests/stdio_test.cpp
parent6e0e03c38bc0e864225b89b3fb273fb56ded8940 (diff)
Fix tmpfile(3).
This could be better, but at least now it works. Change-Id: I88b7cf3f7ce8e5fa0b3fe678b7d1679a68ffffc9
Diffstat (limited to 'tests/stdio_test.cpp')
-rw-r--r--tests/stdio_test.cpp48
1 files changed, 48 insertions, 0 deletions
diff --git a/tests/stdio_test.cpp b/tests/stdio_test.cpp
new file mode 100644
index 000000000..72af7962e
--- /dev/null
+++ b/tests/stdio_test.cpp
@@ -0,0 +1,48 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <gtest/gtest.h>
+
+#include <errno.h>
+#include <stdio.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+TEST(stdio, tmpfile_fileno_fprintf_rewind_fgets) {
+ FILE* fp = tmpfile();
+ ASSERT_TRUE(fp != NULL);
+
+ int fd = fileno(fp);
+ ASSERT_NE(fd, -1);
+
+ struct stat sb;
+ int rc = fstat(fd, &sb);
+ ASSERT_NE(rc, -1);
+ ASSERT_EQ(sb.st_mode & 0777, 0600U);
+
+ rc = fprintf(fp, "hello\n");
+ ASSERT_EQ(rc, 6);
+
+ rewind(fp);
+
+ char buf[16];
+ char* s = fgets(buf, sizeof(buf), fp);
+ ASSERT_TRUE(s != NULL);
+ ASSERT_STREQ("hello\n", s);
+
+ fclose(fp);
+}