2 * Copyright 2017, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
6 * Andrew Aldridge, i80and@foxquill.com
14 #include "CryptTest.h"
16 #include <cppunit/TestCaller.h>
17 #include <cppunit/TestSuite.h>
20 #define PASSWORD "password"
21 #define HASH_SALT "$s$12$101f2cf1a3b35aa671b8e006c6fb037e429d5b4ecb8dab16919097789e2d3a5f$ignorethis"
22 #define HASH_RESULT "$s$12$101f2cf1a3b35aa671b8e006c6fb037e429d5b4ecb8dab16919097789e2d3a5f$4c5c886740871c447639e2dd5eeba004f22c0860ce88c811032ca6de6c95b23e"
24 // This salt is only 31 bytes, while we need 32 bytes
25 #define HASH_BAD_SALT "$s$12$101f2cf1a3b35aa671b8e006c6fb037e429d5b4ecb8dab16919097789e2d3a$ignorethis"
28 CryptTest::CryptTest()
33 CryptTest::~CryptTest()
51 CryptTest::TestLegacy()
53 char* buf
= crypt(PASSWORD
, "1d");
54 CPPUNIT_ASSERT(buf
!= NULL
);
55 CPPUNIT_ASSERT(strcmp(buf
, "1dVzQK99LSks6") == 0);
60 CryptTest::TestCustomSalt()
62 char* buf
= crypt(PASSWORD
, HASH_SALT
);
63 CPPUNIT_ASSERT(buf
!= NULL
);
64 CPPUNIT_ASSERT(strcmp(buf
, HASH_RESULT
) == 0);
69 CryptTest::TestSaltGeneration()
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);
82 CryptTest::TestBadSalt()
85 CPPUNIT_ASSERT(crypt(PASSWORD
, HASH_BAD_SALT
) == NULL
);
86 CPPUNIT_ASSERT(errno
== EINVAL
);
91 CryptTest::AddTests(BTestSuite
& parent
)
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
);