xaizek / vifm (License: GPLv2+) (since 2018-12-07)
Vifm is a file manager with curses interface, which provides Vi[m]-like environment for managing objects within file systems, extended with some useful ideas from mutt.
<root> / tests / misc / registers_shared_memory.c (eed6f1945ca32ce5c08edf31670f8ae67054e971) (11KiB) (mode 100644) [raw]
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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
#include <stic.h>

#include <fcntl.h>
#include <unistd.h>

#include <errno.h> /* errno */
#include <signal.h> /* SIGTERM */
#include <stdio.h> /* fclose() fdopen() fgets() fprintf() fputs() snprintf() */
#include <stdlib.h> /* exit() */
#include <string.h> /* strerror() */

#include <test-utils.h>

#include "../../src/compat/fs_limits.h"
#include "../../src/utils/gmux.h"
#include "../../src/utils/shmem.h"
#include "../../src/utils/utils.h"

static void spawn_regcmd(int number);
static void send_query(int instance, const char query[]);
static void receive_ack(int instance);
static void check_is_initial(int instance, const char reglist[]);
static void receive_answer(int instance, char lnbuf[]);
static void sync_to_from(int instance);
static void sync_from(int instance);
static void check_register_contents(int instance, char register_name,
		const char expected_content[]);
static void test_pat(char result[], size_t patsz, char register_name,
		size_t pat_id_every, char p0, char p1);
static void sync_disable(int instance);
static pid_t popen2(const char cmd[], FILE **in, FILE **out);

#define LINE_SIZE 32768
#define NUM_INSTANCES 3

#define TEST_REGISTERS            "abcdefghijklmnopqrstuvwxyz"
#define TEST_REGISTERS_MINUS_D    "abcefghijklmnopqrstuvwxyz"
#define TEST_REGISTERS_MINUS_DE   "abcfghijklmnopqrstuvwxyz"
#define TEST_REGISTERS_MINUS_DEF  "abcghijklmnopqrstuvwxyz"
#define TEST_REGISTERS_MINUS_DEFG "abchijklmnopqrstuvwxyz"

#define TEST_EXPECT_FOR_D "d,3,nd1,nd2,newd,"
#define TEST_EXPECT_FOR_E "e,4,le1,le2,le3,longerthanbeforee,"
#define TEST_EXPECT_FOR_G "g,1,G,"

static FILE *instance_stdin[NUM_INSTANCES];
static FILE *instance_stdout[NUM_INSTANCES];

static char pat4kib[4096 + 8];

static char *tmpdir_value;

/* All of the tests need to run in exactly the order given for correct
 * results. */

/* An update of Wine broke something, specifically second instance of helper
 * application doesn't seem to be started in SETUP_ONCE. */

/* Tests basic transfer of values from 0 to 1 and spawns processes. */
SETUP_ONCE()
{
	if(!not_wine())
	{
		return;
	}

	/* Put all the temporary files into sandbox to avoid issues with running tests
	 * from multiple accounts. */
	char sandbox[PATH_MAX + 1];
	make_abs_path(sandbox, sizeof(sandbox), SANDBOX_PATH, "", NULL);
	tmpdir_value = mock_env("TMPDIR", sandbox);

	/* Make sure nothing is retained from previous tests. */
	gmux_destroy(gmux_create("regs-test-shmem"));
	shmem_destroy(shmem_create("regs-test-shmem", 10, 10));

	/* Spawn processes. */
	spawn_regcmd(0);
	spawn_regcmd(1);

	/* Initial register values. */
	const char *curletr;
	for(curletr = TEST_REGISTERS; *curletr != '\0'; ++curletr)
	{
		fprintf(instance_stdin[0], "set,%c,_initial%c,i%c1,i%c2,i%c3\n",
			*curletr, *curletr, *curletr, *curletr, *curletr);
		fprintf(instance_stdin[1], "set,%c,lossval%c,l%c1,l%c2,l%c3\n",
			*curletr, *curletr, *curletr, *curletr, *curletr);
	}

	/* Enable shared memory synchronization. */
	send_query(0, "sync_enable,test-shmem\n");
	receive_ack(0);
	send_query(1, "sync_enable,test-shmem\n");
	receive_ack(1);
}

