Greenbone Vulnerability Management Libraries  22.8.0
passwordbasedauthentication_tests.c File Reference
#include "authutils.h"
#include "passwordbasedauthentication.c"
#include <cgreen/cgreen.h>
#include <cgreen/mocks.h>
#include <string.h>
Include dependency graph for passwordbasedauthentication_tests.c:

Go to the source code of this file.

Functions

 Describe (PBA)
 
 BeforeEach (PBA)
 
 AfterEach (PBA)
 
 Ensure (PBA, returns_false_on_not_phc_compliant_setting)
 
 Ensure (PBA, returns_true_on_phc_compliant_setting)
 
 Ensure (PBA, returns_NULL_on_unsupport_settings)
 
 Ensure (PBA, unique_hash_without_adding_used_pepper)
 
 Ensure (PBA, verify_hash)
 
 Ensure (PBA, verify_hash_returns_invalid_on_np_hash_np_password)
 
 Ensure (PBA, defaults)
 
 Ensure (PBA, initialization)
 
 Ensure (PBA, handle_md5_hash)
 
int main (int argc, char **argv)
 

Function Documentation

◆ AfterEach()

AfterEach ( PBA  )

Definition at line 16 of file passwordbasedauthentication_tests.c.

17 {
18 }

◆ BeforeEach()

BeforeEach ( PBA  )

Definition at line 13 of file passwordbasedauthentication_tests.c.

14 {
15 }

◆ Describe()

Describe ( PBA  )

◆ Ensure() [1/9]

Ensure ( PBA  ,
defaults   
)

Definition at line 74 of file passwordbasedauthentication_tests.c.

75 {
76  int i;
77  struct PBASettings *settings = pba_init (NULL, 0, 0, NULL);
78  assert_equal (settings->count, 20000);
79  for (i = 0; i < MAX_PEPPER_SIZE; i++)
80  assert_equal_with_message (settings->pepper[i], 0,
81  "init_without_pepper_should_not_have_pepper");
82  assert_string_equal (settings->prefix, "$6$");
83  pba_finalize (settings);
84 }

References PBASettings::count, MAX_PEPPER_SIZE, pba_finalize(), pba_init(), PBASettings::pepper, and PBASettings::prefix.

Here is the call graph for this function:

◆ Ensure() [2/9]

Ensure ( PBA  ,
handle_md5_hash   
)

Definition at line 105 of file passwordbasedauthentication_tests.c.

106 {
107  struct PBASettings *settings = pba_init (NULL, 0, 0, NULL);
108  char *hash;
109  assert_equal (gvm_auth_init (), 0);
110  hash = get_password_hashes ("admin");
111  assert_equal (pba_verify_hash (settings, hash, "admin"), UPDATE_RECOMMENDED);
112  pba_finalize (settings);
113 }

References get_password_hashes(), gvm_auth_init(), pba_finalize(), pba_init(), pba_verify_hash(), and UPDATE_RECOMMENDED.

Here is the call graph for this function:

◆ Ensure() [3/9]

Ensure ( PBA  ,
initialization   
)

Definition at line 85 of file passwordbasedauthentication_tests.c.

86 {
87  int i;
88  struct PBASettings *settings = pba_init ("444", 3, 1, "$6$");
89  assert_equal (settings->count, 1);
90  for (i = 0; i < MAX_PEPPER_SIZE - 1; i++)
91  assert_equal_with_message (settings->pepper[i], '4',
92  "init_with_pepper_should_be_set");
93  assert_equal_with_message (settings->pepper[MAX_PEPPER_SIZE - 1], '\0',
94  "last_pepper_should_be_unset_by_pepper_3");
95  assert_string_equal (settings->prefix, "$6$");
96  pba_finalize (settings);
97  settings = pba_init ("444", MAX_PEPPER_SIZE + 1, 1, "$6$");
98  assert_equal_with_message (settings, NULL,
99  "should_fail_due_to_too_much_pepper");
100  settings = pba_init ("444", MAX_PEPPER_SIZE, 1, "$WALDFEE$");
101  assert_equal_with_message (settings, NULL,
102  "should_fail_due_to_unknown_prefix");
103 }

References PBASettings::count, MAX_PEPPER_SIZE, pba_finalize(), pba_init(), PBASettings::pepper, and PBASettings::prefix.

Here is the call graph for this function:

◆ Ensure() [4/9]

Ensure ( PBA  ,
returns_false_on_not_phc_compliant_setting   
)

Definition at line 20 of file passwordbasedauthentication_tests.c.

21 {
22  assert_false (pba_is_phc_compliant ("$"));
23  assert_false (pba_is_phc_compliant ("password"));
24 }

References pba_is_phc_compliant().

Here is the call graph for this function:

◆ Ensure() [5/9]

Ensure ( PBA  ,
returns_NULL_on_unsupport_settings   
)

Definition at line 29 of file passwordbasedauthentication_tests.c.

30 {
31  struct PBASettings setting = {"0000", 20000, "$6$"};
32  assert_false (pba_hash (NULL, "*password"));
33  assert_false (pba_hash (&setting, NULL));
34  setting.prefix = "$1$";
35  assert_false (pba_hash (&setting, "*password"));
36 }

References pba_hash(), and PBASettings::prefix.

Here is the call graph for this function:

◆ Ensure() [6/9]

Ensure ( PBA  ,
returns_true_on_phc_compliant_setting   
)

