ffmpeg/libavutil/tests/file.c
Soham Kute dc8183377c avutil/tests/file: replace trivial test with error-path coverage
The original test only mapped the source file and printed its content,
exercising none of the error branches in av_file_map().

Replace it with a test that maps a real file (path via argv[1] for
out-of-tree builds) and verifies it is non-empty, then calls
av_file_map() on a nonexistent file twice: once with log_offset=0 to
confirm the error is logged at AV_LOG_ERROR, and once with log_offset=1
to confirm the level is raised by one, covering the
log_level_offset_offset path in av_vlog().  A custom av_log callback
captures the emitted level independently of the global log level.
The two error cases share a single for() loop to avoid duplication.

Add a FATE entry in tests/fate/libavutil.mak with CMP=null since
there is no fixed stdout to compare.

Signed-off-by: Soham Kute <officialsohamkute@gmail.com>
2026-03-29 23:01:39 +00:00

63 lines
1.9 KiB
C

/*
* 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 <stdarg.h>
#include <stdio.h>
#include "libavutil/file.c"
#include "libavutil/log.h"
static int last_log_level = -1;
static void log_callback(void *ctx, int level, const char *fmt, va_list args)
{
(void)ctx; (void)fmt; (void)args;
last_log_level = level;
}
int main(int argc, char **argv)
{
const char *path = argc > 1 ? argv[1] : "file.c";
uint8_t *buf;
size_t size;
av_log_set_callback(log_callback);
/* map an existing file and verify it is non-empty and readable */
if (av_file_map(path, &buf, &size, 0, NULL) < 0)
return 1;
av_file_unmap(buf, size);
if (size == 0)
return 1;
/* for offset i, error must be logged at AV_LOG_ERROR + i */
for (int i = 0; i < 2; i++) {
last_log_level = -1;
if (av_file_map("no_such_file_xyz", &buf, &size, i, NULL) >= 0) {
av_file_unmap(buf, size);
return 2;
}
if (last_log_level != AV_LOG_ERROR + i) {
fprintf(stderr, "expected level %d with offset=%d, got %d\n",
AV_LOG_ERROR + i, i, last_log_level);
return 3;
}
}
return 0;
}