TEARDOWN_ONCE()
{
	if(!not_wine())
	{
		return;
	}

	/* Make sure nothing is retained from the tests. */
	gmux_destroy(gmux_create("regs-test-shmem"));
	shmem_destroy(shmem_create("regs-test-shmem", 10, 10));

	unmock_env("TMPDIR", tmpdir_value);
}

static void
spawn_regcmd(int number)
{
	const int debug = (getenv("DEBUG") != NULL);
	const char *cmd = debug ? "bin" SL "debug" SL "regs_shmem_app"
	                        : "bin" SL "regs_shmem_app";

	if(popen2(cmd, &instance_stdin[number],
				&instance_stdout[number]) == (pid_t)-1)
	{
		perror("Failed to call fork");
		exit(1);
	}

	setvbuf(instance_stdin[number], NULL, _IONBF, 0);
}

static void
send_query(int instance, const char query[])
{
	if(fputs(query, instance_stdin[instance]) == EOF)
	{
		fprintf(stderr, "Failed to write query to FD %p "
			"(instance %d): %s; query was %s",
			instance_stdin[instance], instance, strerror(errno), query);
		exit(1);
	}
}

static void
receive_ack(int instance)
{
	char bakval;
	char lnbuf[LINE_SIZE];
	receive_answer(instance, lnbuf);
	bakval = lnbuf[3];
	/* Cut off ,... after ack */
	lnbuf[3] = '\0';
	if(strcmp(lnbuf, "ack") != 0)
	{
		lnbuf[3] = bakval;
		fprintf(stderr, "Error: Did not receive ACK but the following "
			"response from instance %d: %s\n", instance, lnbuf);
		exit(1);
	}
}

static void
check_is_initial(int instance, const char reglist[])
{
	const char *curletr;
	for(curletr = reglist; *curletr != '\0'; ++curletr)
	{
		char cmp[27];
		snprintf(cmp, sizeof(cmp), "%c,4,_initial%c,i%c1,i%c2,i%c3,",
			*curletr, *curletr, *curletr, *curletr, *curletr);
		check_register_contents(instance, *curletr, cmp);
	}
}

static void
receive_answer(int instance, char lnbuf[])
{
	if(fgets(lnbuf, LINE_SIZE, instance_stdout[instance]) == NULL)
	{
		printf("Child (instance %d) unexpectedly sent eof\n", instance);
		exit(1);
	}

	/* Replace \n with '\0'. */
	lnbuf[strlen(lnbuf) - 1] = '\0';
}

TEST(make_sure_instance_0_does_not_lose_data, IF(not_wine))
{
	sync_from(0);
	check_is_initial(0, TEST_REGISTERS);
}

static void
sync_to_from(int instance)
{
	send_query(instance, "sync_to\n");
	receive_ack(instance);

	sync_from(!instance);
}

static void
sync_from(int instance)
{
	send_query(instance, "sync_from\n");
	receive_ack(instance);
}

TEST(make_sure_sync_to_instance_1_works, IF(not_wine))
{
	sync_from(1);
	check_is_initial(1, TEST_REGISTERS);
}

TEST(chg_update_in_place, IF(not_wine))
{
	send_query(1, "set,d,newd,nd1,nd2\n");
	sync_to_from(1);

	check_register_contents(0, 'd', TEST_EXPECT_FOR_D);

	/* check others */
	check_is_initial(0, TEST_REGISTERS_MINUS_D);
}

static void
check_register_contents(int instance, char register_name,
		const char expected_content[])
{
	char lnbuf[LINE_SIZE];
	char query[7];
	strcpy(query, "get,X\n");
	query[4] = register_name;

	send_query(instance, query);
	receive_answer(instance, lnbuf);
	assert_string_equal(expected_content, lnbuf);
}

