loadertest.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /*
  2. * Copyright 2015 <copyright holder> <email>
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of
  7. * the License or (at your option) version 3 or any later version
  8. * accepted by the membership of KDE e.V. (or its successor approved
  9. * by the membership of KDE e.V.), which shall act as a proxy
  10. * defined in Section 14 of version 3 of the license.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. #include "loadertest.h"
  22. #include <QtTest/QtTest>
  23. #include <QtTest/QSignalSpy>
  24. void LoaderTest::initTestCase()
  25. {
  26. m_loader = new TestLoader;
  27. m_rpms << (QList<QString *>() << new QString << new QString << new QString);
  28. m_rpms << (QList<QString *>() << new QString << new QString << new QString);
  29. m_temps << (QList<QString *>() << new QString << new QString << new QString);
  30. m_temps << (QList<QString *>() << new QString << new QString << new QString);
  31. m_pwms << (QList<QString *>() << new QString << new QString);
  32. m_pwms << (QList<QString *>() << new QString << new QString);
  33. m_pwmModes << (QList<QString *>() << new QString << new QString);
  34. m_pwmModes << (QList<QString *>() << new QString << new QString);
  35. QCOMPARE(m_loader->hwmons().size(), 0);
  36. m_loader->addHwmon(new TestHwmon(QStringLiteral("radeon"), m_rpms.at(0), m_pwms.at(0), m_pwmModes.at(0), m_temps.at(0), 0, m_loader));
  37. m_loader->addHwmon(new TestHwmon(QStringLiteral("coretemp"), m_rpms.at(1), m_pwms.at(1), m_pwmModes.at(1), m_temps.at(1), 1, m_loader));
  38. QCOMPARE(m_loader->hwmons().size(), 2);
  39. }
  40. void LoaderTest::cleanupTestCase()
  41. {
  42. delete m_loader;
  43. }
  44. void LoaderTest::init()
  45. {
  46. // Called before each testfunction is executed
  47. }
  48. void LoaderTest::cleanup()
  49. {
  50. // Called after every testfunction
  51. }
  52. void LoaderTest::parseIntervalTest_data()
  53. {
  54. QTest::addColumn<QString>("config");
  55. QTest::addColumn<int>("result");
  56. QTest::addColumn<QString>("error");
  57. QTest::addColumn<bool>("critical");
  58. QTest::newRow("valid4") << "INTERVAL=4" << 4 << "" << false;
  59. QTest::newRow("valid2") << "INTERVAL=2" << 2 << "" << false;
  60. QTest::newRow("valid2") << "INTERVAL= 3" << 3 << "" << false;
  61. QTest::newRow("invalid0") << "INTERVAL=0" << 0 << "Interval must be greater or equal to one!" << true;
  62. QTest::newRow("invalid6") << "INTERVA=6" << 6 << "Unrecognized line in config: \'INTERVA=6\'" << true;
  63. QTest::newRow("invalid1") << "INTEVAL=1" << 1 << "Unrecognized line in config: \'INTEVAL=1\'" << true;
  64. }
  65. void LoaderTest::parseIntervalTest()
  66. {
  67. QFETCH(QString, config);
  68. QFETCH(int, result);
  69. QFETCH(QString, error);
  70. QFETCH(bool, critical);
  71. QSignalSpy spy(m_loader, SIGNAL(error(QString, bool)));
  72. m_loader->parse(config);
  73. QCOMPARE(spy.isEmpty(), error.isEmpty());
  74. if (spy.count())
  75. {
  76. QCOMPARE(spy.at(0).at(0).toString(), error);
  77. QCOMPARE(spy.at(0).at(1).toBool(), critical);
  78. }
  79. else
  80. QCOMPARE(m_loader->interval(), result);
  81. }
  82. void LoaderTest::parseFctempTest_data()
  83. {
  84. QTest::addColumn<QString>("config");
  85. QTest::addColumn<PwmFan *>("fan");
  86. QTest::addColumn<Temp *>("temp");
  87. QTest::addColumn<QString>("error");
  88. QTest::addColumn<bool>("critical");
  89. QTest::newRow("fan02temp11") << "FCTEMPS=hwmon0/fan2=hwmon1/temp1" << m_loader->hwmons().at(0)->pwmFans().value(2) << m_loader->hwmons().at(1)->temps().value(1) << "" << false;
  90. QTest::newRow("fan11temp03") << "FCTEMPS=hwmon1/fan1=hwmon0/temp3" << m_loader->hwmons().at(1)->pwmFans().value(1) << m_loader->hwmons().at(0)->temps().value(3) << "" << false;
  91. QTest::newRow("fan12temp12") << "FCTEMPS=hwmon1/fan2=hwmon1/temp2" << m_loader->hwmons().at(1)->pwmFans().value(2) << m_loader->hwmons().at(1)->temps().value(2) << "" << false;
  92. QTest::newRow("invalid0") << "FCTEMPS=hwmon2/fan1=hwmon0/temp3" << static_cast<PwmFan *>(Q_NULLPTR) << m_loader->hwmons().at(0)->temps().value(3) << "Invalid fan entry: \'hwmon2/fan1\'" << true;
  93. QTest::newRow("invalid1") << "FCTEMPS=hwmon0/fan1=hwmon0/temp4" << m_loader->hwmons().at(0)->pwmFans().value(0) << static_cast<Temp *>(Q_NULLPTR) << "Invalid temp entry: \'hwmon0/temp4\'" << true;
  94. }
  95. void LoaderTest::parseFctempTest()
  96. {
  97. QFETCH(QString, config);
  98. QFETCH(PwmFan *, fan);
  99. QFETCH(Temp *, temp);
  100. QFETCH(QString, error);
  101. QFETCH(bool, critical);
  102. QSignalSpy spy(m_loader, SIGNAL(error(QString, bool)));
  103. m_loader->parse(config);
  104. QCOMPARE(spy.isEmpty(), error.isEmpty());
  105. if (fan)
  106. QCOMPARE(fan->temp(), temp);
  107. if (spy.count())
  108. {
  109. QCOMPARE(spy.at(0).at(0).toString(), error);
  110. QCOMPARE(spy.at(0).at(1).toBool(), critical);
  111. }
  112. else if (fan)
  113. QCOMPARE(fan->hasTemp(), true);
  114. }
  115. void LoaderTest::parseDevnameTest_data()
  116. {
  117. QTest::addColumn<QString>("config");
  118. QTest::addColumn<QString>("error");
  119. QTest::addColumn<bool>("critical");
  120. QTest::newRow("valid0") << "DEVNAME=hwmon0=radeon" << "" << false;
  121. QTest::newRow("valid1") << "DEVNAME=hwmon1=coretemp" << "" << false;
  122. QTest::newRow("valid2") << "DEVNAME=hwmon0=radeon hwmon1=coretemp" << "" << false;
  123. QTest::newRow("valid3") << "DEVNAME= hwmon1=coretemp hwmon0=radeon" << "" << false;
  124. QTest::newRow("invalid0") << "DEVNAME=hwmon2=radeon" << "Invalid DEVNAME: \'hwmon2=radeon\'! No hwmon with index 2" << true;
  125. QTest::newRow("invalid0") << "DEVNAME=hwmon1=radeon" << "Wrong name for hwmon1! Should be \'coretemp\'" << true;
  126. }
  127. void LoaderTest::parseDevnameTest()
  128. {
  129. QFETCH(QString, config);
  130. QFETCH(QString, error);
  131. QFETCH(bool, critical);
  132. QSignalSpy spy(m_loader, SIGNAL(error(QString, bool)));
  133. m_loader->parse(config);
  134. QCOMPARE(spy.isEmpty(), error.isEmpty());
  135. if (spy.count())
  136. {
  137. QCOMPARE(spy.at(0).at(0).toString(), error);
  138. QCOMPARE(spy.at(0).at(1).toBool(), critical);
  139. }
  140. }
  141. void LoaderTest::parseMintempTest_data()
  142. {
  143. QTest::addColumn<QString>("config");
  144. QTest::addColumn<PwmFan *>("fan");
  145. QTest::addColumn<int>("result");
  146. QTest::addColumn<QString>("error");
  147. QTest::addColumn<bool>("critical");
  148. QTest::newRow("valid01") << "MINTEMP=hwmon0/fan1=20" << m_loader->pwmFan(0, 1) << 20 << "" << false;
  149. QTest::newRow("valid12") << "MINTEMP=hwmon1/fan2=35" << m_loader->pwmFan(1, 2) << 35 << "" << false;
  150. QTest::newRow("valid02") << "MINTEMP=hwmon0/fan2=-35" << m_loader->pwmFan(0, 2) << -35 << "-35 is not an unsigned integer!" << false;
  151. QTest::newRow("valid11") << "MINTEMP= hwmon1/fan1=40" << m_loader->pwmFan(1, 1) << 40 << "" << false;
  152. }
  153. void LoaderTest::parseMintempTest()
  154. {
  155. QFETCH(QString, config);
  156. QFETCH(PwmFan *, fan);
  157. QFETCH(int, result);
  158. QFETCH(QString, error);
  159. QFETCH(bool, critical);
  160. QSignalSpy spy(m_loader, SIGNAL(error(QString, bool)));
  161. m_loader->parse(config);
  162. QCOMPARE(spy.isEmpty(), error.isEmpty());
  163. if (spy.count())
  164. {
  165. QCOMPARE(spy.at(0).at(0).toString(), error);
  166. QCOMPARE(spy.at(0).at(1).toBool(), critical);
  167. }
  168. else
  169. QCOMPARE(fan->minTemp(), result);
  170. }
  171. void LoaderTest::parseMaxtempTest_data()
  172. {
  173. QTest::addColumn<QString>("config");
  174. QTest::addColumn<PwmFan *>("fan");
  175. QTest::addColumn<int>("result");
  176. QTest::addColumn<QString>("error");
  177. QTest::addColumn<bool>("critical");
  178. QTest::newRow("valid01") << "MAXTEMP=hwmon0/fan1=80" << m_loader->pwmFan(0, 1) << 80 << "" << false;
  179. QTest::newRow("valid12") << "MAXTEMP=hwmon1/fan2=78 #iuf" << m_loader->pwmFan(1, 2) << 78 << "" << false;
  180. QTest::newRow("valid02") << "MAXTEMP=hwmon0/fan2=-78" << m_loader->pwmFan(0, 2) << -78 << "-78 is not an unsigned integer!" << false;
  181. QTest::newRow("valid11") << "MAXTEMP= hwmon1/fan1=53" << m_loader->pwmFan(1, 1) << 53 << "" << false;
  182. }
  183. void LoaderTest::parseMaxtempTest()
  184. {
  185. QFETCH(QString, config);
  186. QFETCH(PwmFan *, fan);
  187. QFETCH(int, result);
  188. QFETCH(QString, error);
  189. QFETCH(bool, critical);
  190. QSignalSpy spy(m_loader, SIGNAL(error(QString, bool)));
  191. m_loader->parse(config);
  192. QCOMPARE(spy.isEmpty(), error.isEmpty());
  193. if (spy.count())
  194. {
  195. QCOMPARE(spy.at(0).at(0).toString(), error);
  196. QCOMPARE(spy.at(0).at(1).toBool(), critical);
  197. }
  198. else
  199. QCOMPARE(fan->maxTemp(), result);
  200. }
  201. void LoaderTest::parseMinstartTest_data()
  202. {
  203. QTest::addColumn<QString>("config");
  204. QTest::addColumn<PwmFan *>("fan");
  205. QTest::addColumn<int>("result");
  206. QTest::addColumn<QString>("error");
  207. QTest::addColumn<bool>("critical");
  208. QTest::newRow("valid01") << "MINSTART=hwmon0/fan1=20" << m_loader->pwmFan(0, 1) << 20 << "" << false;
  209. QTest::newRow("valid12") << "MINSTART=hwmon1/fan2=35" << m_loader->pwmFan(1, 2) << 35 << "" << false;
  210. QTest::newRow("valid02") << "MINSTART=hwmon0/fan2=0#rtg" << m_loader->pwmFan(0, 2) << 0 << "" << false;
  211. QTest::newRow("valid11") << "MINSTART= hwmon1/fan1=40" << m_loader->pwmFan(1, 1) << 40 << "" << false;
  212. }
  213. void LoaderTest::parseMinstartTest()
  214. {
  215. QFETCH(QString, config);
  216. QFETCH(PwmFan *, fan);
  217. QFETCH(int, result);
  218. QFETCH(QString, error);
  219. QFETCH(bool, critical);
  220. QSignalSpy spy(m_loader, SIGNAL(error(QString, bool)));
  221. m_loader->parse(config);
  222. QCOMPARE(spy.isEmpty(), error.isEmpty());
  223. if (spy.count())
  224. {
  225. QCOMPARE(spy.at(0).at(0).toString(), error);
  226. QCOMPARE(spy.at(0).at(1).toBool(), critical);
  227. }
  228. else
  229. QCOMPARE(fan->minStart(), result);
  230. }
  231. void LoaderTest::parseMinstopTest_data()
  232. {
  233. QTest::addColumn<QString>("config");
  234. QTest::addColumn<PwmFan *>("fan");
  235. QTest::addColumn<int>("result");
  236. QTest::addColumn<QString>("error");
  237. QTest::addColumn<bool>("critical");
  238. QTest::newRow("valid01") << "MINSTOP=hwmon0/fan1=20" << m_loader->pwmFan(0, 1) << 20 << "" << false;
  239. QTest::newRow("valid12") << "MINSTOP=hwmon1/fan2=35" << m_loader->pwmFan(1, 2) << 35 << "" << false;
  240. QTest::newRow("valid02") << "MINSTOP=hwmon0/fan2=0" << m_loader->pwmFan(0, 2) << 0 << "" << false;
  241. QTest::newRow("valid11") << "MINSTOP= hwmon1/fan1=40" << m_loader->pwmFan(1, 1) << 40 << "" << false;
  242. }
  243. void LoaderTest::parseMinstopTest()
  244. {
  245. QFETCH(QString, config);
  246. QFETCH(PwmFan *, fan);
  247. QFETCH(int, result);
  248. QFETCH(QString, error);
  249. QFETCH(bool, critical);
  250. QSignalSpy spy(m_loader, SIGNAL(error(QString, bool)));
  251. m_loader->parse(config);
  252. QCOMPARE(spy.isEmpty(), error.isEmpty());
  253. if (spy.count())
  254. {
  255. QCOMPARE(spy.at(0).at(0).toString(), error);
  256. QCOMPARE(spy.at(0).at(1).toBool(), critical);
  257. }
  258. else
  259. QCOMPARE(fan->minStop(), result);
  260. }
  261. void LoaderTest::parseMinpwmTest_data()
  262. {
  263. QTest::addColumn<QString>("config");
  264. QTest::addColumn<PwmFan *>("fan");
  265. QTest::addColumn<int>("result");
  266. QTest::addColumn<QString>("error");
  267. QTest::addColumn<bool>("critical");
  268. QTest::newRow("valid01") << "MINPWM=hwmon0/fan1=20#fgiuh" << m_loader->pwmFan(0, 1) << 20 << "" << false;
  269. QTest::newRow("valid12") << "MINPWM=hwmon1/fan2=35" << m_loader->pwmFan(1, 2) << 35 << "" << false;
  270. QTest::newRow("valid02") << "MINPWM=hwmon0/fan2=0" << m_loader->pwmFan(0, 2) << 0 << "" << false;
  271. QTest::newRow("valid11") << "MINPWM= hwmon1/fan1=40" << m_loader->pwmFan(1, 1) << 40 << "" << false;
  272. QTest::newRow("invalid02") << "MINPWM=hwmon0/fan2=256" << m_loader->pwmFan(0, 2) << 256 << "MinPwm cannot exceed 0-255!" << true;
  273. QTest::newRow("invalid11") << "MINPWM=hwmon1/fan2=-2" << m_loader->pwmFan(1, 2) << -2 << "-2 is not an unsigned integer!" << false;
  274. }
  275. void LoaderTest::parseMinpwmTest()
  276. {
  277. QFETCH(QString, config);
  278. QFETCH(PwmFan *, fan);
  279. QFETCH(int, result);
  280. QFETCH(QString, error);
  281. QFETCH(bool, critical);
  282. QSignalSpy spy(m_loader, SIGNAL(error(QString, bool)));
  283. m_loader->parse(config);
  284. QCOMPARE(spy.isEmpty(), error.isEmpty());
  285. if (spy.count())
  286. {
  287. QCOMPARE(spy.at(0).at(0).toString(), error);
  288. QCOMPARE(spy.at(0).at(1).toBool(), critical);
  289. }
  290. else
  291. QCOMPARE(fan->minPwm(), result);
  292. }
  293. void LoaderTest::parseMaxpwmTest_data()
  294. {
  295. QTest::addColumn<QString>("config");
  296. QTest::addColumn<PwmFan *>("fan");
  297. QTest::addColumn<int>("result");
  298. QTest::addColumn<QString>("error");
  299. QTest::addColumn<bool>("critical");
  300. QTest::newRow("valid01") << "MAXPWM=hwmon0/fan1=20" << m_loader->pwmFan(0, 1) << 20 << "" << false;
  301. QTest::newRow("valid12") << "MAXPWM=hwmon1/fan2=35 #uivnriuhgfdn" << m_loader->pwmFan(1, 2) << 35 << "" << false;
  302. QTest::newRow("valid02") << "MAXPWM=hwmon0/fan2=0" << m_loader->pwmFan(0, 2) << 0 << "" << false;
  303. QTest::newRow("valid11") << "MAXPWM= hwmon1/fan1=40" << m_loader->pwmFan(1, 1) << 40 << "" << false;
  304. QTest::newRow("invalid02") << "MAXPWM=hwmon0/fan2=256" << m_loader->pwmFan(0, 2) << 256 << "MaxPwm cannot exceed 0-255!" << true;
  305. QTest::newRow("invalid11") << "MAXPWM=hwmon1/fan2=-2" << m_loader->pwmFan(1, 2) << -2 << "-2 is not an unsigned integer!" << false;
  306. }
  307. void LoaderTest::parseMaxpwmTest()
  308. {
  309. QFETCH(QString, config);
  310. QFETCH(PwmFan *, fan);
  311. QFETCH(int, result);
  312. QFETCH(QString, error);
  313. QFETCH(bool, critical);
  314. QSignalSpy spy(m_loader, SIGNAL(error(QString, bool)));
  315. m_loader->parse(config);
  316. QCOMPARE(spy.isEmpty(), error.isEmpty());
  317. if (spy.count())
  318. {
  319. QCOMPARE(spy.at(0).at(0).toString(), error);
  320. QCOMPARE(spy.at(0).at(1).toBool(), critical);
  321. }
  322. else
  323. QCOMPARE(fan->maxPwm(), result);
  324. }
  325. void LoaderTest::parseUnrecognizableLineTest_data()
  326. {
  327. QTest::addColumn<QString>("config");
  328. QTest::addColumn<QString>("error");
  329. QTest::addColumn<bool>("critical");
  330. QTest::newRow("valid01") << "MMAXPWM=hwmon0/fan1=20" << "Unrecognized line in config: \'MMAXPWM=hwmon0/fan1=20\'" << true;
  331. }
  332. void LoaderTest::parseUnrecognizableLineTest()
  333. {
  334. QFETCH(QString, config);
  335. QFETCH(QString, error);
  336. QFETCH(bool, critical);
  337. QSignalSpy spy(m_loader, SIGNAL(error(QString, bool)));
  338. m_loader->parse(config);
  339. QCOMPARE(spy.count(), 1);
  340. if (spy.count())
  341. {
  342. QCOMPARE(spy.at(0).at(0).toString(), error);
  343. QCOMPARE(spy.at(0).at(1).toBool(), critical);
  344. }
  345. }
  346. void LoaderTest::getEntryNumberTest_data()
  347. {
  348. QTest::addColumn<QString>("entry");
  349. QTest::addColumn<QPair<uint, uint> >("numbers");
  350. QTest::newRow("valid01") << "hwmon6/device/pwm1" << QPair<uint, uint>(6, 1);
  351. QTest::newRow("valid0") << "hwmon2/pwm3" << QPair<uint, uint>(2, 3);
  352. }
  353. void LoaderTest::getEntryNumberTest()
  354. {
  355. typedef QPair<uint, uint> UPair;
  356. QFETCH(QString, entry);
  357. QFETCH(UPair, numbers);
  358. QCOMPARE(m_loader->getEntryNumbers(entry), numbers);
  359. }
  360. void LoaderTest::createConfigTest()
  361. {
  362. auto pwmFan = m_loader->pwmFan(0, 1);
  363. pwmFan->setTemp(m_loader->temp(1, 1));
  364. pwmFan->setMinTemp(20);
  365. pwmFan->setMaxTemp(60);
  366. pwmFan->setMaxPwm(200);
  367. pwmFan->setMinPwm(100);
  368. pwmFan->setMinStart(120);
  369. pwmFan->setMinStop(80);
  370. pwmFan = m_loader->pwmFan(1, 2);
  371. pwmFan->setTemp(m_loader->temp(1, 3));
  372. pwmFan->setMinTemp(30);
  373. pwmFan->setMaxTemp(70);
  374. pwmFan->setMaxPwm(255);
  375. pwmFan->setMinPwm(120);
  376. pwmFan->setMinStart(110);
  377. pwmFan->setMinStop(75);
  378. m_loader->setInterval(5);
  379. auto config = m_loader->createConfig();
  380. QString expectedConfig = "# This file was created by Fancontrol-GUI\n"
  381. "INTERVAL=5\n"
  382. "DEVPATH=hwmon0= hwmon1= \n"
  383. "DEVNAME=hwmon0=radeon hwmon1=coretemp \n"
  384. "FCTEMPS=hwmon0/pwm1=hwmon1/temp1_input hwmon1/pwm2=hwmon1/temp3_input \n"
  385. "FCFANS=hwmon0/pwm1=hwmon0/fan1_input hwmon1/pwm2=hwmon1/fan2_input \n"
  386. "MINTEMP=hwmon0/pwm1=20 hwmon1/pwm2=30 \n"
  387. "MAXTEMP=hwmon0/pwm1=60 hwmon1/pwm2=70 \n"
  388. "MINSTART=hwmon0/pwm1=120 hwmon1/pwm2=110 \n"
  389. "MINSTOP=hwmon0/pwm1=80 hwmon1/pwm2=75 \n"
  390. "MINPWM=hwmon0/pwm1=100 hwmon1/pwm2=120 \n"
  391. "MAXPWM=hwmon0/pwm1=200 hwmon1/pwm2=255 \n"
  392. "AVERAGE=hwmon0/pwm1=1 hwmon1/pwm2=1 \n";
  393. auto expectedLines = expectedConfig.split(QChar(QChar::LineFeed));
  394. auto configLines = config.split(QChar(QChar::LineFeed));
  395. QCOMPARE(configLines.size(), expectedLines.size());
  396. for (auto i=0; i<configLines.size(); i++)
  397. QCOMPARE(configLines.at(i), expectedLines.at(i));
  398. }
  399. QTEST_MAIN(LoaderTest);