test_cancellock.c
Go to the documentation of this file.
1 /* ========================================================================== */
2 /*! \file
3  * \brief Test of \c core_get_cancel_lock() implementation
4  *
5  * Copyright (c) 2015-2022 by the developers. See the LICENSE file for details.
6  */
7 
8 
9 /* ========================================================================== */
10 /* Include headers */
11 
12 #include "posix.h" /* Include this first because of feature test macros */
13 
14 #include <stdio.h>
15 
16 #include "conf.h"
17 #include "core.h"
18 #include "test.h"
19 #include "test_cancellock.h"
20 
21 
22 /* ========================================================================== */
23 /*! \addtogroup TEST */
24 /*! @{ */
25 
26 
27 /* ========================================================================== */
28 /*! \brief Test \c core_get_cancel_lock() implementation
29  *
30  * The following cases are tested:
31  * - Three different Message-ID/Secret pairs for Cancel-Lock with scheme SHA1
32  *
33  * \return
34  * - \c EXIT_SUCCESS on success
35  * - \c EXIT_FAILURE on error
36  */
37 
38 int test_cancellock(void)
39 {
40 #define TS_NUM (size_t) 3 /* Number of tests */
41  static const char* ts[TS_NUM] =
42  {
43  "Dummy1MessageID@invalid.invalid",
44  "DifferentDummy2MessageID@test.invalid",
45  "Dummy3@x.invalid"
46  };
47  static const char* rs_sha1[TS_NUM] =
48  {
49  "sha1:/bPdszYoGg5jN5ND++QigLj0t9I=",
50  "sha1:iuGB0LPk4gOkm+P8KWUUvREdOgY=",
51  "sha1:NvsbxX7h74kZYytOj2fbYrL3lhY="
52  };
53  static const char* secret_sha1[TS_NUM] =
54  {
55  "This string is used as secret by test system for test 1",
56  "This string is used as secret by test system for test 2",
57  "Secret3"
58  };
59 
60  int res = POSIX_EXIT_SUCCESS;
61  size_t i;
62  const char* buf = NULL;
63  int rv;
64 
65  /* Test SHA1 algorithm */
66  for(i = 0; i < TS_NUM; ++i)
67  {
68  rv = conf_string_replace(&config[CONF_CANCELKEY], secret_sha1[i]);
69  if(rv)
70  {
71  print_error("Preparation of configuration failed");
72  res = POSIX_EXIT_FAILURE;
73  break;
74  }
75  buf = core_get_cancel_lock(CORE_CL_SHA1, ts[i]);
76  if(NULL == buf)
77  {
78  print_error("Cancel-Lock generation failed");
79  res = POSIX_EXIT_FAILURE;
80  break;
81  }
82  if(strcmp(&rs_sha1[i][4], &buf[4]))
83  {
84  print_error("Cancel-Lock is not correct");
85  /* For debugging */
86  fprintf(stderr, TEST_TAB "Secret : \"%s\"\n",
87  config[CONF_CANCELKEY].val.s);
88  fprintf(stderr, TEST_TAB "Message-ID: \"%s\"\n", ts[i]);
89  fprintf(stderr, TEST_TAB "Result is : \"%s\"\n", buf);
90  fprintf(stderr, TEST_TAB "Should be : \"%s\"\n", rs_sha1[i]);
91  res = POSIX_EXIT_FAILURE;
92  break;
93  }
94  core_free((void*) buf);
95  }
96 
97  return(res);
98 }
99 
100 
101 /*! @} */
102 
103 /* EOF */
TEST_TAB
#define TEST_TAB
Tabulator to indent messages from test programs.
Definition: test.h:13
config
struct conf config[CONF_NUM]
Global configuration.
Definition: conf.c:63
test_cancellock
int test_cancellock(void)
Test core_get_cancel_lock() implementation.
Definition: test_cancellock.c:38
core_free
void core_free(void *p)
Free an object allocated by core (exported for UI)
Definition: core.c:6625
core_get_cancel_lock
const char * core_get_cancel_lock(unsigned int scheme, const char *mid)
Create Cancel-Lock for Message-ID (exported for UI)
Definition: core.c:5924
conf_string_replace
int conf_string_replace(struct conf *cfg, const char *s)
Replace configuration string.
Definition: conf.c:912
CORE_CL_SHA1
#define CORE_CL_SHA1
Definition: core.h:208
CONF_CANCELKEY
Definition: conf.h:55
print_error
void print_error(const char *)
Print error message.
Definition: main.cxx:276

Generated at 2024-04-27 using  doxygen