Motr  M0
validation.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-2020 Seagate Technology LLC and/or its Affiliates
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * For any questions about this software or licensing,
17  * please email opensource@seagate.com or cortx-questions@seagate.com.
18  *
19  */
20 
21 
22 #include <glob.h>
23 #include "conf/validation.h"
24 #include "conf/ut/common.h" /* m0_conf_ut_cache_from_file */
25 #include "lib/fs.h" /* m0_file_read */
26 #include "lib/string.h" /* m0_streq */
27 #include "lib/memory.h" /* m0_free */
28 #include "ut/ut.h"
29 
30 static char g_buf[192];
31 
32 static void cache_load(struct m0_conf_cache *cache, const char *path,
33  char **sharp_out);
34 
35 static void test_validation(void)
36 {
37  glob_t g = {0};
38  char **pathv;
39  char *err;
40  char *expected;
41  int rc;
42 
43  rc = glob(M0_SRC_PATH("conf/ut/t_*.xc"), 0, NULL, &g);
44  M0_UT_ASSERT(rc == 0);
45  for (pathv = g.gl_pathv; *pathv != NULL; ++pathv) {
46 #define _UT_ASSERT(cond) \
47  M0_ASSERT_INFO(cond, \
48  "path=%s\n" \
49  "err={%s}\n" \
50  "expected={%s}\n" \
51  "g_buf={%s}", \
52  *pathv, (err ?: ""), (expected ?: ""), g_buf)
53 
56  g_buf, sizeof g_buf);
57  _UT_ASSERT((err == NULL) == (expected == NULL));
58  if (expected != NULL) {
59  /* Strip "[<rule set>.<rule>] " prefix. */
60  err = strstr(err, "] ");
61  M0_UT_ASSERT(err != NULL);
62  err += 2;
64  }
65  free(expected);
66 #undef _UT_ASSERT
67  }
68  globfree(&g);
69 }
70 
80 static char *sharp_comment(const char *input)
81 {
82  const char *start = input;
83  const char *end;
84 
85  if (*start != '#')
86  return NULL; /* not a comment */
87 
88  while (*start == '#')
89  ++start;
90  if (*start != '=')
91  return NULL; /* an ordinary, not a "sharp", comment */
92  for (++start; isblank(*start); ++start) /* NB space_skip() */
93  ; /* lstrip */
94 
95  for (end = start; !M0_IN(*end, (0, '\n')); ++end)
96  ;
97  while (isblank(*(end-1)) && end-1 > start)
98  --end; /* rstrip */
99  return strndup(start, end - start);
100 }
101 
102 static void test_sharp_comment(void)
103 {
104  static const struct {
105  const char *input;
106  const char *result;
107  } samples[] = {
108  { "", NULL },
109  { "# ordinary comment", NULL },
110  { "not a comment", NULL },
111  { " #= not at start of the line", NULL },
112  { "#= special comment\nanything", "special comment" },
113  { "###= \t text\t ", "text" },
114  { "#=no leading space", "no leading space" },
115  { "#= ", "" },
116  { "#==", "=" }
117  };
118  char *s;
119  size_t i;
120 
121  for (i = 0; i < ARRAY_SIZE(samples); ++i) {
122  if (samples[i].result == NULL) {
123  M0_UT_ASSERT(sharp_comment(samples[i].input) == NULL);
124  } else {
125  s = sharp_comment(samples[i].input);
126  M0_UT_ASSERT(m0_streq(s, samples[i].result));
127  free(s);
128  }
129  }
130 }
131 
135 static void
136 cache_load(struct m0_conf_cache *cache, const char *path, char **sharp_out)
137 {
138  char *confstr = NULL;
139  int rc;
140 
141  M0_PRE(path != NULL && *path != '\0');
142 
144  rc = m0_file_read(path, &confstr);
145  M0_UT_ASSERT(rc == 0);
146  if (sharp_out != NULL)
147  *sharp_out = sharp_comment(confstr);
148  m0_free(confstr);
149 }
150 
152  .ts_name = "conf-validation-ut",
153  .ts_init = m0_conf_ut_cache_init,
154  .ts_fini = m0_conf_ut_cache_fini,
155  .ts_tests = {
156  { "sharp-comment", test_sharp_comment },
157  { "validation", test_validation },
158  { NULL, NULL }
159  }
160 };
161 
162 /*
163  * Local variables:
164  * c-indentation-style: "K&R"
165  * c-basic-offset: 8
166  * tab-width: 8
167  * fill-column: 80
168  * scroll-step: 1
169  * End:
170  */
171 /*
172  * vim: tabstop=8 shiftwidth=8 noexpandtab textwidth=80 nowrap
173  */
Definition: beck.c:235
M0_INTERNAL int m0_conf_ut_cache_init(void)
Definition: common.c:107
#define M0_PRE(cond)
#define NULL
Definition: misc.h:38
static void test_validation(void)
Definition: validation.c:35
M0_INTERNAL int m0_file_read(const char *path, char **out)
Definition: fs.c:61
#define M0_SRC_PATH(name)
Definition: misc.h:48
Definition: ut.h:77
struct m0_ut_suite conf_validation_ut
Definition: validation.c:151
static int expected
Definition: locality.c:102
int i
Definition: dir.c:1033
struct m0_conf_cache m0_conf_ut_cache
Definition: common.c:31
static void cache_load(struct m0_conf_cache *cache, const char *path, char **sharp_out)
Definition: validation.c:136
#define m0_streq(a, b)
Definition: string.h:34
M0_INTERNAL int m0_conf_ut_cache_fini(void)
Definition: common.c:114
static char g_buf[192]
Definition: validation.c:30
char * m0_conf_validation_error(struct m0_conf_cache *cache, char *buf, size_t buflen)
Definition: validation.c:60
const char * ts_name
Definition: ut.h:99
M0_INTERNAL void m0_conf_ut_cache_from_file(struct m0_conf_cache *cache, const char *path)
Definition: common.c:123
static char * sharp_comment(const char *input)
Definition: validation.c:80
static void test_sharp_comment(void)
Definition: validation.c:102
static int start(struct m0_fom *fom)
Definition: trigger_fom.c:321
static struct gen g[MAX_GEN]
Definition: beck.c:521
void m0_free(void *data)
Definition: memory.c:146
static struct m0_addb2_source * s
Definition: consumer.c:39
#define _UT_ASSERT(cond)
int32_t rc
Definition: trigger_fop.h:47
#define ARRAY_SIZE(a)
Definition: misc.h:45
#define M0_UT_ASSERT(a)
Definition: ut.h:46