blob: 72af7962ec61de2cde2a37b70ba7b020a587d2b5 [file] [log] [blame]
Elliott Hughes91875dc2012-09-24 17:55:15 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <gtest/gtest.h>
18
19#include <errno.h>
20#include <stdio.h>
21#include <sys/types.h>
22#include <sys/stat.h>
23#include <unistd.h>
24
25TEST(stdio, tmpfile_fileno_fprintf_rewind_fgets) {
26 FILE* fp = tmpfile();
27 ASSERT_TRUE(fp != NULL);
28
29 int fd = fileno(fp);
30 ASSERT_NE(fd, -1);
31
32 struct stat sb;
33 int rc = fstat(fd, &sb);
34 ASSERT_NE(rc, -1);
35 ASSERT_EQ(sb.st_mode & 0777, 0600U);
36
37 rc = fprintf(fp, "hello\n");
38 ASSERT_EQ(rc, 6);
39
40 rewind(fp);
41
42 char buf[16];
43 char* s = fgets(buf, sizeof(buf), fp);
44 ASSERT_TRUE(s != NULL);
45 ASSERT_STREQ("hello\n", s);
46
47 fclose(fp);
48}