cmogstored.git  about / heads / tags
alternative mogstored implementation for MogileFS
blob 6f2fc8021410ccda4a9a613c86f3690684e96535 2939 bytes (raw)
$ git show HEAD:test/cfg-parser-1.c	# shows this blob on the CLI

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
 
/*
 * Copyright (C) 2012-2020 all contributors <cmogstored-public@yhbt.net>
 * License: GPL-3.0+ <https://www.gnu.org/licenses/gpl-3.0.txt>
 */
#include "check.h"
#include "cfg.h"

int main(void)
{
	struct mog_cfg cfg;
	int rc;

	openlog("mog-test-cfg-parser-1", LOG_PID, LOG_USER);

	{
		char *s = xstrdup("127.0.0.1:666");
		struct mog_addrinfo *tmp = mog_listen_parse(s);
		assert(tmp && "failed to parse");
		mog_addrinfo_free(&tmp);
		assert(tmp == NULL && "not nulled");
		free(s);
	}

	{
		char *s = xstrdup("127-0.0.1:666");
		struct mog_addrinfo *tmp = mog_listen_parse(s);
		assert(tmp == NULL && "not null");
		free(s);
	}

	{
		char buf[] = "httplisten = 127.0.0.1:7500 # foo bar\n";
		memset(&cfg, 0, sizeof(struct mog_cfg));
		rc = mog_cfg_parse(&cfg, buf, sizeof(buf) - 1);

		assert(cfg.httplisten && "httplisten unset");
		assert(! cfg.mgmtlisten && "mgmtlisten set");
		assert(! cfg.httpgetlisten && "httpgetlisten set");
		assert(! cfg.daemonize && "daemonize set");
		assert(! cfg.maxconns && "maxconns set");
		assert(rc == 0 && "parser failed");

		mog_addrinfo_free(&cfg.httplisten);
	}

	{
		char buf[] = "httplisten = 127.0.0.1:7500\n"
		             "mgmtlisten = 127.6.6.6:7501\n"
		             "httpgetlisten = 127.6.6.6:7502\n"
			     "\n"
			     "docroot = /var/mogdata\n"
			     "daemonize\n"
			     "pidfile = /tmp/.cmogstored.pid\n"
			     "# hello \n"
			     "maxconns = 666666\n";

		memset(&cfg, 0, sizeof(struct mog_cfg));
		rc = mog_cfg_parse(&cfg, buf, sizeof(buf) - 1);

		assert(rc == 0 && "parser failed");
		assert(cfg.httplisten && "httplisten unset");
		assert(cfg.mgmtlisten && "mgmtlisten set");
		assert(cfg.httpgetlisten && "httpgetlisten set");
		assert(cfg.daemonize && "daemonize set");
		assert(cfg.maxconns == 666666 && "maxconns set");
		assert(cfg.docroot && "docroot set");
		assert(strcmp(cfg.docroot, "/var/mogdata") == 0 &&
		       "docroot mismatch");
		assert(strcmp(cfg.pidfile, "/tmp/.cmogstored.pid") == 0 &&
		       "pidfile mismatch");
		mog_addrinfo_free(&cfg.httplisten);
		mog_addrinfo_free(&cfg.mgmtlisten);
		mog_addrinfo_free(&cfg.httpgetlisten);
		mog_free(cfg.docroot);
		mog_free(cfg.pidfile);
	}

        {
		char buf[] = "httplisten = 666.0.0.1:7500\n";

		memset(&cfg, 0, sizeof(struct mog_cfg));
		rc = mog_cfg_parse(&cfg, buf, sizeof(buf) - 1);

		assert(rc == -1 && "parser should fail");
		assert(!cfg.httplisten && "nothing should've been allocated");
	}

        {
		char buf[] = "pidfile = /foo\0bar\n";

		memset(&cfg, 0, sizeof(struct mog_cfg));
		rc = mog_cfg_parse(&cfg, buf, sizeof(buf) - 1);

		assert(rc == -1 && "parser should fail");
		assert(!cfg.pidfile && "nothing should've been allocated");
	}

        {
		char buf[] = "mgmtlisten = 666.0.0-1:7500\n";

		memset(&cfg, 0, sizeof(struct mog_cfg));
		rc = mog_cfg_parse(&cfg, buf, sizeof(buf) - 1);

		assert(!cfg.mgmtlisten && "nothing should've been allocated");
	}

	return 0;
}

git clone https://yhbt.net/cmogstored.git