| 1 | /*
|
|---|
| 2 | * Copyright (c) 2026 Jiri Svoboda
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | #include <errno.h>
|
|---|
| 30 | #include <fmgt.h>
|
|---|
| 31 | #include <pcut/pcut.h>
|
|---|
| 32 | #include <stdbool.h>
|
|---|
| 33 | #include <stdio.h>
|
|---|
| 34 | #include <str.h>
|
|---|
| 35 | #include <vfs/vfs.h>
|
|---|
| 36 |
|
|---|
| 37 | #include "../private/fsops.h"
|
|---|
| 38 |
|
|---|
| 39 | PCUT_INIT;
|
|---|
| 40 |
|
|---|
| 41 | PCUT_TEST_SUITE(fsops);
|
|---|
| 42 |
|
|---|
| 43 | /** Open file. */
|
|---|
| 44 | PCUT_TEST(open)
|
|---|
| 45 | {
|
|---|
| 46 | fmgt_t *fmgt = NULL;
|
|---|
| 47 | char buf[L_tmpnam];
|
|---|
| 48 | FILE *f;
|
|---|
| 49 | char *p;
|
|---|
| 50 | int fd;
|
|---|
| 51 | int rv;
|
|---|
| 52 | errno_t rc;
|
|---|
| 53 |
|
|---|
| 54 | /* Create name for temporary file */
|
|---|
| 55 | p = tmpnam(buf);
|
|---|
| 56 | PCUT_ASSERT_NOT_NULL(p);
|
|---|
| 57 |
|
|---|
| 58 | f = fopen(p, "wb");
|
|---|
| 59 | PCUT_ASSERT_NOT_NULL(f);
|
|---|
| 60 |
|
|---|
| 61 | rv = fprintf(f, "X");
|
|---|
| 62 | PCUT_ASSERT_TRUE(rv >= 0);
|
|---|
| 63 |
|
|---|
| 64 | rv = fclose(f);
|
|---|
| 65 | PCUT_ASSERT_INT_EQUALS(0, rv);
|
|---|
| 66 |
|
|---|
| 67 | rc = fmgt_create(&fmgt);
|
|---|
| 68 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 69 |
|
|---|
| 70 | rc = fmgt_open(fmgt, p, &fd);
|
|---|
| 71 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 72 |
|
|---|
| 73 | fmgt_destroy(fmgt);
|
|---|
| 74 | vfs_put(fd);
|
|---|
| 75 |
|
|---|
| 76 | rv = remove(p);
|
|---|
| 77 | PCUT_ASSERT_INT_EQUALS(0, rv);
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | /** Create file. */
|
|---|
| 81 | PCUT_TEST(create_file)
|
|---|
| 82 | {
|
|---|
| 83 | fmgt_t *fmgt = NULL;
|
|---|
| 84 | char buf[L_tmpnam];
|
|---|
| 85 | char *p;
|
|---|
| 86 | int fd;
|
|---|
| 87 | int rv;
|
|---|
| 88 | fmgt_exists_action_t exaction;
|
|---|
| 89 | errno_t rc;
|
|---|
| 90 |
|
|---|
| 91 | /* Create name for temporary file */
|
|---|
| 92 | p = tmpnam(buf);
|
|---|
| 93 | PCUT_ASSERT_NOT_NULL(p);
|
|---|
| 94 |
|
|---|
| 95 | rc = fmgt_create(&fmgt);
|
|---|
| 96 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 97 |
|
|---|
| 98 | rc = fmgt_create_file(fmgt, p, &fd, &exaction);
|
|---|
| 99 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 100 |
|
|---|
| 101 | fmgt_destroy(fmgt);
|
|---|
| 102 | vfs_put(fd);
|
|---|
| 103 |
|
|---|
| 104 | rv = remove(p);
|
|---|
| 105 | PCUT_ASSERT_INT_EQUALS(0, rv);
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | /** Create directory. */
|
|---|
| 109 | PCUT_TEST(create_dir)
|
|---|
| 110 | {
|
|---|
| 111 | fmgt_t *fmgt = NULL;
|
|---|
| 112 | char buf[L_tmpnam];
|
|---|
| 113 | char *p;
|
|---|
| 114 | int rv;
|
|---|
| 115 | errno_t rc;
|
|---|
| 116 |
|
|---|
| 117 | /* Create name for temporary directory */
|
|---|
| 118 | p = tmpnam(buf);
|
|---|
| 119 | PCUT_ASSERT_NOT_NULL(p);
|
|---|
| 120 |
|
|---|
| 121 | rc = fmgt_create(&fmgt);
|
|---|
| 122 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 123 |
|
|---|
| 124 | rc = fmgt_create_dir(fmgt, p);
|
|---|
| 125 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 126 |
|
|---|
| 127 | fmgt_destroy(fmgt);
|
|---|
| 128 |
|
|---|
| 129 | rv = remove(p);
|
|---|
| 130 | PCUT_ASSERT_INT_EQUALS(0, rv);
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | /** Remove file. */
|
|---|
| 134 | PCUT_TEST(remove)
|
|---|
| 135 | {
|
|---|
| 136 | fmgt_t *fmgt = NULL;
|
|---|
| 137 | char buf[L_tmpnam];
|
|---|
| 138 | FILE *f;
|
|---|
| 139 | char *p;
|
|---|
| 140 | int rv;
|
|---|
| 141 | errno_t rc;
|
|---|
| 142 |
|
|---|
| 143 | /* Create name for temporary file */
|
|---|
| 144 | p = tmpnam(buf);
|
|---|
| 145 | PCUT_ASSERT_NOT_NULL(p);
|
|---|
| 146 |
|
|---|
| 147 | f = fopen(p, "wb");
|
|---|
| 148 | PCUT_ASSERT_NOT_NULL(f);
|
|---|
| 149 |
|
|---|
| 150 | rv = fprintf(f, "X");
|
|---|
| 151 | PCUT_ASSERT_TRUE(rv >= 0);
|
|---|
| 152 |
|
|---|
| 153 | rv = fclose(f);
|
|---|
| 154 | PCUT_ASSERT_INT_EQUALS(0, rv);
|
|---|
| 155 |
|
|---|
| 156 | rc = fmgt_create(&fmgt);
|
|---|
| 157 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 158 |
|
|---|
| 159 | rc = fmgt_remove(fmgt, p);
|
|---|
| 160 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 161 |
|
|---|
| 162 | fmgt_destroy(fmgt);
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | /** Read data from file. */
|
|---|
| 166 | PCUT_TEST(read)
|
|---|
| 167 | {
|
|---|
| 168 | fmgt_t *fmgt = NULL;
|
|---|
| 169 | char buf[L_tmpnam];
|
|---|
| 170 | char dbuffer[64];
|
|---|
| 171 | FILE *f;
|
|---|
| 172 | char *p;
|
|---|
| 173 | int fd;
|
|---|
| 174 | int rv;
|
|---|
| 175 | aoff64_t pos;
|
|---|
| 176 | size_t nr;
|
|---|
| 177 | errno_t rc;
|
|---|
| 178 |
|
|---|
| 179 | /* Create name for temporary file */
|
|---|
| 180 | p = tmpnam(buf);
|
|---|
| 181 | PCUT_ASSERT_NOT_NULL(p);
|
|---|
| 182 |
|
|---|
| 183 | f = fopen(p, "wb");
|
|---|
| 184 | PCUT_ASSERT_NOT_NULL(f);
|
|---|
| 185 |
|
|---|
| 186 | rv = fprintf(f, "XYZ");
|
|---|
| 187 | PCUT_ASSERT_TRUE(rv >= 0);
|
|---|
| 188 |
|
|---|
| 189 | (void)fclose(f);
|
|---|
| 190 |
|
|---|
| 191 | rc = vfs_lookup_open(p, WALK_REGULAR, MODE_READ, &fd);
|
|---|
| 192 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 193 |
|
|---|
| 194 | rc = fmgt_create(&fmgt);
|
|---|
| 195 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 196 |
|
|---|
| 197 | pos = 0;
|
|---|
| 198 | rc = fmgt_read(fmgt, fd, p, &pos, dbuffer, sizeof(dbuffer), &nr);
|
|---|
| 199 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 200 | PCUT_ASSERT_INT_EQUALS(3, nr);
|
|---|
| 201 | PCUT_ASSERT_INT_EQUALS(3, pos);
|
|---|
| 202 |
|
|---|
| 203 | fmgt_destroy(fmgt);
|
|---|
| 204 | vfs_put(fd);
|
|---|
| 205 |
|
|---|
| 206 | rv = remove(p);
|
|---|
| 207 | PCUT_ASSERT_INT_EQUALS(0, rv);
|
|---|
| 208 |
|
|---|
| 209 | }
|
|---|
| 210 |
|
|---|
| 211 | /** Write data to file. */
|
|---|
| 212 | PCUT_TEST(write)
|
|---|
| 213 | {
|
|---|
| 214 |
|
|---|
| 215 | fmgt_t *fmgt = NULL;
|
|---|
| 216 | char buf[L_tmpnam];
|
|---|
| 217 | char dbuffer[64];
|
|---|
| 218 | char data[3] = "XYZ";
|
|---|
| 219 | FILE *f;
|
|---|
| 220 | char *p;
|
|---|
| 221 | int fd;
|
|---|
| 222 | int rv;
|
|---|
| 223 | aoff64_t pos;
|
|---|
| 224 | size_t nr;
|
|---|
| 225 | errno_t rc;
|
|---|
| 226 |
|
|---|
| 227 | /* Create name for temporary file */
|
|---|
| 228 | p = tmpnam(buf);
|
|---|
| 229 | PCUT_ASSERT_NOT_NULL(p);
|
|---|
| 230 |
|
|---|
| 231 | rc = fmgt_create(&fmgt);
|
|---|
| 232 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 233 |
|
|---|
| 234 | rc = vfs_lookup_open(p, WALK_REGULAR | WALK_MUST_CREATE, MODE_WRITE,
|
|---|
| 235 | &fd);
|
|---|
| 236 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 237 |
|
|---|
| 238 | pos = 0;
|
|---|
| 239 | rc = fmgt_write(fmgt, fd, p, &pos, data, sizeof(data));
|
|---|
| 240 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 241 | PCUT_ASSERT_INT_EQUALS(pos, 3);
|
|---|
| 242 |
|
|---|
| 243 | fmgt_destroy(fmgt);
|
|---|
| 244 | vfs_put(fd);
|
|---|
| 245 |
|
|---|
| 246 | f = fopen(p, "rb");
|
|---|
| 247 | PCUT_ASSERT_NOT_NULL(f);
|
|---|
| 248 |
|
|---|
| 249 | nr = fread(dbuffer, 1, sizeof(dbuffer), f);
|
|---|
| 250 | PCUT_ASSERT_INT_EQUALS(nr, 3);
|
|---|
| 251 | (void)fclose(f);
|
|---|
| 252 |
|
|---|
| 253 | PCUT_ASSERT_TRUE(memcmp(data, dbuffer, sizeof(data)) == 0);
|
|---|
| 254 |
|
|---|
| 255 | rv = remove(p);
|
|---|
| 256 | PCUT_ASSERT_INT_EQUALS(0, rv);
|
|---|
| 257 | }
|
|---|
| 258 |
|
|---|
| 259 | PCUT_EXPORT(fsops);
|
|---|