Tests av_adts_header_parse and avpriv_adts_header_parse covering
previously untested branches: NULL input guards, auto-allocation
success and failure paths, and pre-allocated struct path.
---
libavcodec/Makefile | 1 +
libavcodec/tests/adts_parser.c | 111 +++++++++++++++++++++++++++++++++
tests/fate/libavcodec.mak | 5 ++
3 files changed, 117 insertions(+)
create mode 100644 libavcodec/tests/adts_parser.c
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 85d35913f3..f1c6dc34bc 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -1372,6 +1372,7 @@ TESTPROGS = avcodec
\
jpeg2000dwt \
mathops \
+TESTPROGS-$(CONFIG_ADTS_HEADER) += adts_parser
TESTPROGS-$(CONFIG_APV_DECODER) += apv
TESTPROGS-$(CONFIG_AV1_VAAPI_ENCODER) += av1_levels
TESTPROGS-$(CONFIG_CABAC) += cabac
diff --git a/libavcodec/tests/adts_parser.c b/libavcodec/tests/adts_parser.c
new file mode 100644
index 0000000000..3e276c5575
--- /dev/null
+++ b/libavcodec/tests/adts_parser.c
@@ -0,0 +1,111 @@
+/*
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <stdint.h>
+#include <stdio.h>
+
+#include "libavcodec/adts_header.h"
+#include "libavcodec/adts_parser.h"
+#include "libavutil/error.h"
+#include "libavutil/mem.h"
+
+/* minimal valid ADTS header: sync=0xFFF, MPEG-4, AAC-LC, 44100Hz, stereo,
+ * frame_length=7, protection_absent=1 (no CRC) */
+static const uint8_t valid_hdr[AV_AAC_ADTS_HEADER_SIZE] = {
+ 0xFF, 0xF1, 0x50, 0x80, 0x00, 0xFF, 0xFC
+};
+
+/* invalid header: missing sync word, triggers parse error */
+static const uint8_t invalid_hdr[AV_AAC_ADTS_HEADER_SIZE] = { 0 };
+
+int main(void)
+{
+ AACADTSHeaderInfo *hdr = NULL;
+ uint32_t samples;
+ uint8_t frames;
+ int ret;
+
+ /* av_adts_header_parse: NULL buf should return EINVAL */
+ ret = av_adts_header_parse(NULL, &samples, &frames);
+ if (ret != AVERROR(EINVAL)) {
+ fprintf(stderr, "expected EINVAL for NULL buf, got %d\n", ret);
+ return 1;
+ }
+
+ /* av_adts_header_parse: valid header should succeed */
+ ret = av_adts_header_parse(valid_hdr, &samples, &frames);
+ if (ret != 0) {
+ fprintf(stderr, "expected 0 for valid header, got %d\n", ret);
+ return 1;
+ }
+
+ /* avpriv_adts_header_parse: NULL phdr */
+ ret = avpriv_adts_header_parse(NULL, valid_hdr, AV_AAC_ADTS_HEADER_SIZE);
+ if (ret != AVERROR_INVALIDDATA) {
+ fprintf(stderr, "expected AVERROR_INVALIDDATA for NULL phdr, got
%d\n", ret);
+ return 1;
+ }
+
+ /* avpriv_adts_header_parse: NULL buf */
+ ret = avpriv_adts_header_parse(&hdr, NULL, AV_AAC_ADTS_HEADER_SIZE);
+ if (ret != AVERROR_INVALIDDATA) {
+ fprintf(stderr, "expected AVERROR_INVALIDDATA for NULL buf, got %d\n",
ret);
+ return 1;
+ }
+
+ /* avpriv_adts_header_parse: size too small */
+ ret = avpriv_adts_header_parse(&hdr, valid_hdr, AV_AAC_ADTS_HEADER_SIZE -
1);
+ if (ret != AVERROR_INVALIDDATA) {
+ fprintf(stderr, "expected AVERROR_INVALIDDATA for small size, got
%d\n", ret);
+ return 1;
+ }
+
+ /* avpriv_adts_header_parse: *phdr == NULL, let function allocate */
+ hdr = NULL;
+ ret = avpriv_adts_header_parse(&hdr, valid_hdr, AV_AAC_ADTS_HEADER_SIZE);
+ if (ret != 0 || !hdr) {
+ fprintf(stderr, "auto-alloc success path failed: ret=%d hdr=%p\n",
ret, (void *)hdr);
+ av_freep(&hdr);
+ return 1;
+ }
+ av_freep(&hdr);
+
+ /* avpriv_adts_header_parse: *phdr != NULL, use pre-allocated struct */
+ hdr = av_mallocz(sizeof(*hdr));
+ if (!hdr)
+ return 1;
+ ret = avpriv_adts_header_parse(&hdr, valid_hdr, AV_AAC_ADTS_HEADER_SIZE);
+ if (ret != 0) {
+ fprintf(stderr, "pre-alloc success path failed: ret=%d\n", ret);
+ av_freep(&hdr);
+ return 1;
+ }
+ av_freep(&hdr);
+
+ /* avpriv_adts_header_parse: *phdr == NULL + bad header -> alloc then free
+ * hdr must be NULL after this call (av_freep zeroes the pointer) */
+ hdr = NULL;
+ ret = avpriv_adts_header_parse(&hdr, invalid_hdr, AV_AAC_ADTS_HEADER_SIZE);
+ if (ret >= 0 || hdr != NULL) {
+ fprintf(stderr, "alloc+parse-fail path: ret=%d hdr=%p\n", ret, (void
*)hdr);
+ av_freep(&hdr);
+ return 1;
+ }
+
+ return 0;
+}
diff --git a/tests/fate/libavcodec.mak b/tests/fate/libavcodec.mak
index e2d616e307..b6cb7940ed 100644
--- a/tests/fate/libavcodec.mak
+++ b/tests/fate/libavcodec.mak
@@ -8,6 +8,11 @@ fate-apv-entropy: libavcodec/tests/apv$(EXESUF)
fate-apv-entropy: CMD = run libavcodec/tests/apv$(EXESUF)
fate-apv-entropy: REF = /dev/null
+FATE_LIBAVCODEC-$(CONFIG_ADTS_HEADER) += fate-avcodec-adts-parser
+fate-avcodec-adts-parser: libavcodec/tests/adts_parser$(EXESUF)
+fate-avcodec-adts-parser: CMD = run libavcodec/tests/adts_parser
+fate-avcodec-adts-parser: CMP = null
+
FATE_LIBAVCODEC-yes += fate-avpacket
fate-avpacket: libavcodec/tests/avpacket$(EXESUF)
fate-avpacket: CMD = run libavcodec/tests/avpacket$(EXESUF)
--
2.51.0
_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]