From patchwork Fri Jun 1 19:51:48 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jacob Trimble X-Patchwork-Id: 9223 Delivered-To: ffmpegpatchwork@gmail.com Received: by 2002:a02:11c:0:0:0:0:0 with SMTP id c28-v6csp1231381jad; Fri, 1 Jun 2018 12:52:28 -0700 (PDT) X-Google-Smtp-Source: ADUXVKL2fvYcDsqKn0I9zJ4DNcgJRPABnWVLfGmUHSGW2bXMKVAzM9abUoPGEidWFAOgyfJznYSH X-Received: by 2002:a1c:ec0a:: with SMTP id k10-v6mr3313667wmh.4.1527882748214; Fri, 01 Jun 2018 12:52:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527882748; cv=none; d=google.com; s=arc-20160816; b=oDesJqFjDClTlhlLmjI1w1MNApBvqSAIeoB3EaeCsAu14Q4TVO66/Qb2YT4sc4KUFJ DyLc7so8aSdWeaWu6K6JJvxG3o4X1sujFuiieZTRhNSdR1YAWoUZb/wJ7siMamy7Jipx ywd/0qL9dr2J24dRPdJJjaMC300usyQMFM/vtYUS+khfG1aQJleFJrd0vejusFJ/ZKM5 /dB20zqd/nth8erj8QOxF1L+50nMR2r2CYp0OrSj+TjYn432FFb7ros4QFIZmImvStaP hfW/Yxg8xRpmW6GAIk8/H20XPrWd+JDip4C1myHJXRghsNTz++j4ZMeXm39X8VLcJaZC u9hQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:to:from:message-id:date:mime-version :dkim-signature:delivered-to:arc-authentication-results; bh=/knol8cesdXofJF+6a1xw/WRMKqi7crlmL03mYGuG4s=; b=nylEJFi8d702QAKZCy9P/AYq5d+RlyL/zsmx6cEBg3K2GVRwK4JW3ycvZK/Ip3aBUi F8gGGSP/dNy/lxfcH0TYKGZDvPy4O8zN7vvrxBBsKe7CyYQdaDMFrJ+elwwLI+zwLDLt rDnhiQ/EVn3TAj7ePHWZpPpfzbBhUAkXk/ml/v9AZqHipRcs95wmBTTafuDObSDrw4No frH1f5hGNHvFaNsJU1eGxdwrh2uMh5Jv/kGFanyM5HQLNqYL5ZF+/pOUC55unTPIrQ4J DWVOIjdvKs148g0pNkV3dXC/oMxDatNdT2PsvDZ51LTgTtwfbP941IVD7Yp1zjoUHzib +DIw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@google.com header.s=20161025 header.b=wH6oxU5T; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id a66-v6si2045637wmc.42.2018.06.01.12.52.27; Fri, 01 Jun 2018 12:52:28 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@google.com header.s=20161025 header.b=wH6oxU5T; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 12D6768A4F5; Fri, 1 Jun 2018 22:51:41 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-yw0-f201.google.com (mail-yw0-f201.google.com [209.85.161.201]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 3A945689C61 for ; Fri, 1 Jun 2018 22:51:35 +0300 (EEST) Received: by mail-yw0-f201.google.com with SMTP id d129-v6so6925246ywe.4 for ; Fri, 01 Jun 2018 12:52:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:date:message-id:subject:from:to:cc; bh=68C9HbS/aAmx24zPLyAzdEr2Y/rlVOyOOHGq6bSqmyA=; b=wH6oxU5Tcg5HqpvBExMh26maIwM/AVr0ZAU0xGtkG2/ihp4juy9H+tJD4+EUBFZSPe 7FO1bzGTwMhFjZgtA+wrbgx7XMjZ7a2sgOss9iEk3R/ztm3Jf1kofZaYafFR/c6+N3j4 P0dAkD09vqa8edHc8NIYoNTfFTxBFUVb6VEo9+HxLI/WGvPKijFWgdCRMb6lfbUVP35s A4DMGRlXlRQj9CDuDNOU8veLIRLJ3yBySi+1C7GRNLeRkiqy6SkIPHw+uLIjBxBS5ZZU cV4i34zdRXYMyoyIij3f8hRmM1pGcAVUdpt5Zvmi66zsVXNfY5lPgRZJYxkGUaDO22sa 4fdg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:date:message-id:subject:from:to:cc; bh=68C9HbS/aAmx24zPLyAzdEr2Y/rlVOyOOHGq6bSqmyA=; b=i64PDpitYNrPCU7KYjHOGj9GMqlmQyiUVtNsxAAOUmUzWu77kBGeeVjJHSLEY1gPEg OC1/ikPmvwZaT1wzZyHl+nScYqnZYqg8Ljhg8arCqUbOXSi6tDjkDmMGmKBbrsRHUgXG Wbbcn4OfZakWByO52QmGPPsd2kC7QC3QlQlSJMA47oC7ERumFj30bg3gQb4NXHFHHTuP zO4yK8gIyTYfO0pHb4eYKrATGvjYrGy0M2ZSZNX/5HjeA3+rC7eHqzMd91KUbYLdLl8q fbYE1wCDCmyWVH3d1NaehC3zLHJsFm7FfY3/+8GDPW3CbPri8Ld9RYcj9AdPP1d7zQgn ktbQ== X-Gm-Message-State: ALKqPwcvBe3mUVX054jx2ucduPxv7EZhQPgJYf42qDBpVe7VEhVTQaIE /QZzk6UGTfn1v27ay8oW1lH4E909uVRfh8k/rs7Nbx2jT+IkBwNlBL0EhZzvKAiM8bTYbu4cqj5 1EKGf5v5r9an7BP2or79FJo5cAyzGEcLhLkOqt+NWxg9TL7djwRs2AdfwkiDbX7/aNxze MIME-Version: 1.0 X-Received: by 2002:a81:62c3:: with SMTP id w186-v6mr3097249ywb.43.1527882739016; Fri, 01 Jun 2018 12:52:19 -0700 (PDT) Date: Fri, 1 Jun 2018 12:51:48 -0700 Message-Id: <20180601195148.251265-1-modmaker@google.com> X-Mailer: git-send-email 2.17.1.1185.g55be947832-goog From: Jacob Trimble To: ffmpeg-devel@ffmpeg.org Subject: [FFmpeg-devel] [PATCH] libavutil/encryption_info: Add unit tests. X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Jacob Trimble Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Jacob Trimble --- libavutil/Makefile | 1 + libavutil/encryption_info.h | 2 +- libavutil/tests/.gitignore | 2 + libavutil/tests/encryption_info.c | 176 ++++++++++++++++++++++++++++++ tests/fate/libavutil.mak | 4 + tests/ref/fate/encryption-info | 0 6 files changed, 184 insertions(+), 1 deletion(-) create mode 100644 libavutil/tests/encryption_info.c create mode 100644 tests/ref/fate/encryption-info diff --git a/tests/ref/fate/encryption-info b/tests/ref/fate/encryption-info new file mode 100644 index 0000000000..e69de29bb2 diff --git a/libavutil/Makefile b/libavutil/Makefile index d0632f16a6..9ed24cfc82 100644 --- a/libavutil/Makefile +++ b/libavutil/Makefile @@ -200,6 +200,7 @@ TESTPROGS = adler32 \ des \ dict \ display \ + encryption_info \ error \ eval \ file \ diff --git a/libavutil/encryption_info.h b/libavutil/encryption_info.h index 9140968fde..8fe7ebfe43 100644 --- a/libavutil/encryption_info.h +++ b/libavutil/encryption_info.h @@ -129,7 +129,7 @@ typedef struct AVEncryptionInitInfo { * * @param subsample_count The number of subsamples. * @param key_id_size The number of bytes in the key ID, should be 16. - * @param key_id_size The number of bytes in the IV, should be 16. + * @param iv_size The number of bytes in the IV, should be 16. * * @return The new AVEncryptionInfo structure, or NULL on error. */ diff --git a/libavutil/tests/.gitignore b/libavutil/tests/.gitignore index 71f75a8ee9..9d90827954 100644 --- a/libavutil/tests/.gitignore +++ b/libavutil/tests/.gitignore @@ -17,6 +17,7 @@ /dict /display /error +/encryption_info /eval /fifo /file @@ -24,6 +25,7 @@ /hmac /hwdevice /imgutils +/integer /lfg /lls /log diff --git a/libavutil/tests/encryption_info.c b/libavutil/tests/encryption_info.c new file mode 100644 index 0000000000..d489612b7d --- /dev/null +++ b/libavutil/tests/encryption_info.c @@ -0,0 +1,176 @@ +/* + * 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 "libavutil/encryption_info.h" + +#include +#include + +#include "libavutil/avassert.h" + +static const AVSubsampleEncryptionInfo test_subsamples[] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}}; +static const size_t test_subsample_count = sizeof(test_subsamples) / sizeof(test_subsamples[0]); +static const uint8_t test_iv[] = {0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18}; +static const uint8_t test_key_id[] = {0x21, 0x22, 0x23, 0x24}; +static const uint8_t test_key_id_2[] = {0x31, 0x32, 0x33, 0x34}; +static const uint8_t test_system_id[] = {0x41, 0x42, 0x43}; +static const uint8_t test_data[] = {0x51, 0x52}; + +static int compare_encryption_info(const AVEncryptionInfo *a, const AVEncryptionInfo *b) { + if (!a || !b || a->scheme != b->scheme || a->crypt_byte_block != b->crypt_byte_block || + a->skip_byte_block != b->skip_byte_block || a->key_id_size != b->key_id_size || + a->iv_size != b->iv_size || a->subsample_count != b->subsample_count) + return 1; + + if (memcmp(a->key_id, b->key_id, a->key_id_size) != 0 || + memcmp(a->iv, b->iv, a->iv_size) != 0 || + memcmp(a->subsamples, b->subsamples, a->subsample_count * sizeof(a->subsamples[0]))) + return 1; + + return 0; +} + +static int compare_encryption_init_info(const AVEncryptionInitInfo *a, const AVEncryptionInitInfo *b) { + if (!a || !b || a->system_id_size != b->system_id_size || + a->num_key_ids != b->num_key_ids || a->key_id_size != b->key_id_size || + a->data_size != b->data_size) + return 1; + + if (memcmp(a->system_id, b->system_id, a->system_id_size) != 0 || + memcmp(a->data, b->data, a->data_size) != 0) + return 1; + + for (uint32_t i = 0; i < a->num_key_ids; i++) { + if (memcmp(a->key_ids[i], b->key_ids[i], a->key_id_size) != 0) + return 1; + } + + if (a->next || b->next) { + if (!a->next || !b->next) + return 1; + if (compare_encryption_init_info(a->next, b->next) != 0) + return 1; + } + + return 0; +} + +static void run_encryption_info_test(void) +{ + AVEncryptionInfo *info, *copy; + uint8_t *side_data; + size_t side_data_size; + + info = av_encryption_info_alloc(test_subsample_count, sizeof(test_key_id), sizeof(test_iv)); + av_assert0(info); + av_assert0(info->key_id); + av_assert0(info->key_id_size == sizeof(test_key_id)); + av_assert0(info->iv); + av_assert0(info->iv_size == sizeof(test_iv)); + av_assert0(info->subsamples); + av_assert0(info->subsample_count == test_subsample_count); + + info->scheme = 1234; + info->crypt_byte_block = 333; + info->skip_byte_block = 444; + memcpy(info->key_id, test_key_id, sizeof(test_key_id)); + memcpy(info->iv, test_iv, sizeof(test_iv)); + memcpy(info->subsamples, test_subsamples, sizeof(test_subsamples)); + + copy = av_encryption_info_clone(info); + av_assert0(copy); + av_assert0(copy != info); + av_assert0(compare_encryption_info(info, copy) == 0); + av_encryption_info_free(copy); + + side_data = av_encryption_info_add_side_data(info, &side_data_size); + av_assert0(side_data); + av_assert0(side_data_size > 0); + + copy = av_encryption_info_get_side_data(side_data, side_data_size); + av_assert0(copy); + av_assert0(copy != info); + av_assert0(compare_encryption_info(info, copy) == 0); + av_encryption_info_free(copy); + av_free(side_data); + + av_encryption_info_free(info); +} + +static AVEncryptionInitInfo *create_init_info(void) +{ + AVEncryptionInitInfo *info; + + info = av_encryption_init_info_alloc(sizeof(test_system_id), 2, sizeof(test_key_id), sizeof(test_data)); + av_assert0(info); + av_assert0(info->system_id); + av_assert0(info->system_id_size == sizeof(test_system_id)); + av_assert0(info->key_ids); + av_assert0(info->num_key_ids == 2); + av_assert0(info->key_id_size == sizeof(test_key_id)); + av_assert0(info->key_ids[0]); + av_assert0(info->key_ids[1]); + av_assert0(info->data); + av_assert0(info->data_size == sizeof(test_data)); + av_assert0(!info->next); + + memcpy(info->system_id, test_system_id, sizeof(test_system_id)); + memcpy(info->key_ids[0], test_key_id, sizeof(test_key_id)); + memcpy(info->key_ids[1], test_key_id_2, sizeof(test_key_id_2)); + memcpy(info->data, test_data, sizeof(test_data)); + + return info; +} + +static void run_encryption_init_info_test(void) +{ + AVEncryptionInitInfo *info, *copy; + uint8_t *side_data; + size_t side_data_size; + + info = create_init_info(); + + side_data = av_encryption_init_info_add_side_data(info, &side_data_size); + av_assert0(side_data); + av_assert0(side_data_size > 0); + copy = av_encryption_init_info_get_side_data(side_data, side_data_size); + av_assert0(copy); + av_assert0(compare_encryption_init_info(info, copy) == 0); + av_encryption_init_info_free(copy); + av_free(side_data); + + // Make the first init info different from the second to test the correct order. + memset(info->system_id, 0, info->system_id_size); + info->next = create_init_info(); + side_data = av_encryption_init_info_add_side_data(info, &side_data_size); + av_assert0(side_data); + copy = av_encryption_init_info_get_side_data(side_data, side_data_size); + av_assert0(copy); + av_assert0(compare_encryption_init_info(info, copy) == 0); + av_encryption_init_info_free(copy); + av_free(side_data); + + av_encryption_init_info_free(info); +} + +int main(int argc, char **argv) +{ + run_encryption_info_test(); + run_encryption_init_info_test(); + return 0; +} diff --git a/tests/fate/libavutil.mak b/tests/fate/libavutil.mak index 9b32d880f5..a1d9313c3d 100644 --- a/tests/fate/libavutil.mak +++ b/tests/fate/libavutil.mak @@ -70,6 +70,10 @@ FATE_LIBAVUTIL += fate-dict fate-dict: libavutil/tests/dict$(EXESUF) fate-dict: CMD = run libavutil/tests/dict +FATE_LIBAVUTIL += fate-encryption-info +fate-encryption-info: libavutil/tests/encryption_info$(EXESUF) +fate-encryption-info: CMD = run libavutil/tests/encryption_info + FATE_LIBAVUTIL += fate-eval fate-eval: libavutil/tests/eval$(EXESUF) fate-eval: CMD = run libavutil/tests/eval