summaryrefslogtreecommitdiffstats
path: root/tests/modules/getchannels.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-14 13:40:54 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-14 13:40:54 +0000
commit317c0644ccf108aa23ef3fd8358bd66c2840bfc0 (patch)
treec417b3d25c86b775989cb5ac042f37611b626c8a /tests/modules/getchannels.c
parentInitial commit. (diff)
downloadredis-317c0644ccf108aa23ef3fd8358bd66c2840bfc0.tar.xz
redis-317c0644ccf108aa23ef3fd8358bd66c2840bfc0.zip
Adding upstream version 5:7.2.4.upstream/5%7.2.4upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/modules/getchannels.c')
-rw-r--r--tests/modules/getchannels.c69
1 files changed, 69 insertions, 0 deletions
diff --git a/tests/modules/getchannels.c b/tests/modules/getchannels.c
new file mode 100644
index 0000000..330531d
--- /dev/null
+++ b/tests/modules/getchannels.c
@@ -0,0 +1,69 @@
+#include "redismodule.h"
+#include <strings.h>
+#include <assert.h>
+#include <unistd.h>
+#include <errno.h>
+
+/* A sample with declarable channels, that are used to validate against ACLs */
+int getChannels_subscribe(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
+ if ((argc - 1) % 3 != 0) {
+ RedisModule_WrongArity(ctx);
+ return REDISMODULE_OK;
+ }
+ char *err = NULL;
+
+ /* getchannels.command [[subscribe|unsubscribe|publish] [pattern|literal] <channel> ...]
+ * This command marks the given channel is accessed based on the
+ * provided modifiers. */
+ for (int i = 1; i < argc; i += 3) {
+ const char *operation = RedisModule_StringPtrLen(argv[i], NULL);
+ const char *type = RedisModule_StringPtrLen(argv[i+1], NULL);
+ int flags = 0;
+
+ if (!strcasecmp(operation, "subscribe")) {
+ flags |= REDISMODULE_CMD_CHANNEL_SUBSCRIBE;
+ } else if (!strcasecmp(operation, "unsubscribe")) {
+ flags |= REDISMODULE_CMD_CHANNEL_UNSUBSCRIBE;
+ } else if (!strcasecmp(operation, "publish")) {
+ flags |= REDISMODULE_CMD_CHANNEL_PUBLISH;
+ } else {
+ err = "Invalid channel operation";
+ break;
+ }
+
+ if (!strcasecmp(type, "literal")) {
+ /* No op */
+ } else if (!strcasecmp(type, "pattern")) {
+ flags |= REDISMODULE_CMD_CHANNEL_PATTERN;
+ } else {
+ err = "Invalid channel type";
+ break;
+ }
+ if (RedisModule_IsChannelsPositionRequest(ctx)) {
+ RedisModule_ChannelAtPosWithFlags(ctx, i+2, flags);
+ }
+ }
+
+ if (!RedisModule_IsChannelsPositionRequest(ctx)) {
+ if (err) {
+ RedisModule_ReplyWithError(ctx, err);
+ } else {
+ /* Normal implementation would go here, but for tests just return okay */
+ RedisModule_ReplyWithSimpleString(ctx, "OK");
+ }
+ }
+
+ return REDISMODULE_OK;
+}
+
+int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
+ REDISMODULE_NOT_USED(argv);
+ REDISMODULE_NOT_USED(argc);
+ if (RedisModule_Init(ctx, "getchannels", 1, REDISMODULE_APIVER_1) == REDISMODULE_ERR)
+ return REDISMODULE_ERR;
+
+ if (RedisModule_CreateCommand(ctx, "getchannels.command", getChannels_subscribe, "getchannels-api", 0, 0, 0) == REDISMODULE_ERR)
+ return REDISMODULE_ERR;
+
+ return REDISMODULE_OK;
+}