repository_infos: Enable automatic updates on the main Haiku repostiory.
[haiku.git] / src / tests / system / libroot / posix / CryptTest.cpp
blobc290757c275eacc545fbc0088fa3ef1f67d49ecd
1 /*
2 * Copyright 2017, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Andrew Aldridge, i80and@foxquill.com
7 */
10 #include <errno.h>
11 #include <string.h>
12 #include <unistd.h>
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()
38 void
39 CryptTest::setUp()
44 void
45 CryptTest::tearDown()
50 void
51 CryptTest::TestLegacy()
53 char* buf = crypt(PASSWORD, "1d");
54 CPPUNIT_ASSERT(buf != NULL);
55 CPPUNIT_ASSERT(strcmp(buf, "1dVzQK99LSks6") == 0);
59 void
60 CryptTest::TestCustomSalt()
62 char* buf = crypt(PASSWORD, HASH_SALT);
63 CPPUNIT_ASSERT(buf != NULL);
64 CPPUNIT_ASSERT(strcmp(buf, HASH_RESULT) == 0);
68 void
69 CryptTest::TestSaltGeneration()
71 char tmp[200];
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);
81 void
82 CryptTest::TestBadSalt()
84 errno = 0;
85 CPPUNIT_ASSERT(crypt(PASSWORD, HASH_BAD_SALT) == NULL);
86 CPPUNIT_ASSERT(errno == EINVAL);
90 void
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);