TEST(chg_append_to_end, IF(not_wine))
{
	send_query(1, "set,e,longerthanbeforee,le1,le2,le3\n");
	sync_to_from(1);

	/* Ignore D. */

	/* Check E. */
	check_register_contents(0, 'e', TEST_EXPECT_FOR_E);

	/* Check others. */
	check_is_initial(0, TEST_REGISTERS_MINUS_DE);
}

TEST(chg_double_allocation_size_till_fit, IF(not_wine))
{
	test_pat(pat4kib, 4096, 'f', 128, '4', 'f');
	/* Check initial. */
	check_is_initial(0, TEST_REGISTERS_MINUS_DEF);
}

/* Result needs to have patsz + 8 entries (set,X, + \n + 0-byte). */
static void
test_pat(char result[], size_t patsz, char register_name, size_t pat_id_every,
		char p0, char p1)
{
	/* Prepare pattern. */
	size_t i;
	char pre[] = "set,X,";
	strcpy(result, pre);
	result[4] = register_name;
	for(i = 0; i < patsz; ++i)
	{
		result[sizeof(pre) - 1 + i] = ((i % pat_id_every) == 0)? p0: p1;
	}
	strcpy(result + patsz + 6, "\n"); /* add \n and 0 terminator */

	send_query(1, result);
	sync_to_from(1);

	/* Change set,f, to [se]f,1, */
	result[2] = register_name;
	char constpartcmp[] = ",1,";
	memcpy(result + 3, constpartcmp, sizeof(constpartcmp) - 1);
	/* For comparison change \n to , */
	result[patsz + 6] = ',';

	/* +2 to skip `se` */
	check_register_contents(0, register_name, result + 2);
}

TEST(chg_double_allocation_to_max, IF(not_wine))
{
	char result[16384 + 8];
	test_pat(result, 16384, 'g', 512, '6', 'g');
	check_is_initial(0, TEST_REGISTERS_MINUS_DEFG);
}

TEST(chg_halve_allocation, IF(not_wine))
{
	send_query(0, "set,g,G\n");
	sync_to_from(0);

	check_register_contents(1, 'g', TEST_EXPECT_FOR_G);
	check_is_initial(1, TEST_REGISTERS_MINUS_DEFG);
}

TEST(handover, IF(not_wine))
{
	/* Open third instance. */
	spawn_regcmd(2);
	send_query(2, "sync_enable,test-shmem\n");
	receive_ack(2);

	/* Close previous instances. */
	sync_disable(0);
	sync_disable(1);

	sync_from(2);

	check_is_initial(2, TEST_REGISTERS_MINUS_DEFG);
	check_register_contents(2, 'd', TEST_EXPECT_FOR_D);
	check_register_contents(2, 'e', TEST_EXPECT_FOR_E);
	check_register_contents(2, 'f', pat4kib + 2);
	check_register_contents(2, 'g', TEST_EXPECT_FOR_G);
}

static void
sync_disable(int instance)
{
	send_query(instance, "sync_disable\n");
	receive_ack(instance);
	fclose(instance_stdin[instance]);
	fclose(instance_stdout[instance]);
}

TEST(teardown_once, IF(not_wine))
{
	sync_disable(2);
}

#ifndef _WIN32

