Skip to content

Commit 4fa9d74

Browse files
committed
Improve n parameter tests
These are currently not working, but the tests said OK because it was not checked what happens if the returned parameter is empty! -> #1339
1 parent dd0f955 commit 4fa9d74

7 files changed

Lines changed: 44 additions & 322 deletions

File tree

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeJavaScriptPlayerManager.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.schabi.newpipe.extractor.services.youtube;
22

3+
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
4+
35
import org.schabi.newpipe.extractor.exceptions.ParsingException;
46
import org.schabi.newpipe.extractor.utils.JavaScript;
57

@@ -264,6 +266,10 @@ public static String getUrlWithThrottlingParameterDeobfuscated(
264266
cachedThrottlingDeobfuscationFunctionName,
265267
obfuscatedThrottlingParameter);
266268

269+
if (isNullOrEmpty(deobfuscatedThrottlingParameter)) {
270+
throw new IllegalStateException("Extracted n-parameter is empty");
271+
}
272+
267273
CACHED_THROTTLING_PARAMETERS.put(
268274
obfuscatedThrottlingParameter, deobfuscatedThrottlingParameter);
269275

extractor/src/main/java/org/schabi/newpipe/extractor/utils/Parser.java

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -91,22 +91,19 @@ public static Matcher matchMultiplePatterns(final Pattern[] patterns, final Stri
9191
if (matcher.find()) {
9292
return matcher;
9393
} else if (exception == null) {
94-
// only pass input to exception message when it is not too long
95-
if (input.length() > 1024) {
96-
exception = new RegexException("Failed to find pattern \"" + pattern.pattern()
97-
+ "\"");
98-
} else {
99-
exception = new RegexException("Failed to find pattern \"" + pattern.pattern()
100-
+ "\" inside of \"" + input + "\"");
101-
}
94+
exception = new RegexException("Failed to find pattern \"" + pattern.pattern()
95+
+ "\""
96+
// only pass input to exception message when it is not too long
97+
+ (input.length() <= 1000
98+
? "inside of \"" + input + "\""
99+
: "")
100+
);
102101
}
103102
}
104103

105-
if (exception == null) {
106-
throw new RegexException("Empty patterns array passed to matchMultiplePatterns");
107-
} else {
108-
throw exception;
109-
}
104+
throw exception != null
105+
? exception
106+
: new RegexException("Empty patterns array passed to matchMultiplePatterns");
110107
}
111108

