Skip to content

Commit cea99e5

Browse files
authored
Merge pull request #787 from wolfSSL/devin/1740666408-add-fatfs-test-action
FATFS improvements, test and Linux example
2 parents c1b983a + d8ad0c3 commit cea99e5

File tree

10 files changed

+720
-9
lines changed

10 files changed

+720
-9
lines changed

.github/workflows/test-fatfs.yml

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
name: Test FATFS Support
2+
3+
on:
4+
push:
5+
branches: [ master ]
6+
pull_request:
7+
branches: [ master ]
8+
workflow_dispatch:
9+
10+
jobs:
11+
get-fatfs:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Set up cache key
15+
id: cache-key
16+
run: echo "CACHE_KEY=ff15a-cache" >> $GITHUB_ENV
17+
18+
- name: Download FF15a.zip
19+
id: download
20+
run: |
21+
wget http://elm-chan.org/fsw/ff/arc/ff15a.zip -O ff15a.zip
22+
23+
- name: Cache the downloaded ZIP file
24+
uses: actions/cache@v4
25+
with:
26+
path: ./ff15a.zip
27+
key: ${{ env.CACHE_KEY }}
28+
restore-keys: |
29+
${{ env.CACHE_KEY }}
30+
31+
test-fatfs:
32+
needs: get-fatfs
33+
runs-on: ubuntu-latest
34+
steps:
35+
- name: Checkout code
36+
uses: actions/checkout@v3
37+
38+
- name: Checking cache for FATFS
39+
uses: actions/cache@v4
40+
with:
41+
path: ./ff15a.zip
42+
key: ff15a-cache
43+
fail-on-cache-miss: true
44+
45+
- name: Install dependencies
46+
run: |
47+
sudo apt-get update
48+
sudo apt-get install -y build-essential autoconf automake libtool pkg-config openssh-server dosfstools
49+
50+
- name: Clone wolfSSL
51+
run: |
52+
cd ..
53+
git clone https://github.com/wolfSSL/wolfssl.git
54+
cd wolfssl
55+
./autogen.sh
56+
57+
- name: Configure and build wolfSSL
58+
run: |
59+
cd ../wolfssl
60+
./configure --enable-wolfssh --enable-intelasm --disable-crl --disable-examples --disable-filesystem CFLAGS="-DNO_WOLFSSL_DIR"
61+
make
62+
sudo make install
63+
sudo ldconfig
64+
65+
- name: Compile FATFS library
66+
run: |
67+
cd ide/Linux-FATFS
68+
unzip ../../ff15a.zip
69+
cp ffconf.h source/
70+
make
71+
72+
- name: Configure and build wolfSSH with FATFS
73+
run: |
74+
./autogen.sh
75+
export LD_LIBRARY_PATH=$(pwd)/ide/Linux-FATFS
76+
./configure --enable-sftp CFLAGS="-DWOLFSSH_FATFS -Iide/Linux-FATFS/source -DSTDIN_FILENO=0 -DPRINTF=printf" LDFLAGS="-Lide/Linux-FATFS -lfatfs"
77+
make
78+
79+
80+
81+
- name: Create test file
82+
run: |
83+
dd if=/dev/urandom of=test_file.bin bs=1M count=1
84+
85+
- name: Setup SSH server
86+
run: |
87+
sudo mkdir -p /run/sshd
88+
sudo /usr/sbin/sshd
89+
mkdir -p ~/.ssh
90+
ssh-keygen -t rsa -f ~/.ssh/id_rsa -N ""
91+
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
92+
chmod 600 ~/.ssh/authorized_keys
93+
echo "Host localhost" > ~/.ssh/config
94+
echo " StrictHostKeyChecking no" >> ~/.ssh/config
95+
echo " UserKnownHostsFile=/dev/null" >> ~/.ssh/config
96+
chmod 600 ~/.ssh/config
97+
98+
- name: Install expect
99+
run: |
100+
sudo apt-get update
101+
sudo apt-get install -y expect
102+
103+
- name: Run wolfsftp client to get file
104+
run: |
105+
# Export the library path
106+
export LD_LIBRARY_PATH=$(pwd)/ide/Linux-FATFS
107+
108+
# Get the full path to the test file
109+
TEST_FILE_PATH=$(pwd)/test_file.bin
110+
111+
# Change to the sftpclient directory
112+
cd examples/sftpclient
113+
114+
# Create the FATFS image file directly in the sftpclient directory
115+
dd if=/dev/zero of=fatfs_image.img bs=1M count=32
116+
mkdosfs fatfs_image.img
117+
118+
# Create a test user with a known password and add to the same group as the runner
119+
sudo useradd -m testuser
120+
echo "testuser:password123" | sudo chpasswd
121+
sudo usermod -aG $(id -gn) testuser
122+
123+
# Make the test file accessible to testuser
124+
chmod 644 ${TEST_FILE_PATH}
125+
sudo chown testuser:$(id -gn) ${TEST_FILE_PATH}
126+
127+
# Configure SSH server to allow SFTP access
128+
sudo sed -i 's/^#Subsystem\s\+sftp.*/Subsystem sftp \/usr\/lib\/openssh\/sftp-server/' /etc/ssh/sshd_config
129+
sudo service ssh restart || sudo /etc/init.d/ssh restart || sudo /usr/sbin/sshd -t && sudo kill -HUP $(pgrep -f sshd)
130+
131+
# Create expect script to automate the wolfsftp client interaction
132+
cat > /tmp/sftp_test.exp << EOF
133+
#!/usr/bin/expect -f
134+
set timeout 60
135+
spawn ./wolfsftp -N -h localhost -p 22 -u testuser
136+
expect "Password:"
137+
send "password123\r"
138+
expect "wolfSSH sftp>"
139+
send "get ${TEST_FILE_PATH} test_file.bin\r"
140+
expect "wolfSSH sftp>"
141+
send "exit\r"
142+
expect eof
143+
EOF
144+
chmod +x /tmp/sftp_test.exp
145+
146+
# Run the expect script
147+
/tmp/sftp_test.exp
148+
149+
- name: Verify file in FATFS image
150+
run: |
151+
cd examples/sftpclient
152+
sudo mkdir -p /mnt/fatfs
153+
LOOPDEV=$(sudo losetup -f)
154+
sudo losetup $LOOPDEV fatfs_image.img
155+
sudo mount $LOOPDEV /mnt/fatfs
156+
if [ -f /mnt/fatfs/test_file.bin ]; then
157+
echo "File exists in FATFS image!"
158+
ls -la /mnt/fatfs/
159+
160+
# Verify file contents match
161+
if cmp -s ../../test_file.bin /mnt/fatfs/test_file.bin; then
162+
echo "File contents match! FATFS test PASSED."
163+
sudo umount /mnt/fatfs
164+
sudo losetup -d $LOOPDEV
165+
exit 0
166+
else
167+
echo "File contents do not match! FATFS test FAILED."
168+
sudo umount /mnt/fatfs
169+
sudo losetup -d $LOOPDEV
170+
exit 1
171+
fi
172+
else
173+
echo "File does not exist in FATFS image! FATFS test FAILED."
174+
ls -la /mnt/fatfs/
175+
sudo umount /mnt/fatfs
176+
sudo losetup -d $LOOPDEV
177+
exit 1
178+
fi

