From 1fa85ec6ca81ed6b13c26e425f26217072324301 Mon Sep 17 00:00:00 2001 From: AudricV <74829229+AudricV@users.noreply.github.com> Date: Sun, 17 Sep 2023 22:10:17 +0200 Subject: [PATCH] [YouTube] Add tests for signature timestamp extraction and signature deobfuscation function extraction and execution --- .../youtube/YoutubeSignaturesTest.java | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeSignaturesTest.java diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeSignaturesTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeSignaturesTest.java new file mode 100644 index 000000000..e2f2d9f97 --- /dev/null +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeSignaturesTest.java @@ -0,0 +1,53 @@ +package org.schabi.newpipe.extractor.services.youtube; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.ValueSource; +import org.schabi.newpipe.downloader.DownloaderTestImpl; +import org.schabi.newpipe.extractor.NewPipe; + +import javax.annotation.Nonnull; +import java.io.IOException; + +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class YoutubeSignaturesTest { + + @BeforeEach + void setUp() throws IOException { + NewPipe.init(DownloaderTestImpl.getInstance()); + YoutubeTestsUtils.ensureStateless(); + } + + @ValueSource(strings = { + "QzUGs1qRTEI", + "" + }) + @ParameterizedTest + void testSignatureTimestampExtraction(@Nonnull final String videoId) throws Exception { + final Integer signatureTimestamp = + YoutubeJavaScriptPlayerManager.getSignatureTimestamp(videoId); + assertTrue(signatureTimestamp > 0, "signatureTimestamp is <= 0"); + } + + /* + The first column of the CSV entries is a video ID + The second one of these entries are not real signatures, but as the deobfuscation function + manipulates strings, we can use random characters combined as strings to test the extraction + and the execution of the function + */ + @CsvSource(value = { + "QzUGs1qRTEI,5QjJrWzVcOutYYNyxkDJVkzQDZQxNbbxGi4hRoh2h4PomQMQq9vo2WPHVpHgxRn7qT3WyhRiJa1k1t1DL3lynZtupHmG3wW4qh59faKjtY4UVu", + ",7vIK4hG6NbcIEQP4ZIRjonOzuPHh7wTrEgBdEMYyfE4F5Pq0FiGdv04kptb587c8aToH345ETJ8dMbXnpOmjanP3nzgJ0iNg8oHIm8oeQODPSP" + }) + @ParameterizedTest + void testSignatureDeobfuscation(@Nonnull final String videoId, + @Nonnull final String sampleString) throws Exception { + // As the signature deobfuscation changes frequently with player versions, we can only test + // that we get a different string than the original one + assertNotEquals(sampleString, + YoutubeJavaScriptPlayerManager.deobfuscateSignature(videoId, sampleString)); + } +}