Skip to content

Commit ab517ed

Browse files
committed
tests: posix: add testsuite for POSIX_FILE_LOCKING Option Group
Add a testsuite for the POSIX_FILE_LOCKING Option Group. Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
1 parent 5b969ad commit ab517ed

File tree

7 files changed

+532
-0
lines changed

7 files changed

+532
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
5+
project(posix_file_locking)
6+
7+
FILE(GLOB app_sources src/*.c)
8+
target_sources(app PRIVATE ${app_sources})
9+
10+
target_compile_options(app PRIVATE -U_POSIX_C_SOURCE -D_POSIX_C_SOURCE=200809L)

tests/posix/file_locking/Kconfig

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright (c) 2024 Tenstorrent AI ULC
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
source "Kconfig.zephyr"
5+
6+
menu "File Locking Tests"
7+
8+
config TEST_NEGLIGIBLE_DELAY_MS
9+
int "Negligible delay in milliseconds"
10+
default 10
11+
help
12+
Delays less than or equal to this value are considered neglible for the purposes of this
13+
testsuite.
14+
15+
config TEST_LOCK_PERIOD_MS
16+
int "Time to hold a lock in milliseconds"
17+
default 100
18+
help
19+
The amount of time to hold a lock before releasing it, for the purposes of this testuite.
20+
21+
endmenu

tests/posix/file_locking/app.overlay

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* Copyright (c) 2023 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/ {
8+
ramdisk0 {
9+
compatible = "zephyr,ram-disk";
10+
disk-name = "RAM";
11+
sector-size = <512>;
12+
sector-count = <160>;
13+
};
14+
};

tests/posix/file_locking/prj.conf

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
CONFIG_POSIX_API=y
2+
CONFIG_ZTEST=y
3+
4+
CONFIG_POSIX_AEP_CHOICE_BASE=y
5+
CONFIG_POSIX_FILE_LOCKING=y
6+
CONFIG_FAT_FILESYSTEM_ELM=y
7+
8+
# pthreads are used for simplicity here, although that isn't strictly necessary
9+
CONFIG_THREAD_STACK_INFO=y
10+
CONFIG_DYNAMIC_THREAD=y
11+
CONFIG_DYNAMIC_THREAD_POOL_SIZE=4
12+
13+
CONFIG_MAIN_STACK_SIZE=4096

tests/posix/file_locking/src/fs.c

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright (c) 2024 Tenstorrent AI ULC
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <errno.h>
8+
#include <stdio.h>
9+
#include <string.h>
10+
11+
#include <ff.h>
12+
#include <zephyr/fs/fs.h>
13+
#include <zephyr/sys/util.h>
14+
#include <zephyr/ztest.h>
15+
16+
static FATFS _fs;
17+
18+
static struct fs_mount_t _mnt = {
19+
.type = FS_FATFS,
20+
.mnt_point = "/",
21+
.fs_data = &_fs,
22+
};
23+
24+
struct _entry {
25+
const char *const name;
26+
const char *const data;
27+
FILE *fp;
28+
};
29+
30+
static struct _entry _data[] = {
31+
{.name = "/tmp/foo.txt", .data = ""},
32+
{.name = "/tmp/bar.txt", .data = ""},
33+
};
34+
35+
void setup_callback(void *arg);
36+
void before_callback(void *arg);
37+
void after_callback(void *arg);
38+
39+
void *setup(void)
40+
{
41+
int ret;
42+
43+
memset(&_fs, 0, sizeof(_fs));
44+
45+
ret = fs_mount(&_mnt);
46+
zassert_ok(ret, "mount failed: %d", ret);
47+
48+
ret = fs_mkdir("/tmp");
49+
zassert_ok(ret, "mkdir failed: %d", ret);
50+
51+
setup_callback(NULL);
52+
53+
return NULL;
54+
}
55+
56+
void before(void *arg)
57+
{
58+
ARG_UNUSED(arg);
59+
60+
ARRAY_FOR_EACH_PTR(_data, entry) {
61+
int len;
62+
int ret;
63+
64+
entry->fp = fopen(entry->name, "w+");
65+
zassert_not_null(entry->fp, "fopen() failed: %d", errno);
66+
67+
len = strlen(entry->data);
68+
if (len > 0) {
69+
ret = (int)fwrite(entry->data, len, 1, entry->fp);
70+
zassert_equal(ret, len, "%s returned %d instead of %d: %d", "fwrite", ret,
71+
len, errno);
72+
}
73+
74+
before_callback(entry->fp);
75+
};
76+
}
77+
78+
void after(void *arg)
79+
{
80+
ARG_UNUSED(arg);
81+
82+
ARRAY_FOR_EACH_PTR(_data, entry) {
83+
(void)fclose(entry->fp);
84+
};
85+
86+
after_callback(NULL);
87+
}
88+
89+
void teardown(void *arg)
90+
{
91+
ARG_UNUSED(arg);
92+
(void)fs_unmount(&_mnt);
93+
}

0 commit comments

Comments
 (0)