static pid_t
popen2(const char cmd[], FILE **in, FILE **out)
{
	pid_t pid;
	int in_pipe[2];
	int out_pipe[2];

	if(pipe(out_pipe) != 0)
	{
		return (pid_t)-1;
	}

	if(pipe(in_pipe) != 0)
	{
		close(out_pipe[0]);
		close(out_pipe[1]);
		return (pid_t)-1;
	}

	if((pid = fork()) == -1)
	{
		close(in_pipe[0]);
		close(in_pipe[1]);
		close(out_pipe[0]);
		close(out_pipe[1]);
		return (pid_t)-1;
	}

	if(pid == 0)
	{
		close(in_pipe[1]);
		close(out_pipe[0]);
		if(dup2(in_pipe[0], STDIN_FILENO) == -1)
		{
			_Exit(EXIT_FAILURE);
		}
		if(dup2(out_pipe[1], STDOUT_FILENO) == -1)
		{
			_Exit(EXIT_FAILURE);
		}
		if(dup2(out_pipe[1], STDERR_FILENO) == -1)
		{
			_Exit(EXIT_FAILURE);
		}

		close(in_pipe[0]);
		close(out_pipe[1]);

		execvp("/bin/sh", make_execv_array("/bin/sh", "-c", (char *)cmd));
		_Exit(127);
	}

	close(in_pipe[0]);
	close(out_pipe[1]);
	*in = fdopen(in_pipe[1], "w");
	if(*in == NULL)
	{
		close(in_pipe[1]);
		close(out_pipe[0]);
		kill(pid, SIGTERM);
		return (pid_t)-1;
	}
	*out = fdopen(out_pipe[0], "r");
	if(*out == NULL)
	{
		fclose(*in);
		close(out_pipe[0]);
		kill(pid, SIGTERM);
		return (pid_t)-1;
	}

	return pid;
}

#else

/* Runs command in background and redirects all its streams to file streams
 * which are set.  Returns (pid_t)0 or (pid_t)-1 on error. */
static pid_t
popen2_int(const char cmd[], FILE **in, FILE **out,
		int in_pipe[2], int out_pipe[2])
{
	const char *args[4];
	int code;

	if(_dup2(in_pipe[0], _fileno(stdin)) != 0)
		return (pid_t)-1;
	if(_dup2(out_pipe[1], _fileno(stdout)) != 0)
		return (pid_t)-1;
	if(_dup2(out_pipe[1], _fileno(stderr)) != 0)
		return (pid_t)-1;

	args[0] = "cmd";
	args[1] = "/C";
	args[2] = cmd;
	args[3] = NULL;

	code = _spawnvp(P_NOWAIT, "cmd", args);

	if(code == 0)
	{
		return (pid_t)-1;
	}

	if((*in = _fdopen(in_pipe[1], "w")) == NULL)
		return (pid_t)-1;
	if((*out = _fdopen(out_pipe[0], "r")) == NULL)
	{
		fclose(*in);
		return (pid_t)-1;
	}

	return 0;
}

static pid_t
popen2(const char cmd[], FILE **in, FILE **out)
{
	int in_fd, in_pipe[2];
	int out_fd, out_pipe[2];
	int err_fd;
	pid_t pid;

	if(_pipe(in_pipe, 32*1024, O_NOINHERIT) != 0)
	{
		return (pid_t)-1;
	}

	if(_pipe(out_pipe, 32*1024, O_NOINHERIT) != 0)
	{
		close(in_pipe[0]);
		close(in_pipe[1]);
		return (pid_t)-1;
	}

	in_fd = dup(_fileno(stdin));
	out_fd = dup(_fileno(stdout));
	err_fd = dup(_fileno(stderr));

	pid = popen2_int(cmd, in, out, in_pipe, out_pipe);

	_close(in_pipe[0]);
	_close(out_pipe[1]);

	_dup2(in_fd, _fileno(stdin));
	_dup2(out_fd, _fileno(stdout));
	_dup2(err_fd, _fileno(stderr));

	_close(in_fd);
	_close(out_fd);
	_close(err_fd);

	if(pid == (pid_t)-1)
	{
		_close(in_pipe[1]);
		_close(out_pipe[0]);
	}

	return pid;
}

#endif

/* vim: set tabstop=2 softtabstop=2 shiftwidth=2 noexpandtab cinoptions-=(0 : */
/* vim: set cinoptions+=t0 filetype=c : */
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/vifm

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/vifm

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master