Definition at line 25 of file passwordbasedauthentication_tests.c.

26 {
27  assert_true (pba_is_phc_compliant ("$password"));
28 }

References pba_is_phc_compliant().

Here is the call graph for this function:

◆ Ensure() [7/9]

Ensure ( PBA  ,
unique_hash_without_adding_used_pepper   
)

Definition at line 37 of file passwordbasedauthentication_tests.c.

38 {
39  struct PBASettings setting = {"4242", 20000, "$6$"};
40  char *cmp_hash, *hash;
41  hash = pba_hash (&setting, "*password");
42  assert_not_equal (hash, NULL);
43  assert_false (string_contains (hash, setting.pepper));
44  cmp_hash = pba_hash (&setting, "*password");
45  assert_string_not_equal (hash, cmp_hash);
46  free (hash);
47  free (cmp_hash);
48 }

References pba_hash(), and PBASettings::pepper.

Here is the call graph for this function:

◆ Ensure() [8/9]

Ensure ( PBA  ,
verify_hash   
)

Definition at line 49 of file passwordbasedauthentication_tests.c.

50 {
51  struct PBASettings setting = {"4242", 20000, "$6$"};
52  char *hash;
53  hash = pba_hash (&setting, "*password");
54  assert_not_equal (hash, NULL);
55  assert_equal (pba_verify_hash (&setting, hash, "*password"), VALID);
56  assert_equal (pba_verify_hash (&setting, hash, "*password1"), INVALID);
57  free (hash);
58  struct PBASettings setting_wo_pepper = {"\0\0\0\0", 20000, "$6$"};
59  hash = pba_hash (&setting_wo_pepper, "*password");
60  assert_equal (pba_verify_hash (&setting_wo_pepper, hash, "*password"), VALID);
61  free (hash);
62 }

References INVALID, pba_hash(), pba_verify_hash(), and VALID.

Here is the call graph for this function:

◆ Ensure() [9/9]

Ensure ( PBA  ,
verify_hash_returns_invalid_on_np_hash_np_password   
)

Definition at line 64 of file passwordbasedauthentication_tests.c.

65 {
66  struct PBASettings setting = {"4242", 20000, "$6$"};
67  char *hash;
68  hash = pba_hash (&setting, "*password");
69  assert_not_equal (hash, NULL);
70  assert_equal (pba_verify_hash (&setting, NULL, "*password"), INVALID);
71  assert_equal (pba_verify_hash (&setting, hash, NULL), INVALID);
72 }

References INVALID, pba_hash(), and pba_verify_hash().

Here is the call graph for this function:

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 116 of file passwordbasedauthentication_tests.c.

117 {
118  TestSuite *suite;
119 
120  suite = create_test_suite ();
121 
122  add_test_with_context (suite, PBA,
123  returns_false_on_not_phc_compliant_setting);
124  add_test_with_context (suite, PBA, returns_true_on_phc_compliant_setting);
125  add_test_with_context (suite, PBA, returns_NULL_on_unsupport_settings);
126  add_test_with_context (suite, PBA, unique_hash_without_adding_used_pepper);
127  add_test_with_context (suite, PBA, verify_hash);
128  add_test_with_context (suite, PBA,
129  verify_hash_returns_invalid_on_np_hash_np_password);
130  add_test_with_context (suite, PBA, handle_md5_hash);
131  add_test_with_context (suite, PBA, defaults);
132  add_test_with_context (suite, PBA, initialization);
133  if (argc > 1)
134  return run_single_test (suite, argv[1], create_text_reporter ());
135  return run_test_suite (suite, create_text_reporter ());
136 }
PBASettings::pepper
char pepper[MAX_PEPPER_SIZE]
Definition: passwordbasedauthentication.h:24
pba_finalize
void pba_finalize(struct PBASettings *settings)
Definition: passwordbasedauthentication.c:152
get_password_hashes
gchar * get_password_hashes(const gchar *password)
Generate a pair of md5 hashes to be used in the "auth/hash" file for the user.
Definition: authutils.c:189
pba_is_phc_compliant
static int pba_is_phc_compliant(const char *setting)
Definition: passwordbasedauthentication.c:158
MAX_PEPPER_SIZE
#define MAX_PEPPER_SIZE
Definition: passwordbasedauthentication.h:10
UPDATE_RECOMMENDED
@ UPDATE_RECOMMENDED
Definition: passwordbasedauthentication.h:48
gvm_auth_init
int gvm_auth_init(void)
Initializes Gcrypt.
Definition: authutils.c:89
pba_verify_hash
enum pba_rc pba_verify_hash(const struct PBASettings *setting, const char *hash, const char *password)
Definition: passwordbasedauthentication.c:215
VALID
@ VALID
Definition: passwordbasedauthentication.h:47
PBASettings
Definition: passwordbasedauthentication.h:23
PBASettings::count
unsigned int count
Definition: passwordbasedauthentication.h:25
pba_hash
char * pba_hash(struct PBASettings *setting, const char *password)
Definition: passwordbasedauthentication.c:168
PBASettings::prefix
char * prefix
Definition: passwordbasedauthentication.h:26
INVALID
@ INVALID
Definition: passwordbasedauthentication.h:49
pba_init
struct PBASettings * pba_init(const char *pepper, unsigned int pepper_size, unsigned int count, char *prefix)
Definition: passwordbasedauthentication.c:133