aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorYP3 Audiobook <noreply@example.invalid>2026-11-25 17:45:43 -0500
committerYP3 Audiobook <noreply@example.invalid>2026-11-25 17:45:43 -0500
commit013e3857a1c6480dfd241fb14b36c55b0421939e (patch)
tree59b3aa16d4f658eb7b76e5dfbc475c3a408507c7 /tests
downloadyp3-audiobook-firmware-013e3857a1c6480dfd241fb14b36c55b0421939e.tar.gz
initial commit
Diffstat (limited to 'tests')
-rwxr-xr-xtests/chapters/run.sh86
-rw-r--r--tests/chapters/shim/audio.h3
-rw-r--r--tests/chapters/shim/config.h7
-rw-r--r--tests/chapters/shim/file.h5
-rw-r--r--tests/chapters/shim/list.h6
-rw-r--r--tests/chapters/shim/logf.h2
-rw-r--r--tests/chapters/shim/metadata.h3
-rw-r--r--tests/chapters/shim/misc.h3
-rw-r--r--tests/chapters/shim/playback.h2
-rw-r--r--tests/chapters/shim/playlist.h3
-rw-r--r--tests/chapters/shim/rbunicode.h2
-rw-r--r--tests/chapters/shim/root_menu.h2
-rw-r--r--tests/chapters/shim/screen_access.h3
-rw-r--r--tests/chapters/shim/string-extra.h3
-rw-r--r--tests/chapters/shim/system.h2
-rw-r--r--tests/chapters/test.c37
16 files changed, 169 insertions, 0 deletions
diff --git a/tests/chapters/run.sh b/tests/chapters/run.sh
new file mode 100755
index 0000000..0bb70b3
--- /dev/null
+++ b/tests/chapters/run.sh
@@ -0,0 +1,86 @@
+#!/bin/sh
+# Host test of apps/chapters.c (the M4B chapter reader) against audiobooks
+# made with ffmpeg: Nero 'chpl' chapters, QuickTime chapter tracks only,
+# the index (moov) at the end or the start, UTF-8 / long / empty titles,
+# and more chapters than the 255 kept. Built with AddressSanitizer and
+# UBSan; the shim/ headers stand in for the Rockbox ones.
+#
+# tests/chapters/run.sh [ROCKBOX_TREE] default: build/rockbox (scripts/build.sh)
+#
+# Needs: gcc, ffmpeg, python3.
+set -eu
+HERE=$(cd "$(dirname "$0")" && pwd)
+REPO=$(cd "$HERE/../.." && pwd)
+TREE=${1:-$REPO/build/rockbox}
+[ -f "$TREE/apps/chapters.c" ] || {
+ echo "no $TREE/apps/chapters.c: run scripts/build.sh first, or name a patched tree" >&2
+ exit 1
+}
+T=$(mktemp -d)
+trap 'rm -rf "$T"' EXIT
+
+# chapters.c includes by quotes: compile copies next to the shims
+mkdir -p "$T/src" "$T/media"
+cp "$HERE"/shim/*.h "$TREE/apps/chapters.c" "$TREE/apps/chapters.h" "$T/src/"
+gcc -O1 -g -fsanitize=address,undefined -fno-sanitize-recover=all \
+ -Wall -Wextra -Wno-unused-parameter -I"$T/src" \
+ -o "$T/chtest" "$HERE/test.c" "$T/src/chapters.c"
+
+cd "$T/media"
+python3 -I - <<'EOF'
+def meta(name, chapters, total_ms):
+ with open(name, 'w') as f:
+ f.write(";FFMETADATA1\ntitle=Test Book\nartist=Some Author\n")
+ for i, (start, title) in enumerate(chapters):
+ end = chapters[i + 1][0] if i + 1 < len(chapters) else total_ms
+ f.write(f"[CHAPTER]\nTIMEBASE=1/1000\nSTART={start}\nEND={end}\ntitle={title}\n")
+meta('basic.txt', [(0, 'Opening Credits'), (65000, 'Chapter One: The Beginning'),
+ (190500, 'Chäpter Twö – ünïcode ✓ 日本語'), (400000, 'X' * 200),
+ (500000, '')], 600000)
+meta('many.txt', [(i * 2000, f'Part {i + 1}') for i in range(300)], 600000)
+EOF
+enc() { # metadata output [movflags]
+ ffmpeg -nostdin -loglevel error -f lavfi -i "sine=frequency=440:duration=600" \
+ -i "$1" -map_metadata 1 -map_chapters 1 -c:a aac -b:a 32k -ac 1 \
+ ${3:+-movflags "$3"} -y "$2"
+}
+for f in basic many; do
+ enc $f.txt $f.m4b # chpl + chapter track
+ enc $f.txt $f-qt.m4b +disable_chpl # chapter track only
+ enc $f.txt $f-qt-fast.m4b +faststart+disable_chpl # ... index first
+done
+ffmpeg -nostdin -loglevel error -f lavfi -i "sine=frequency=440:duration=60" \
+ -c:a aac -b:a 32k -y nochap.m4b
+
+"$T/chtest" basic.m4b basic-qt.m4b basic-qt-fast.m4b many.m4b many-qt.m4b \
+ many-qt-fast.m4b nochap.m4b /nonexistent.m4b > "$T/out.txt"
+cat "$T/out.txt"
+
+# expected results
+fail=0
+expect() { # file-section pattern
+ if ! awk -v f="== $1:" -v p="$2" '
+ index($0, "== ") == 1 { on = (index($0, f) == 1) }
+ on && index($0, p) { found = 1 } END { exit !found }' "$T/out.txt"; then
+ echo "FAIL: $1: expected \"$2\""; fail=1
+ fi
+}
+for f in basic.m4b basic-qt.m4b basic-qt-fast.m4b; do
+ expect $f "5 chapters"
+ expect $f "65000 ms [Chapter One: The Beginning]"
+ expect $f "190500 ms [Chäpter Twö – ünïcode ✓ 日本語]"
+ expect $f "500000 ms [Chapter 5]"
+ expect $f "prev 1s into ch2 -> 0"
+ expect $f "prev 10s into ch2 -> 65000"
+ expect $f "next from ch2 -> 190500"
+ expect $f "next from last -> nomore=1"
+ expect $f "next title [XXXX"
+done
+for f in many.m4b many-qt.m4b many-qt-fast.m4b; do
+ expect $f "255 chapters"
+ expect $f "508000 ms [Part 255]"
+done
+expect nochap.m4b "0 chapters"
+expect nochap.m4b "prevs=1 next->nexts=1 prev@10s->seek 0"
+expect /nonexistent.m4b "0 chapters"
+[ $fail = 0 ] && echo "PASS" || { echo "FAILED"; exit 1; }
diff --git a/tests/chapters/shim/audio.h b/tests/chapters/shim/audio.h
new file mode 100644
index 0000000..4d27bcb
--- /dev/null
+++ b/tests/chapters/shim/audio.h
@@ -0,0 +1,3 @@
+#pragma once
+struct mp3entry *audio_current_track(void);
+void audio_next(void); void audio_prev(void);
diff --git a/tests/chapters/shim/config.h b/tests/chapters/shim/config.h
new file mode 100644
index 0000000..9d471b9
--- /dev/null
+++ b/tests/chapters/shim/config.h
@@ -0,0 +1,7 @@
+#pragma once
+#define HAVE_AUDIOBOOK_CHAPTERS
+#define MAX_PATH 260
+#include <stdbool.h>
+#include <stdint.h>
+#include <stddef.h>
+#include <sys/types.h>
diff --git a/tests/chapters/shim/file.h b/tests/chapters/shim/file.h
new file mode 100644
index 0000000..7601b39
--- /dev/null
+++ b/tests/chapters/shim/file.h
@@ -0,0 +1,5 @@
+#pragma once
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/stat.h>
+static inline off_t ffilesize(int fd){ struct stat st; return fstat(fd,&st)?-1:st.st_size; }
diff --git a/tests/chapters/shim/list.h b/tests/chapters/shim/list.h
new file mode 100644
index 0000000..0df3489
--- /dev/null
+++ b/tests/chapters/shim/list.h
@@ -0,0 +1,6 @@
+#pragma once
+struct gui_synclist;
+typedef const char *list_get_name(int, void *, char *, size_t);
+struct simplelist_info { const char *title; int count; int selection; bool hide_theme; list_get_name *get_name; };
+static inline void simplelist_info_init(struct simplelist_info *i, char *t, int c, void *d){(void)d; i->title=t;i->count=c;i->selection=0;}
+static inline bool simplelist_show_list(struct simplelist_info *i){(void)i; return true;}
diff --git a/tests/chapters/shim/logf.h b/tests/chapters/shim/logf.h
new file mode 100644
index 0000000..e761f85
--- /dev/null
+++ b/tests/chapters/shim/logf.h
@@ -0,0 +1,2 @@
+#pragma once
+#define logf(...) do{}while(0)
diff --git a/tests/chapters/shim/metadata.h b/tests/chapters/shim/metadata.h
new file mode 100644
index 0000000..11d7cb6
--- /dev/null
+++ b/tests/chapters/shim/metadata.h
@@ -0,0 +1,3 @@
+#pragma once
+enum { AFMT_UNKNOWN, AFMT_MP4_ALAC, AFMT_MP4_AAC, AFMT_MP4_AAC_HE, AFMT_MPA_L3 };
+struct mp3entry { char path[260]; int codectype; unsigned long elapsed, length; char *title, *album; };
diff --git a/tests/chapters/shim/misc.h b/tests/chapters/shim/misc.h
new file mode 100644
index 0000000..226e90b
--- /dev/null
+++ b/tests/chapters/shim/misc.h
@@ -0,0 +1,3 @@
+#pragma once
+enum system_sound { SOUND_TRACK_NO_MORE };
+void system_sound_play(enum system_sound s);
diff --git a/tests/chapters/shim/playback.h b/tests/chapters/shim/playback.h
new file mode 100644
index 0000000..55a40b4
--- /dev/null
+++ b/tests/chapters/shim/playback.h
@@ -0,0 +1,2 @@
+#pragma once
+void audio_pre_ff_rewind(void); void audio_ff_rewind(long ms);
diff --git a/tests/chapters/shim/playlist.h b/tests/chapters/shim/playlist.h
new file mode 100644
index 0000000..5082522
--- /dev/null
+++ b/tests/chapters/shim/playlist.h
@@ -0,0 +1,3 @@
+#pragma once
+#include <stdbool.h>
+bool playlist_check(int steps);
diff --git a/tests/chapters/shim/rbunicode.h b/tests/chapters/shim/rbunicode.h
new file mode 100644
index 0000000..87b7e43
--- /dev/null
+++ b/tests/chapters/shim/rbunicode.h
@@ -0,0 +1,2 @@
+#pragma once
+unsigned char* utf8encode(unsigned long ucs, unsigned char *utf8);
diff --git a/tests/chapters/shim/root_menu.h b/tests/chapters/shim/root_menu.h
new file mode 100644
index 0000000..27ec430
--- /dev/null
+++ b/tests/chapters/shim/root_menu.h
@@ -0,0 +1,2 @@
+#pragma once
+#define GO_TO_WPS 2
diff --git a/tests/chapters/shim/screen_access.h b/tests/chapters/shim/screen_access.h
new file mode 100644
index 0000000..061b8c7
--- /dev/null
+++ b/tests/chapters/shim/screen_access.h
@@ -0,0 +1,3 @@
+#pragma once
+#define DRMODE_COMPLEMENT 0
+struct screen { void (*set_drawmode)(int); void (*vline)(int,int,int); };
diff --git a/tests/chapters/shim/string-extra.h b/tests/chapters/shim/string-extra.h
new file mode 100644
index 0000000..3303faf
--- /dev/null
+++ b/tests/chapters/shim/string-extra.h
@@ -0,0 +1,3 @@
+#pragma once
+#include <string.h>
+static inline char *strmemccpy(char *d, const char *s, size_t n){ char *r = memccpy(d,s,0,n); if(!r && n) d[n-1]=0; return r; }
diff --git a/tests/chapters/shim/system.h b/tests/chapters/shim/system.h
new file mode 100644
index 0000000..344be9e
--- /dev/null
+++ b/tests/chapters/shim/system.h
@@ -0,0 +1,2 @@
+#pragma once
+#define MIN(a,b) ((a)<(b)?(a):(b))
diff --git a/tests/chapters/test.c b/tests/chapters/test.c
new file mode 100644
index 0000000..c6982e6
--- /dev/null
+++ b/tests/chapters/test.c
@@ -0,0 +1,37 @@
+#include <stdio.h>
+#include <string.h>
+#include "config.h"
+#include "metadata.h"
+#include "misc.h"
+#include "chapters.h"
+/* minimal UTF-8 encoder like rockbox's */
+unsigned char* utf8encode(unsigned long c, unsigned char *u){
+ if(c<0x80){*u++=c;} else if(c<0x800){*u++=0xc0|(c>>6);*u++=0x80|(c&63);}
+ else {*u++=0xe0|(c>>12);*u++=0x80|((c>>6)&63);*u++=0x80|(c&63);} return u; }
+long seeked=-2; int nexts, prevs, nomore;
+void system_sound_play(enum system_sound s){(void)s; nomore++;}
+struct mp3entry cur; struct mp3entry *audio_current_track(void){return &cur;}
+void audio_next(void){nexts++;} void audio_prev(void){prevs++;}
+void audio_pre_ff_rewind(void){} void audio_ff_rewind(long ms){seeked=ms;}
+bool playlist_check(int s){(void)s; return true;}
+int main(int argc, char **argv){
+ for (int a=1;a<argc;a++){
+ memset(&cur,0,sizeof cur); strcpy(cur.path, argv[a]); cur.codectype=AFMT_MP4_AAC;
+ int n = chapters_update(&cur);
+ printf("== %s: %d chapters\n", argv[a], n);
+ char buf[100];
+ for (int i=0;i<n;i++) if (i<6 || i>n-3) printf(" %3d %8lu ms [%s]\n", i+1, chapters_start(i), chapters_title(i,buf,sizeof buf));
+ if (n>2){
+ cur.elapsed = chapters_start(1)+1000; chapters_skip(&cur,-1,3000); printf(" prev 1s into ch2 -> %ld\n", seeked);
+ cur.elapsed = chapters_start(1)+10000; chapters_skip(&cur,-1,3000); printf(" prev 10s into ch2 -> %ld\n", seeked);
+ cur.elapsed = chapters_start(1)+10000; chapters_skip(&cur,1,3000); printf(" next from ch2 -> %ld\n", seeked);
+ cur.elapsed = chapters_start(n-1)+10; nomore=0; chapters_skip(&cur,1,3000); printf(" next from last -> nomore=%d\n", nomore);
+ cur.elapsed = chapters_start(2)+5; printf(" index at ch3+5ms = %d, current title [%s]\n", chapters_index(cur.elapsed), chapters_current_title(&cur));
+ printf(" next title [%s]\n", chapters_next_title(&cur)); printf(" changed: %d %d\n", chapters_changed(&cur), chapters_changed(&cur));
+ } else if (n==0) {
+ nexts=prevs=0; cur.elapsed=1000; chapters_skip(&cur,-1,3000); chapters_skip(&cur,1,3000);
+ cur.elapsed=10000; seeked=-2; chapters_skip(&cur,-1,3000);
+ printf(" no chapters: prev@1s->prevs=%d next->nexts=%d prev@10s->seek %ld\n", prevs, nexts, seeked);
+ }
+ }
+}