1 /*
2  *  This file is part of nzbget. See <http://nzbget.net>.
3  *
4  *  Copyright (C) 2015-2016 Andrey Prygunkov <hugbug@users.sourceforge.net>
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 
21 #include "nzbget.h"
22 
23 #include "catch.h"
24 
25 #include "Options.h"
26 
27 class OptionsExtenderMock : public Options::Extender
28 {
29 public:
30 	int m_newsServers;
31 	int m_feeds;
32 	int m_tasks;
33 
OptionsExtenderMock()34 	OptionsExtenderMock() : m_newsServers(0), m_feeds(0), m_tasks(0) {}
35 
36 protected:
AddNewsServer(int id,bool active,const char * name,const char * host,int port,int ipVersion,const char * user,const char * pass,bool joinGroup,bool tls,const char * cipher,int maxConnections,int retention,int level,int group,bool optional)37 	virtual void AddNewsServer(int id, bool active, const char* name, const char* host,
38 		int port, int ipVersion, const char* user, const char* pass, bool joinGroup, bool tls,
39 		const char* cipher, int maxConnections, int retention, int level, int group, bool optional)
40 	{
41 		m_newsServers++;
42 	}
43 
AddFeed(int id,const char * name,const char * url,int interval,const char * filter,bool backlog,bool pauseNzb,const char * category,int priority,const char * feedScript)44 	virtual void AddFeed(int id, const char* name, const char* url, int interval,
45 		const char* filter, bool backlog, bool pauseNzb, const char* category, int priority, const char* feedScript)
46 	{
47 		m_feeds++;
48 	}
49 
AddTask(int id,int hours,int minutes,int weekDaysBits,Options::ESchedulerCommand command,const char * param)50 	virtual void AddTask(int id, int hours, int minutes, int weekDaysBits, Options::ESchedulerCommand command, const char* param)
51 	{
52 		m_tasks++;
53 	}
54 };
55 
56 TEST_CASE("Options: initializing without configuration file", "[Options][Quick]")
57 {
58 	Options options(nullptr, nullptr);
59 
60 	REQUIRE(options.GetConfigFilename() == nullptr);
61 #ifdef WIN32
62 	REQUIRE(strcmp(options.GetTempDir(), "nzbget/tmp") == 0);
63 #else
64 	REQUIRE(strcmp(options.GetTempDir(), "~/downloads/tmp") == 0);
65 #endif
66 }
67 
68 TEST_CASE("Options: passing command line options", "[Options][Quick]")
69 {
70 	Options::CmdOptList cmdOpts;
71 	cmdOpts.push_back("ControlUsername=my-user-name-1");
72 	cmdOpts.push_back("ControlUsername=my-user-name-2");
73 
74 	Options options(&cmdOpts, nullptr);
75 
76 	REQUIRE(options.GetConfigFilename() == nullptr);
77 	REQUIRE(strcmp(options.GetControlUsername(), "my-user-name-2") == 0);
78 }
79 
80 TEST_CASE("Options: calling extender", "[Options][Quick]")
81 {
82 	Options::CmdOptList cmdOpts;
83 	cmdOpts.push_back("Server1.Host=news.mynewsserver.com");
84 	cmdOpts.push_back("Server1.Port=119");
85 	cmdOpts.push_back("Server1.Connections=4");
86 
87 	cmdOpts.push_back("Server2.Host=news1.mynewsserver.com");
88 	cmdOpts.push_back("Server2.Port=563");
89 	cmdOpts.push_back("Server2.Connections=2");
90 
91 	cmdOpts.push_back("Feed1.Url=http://my.feed.com");
92 
93 	cmdOpts.push_back("Task1.Time=*:00");
94 	cmdOpts.push_back("Task1.WeekDays=1-7");
95 	cmdOpts.push_back("Task1.Command=pausedownload");
96 
97 	OptionsExtenderMock extender;
98 	Options options(&cmdOpts, &extender);
99 
100 	REQUIRE(extender.m_newsServers == 2);
101 	REQUIRE(extender.m_feeds == 1);
102 	REQUIRE(extender.m_tasks == 24);
103 }
104