File: UtilTest.cpp

package info (click to toggle)
nzbget 21.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,128 kB
  • sloc: cpp: 62,884; sh: 5,311; python: 1,381; makefile: 491
file content (120 lines) | stat: -rw-r--r-- 4,591 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 *  This file is part of nzbget. See <http://nzbget.net>.
 *
 *  Copyright (C) 2015-2016 Andrey Prygunkov <hugbug@users.sourceforge.net>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


#include "nzbget.h"

#include "catch.h"

#include "Util.h"

TEST_CASE("WebUtil: XmlStripTags", "[Util][Quick]")
{
	const char* xml  = "<div><img style=\"margin-left:10px;margin-bottom:10px;float:right;\" src=\"https://xxx/cover.jpg\"/><ul><li>ID: 12345678</li><li>Name: <a href=\"https://xxx/12344\">Show name</a></li><li>Size: 3.00 GB </li><li>Attributes: Category - <a href=\"https://xxx/2040\">Movies > HD</a></li></li></ul></div>";
	const char* text = "                                                                                                        ID: 12345678         Name:                             Show name             Size: 3.00 GB          Attributes: Category -                            Movies > HD                         ";
	char* testString = strdup(xml);
	WebUtil::XmlStripTags(testString);

	REQUIRE(strcmp(testString, text) == 0);

	free(testString);
}

TEST_CASE("WebUtil: XmlDecode", "[Util][Quick]")
{
	const char* xml  = "Poster: Bob &lt;bob@home&gt; bad&mdash;and there&#039;s one thing";
	const char* text = "Poster: Bob <bob@home> bad&mdash;and there's one thing";
	char* testString = strdup(xml);
	WebUtil::XmlDecode(testString);

	REQUIRE(strcmp(testString, text) == 0);

	free(testString);
}

TEST_CASE("WebUtil: XmlRemoveEntities", "[Util][Quick]")
{
	const char* xml  = "Poster: Bob &lt;bob@home&gt; bad&mdash;and there&#039;s one thing";
	const char* text = "Poster: Bob  bob@home  bad and there s one thing";
	char* testString = strdup(xml);
	WebUtil::XmlRemoveEntities(testString);

	REQUIRE(strcmp(testString, text) == 0);

	free(testString);
}

TEST_CASE("WebUtil: URLEncode", "[Util][Quick]")
{
	const char* badUrl = "http://www.example.com/nzb_get/12344/Debian V7 6 64 bit OS.nzb";
	const char* correctedUrl = "http://www.example.com/nzb_get/12344/Debian%20V7%206%2064%20bit%20OS.nzb";
	CString testString = WebUtil::UrlEncode(badUrl);

	REQUIRE(strcmp(testString, correctedUrl) == 0);
}

TEST_CASE("Util: WildMask", "[Util][Quick]")
{
	WildMask mask("*.par2", true);
	REQUIRE_FALSE(mask.Match("Debian V7 6 64 bit OS.nzb"));
	REQUIRE_FALSE(mask.Match("Debian V7 6 64 bit OS.par2.nzb"));
	REQUIRE(mask.Match("Debian V7 6 64 bit OS.par2"));
	REQUIRE(mask.Match(".par2"));
	REQUIRE_FALSE(mask.Match("par2"));
}

TEST_CASE("Util: RegEx", "[Util][Quick]")
{
	RegEx regExRar(".*\\.rar$");
	RegEx regExRarMultiSeq(".*\\.[r-z][0-9][0-9]$");
	RegEx regExSevenZip(".*\\.7z$|.*\\.7z\\.[0-9]+$");
	RegEx regExNumExt(".*\\.[0-9]+$");

	REQUIRE(regExRar.Match("filename.rar"));
	REQUIRE(regExRar.Match("filename.part001.rar"));
	REQUIRE_FALSE(regExRar.Match("filename.rar.txt"));

	REQUIRE_FALSE(regExRarMultiSeq.Match("filename.rar"));
	REQUIRE(regExRarMultiSeq.Match("filename.r01"));
	REQUIRE(regExRarMultiSeq.Match("filename.r99"));
	REQUIRE_FALSE(regExRarMultiSeq.Match("filename.r001"));
	REQUIRE(regExRarMultiSeq.Match("filename.s01"));
	REQUIRE(regExRarMultiSeq.Match("filename.t99"));

	REQUIRE(regExSevenZip.Match("filename.7z"));
	REQUIRE_FALSE(regExSevenZip.Match("filename.7z.rar"));
	REQUIRE(regExSevenZip.Match("filename.7z.1"));
	REQUIRE(regExSevenZip.Match("filename.7z.001"));
	REQUIRE(regExSevenZip.Match("filename.7z.123"));
	REQUIRE(regExSevenZip.Match("filename.7z.999"));

	REQUIRE(regExNumExt.Match("filename.7z.1"));
	REQUIRE(regExNumExt.Match("filename.7z.9"));
	REQUIRE(regExNumExt.Match("filename.7z.001"));
	REQUIRE(regExNumExt.Match("filename.7z.123"));
	REQUIRE(regExNumExt.Match("filename.7z.999"));

	const char* testStr = "My.Show.Name.S01E02.ABC.720";
	RegEx seasonEpisode(".*S([0-9]+)E([0-9]+).*");
	REQUIRE(seasonEpisode.IsValid());
	REQUIRE(seasonEpisode.Match(testStr));
	REQUIRE(seasonEpisode.GetMatchCount() == 3);
	REQUIRE(seasonEpisode.GetMatchStart(1) == 14);
	REQUIRE(seasonEpisode.GetMatchLen(1) == 2);
}