2016-02-06 14:52:32 +01:00
|
|
|
#include <platform.h>
|
|
|
|
#include "password.h"
|
2016-02-13 07:50:06 +01:00
|
|
|
#include <CuTest.h>
|
2016-02-13 13:56:49 +01:00
|
|
|
#include <string.h>
|
2016-02-06 14:52:32 +01:00
|
|
|
|
|
|
|
static void test_passwords(CuTest *tc) {
|
2016-02-12 07:14:12 +01:00
|
|
|
const char *hash, *expect;
|
|
|
|
expect = "$apr1$FqQLkl8g$.icQqaDJpim4BVy.Ho5660";
|
2016-02-25 10:46:46 +01:00
|
|
|
if (password_is_implemented(PASSWORD_APACHE_MD5)) {
|
|
|
|
CuAssertIntEquals(tc, VERIFY_OK, password_verify(expect, "Hodor"));
|
|
|
|
hash = password_encode("Hodor", PASSWORD_APACHE_MD5);
|
|
|
|
CuAssertPtrNotNull(tc, hash);
|
|
|
|
CuAssertIntEquals(tc, 0, strncmp(hash, expect, 6));
|
|
|
|
} else {
|
|
|
|
CuAssertIntEquals(tc, VERIFY_UNKNOWN, password_verify(expect, "Hodor"));
|
|
|
|
}
|
2016-02-06 14:52:32 +01:00
|
|
|
|
2016-02-12 07:14:12 +01:00
|
|
|
expect = "$1$ZouUn04i$yNnT1Oy8azJ5V.UM9ppP5/";
|
2016-02-25 10:46:46 +01:00
|
|
|
if (password_is_implemented(PASSWORD_MD5)) {
|
|
|
|
CuAssertIntEquals(tc, VERIFY_OK, password_verify(expect, "jollygood"));
|
|
|
|
hash = password_encode("jollygood", PASSWORD_MD5);
|
|
|
|
CuAssertPtrNotNull(tc, hash);
|
|
|
|
CuAssertIntEquals(tc, 0, strncmp(hash, expect, 3));
|
|
|
|
} else {
|
|
|
|
CuAssertIntEquals(tc, VERIFY_UNKNOWN, password_verify(expect, "jollygood"));
|
|
|
|
}
|
2016-02-06 14:52:32 +01:00
|
|
|
|
2016-02-21 17:22:43 +01:00
|
|
|
expect = "password";
|
2016-02-25 10:46:46 +01:00
|
|
|
if (password_is_implemented(PASSWORD_PLAINTEXT)) {
|
|
|
|
hash = password_encode("password", PASSWORD_PLAINTEXT);
|
|
|
|
CuAssertPtrNotNull(tc, hash);
|
|
|
|
CuAssertStrEquals(tc, hash, expect);
|
|
|
|
CuAssertIntEquals(tc, VERIFY_OK, password_verify(expect, "password"));
|
|
|
|
CuAssertIntEquals(tc, VERIFY_FAIL, password_verify(expect, "arseword"));
|
|
|
|
} else {
|
|
|
|
CuAssertIntEquals(tc, VERIFY_UNKNOWN, password_verify(expect, "password"));
|
|
|
|
}
|
|
|
|
|
2016-02-21 17:22:43 +01:00
|
|
|
expect = "$0$password";
|
2016-02-25 10:46:46 +01:00
|
|
|
if (password_is_implemented(PASSWORD_NOCRYPT)) {
|
|
|
|
hash = password_encode("password", PASSWORD_NOCRYPT);
|
|
|
|
CuAssertPtrNotNull(tc, hash);
|
|
|
|
CuAssertStrEquals(tc, hash, expect);
|
|
|
|
CuAssertIntEquals(tc, VERIFY_OK, password_verify(expect, "password"));
|
|
|
|
CuAssertIntEquals(tc, VERIFY_FAIL, password_verify(expect, "arseword"));
|
|
|
|
} else {
|
|
|
|
CuAssertIntEquals(tc, VERIFY_UNKNOWN, password_verify(expect, "password"));
|
|
|
|
}
|
2016-02-12 07:14:12 +01:00
|
|
|
|
|
|
|
expect = "$2y$05$RJ8qAhu.foXyJLdc2eHTLOaK4MDYn3/v4HtOVCq0Plv2yxcrEB7Wm";
|
2016-02-25 10:46:46 +01:00
|
|
|
if (password_is_implemented(PASSWORD_BCRYPT)) {
|
|
|
|
CuAssertIntEquals(tc, VERIFY_OK, password_verify(expect, "Hodor"));
|
|
|
|
hash = password_encode("Hodor", PASSWORD_BCRYPT);
|
|
|
|
CuAssertPtrNotNull(tc, hash);
|
|
|
|
CuAssertIntEquals(tc, 0, strncmp(hash, expect, 7));
|
|
|
|
} else {
|
|
|
|
CuAssertIntEquals(tc, VERIFY_UNKNOWN, password_verify(expect, "Hodor"));
|
|
|
|
}
|
|
|
|
|
2016-02-21 17:22:43 +01:00
|
|
|
CuAssertIntEquals(tc, VERIFY_UNKNOWN, password_verify("$9$saltyfish$password", "password"));
|
2016-02-06 14:52:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CuSuite *get_password_suite(void) {
|
|
|
|
CuSuite *suite = CuSuiteNew();
|
|
|
|
SUITE_ADD_TEST(suite, test_passwords);
|
|
|
|
return suite;
|
|
|
|
}
|