1 /* 2 * Copyright 2019, Andrew Lindesay <apl@lindesay.co.nz>. 3 * All rights reserved. Distributed under the terms of the MIT License. 4 */ 5 6 #include "ValidationUtilsTest.h" 7 8 #include <cppunit/TestCaller.h> 9 #include <cppunit/TestSuite.h> 10 11 #include "ValidationUtils.h" 12 13 14 ValidationUtilsTest::ValidationUtilsTest() 15 { 16 } 17 18 19 ValidationUtilsTest::~ValidationUtilsTest() 20 { 21 } 22 23 24 void 25 ValidationUtilsTest::TestEmailValid() 26 { 27 BString email("weta@example.com"); 28 29 // ---------------------- 30 bool result = ValidationUtils::IsValidEmail(email); 31 // ---------------------- 32 33 CPPUNIT_ASSERT_EQUAL_MESSAGE("!Email valid", true, result); 34 } 35 36 37 void TestEmailInvalidNoAt(); 38 void TestEmailInvalidNoMailbox(); 39 void TestEmailInvalidNoDomain(); 40 void TestEmailInvalidTwoAts(); 41 42 void 43 ValidationUtilsTest::TestEmailInvalidNoAt() 44 { 45 BString email("wetaexample.com"); 46 47 // ---------------------- 48 bool result = ValidationUtils::IsValidEmail(email); 49 // ---------------------- 50 51 CPPUNIT_ASSERT_EQUAL_MESSAGE("!Email invalid - no @", false, result); 52 } 53 54 55 void 56 ValidationUtilsTest::TestEmailInvalidNoMailbox() 57 { 58 BString email("@example.com"); 59 60 // ---------------------- 61 bool result = ValidationUtils::IsValidEmail(email); 62 // ---------------------- 63 64 CPPUNIT_ASSERT_EQUAL_MESSAGE("!Email invalid - no mailbox", false, result); 65 } 66 67 68 void 69 ValidationUtilsTest::TestEmailInvalidNoDomain() 70 { 71 BString email("fredric@"); 72 73 // ---------------------- 74 bool result = ValidationUtils::IsValidEmail(email); 75 // ---------------------- 76 77 CPPUNIT_ASSERT_EQUAL_MESSAGE("!Email invalid - no domain", false, result); 78 } 79 80 81 void 82 ValidationUtilsTest::TestNicknameValid() 83 { 84 BString nickname("erik55"); 85 86 // ---------------------- 87 bool result = ValidationUtils::IsValidNickname(nickname); 88 // ---------------------- 89 90 CPPUNIT_ASSERT_EQUAL_MESSAGE("!Nickname valid", true, result); 91 } 92 93 94 void 95 ValidationUtilsTest::TestNicknameInvalid() 96 { 97 BString nickname("not a Nickname!"); 98 99 // ---------------------- 100 bool result = ValidationUtils::IsValidNickname(nickname); 101 // ---------------------- 102 103 CPPUNIT_ASSERT_EQUAL_MESSAGE("!Nickname invalid", false, result); 104 } 105 106 107 void 108 ValidationUtilsTest::TestNicknameInvalidBadChars() 109 { 110 BString nickname("erik!!10"); 111 112 // ---------------------- 113 bool result = ValidationUtils::IsValidNickname(nickname); 114 // ---------------------- 115 116 CPPUNIT_ASSERT_EQUAL_MESSAGE("!Nickname invalid (bad chars)", 117 false, result); 118 } 119 120 121 void 122 ValidationUtilsTest::TestPasswordClearValid() 123 { 124 BString passwordClear("P4NhelQoad4"); 125 126 // ---------------------- 127 bool result = ValidationUtils::IsValidPasswordClear(passwordClear); 128 // ---------------------- 129 130 CPPUNIT_ASSERT_EQUAL_MESSAGE("!Password clear valid", true, result); 131 } 132 133 134 void 135 ValidationUtilsTest::TestPasswordClearInvalid() 136 { 137 BString passwordClear("only has lower case letters"); 138 // needs some numbers / upper case characters in there too 139 140 // ---------------------- 141 bool result = ValidationUtils::IsValidPasswordClear(passwordClear); 142 // ---------------------- 143 144 CPPUNIT_ASSERT_EQUAL_MESSAGE("!Password clear invalid", false, result); 145 } 146 147 148 /*static*/ void 149 ValidationUtilsTest::AddTests(BTestSuite& parent) 150 { 151 CppUnit::TestSuite& suite = *new CppUnit::TestSuite( 152 "ValidationUtilsTest"); 153 154 suite.addTest( 155 new CppUnit::TestCaller<ValidationUtilsTest>( 156 "ValidationUtilsTest::TestEmailValid", 157 &ValidationUtilsTest::TestEmailValid)); 158 suite.addTest( 159 new CppUnit::TestCaller<ValidationUtilsTest>( 160 "ValidationUtilsTest::TestNicknameInvalid", 161 &ValidationUtilsTest::TestNicknameInvalid)); 162 suite.addTest( 163 new CppUnit::TestCaller<ValidationUtilsTest>( 164 "ValidationUtilsTest::TestNicknameValid", 165 &ValidationUtilsTest::TestNicknameValid)); 166 suite.addTest( 167 new CppUnit::TestCaller<ValidationUtilsTest>( 168 "ValidationUtilsTest::TestNicknameInvalidBadChars", 169 &ValidationUtilsTest::TestNicknameInvalidBadChars)); 170 suite.addTest( 171 new CppUnit::TestCaller<ValidationUtilsTest>( 172 "ValidationUtilsTest::TestPasswordClearInvalid", 173 &ValidationUtilsTest::TestPasswordClearInvalid)); 174 suite.addTest( 175 new CppUnit::TestCaller<ValidationUtilsTest>( 176 "ValidationUtilsTest::TestPasswordClearValid", 177 &ValidationUtilsTest::TestPasswordClearValid)); 178 179 parent.addTest("ValidationUtilsTest", &suite); 180 } 181