libwebsockets
Lightweight C library for HTML5 websockets
lws-fault-injection.h
Go to the documentation of this file.
1 /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010 - 2021 Andy Green <andy@warmcat.com>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22  * IN THE SOFTWARE.
23  *
24  * Fault injection api if built with LWS_WITH_SYS_FAULT_INJECTION
25  */
26 
27 typedef struct lws_xos {
28  uint64_t s[4];
30 
40 lws_xos_init(struct lws_xos *xos, uint64_t seed);
41 
52 lws_xos(struct lws_xos *xos);
53 
65 lws_xos_percent(struct lws_xos *xos, int percent);
66 
67 #if defined(LWS_WITH_SYS_FAULT_INJECTION)
68 
69 enum {
70  LWSFI_ALWAYS,
71  LWSFI_DETERMINISTIC, /* do .count injections after .pre then stop */
72  LWSFI_PROBABILISTIC, /* .pre % chance of injection */
73  LWSFI_PATTERN, /* use .count bits in .pattern after .pre */
74  LWSFI_PATTERN_ALLOC, /* as _PATTERN, but .pattern is malloc'd */
75  LWSFI_RANGE /* pick a number between pre and count */
76 };
77 
78 typedef struct lws_fi {
79  const char *name;
80  const uint8_t *pattern;
81  uint64_t pre;
82  uint64_t count;
83  uint64_t times; /* start at 0, tracks usage */
84  char type; /* LWSFI_* */
85 } lws_fi_t;
86 
87 typedef struct lws_fi_ctx {
88  lws_dll2_owner_t fi_owner;
89  struct lws_xos xos;
90  const char *name;
91 } lws_fi_ctx_t;
92 
110 lws_fi(const lws_fi_ctx_t *fic, const char *fi_name);
111 
131 lws_fi_range(const lws_fi_ctx_t *fic, const char *name, uint64_t *result);
132 
143 lws_fi_add(lws_fi_ctx_t *fic, const lws_fi_t *fi);
144 
155 lws_fi_remove(lws_fi_ctx_t *fic, const char *name);
156 
167 lws_fi_import(lws_fi_ctx_t *fic_dest, const lws_fi_ctx_t *fic_src);
168 
183 lws_fi_inherit_copy(lws_fi_ctx_t *fic_dest, const lws_fi_ctx_t *fic_src,
184  const char *scope, const char *value);
185 
196 lws_fi_destroy(const lws_fi_ctx_t *fic);
197 
212 lws_fi_deserialize(lws_fi_ctx_t *fic, const char *sers);
213 
215 _lws_fi_user_wsi_fi(struct lws *wsi, const char *name);
217 _lws_fi_user_context_fi(struct lws_context *ctx, const char *name);
218 
219 #if defined(LWS_WITH_SECURE_STREAMS)
220 struct lws_ss_handle;
222 _lws_fi_user_ss_fi(struct lws_ss_handle *h, const char *name);
223 #if defined(LWS_WITH_SECURE_STREAMS_PROXY_API)
224 struct lws_sspc_handle;
226 _lws_fi_user_sspc_fi(struct lws_sspc_handle *h, const char *name);
227 #endif
228 #endif
229 
230 #define lws_fi_user_wsi_fi(_wsi, _name) _lws_fi_user_wsi_fi(_wsi, _name)
231 #define lws_fi_user_context_fi(_ctx, _name) _lws_fi_user_context_fi(_ctx, _name)
232 #define lws_fi_user_ss_fi(_h, _name) _lws_fi_user_ss_fi(_h, _name)
233 #define lws_fi_user_sspc_fi(_h, _name) _lws_fi_user_sspc_fi(_h, _name)
234 
235 #else
236 
237 /*
238  * Helper so we can leave lws_fi() calls embedded in the code being tested,
239  * if fault injection is not enabled then it just always says "no" at buildtime.
240  */
241 
242 #define lws_fi(_fi_name, _fic) (0)
243 #define lws_fi_destroy(_x)
244 #define lws_fi_inherit_copy(_a, _b, _c, _d)
245 #define lws_fi_deserialize(_x, _y)
246 #define lws_fi_user_wsi_fi(_wsi, _name) (0)
247 #define lws_fi_user_context_fi(_wsi, _name) (0)
248 #define lws_fi_user_ss_fi(_h, _name) (0)
249 #define lws_fi_user_sspc_fi(_h, _name) (0)
250 
251 #endif
#define LWS_EXTERN
unsigned char uint8_t
#define LWS_WARN_UNUSED_RESULT
#define LWS_VISIBLE
LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT lws_xos_percent(struct lws_xos *xos, int percent)
#define lws_fi(_fi_name, _fic)
#define lws_fi_deserialize(_x, _y)
struct lws_xos lws_xos_t
LWS_VISIBLE LWS_EXTERN uint64_t LWS_WARN_UNUSED_RESULT lws_xos(struct lws_xos *xos)
uint64_t s[4]
LWS_VISIBLE LWS_EXTERN void lws_xos_init(struct lws_xos *xos, uint64_t seed)
#define lws_fi_inherit_copy(_a, _b, _c, _d)
#define lws_fi_destroy(_x)