| 1 | /*
|
|---|
| 2 | * SPDX-FileCopyrightText: 2018 Jiri Svoboda
|
|---|
| 3 | *
|
|---|
| 4 | * SPDX-License-Identifier: BSD-3-Clause
|
|---|
| 5 | */
|
|---|
| 6 |
|
|---|
| 7 | /** @addtogroup libc
|
|---|
| 8 | * @{
|
|---|
| 9 | */
|
|---|
| 10 | /**
|
|---|
| 11 | * @file
|
|---|
| 12 | * @brief Test stdio module
|
|---|
| 13 | */
|
|---|
| 14 |
|
|---|
| 15 | #include <errno.h>
|
|---|
| 16 | #include <pcut/pcut.h>
|
|---|
| 17 | #include <stdio.h>
|
|---|
| 18 | #include <str.h>
|
|---|
| 19 | #include <tmpfile.h>
|
|---|
| 20 | #include <vfs/vfs.h>
|
|---|
| 21 |
|
|---|
| 22 | PCUT_INIT;
|
|---|
| 23 |
|
|---|
| 24 | PCUT_TEST_SUITE(stdio);
|
|---|
| 25 |
|
|---|
| 26 | /** remove function */
|
|---|
| 27 | PCUT_TEST(remove)
|
|---|
| 28 | {
|
|---|
| 29 | char buf[L_tmpnam];
|
|---|
| 30 | char *p;
|
|---|
| 31 | FILE *f;
|
|---|
| 32 | int rc;
|
|---|
| 33 |
|
|---|
| 34 | /* Generate unique file name */
|
|---|
| 35 | p = tmpnam(buf);
|
|---|
| 36 | PCUT_ASSERT_NOT_NULL(p);
|
|---|
| 37 |
|
|---|
| 38 | /* Removing it should fail */
|
|---|
| 39 | rc = remove(buf);
|
|---|
| 40 | PCUT_ASSERT_TRUE(rc != 0);
|
|---|
| 41 |
|
|---|
| 42 | f = fopen(buf, "wx");
|
|---|
| 43 | PCUT_ASSERT_NOT_NULL(f);
|
|---|
| 44 | fclose(f);
|
|---|
| 45 |
|
|---|
| 46 | /* Remove for the first time */
|
|---|
| 47 | rc = remove(buf);
|
|---|
| 48 | PCUT_ASSERT_INT_EQUALS(0, rc);
|
|---|
| 49 |
|
|---|
| 50 | /* Should fail the second time */
|
|---|
| 51 | rc = remove(buf);
|
|---|
| 52 | PCUT_ASSERT_TRUE(rc != 0);
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | /** rename function */
|
|---|
| 56 | PCUT_TEST(rename)
|
|---|
| 57 | {
|
|---|
| 58 | char buf1[L_tmpnam];
|
|---|
| 59 | char buf2[L_tmpnam];
|
|---|
| 60 | char *p;
|
|---|
| 61 | FILE *f;
|
|---|
| 62 | int rc;
|
|---|
| 63 |
|
|---|
| 64 | /* Generate first unique file name */
|
|---|
| 65 | p = tmpnam(buf1);
|
|---|
| 66 | PCUT_ASSERT_NOT_NULL(p);
|
|---|
| 67 |
|
|---|
| 68 | /* Generate second unique file name */
|
|---|
| 69 | p = tmpnam(buf2);
|
|---|
| 70 | PCUT_ASSERT_NOT_NULL(p);
|
|---|
| 71 |
|
|---|
| 72 | f = fopen(buf1, "wx");
|
|---|
| 73 | PCUT_ASSERT_NOT_NULL(f);
|
|---|
| 74 | fclose(f);
|
|---|
| 75 |
|
|---|
| 76 | /* Rename to the second name */
|
|---|
| 77 | rc = rename(buf1, buf2);
|
|---|
| 78 | PCUT_ASSERT_INT_EQUALS(0, rc);
|
|---|
| 79 |
|
|---|
| 80 | /* First name should no longer exist */
|
|---|
| 81 | rc = remove(buf1);
|
|---|
| 82 | PCUT_ASSERT_TRUE(rc != 0);
|
|---|
| 83 |
|
|---|
| 84 | /* Second can be removed */
|
|---|
| 85 | rc = remove(buf2);
|
|---|
| 86 | PCUT_ASSERT_INT_EQUALS(0, rc);
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | /** tmpfile function */
|
|---|
| 90 | PCUT_TEST(tmpfile)
|
|---|
| 91 | {
|
|---|
| 92 | FILE *f;
|
|---|
| 93 | char c;
|
|---|
| 94 | size_t n;
|
|---|
| 95 |
|
|---|
| 96 | f = tmpfile();
|
|---|
| 97 | PCUT_ASSERT_NOT_NULL(f);
|
|---|
| 98 |
|
|---|
| 99 | c = 'x';
|
|---|
| 100 | n = fwrite(&c, sizeof(c), 1, f);
|
|---|
| 101 | PCUT_ASSERT_INT_EQUALS(1, n);
|
|---|
| 102 |
|
|---|
| 103 | rewind(f);
|
|---|
| 104 | c = '\0';
|
|---|
| 105 | n = fread(&c, sizeof(c), 1, f);
|
|---|
| 106 | PCUT_ASSERT_INT_EQUALS(1, n);
|
|---|
| 107 | PCUT_ASSERT_INT_EQUALS('x', c);
|
|---|
| 108 |
|
|---|
| 109 | fclose(f);
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | /** tmpnam function with buffer argument */
|
|---|
| 113 | PCUT_TEST(tmpnam_buf)
|
|---|
| 114 | {
|
|---|
| 115 | char buf[L_tmpnam];
|
|---|
| 116 | char *p;
|
|---|
| 117 | FILE *f;
|
|---|
| 118 |
|
|---|
| 119 | p = tmpnam(buf);
|
|---|
| 120 | PCUT_ASSERT_NOT_NULL(p);
|
|---|
| 121 |
|
|---|
| 122 | f = fopen(p, "w+x");
|
|---|
| 123 | PCUT_ASSERT_NOT_NULL(f);
|
|---|
| 124 | (void) remove(p);
|
|---|
| 125 | fclose(f);
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | /** tmpnam function called twice */
|
|---|
| 129 | PCUT_TEST(tmpnam_twice)
|
|---|
| 130 | {
|
|---|
| 131 | char buf1[L_tmpnam];
|
|---|
| 132 | char buf2[L_tmpnam];
|
|---|
| 133 | char *p;
|
|---|
| 134 |
|
|---|
| 135 | p = tmpnam(buf1);
|
|---|
| 136 | PCUT_ASSERT_NOT_NULL(p);
|
|---|
| 137 |
|
|---|
| 138 | p = tmpnam(buf2);
|
|---|
| 139 | PCUT_ASSERT_NOT_NULL(p);
|
|---|
| 140 |
|
|---|
| 141 | /* We must get two distinct names */
|
|---|
| 142 | PCUT_ASSERT_TRUE(str_cmp(buf1, buf2) != 0);
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | /** tmpnam function with NULL argument */
|
|---|
| 146 | PCUT_TEST(tmpnam_null)
|
|---|
| 147 | {
|
|---|
| 148 | char *p;
|
|---|
| 149 | FILE *f;
|
|---|
| 150 |
|
|---|
| 151 | p = tmpnam(NULL);
|
|---|
| 152 | PCUT_ASSERT_NOT_NULL(p);
|
|---|
| 153 |
|
|---|
| 154 | f = fopen(p, "w+x");
|
|---|
| 155 | PCUT_ASSERT_NOT_NULL(f);
|
|---|
| 156 | (void) remove(p);
|
|---|
| 157 | fclose(f);
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | /** fgetpos and fsetpos functions */
|
|---|
| 161 | PCUT_TEST(fgetpos_setpos)
|
|---|
| 162 | {
|
|---|
| 163 | fpos_t pos;
|
|---|
| 164 | int rc;
|
|---|
| 165 | FILE *f;
|
|---|
| 166 |
|
|---|
| 167 | f = tmpfile();
|
|---|
| 168 | PCUT_ASSERT_NOT_NULL(f);
|
|---|
| 169 |
|
|---|
| 170 | rc = fputs("abc", f);
|
|---|
| 171 | PCUT_ASSERT_TRUE(rc >= 0);
|
|---|
| 172 |
|
|---|
| 173 | rc = fgetpos(f, &pos);
|
|---|
| 174 | PCUT_ASSERT_TRUE(rc >= 0);
|
|---|
| 175 |
|
|---|
| 176 | rewind(f);
|
|---|
| 177 |
|
|---|
| 178 | rc = fsetpos(f, &pos);
|
|---|
| 179 | PCUT_ASSERT_TRUE(rc >= 0);
|
|---|
| 180 |
|
|---|
| 181 | (void) fclose(f);
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | /** perror function with NULL as argument */
|
|---|
| 185 | PCUT_TEST(perror_null_msg)
|
|---|
| 186 | {
|
|---|
| 187 | errno = EINVAL;
|
|---|
| 188 | perror(NULL);
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | /** perror function with empty string as argument */
|
|---|
| 192 | PCUT_TEST(perror_empty_msg)
|
|---|
| 193 | {
|
|---|
| 194 | errno = EINVAL;
|
|---|
| 195 | perror("");
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | /** perror function with a message argument */
|
|---|
| 199 | PCUT_TEST(perror_msg)
|
|---|
| 200 | {
|
|---|
| 201 | errno = EINVAL;
|
|---|
| 202 | perror("This is a test");
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | PCUT_EXPORT(stdio);
|
|---|