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 <crypt.h> 11#include <errno.h> 12#include <string.h> 13#include <unistd.h> 14 15#include "CryptTest.h" 16 17#include <cppunit/TestCaller.h> 18#include <cppunit/TestSuite.h> 19 20 21#define PASSWORD "password" 22#define HASH_SALT "$s$12$101f2cf1a3b35aa671b8e006c6fb037e429d5b4ecb8dab16919097789e2d3a5f$ignorethis" 23#define HASH_RESULT "$s$12$101f2cf1a3b35aa671b8e006c6fb037e429d5b4ecb8dab16919097789e2d3a5f$4c5c886740871c447639e2dd5eeba004f22c0860ce88c811032ca6de6c95b23e" 24 25// This salt is only 31 bytes, while we need 32 bytes 26#define HASH_BAD_SALT "$s$12$101f2cf1a3b35aa671b8e006c6fb037e429d5b4ecb8dab16919097789e2d3a$ignorethis" 27 28#define LEGACY_SALT "1d" 29#define LEGACY_RESULT "1dVzQK99LSks6" 30 31#define BSD_SALT "_7C/.Bf/4" 32#define BSD_RESULT "_7C/.Bf/4gZk10RYRs4Y" 33 34 35CryptTest::CryptTest() 36{ 37} 38 39 40CryptTest::~CryptTest() 41{ 42} 43 44 45void 46CryptTest::setUp() 47{ 48} 49 50 51void 52CryptTest::tearDown() 53{ 54} 55 56 57void 58CryptTest::TestLegacy() 59{ 60 char* buf = crypt(PASSWORD, LEGACY_SALT); 61 CPPUNIT_ASSERT(buf != NULL); 62 CPPUNIT_ASSERT(strcmp(buf, LEGACY_RESULT) == 0); 63} 64 65 66void 67CryptTest::TestLegacyBSD() 68{ 69 char* buf = crypt(PASSWORD, BSD_SALT); 70 CPPUNIT_ASSERT(buf != NULL); 71 CPPUNIT_ASSERT(strcmp(buf, BSD_RESULT) == 0); 72} 73 74 75void 76CryptTest::TestCustomSalt() 77{ 78 char* buf = crypt(PASSWORD, HASH_SALT); 79 CPPUNIT_ASSERT(buf != NULL); 80 CPPUNIT_ASSERT(strcmp(buf, HASH_RESULT) == 0); 81} 82 83 84void 85CryptTest::TestSaltGeneration() 86{ 87 char tmp[200]; 88 89 char* buf = crypt(PASSWORD, NULL); 90 CPPUNIT_ASSERT(buf != NULL); 91 strlcpy(tmp, buf, sizeof(tmp)); 92 buf = crypt(PASSWORD, tmp); 93 CPPUNIT_ASSERT(strcmp(buf, tmp) == 0); 94} 95 96 97void 98CryptTest::TestBadSalt() 99{ 100 errno = 0; 101 CPPUNIT_ASSERT(crypt(PASSWORD, HASH_BAD_SALT) == NULL); 102 CPPUNIT_ASSERT(errno == EINVAL); 103} 104 105 106void 107CryptTest::TestCryptR() 108{ 109 char tmp[200]; 110 111 struct crypt_data data; 112 data.initialized = 0; 113 114 char* buf = crypt_r(PASSWORD, NULL, &data); 115 CPPUNIT_ASSERT(buf != NULL); 116 strlcpy(tmp, buf, sizeof(tmp)); 117 buf = crypt(PASSWORD, tmp); 118 CPPUNIT_ASSERT(strcmp(buf, tmp) == 0); 119} 120 121 122void 123CryptTest::AddTests(BTestSuite& parent) 124{ 125 CppUnit::TestSuite& suite = *new CppUnit::TestSuite("CryptTest"); 126 suite.addTest(new CppUnit::TestCaller<CryptTest>( 127 "CryptTest::TestLegacy", 128 &CryptTest::TestLegacy)); 129 suite.addTest(new CppUnit::TestCaller<CryptTest>( 130 "CryptTest::TestLegacyBSD", 131 &CryptTest::TestLegacyBSD)); 132 suite.addTest(new CppUnit::TestCaller<CryptTest>( 133 "CryptTest::TestCustomSalt", 134 &CryptTest::TestCustomSalt)); 135 suite.addTest(new CppUnit::TestCaller<CryptTest>( 136 "CryptTest::TestSaltGeneration", 137 &CryptTest::TestSaltGeneration)); 138 suite.addTest(new CppUnit::TestCaller<CryptTest>( 139 "CryptTest::TestBadSalt", 140 &CryptTest::TestBadSalt)); 141 suite.addTest(new CppUnit::TestCaller<CryptTest>( 142 "CryptTest::TestCryptR", 143 &CryptTest::TestCryptR)); 144 parent.addTest("CryptTest", &suite); 145} 146