PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/fwbuilder-4.1.2/src/gui/unit_tests/CustomServiceDialogTest/CustomServiceDialogTest.cpp

#
C++ | 203 lines | 149 code | 22 blank | 32 comment | 21 complexity | 98fcba50bfe683fd152f5130bbd89fad MD5 | raw file
Possible License(s): GPL-2.0
  1. /*
  2. Firewall Builder
  3. Copyright (C) 2010 NetCitadel, LLC
  4. Author: Roman Bovsunivskiy a2k0001@gmail.com
  5. $Id: CustomServiceDialogTest.cpp 2723 2010-03-16 17:32:18Z a2k $
  6. This program is free software which we release under the GNU General Public
  7. License. You may redistribute and/or modify this program under the terms
  8. of that license as published by the Free Software Foundation; either
  9. version 2 of the License, or (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. To get a copy of the GNU General Public License, write to the Free Software
  15. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. */
  17. #include "CustomServiceDialogTest.h"
  18. #include "../../../../config.h"
  19. //#include "../../global.h"
  20. #include <qapplication.h>
  21. #include <qfile.h>
  22. #include <qtextstream.h>
  23. #include <QTest>
  24. #include <iostream>
  25. #include <QProcess>
  26. #include <QRegExp>
  27. #include <QDebug>
  28. #include <QToolButton>
  29. #include <QMessageBox>
  30. #include <QWidget>
  31. #include <QLineEdit>
  32. #include <QRadioButton>
  33. #include "FWWindow.h"
  34. #include "ProjectPanel.h"
  35. #include "ObjectTreeView.h"
  36. #include "ObjectTreeViewItem.h"
  37. #include "ObjectEditor.h"
  38. #include "FWObjectClipboard.h"
  39. #include "TextEditWidget.h"
  40. #include "fwbuilder/Address.h"
  41. #include "fwbuilder/IPv4.h"
  42. #include "fwbuilder/IPv6.h"
  43. #include "FWBTree.h"
  44. #include "fwbuilder/Library.h"
  45. #include "fwbuilder/FWObjectDatabase.h"
  46. #include "CustomServiceDialogTest.h"
  47. #include "StartTipDialog.h"
  48. #include "fwbuilder/FWObjectDatabase.h"
  49. #include "fwbuilder/Interface.h"
  50. #include "IPServiceDialog.h"
  51. #include "fwbuilder/IPService.h"
  52. #include "fwbuilder/CustomService.h"
  53. #include "CustomServiceDialog.h"
  54. #include "platforms.h"
  55. using namespace std;
  56. using namespace libfwbuilder;
  57. void CustomServiceDialogTest::initTestCase()
  58. {
  59. new FWObjectClipboard();
  60. mw = new FWWindow();
  61. mw->show();
  62. mw->startupLoad();
  63. StartTipDialog *d = mw->findChild<StartTipDialog*>();
  64. if (d!=NULL) d->close();
  65. om = dynamic_cast<ObjectManipulator*>(mw->getCurrentObjectTree()->parent()->parent());
  66. QTest::qWait(1000);
  67. }
  68. Library* CustomServiceDialogTest::findUserLibrary()
  69. {
  70. Library *lib = NULL;
  71. foreach (FWObject *obj, mw->db()->getByType(Library::TYPENAME))
  72. {
  73. if (obj->getName() == "User")
  74. {
  75. lib = Library::cast(obj);
  76. break;
  77. }
  78. }
  79. return lib;
  80. }
  81. void CustomServiceDialogTest::selectComboItem(QWidget *widget, QString name)
  82. {
  83. QComboBox * combo = dynamic_cast<QComboBox*>(widget);
  84. Q_ASSERT(combo != NULL);
  85. int id = combo->findText(name);
  86. combo->setCurrentIndex(id);
  87. }
  88. void CustomServiceDialogTest::testDialog()
  89. {
  90. CustomService *service = CustomService::cast(om->createObject(FWBTree().getStandardSlotForObject(findUserLibrary(), CustomService::TYPENAME), CustomService::TYPENAME, "testCustomService"));
  91. om->editObject(service);
  92. CustomServiceDialog *dialog = mw->findChild<CustomServiceDialog*>("w_CustomServiceDialog");
  93. QLineEdit *obj_name = dialog->findChild<QLineEdit*>("obj_name");
  94. QComboBox *platform = dialog->findChild<QComboBox*>("platform");
  95. QLineEdit *code = dialog->findChild<QLineEdit*>("code");
  96. QComboBox *protocol = dialog->findChild<QComboBox*>("protocol");
  97. QRadioButton *ipv4 = dialog->findChild<QRadioButton*>("ipv4");
  98. QRadioButton *ipv6 = dialog->findChild<QRadioButton*>("ipv6");
  99. TextEditWidget *comment = dialog->findChild<TextEditWidget*>("comment");
  100. obj_name->clear();
  101. QTest::keyClicks(obj_name, "TestCustomService");
  102. QTest::keyClick(obj_name, Qt::Key_Enter);
  103. QVERIFY(service->getName() == "TestCustomService");
  104. comment->clear();
  105. QTest::mouseClick(comment, Qt::LeftButton);
  106. QTest::keyClicks(comment, "Test comment");
  107. QTest::mouseClick(comment, Qt::LeftButton);
  108. QTest::keyClick(comment, Qt::Key_Tab);
  109. QTest::qWait(100);
  110. QVERIFY (service->getComment() == "Test comment");
  111. // testing saving platform code to service object
  112. selectComboItem(platform, "iptables");
  113. dialog->platformChanged();
  114. QTest::keyClicks(code, "code for iptables");
  115. QTest::keyClick(code, Qt::Key_Tab);
  116. QTest::keyClick(code, Qt::Key_Enter);
  117. QTest::qWait(100);
  118. QVERIFY(service->getCodeForPlatform("iptables") == "code for iptables");
  119. // platform code should clear when we change platform to one that currently does not have code
  120. selectComboItem(platform, "PF");
  121. dialog->platformChanged();
  122. QVERIFY(code->text().isEmpty());
  123. // platform code for iptables should not change when setting code for PF or changing platform
  124. QVERIFY(service->getCodeForPlatform("iptables") == "code for iptables");
  125. QTest::keyClicks(code, "code for pf");
  126. QTest::keyClick(code, Qt::Key_Enter);
  127. QVERIFY(service->getCodeForPlatform("iptables") == "code for iptables");
  128. QVERIFY(service->getCodeForPlatform("pf") == "code for pf");
  129. // testing saving platform code to service object for all platforms
  130. QMap<QString, QString> platforms = getAllPlatforms();
  131. foreach (QString key, platforms.keys())
  132. {
  133. selectComboItem(platform, platforms[key]);
  134. dialog->platformChanged();
  135. code->clear();
  136. QTest::keyClicks(code, "code for "+key);
  137. QTest::keyClick(code, Qt::Key_Tab);
  138. QTest::keyClick(code, Qt::Key_Enter);
  139. QTest::qWait(100);
  140. QVERIFY2(service->getCodeForPlatform(key.toStdString().c_str()) == (string("code for ")+key.toStdString()),
  141. (string("failed for platform ") + key.toStdString()).c_str());
  142. }
  143. // testing changing protocol to tcp
  144. selectComboItem(platform, "iptables");
  145. dialog->platformChanged();
  146. selectComboItem(protocol, "tcp");
  147. QVERIFY(service->getProtocol() == "tcp");
  148. selectComboItem(platform, "pf");
  149. dialog->platformChanged();
  150. QVERIFY(service->getProtocol() == "tcp");
  151. // testing saving address family to service object
  152. selectComboItem(platform, "iptables");
  153. dialog->platformChanged();
  154. QTest::mouseClick(ipv6, Qt::LeftButton, Qt::NoModifier, QPoint(10,10));
  155. QVERIFY(service->getAddressFamily() == 10);
  156. selectComboItem(platform, "PF");
  157. dialog->platformChanged();
  158. QVERIFY(service->getAddressFamily() == 10);
  159. // testing that changing address family does not change platform code
  160. foreach (QString key, platforms.keys())
  161. {
  162. string oldcode = service->getCodeForPlatform(key.toStdString().c_str());
  163. string oldprotocol = service->getProtocol();
  164. QTest::mouseClick(ipv4, Qt::LeftButton, Qt::NoModifier, QPoint(10,10));
  165. QVERIFY(service->getAddressFamily() == 2);
  166. QVERIFY(oldcode == service->getCodeForPlatform(key.toStdString().c_str()));
  167. QVERIFY(oldprotocol == service->getProtocol());
  168. QTest::mouseClick(ipv6, Qt::LeftButton, Qt::NoModifier, QPoint(10,10));
  169. QVERIFY(service->getAddressFamily() == 10);
  170. QVERIFY(oldcode == service->getCodeForPlatform(key.toStdString().c_str()));
  171. QVERIFY(oldprotocol == service->getProtocol());
  172. }
  173. }