xref: /haiku/src/tests/system/libroot/posix/CryptTest.cpp (revision 125b262675217084e0c59014b4a98f724f1c4fb3)
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 
29 CryptTest::CryptTest()
30 {
31 }
32 
33 
34 CryptTest::~CryptTest()
35 {
36 }
37 
38 
39 void
40 CryptTest::setUp()
41 {
42 }
43 
44 
45 void
46 CryptTest::tearDown()
47 {
48 }
49 
50 
51 void
52 CryptTest::TestLegacy()
53 {
54 	char* buf = crypt(PASSWORD, "1d");
55 	CPPUNIT_ASSERT(buf != NULL);
56 	CPPUNIT_ASSERT(strcmp(buf, "1dVzQK99LSks6") == 0);
57 }
58 
59 
60 void
61 CryptTest::TestCustomSalt()
62 {
63 	char* buf = crypt(PASSWORD, HASH_SALT);
64 	CPPUNIT_ASSERT(buf != NULL);
65 	CPPUNIT_ASSERT(strcmp(buf, HASH_RESULT) == 0);
66 }
67 
68 
69 void
70 CryptTest::TestSaltGeneration()
71 {
72 	char tmp[200];
73 
74 	char* buf = crypt(PASSWORD, NULL);
75 	CPPUNIT_ASSERT(buf != NULL);
76 	strlcpy(tmp, buf, sizeof(tmp));
77 	buf = crypt(PASSWORD, tmp);
78 	CPPUNIT_ASSERT(strcmp(buf, tmp) == 0);
79 }
80 
81 
82 void
83 CryptTest::TestBadSalt()
84 {
85 	errno = 0;
86 	CPPUNIT_ASSERT(crypt(PASSWORD, HASH_BAD_SALT) == NULL);
87 	CPPUNIT_ASSERT(errno == EINVAL);
88 }
89 
90 
91 void
92 CryptTest::TestCryptR()
93 {
94 	char tmp[200];
95 
96 	struct crypt_data data;
97 	data.initialized = 0;
98 
99 	char* buf = crypt_r(PASSWORD, NULL, &data);
100 	CPPUNIT_ASSERT(buf != NULL);
101 	strlcpy(tmp, buf, sizeof(tmp));
102 	buf = crypt(PASSWORD, tmp);
103 	CPPUNIT_ASSERT(strcmp(buf, tmp) == 0);
104 }
105 
106 
107 void
108 CryptTest::AddTests(BTestSuite& parent)
109 {
110 	CppUnit::TestSuite& suite = *new CppUnit::TestSuite("CryptTest");
111 	suite.addTest(new CppUnit::TestCaller<CryptTest>(
112 		"CryptTest::TestLegacy",
113 		&CryptTest::TestLegacy));
114 	suite.addTest(new CppUnit::TestCaller<CryptTest>(
115 		"CryptTest::TestCustomSalt",
116 		&CryptTest::TestCustomSalt));
117 	suite.addTest(new CppUnit::TestCaller<CryptTest>(
118 		"CryptTest::TestSaltGeneration",
119 		&CryptTest::TestSaltGeneration));
120 	suite.addTest(new CppUnit::TestCaller<CryptTest>(
121 		"CryptTest::TestBadSalt",
122 		&CryptTest::TestBadSalt));
123 	suite.addTest(new CppUnit::TestCaller<CryptTest>(
124 		"CryptTest::TestCryptR",
125 		&CryptTest::TestCryptR));
126 	parent.addTest("CryptTest", &suite);
127 }
128