112109
public static boolean isMatch(final String pattern, final String input) {

extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeThrottlingParameterDeobfuscationTest.java

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,31 @@
11
package org.schabi.newpipe.extractor.services.youtube;
22

3+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
34
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
import static org.junit.jupiter.api.Assertions.assertFalse;
46
import static org.junit.jupiter.api.Assertions.assertNotEquals;
5-
import static org.junit.jupiter.api.Assertions.fail;
67

8+
import org.junit.jupiter.api.Assertions;
79
import org.junit.jupiter.api.BeforeEach;
10+
import org.junit.jupiter.api.Disabled;
811
import org.junit.jupiter.api.Test;
12+
import org.junit.jupiter.api.function.Executable;
913
import org.schabi.newpipe.extractor.InitNewPipeTest;
1014
import org.schabi.newpipe.extractor.exceptions.ParsingException;
1115

16+
import java.util.Arrays;
17+
import java.util.stream.Stream;
18+
19+
@Disabled("Throttling parameter deobfuscation is currently not functional, "
20+
+ "see https://github.com/TeamNewPipe/NewPipeExtractor/issues/1339")
1221
class YoutubeThrottlingParameterDeobfuscationTest implements InitYoutubeTest {
1322
@BeforeEach
1423
void beforeEach() {
1524
InitNewPipeTest.initEmpty();
1625
YoutubeTestsUtils.ensureStateless();
1726
}
1827

28+
1929
@Test
2030
void testExtractFunction__success() {
2131
InitNewPipeTest.initNewPipe(getClass(), "extractSuccess");
@@ -24,17 +34,15 @@ void testExtractFunction__success() {
2434

2535
final String obfuscatedUrl = "https://r6---sn-4g5ednek.googlevideo.com/videoplayback?expire=1626562120&ei=6AnzYO_YBpql1gLGkb_IBQ&ip=127.0.0.1&id=o-ANhBEf36Z5h-8U9DDddtPDqtS0ZNwf0XJAAigudKI2uI&itag=278&aitags=133%2C134%2C135%2C136%2C137%2C160%2C242%2C243%2C244%2C247%2C248%2C278&source=youtube&requiressl=yes&vprv=1&mime=video%2Fwebm&ns=TvecOReN0vPuXb3j_zq157IG&gir=yes&clen=2915100&dur=270.203&lmt=1608157174907785&keepalive=yes&fexp=24001373,24007246&c=WEB&txp=5535432&n=N9BWSTFT7vvBJrvQ&sparams=expire%2Cei%2Cip%2Cid%2Caitags%2Csource%2Crequiressl%2Cvprv%2Cmime%2Cns%2Cgir%2Cclen%2Cdur%2Clmt&alr=yes&sig=AOq0QJ8wRQIgW6XnUDKPDSxiT0_KE_tDDMpcaCJl2Un5p0Fu9qZNQGkCIQDWxsDHi_s2BEmRqIbd1C5g_gzfihB7RZLsScKWNMwzzA%3D%3D&cpn=9r2yt3BqcYmeb2Yu&cver=2.20210716.00.00&redirect_counter=1&cm2rm=sn-4g5ezy7s&cms_redirect=yes&mh=Y5&mm=34&mn=sn-4g5ednek&ms=ltu&mt=1626540524&mv=m&mvi=6&pl=43&lsparams=mh,mm,mn,ms,mv,mvi,pl&lsig=AG3C_xAwRQIhAIUzxTn9Vw1-vm-_7OQ5-0h1M6AZsY9Bx1FlCCTeMICzAiADtGggbn4Znsrh2EnvyOsGnYdRGcbxn4mW9JMOQiInDQ%3D%3D&range=259165-480735&rn=11&rbuf=20190";
2636

27-
for (final String videoId : videoIds) {
28-
try {
29-
final String deobfuscatedUrl =
30-
YoutubeJavaScriptPlayerManager.getUrlWithThrottlingParameterDeobfuscated(
31-
videoId, obfuscatedUrl);
32-
assertNotEquals(obfuscatedUrl, deobfuscatedUrl);
33-
} catch (final Exception e) {
34-
fail("Failed to extract throttling parameter deobfuscation function or run its code for video "
35-
+ videoId, e);
36-
}
37-
}
37+
Assertions.assertAll(
38+
Arrays.stream(videoIds)
39+
.flatMap(videoId -> {
40+
final String deobfuscatedUrl =
41+
assertDoesNotThrow(() -> YoutubeJavaScriptPlayerManager.getUrlWithThrottlingParameterDeobfuscated(
42+
videoId, obfuscatedUrl));
43+
return assertDeobfuscation(obfuscatedUrl, deobfuscatedUrl);
44+
})
45+
);
3846
}
3947

4048
@Test
@@ -48,7 +56,14 @@ void testDecode__success() throws ParsingException {
4856
"jE1USQrs1rw", obfuscatedUrl);
4957
// The deobfuscation function changes over time, so we just check if the corresponding
5058
// parameter changed
51-
assertNotEquals(obfuscatedUrl, deobfuscatedUrl);
59+
Assertions.assertAll(assertDeobfuscation(obfuscatedUrl, deobfuscatedUrl));
60+
}
61+
62+
private Stream<Executable> assertDeobfuscation(final String obfuscatedUrl, final String deobfuscatedUrl) {
63+
return Stream.of(
64+
() -> assertNotEquals(obfuscatedUrl, deobfuscatedUrl),
65+
() -> assertFalse(deobfuscatedUrl.contains("&n=&"), "Extracted n parameter is empty for " + deobfuscatedUrl)
66+
);
5267
}
5368

5469
@Test

extractor/src/test/resources/mocks/v1/org/schabi/newpipe/extractor/services/youtube/youtubethrottlingparameterdeobfuscation/decodesuccess/generated_mock_0.json

Lines changed: 0 additions & 80 deletions
This file was deleted.

extractor/src/test/resources/mocks/v1/org/schabi/newpipe/extractor/services/youtube/youtubethrottlingparameterdeobfuscation/decodesuccess/generated_mock_1.json

Lines changed: 0 additions & 68 deletions
This file was deleted.

extractor/src/test/resources/mocks/v1/org/schabi/newpipe/extractor/services/youtube/youtubethrottlingparameterdeobfuscation/extractsuccess/generated_mock_0.json

Lines changed: 0 additions & 80 deletions
This file was deleted.

extractor/src/test/resources/mocks/v1/org/schabi/newpipe/extractor/services/youtube/youtubethrottlingparameterdeobfuscation/extractsuccess/generated_mock_1.json

Lines changed: 0 additions & 68 deletions
This file was deleted.

0 commit comments

Comments
 (0)