1 /* 2 * Copyright 2017, Haiku, Inc. All Rights Reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Andrew Aldridge, i80and@foxquill.com 7 */ 8 9 10 #include <errno.h> 11 #include <string.h> 12 #include <unistd.h> 13 14 #include "CryptTest.h" 15 16 #include <cppunit/TestCaller.h> 17 #include <cppunit/TestSuite.h> 18 19 20 #define PASSWORD "password" 21 #define HASH_SALT "$s$12$101f2cf1a3b35aa671b8e006c6fb037e429d5b4ecb8dab16919097789e2d3a5f$ignorethis" 22 #define HASH_RESULT "$s$12$101f2cf1a3b35aa671b8e006c6fb037e429d5b4ecb8dab16919097789e2d3a5f$4c5c886740871c447639e2dd5eeba004f22c0860ce88c811032ca6de6c95b23e" 23 24 // This salt is only 31 bytes, while we need 32 bytes 25 #define HASH_BAD_SALT "$s$12$101f2cf1a3b35aa671b8e006c6fb037e429d5b4ecb8dab16919097789e2d3a$ignorethis" 26 27 28 CryptTest::CryptTest() 29 { 30 } 31 32 33 CryptTest::~CryptTest() 34 { 35 } 36 37 38 void 39 CryptTest::setUp() 40 { 41 } 42 43 44 void 45 CryptTest::tearDown() 46 { 47 } 48 49 50 void 51 CryptTest::TestLegacy() 52 { 53 char* buf = crypt(PASSWORD, "1d"); 54 CPPUNIT_ASSERT(buf != NULL); 55 CPPUNIT_ASSERT(strcmp(buf, "1dVzQK99LSks6") == 0); 56 } 57 58 59 void 60 CryptTest::TestCustomSalt() 61 { 62 char* buf = crypt(PASSWORD, HASH_SALT); 63 CPPUNIT_ASSERT(buf != NULL); 64 CPPUNIT_ASSERT(strcmp(buf, HASH_RESULT) == 0); 65 } 66 67 68 void 69 CryptTest::TestSaltGeneration() 70 { 71 char tmp[200]; 72 73 char* buf = crypt(PASSWORD, NULL); 74 CPPUNIT_ASSERT(buf != NULL); 75 strlcpy(tmp, buf, sizeof(tmp)); 76 buf = crypt(PASSWORD, tmp); 77 CPPUNIT_ASSERT(strcmp(buf, tmp) == 0); 78 } 79 80 81 void 82 CryptTest::TestBadSalt() 83 { 84 errno = 0; 85 CPPUNIT_ASSERT(crypt(PASSWORD, HASH_BAD_SALT) == NULL); 86 CPPUNIT_ASSERT(errno == EINVAL); 87 } 88 89 90 void 91 CryptTest::AddTests(BTestSuite& parent) 92 { 93 CppUnit::TestSuite& suite = *new CppUnit::TestSuite("CryptTest"); 94 suite.addTest(new CppUnit::TestCaller<CryptTest>( 95 "CryptTest::TestLegacy", 96 &CryptTest::TestLegacy)); 97 suite.addTest(new CppUnit::TestCaller<CryptTest>( 98 "CryptTest::TestCustomSalt", 99 &CryptTest::TestCustomSalt)); 100 suite.addTest(new CppUnit::TestCaller<CryptTest>( 101 "CryptTest::TestSaltGeneration", 102 &CryptTest::TestSaltGeneration)); 103 suite.addTest(new CppUnit::TestCaller<CryptTest>( 104 "CryptTest::TestBadSalt", 105 &CryptTest::TestBadSalt)); 106 parent.addTest("CryptTest", &suite); 107 } 108