examples/sftpclient/sftpclient.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1476,6 +1476,9 @@ THREAD_RETURN WOLFSSH_THREAD sftpclient_test(void* args)
14761476

14771477
int main(int argc, char** argv)
14781478
{
1479+
#ifdef WOLFSSH_FATFS
1480+
FATFS fs;
1481+
#endif
14791482
func_args args;
14801483

14811484
args.argc = argc;
@@ -1486,6 +1489,13 @@ THREAD_RETURN WOLFSSH_THREAD sftpclient_test(void* args)
14861489
args.sftp_cb = NULL;
14871490
#endif
14881491

1492+
#ifdef WOLFSSH_FATFS
1493+
if (f_mount(&fs, "0:", 1) != FR_OK) {
1494+
fprintf(stderr, "Failed to mount filesystem\n");
1495+
return 1;
1496+
}
1497+
#endif
1498+
14891499
WSTARTTCP();
14901500

14911501
#ifdef DEBUG_WOLFSSH
@@ -1499,6 +1509,10 @@ THREAD_RETURN WOLFSSH_THREAD sftpclient_test(void* args)
14991509

15001510
wolfSSH_Cleanup();
15011511

1512+
#ifdef WOLFSSH_FATFS
1513+
f_mount(NULL, "0:", 1);
1514+
#endif
1515+
15021516
#if defined(WOLFSSH_SFTP) && !defined(NO_WOLFSSH_CLIENT)
15031517
return args.return_code;
15041518
#else

ide/Linux-FATFS/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fatfs_image.img

ide/Linux-FATFS/Makefile

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Compiler and flags
2+
CC = gcc
3+
CFLAGS = -g -Wall -O2 -fPIC -Isource
4+
LDFLAGS = -shared
5+
6+
# Source files
7+
SRCS = source/ff.c source/ffunicode.c fatfs_example.c
8+
9+
# Object files
10+
OBJS = $(SRCS:.c=.o)
11+
12+
# Target library
13+
TARGET = libfatfs.so
14+
15+
all: $(TARGET)
16+
17+
$(TARGET): $(OBJS)
18+
$(CC) $(LDFLAGS) -o $@ $^
19+
20+
%.o: %.c
21+
$(CC) $(CFLAGS) -c $< -o $@
22+
23+
clean:
24+
rm -f $(OBJS) $(TARGET)
25+
26+
.PHONY: all clean

ide/Linux-FATFS/README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# FATFS Linux Example
2+
3+
This is a FATFS example that uses a single file on the Linux filesystem as the
4+
FATFS file system.
5+
6+
## Obtaining FATFS
7+
8+
You can download the source code from
9+
[The FATFS download site](http://elm-chan.org/fsw/ff/archives.html). Extract it
10+
into this directory.
11+
12+
The example has been tested against FATFS 0.15a
13+
14+
## Compiling Library
15+
16+
First copy the config file into the correct place:
17+
18+
```sh
19+
cp ffconf.h source/
20+
```
21+
22+
Then to compile the FATFS library simply run `make`.
23+
24+
## Setup filesystem
25+
26+
The single file used for FATFS should be generated using:
27+
28+
```sh
29+
dd if=/dev/zero of=fatfs_image.img bs=1M count=32
30+
mkdosfs fatfs_image.img
31+
```
32+
33+
Note that this file will need to be local to wherever you execute anything using
34+
the library.
35+
36+
## Compiling wolfSSH and wolfSSL
37+
38+
### wolfSSL
39+
40+
```sh
41+
./configure --enable-wolfssh --enable-intelasm --disable-crl --disable-examples --disable-filesystem CFLAGS="-DNO_WOLFSSL_DIR"
42+
```
43+
44+
### wolfSSH
45+
46+
```sh
47+
LD_LIBRARY_PATH=ide/Linux-FATFS ./configure --enable-sftp CFLAGS="-DWOLFSSH_FATFS -Iide/Linux-FATFS/source -DSTDIN_FILENO=0 -DPRINTF=printf" LDFLAGS="-Lide/Linux-FATFS -lfatfs"
48+
```
49+

0 commit comments

Comments
 (0)