コード例 #1
0
ファイル: aes-test.c プロジェクト: 21superman/strongswan
static bool get_next_test_vector(test_vector_t *test)
{
	param_t param = PARAM_UNKNOWN;
	char line[512];

	memset(test, 0, sizeof(test_vector_t));

	while (fgets(line, sizeof(line), ctx.in))
	{
		enumerator_t *enumerator;
		chunk_t value = chunk_empty;
		char *token;
		int i;

		switch (line[0])
		{
			case '\n':
			case '\r':
			case '#':
			case '\0':
				/* copy comments, empty lines etc. directly to the output */
				if (param != PARAM_UNKNOWN)
				{	/* seems we got a complete test vector */
					return TRUE;
				}
				fputs(line, ctx.out);
				continue;
			case '[':
				/* control directives */
				fputs(line, ctx.out);
				if (strpfx(line, "[ENCRYPT]"))
				{
					ctx.decrypt = FALSE;
				}
				else if (strpfx(line, "[DECRYPT]"))
				{
					ctx.decrypt = TRUE;
				}
				else if (strcasepfx(line, "[IVlen = "))
				{
					ctx.ivlen = atoi(line + strlen("[IVlen = "));
				}
				else if (strcasepfx(line, "[Taglen = "))
				{
					ctx.icvlen = atoi(line + strlen("[Taglen = "));
				}
				continue;
			default:
				/* we assume the rest of the lines are PARAM = VALUE pairs*/
				fputs(line, ctx.out);
				break;
		}

		i = 0;
		enumerator = enumerator_create_token(line, "=", " \n\r");
		while (enumerator->enumerate(enumerator, &token))
		{
			switch (i++)
			{
				case 0: /* PARAM */
					param = parse_parameter(token);
					continue;
				case 1: /* VALUE */
					if (param != PARAM_UNKNOWN && param != PARAM_COUNT)
					{
						value = chunk_from_hex(chunk_from_str(token), NULL);
					}
					else
					{
						value = chunk_empty;
					}
					continue;
				default:
					break;
			}
			break;
		}
		enumerator->destroy(enumerator);
		if (i < 2)
		{
			value = chunk_empty;
		}
		switch (param)
		{
			case PARAM_KEY:
				test->key = value;
				break;
			case PARAM_IV:
				test->iv = value;
				test->external_iv = TRUE;
				break;
			case PARAM_PLAINTEXT:
				test->plain = value;
				break;
			case PARAM_CIPHERTEXT:
				test->cipher = value;
				break;
			case PARAM_AAD:
				test->aad = value;
				break;
			case PARAM_ICV:
				test->icv = value;
				break;
			default:
				chunk_free(&value);
				break;
		}
	}
	if (param != PARAM_UNKNOWN)
	{	/* could be that the file ended with a complete test vector */
		return TRUE;
	}
	return FALSE;
}
コード例 #2
0
ファイル: jc.c プロジェクト: herrsergio/libjsw
/*
 *	Creates a new joystick calibrator window.
 */
jc_struct *JCNew(gint argc, gchar **argv)
{
	gint i, status;
	const gchar *s, *s2, *arg;
	js_data_struct *jsd;
	jc_struct *jc = (jc_struct *)g_malloc0(sizeof(jc_struct));
	if(jc == NULL)
	    return(jc);


	/* Begin resetting jc structure. */
	jc->initialized = TRUE;
	jc->map_state = FALSE;
	jc->processing = FALSE;

	/* Reset joystick data structure values. */
	jsd = &jc->jsd;
	jsd->name = NULL;
	jsd->axis = NULL;
	jsd->total_axises = 0;
	jsd->button = NULL;
	jsd->total_buttons = 0;
	jsd->device_name = NULL;
	jsd->calibration_file = NULL;
	jsd->fd = -1;
	jsd->flags = 0;
	jsd->driver_version = 0;

	jc->manage_toid = (guint)-1;
	jc->calib_file = NULL;
	jc->tried_load_device_on_init = FALSE;


	/* Set default joystick calibration file path. */
	s = g_getenv(ENV_VAR_NAME_HOME);
	if(s == NULL)
	     s2 = JSDefaultCalibration;
	else
	     s2 = PrefixPaths(s, JSDefaultCalibration);
	jc->calib_file = STRDUP(s2);
	jc->tried_load_device_on_init = FALSE;

	/* Reset has changes marker. */
	jc->has_changes = FALSE;


	/* Create widgets for the joystick calibrator window. */
	status = JCCreateWidgets(jc, argc, argv);
	if(status)
	{
	    g_free(jc);
	    jc = NULL;
	    return(jc);
	}

	/* Set timeout callback. */
	jc->manage_toid = gtk_timeout_add(
	    16,                 /* Every 16 milliseconds. */
	    (GtkFunction)JCTimeoutCB,
	    (gpointer)jc
	);


	/* Handle arguments. */
	for(i = 1; i < argc; i++)
	{
	    arg = argv[i];
	    if(arg == NULL)
		continue;

	    /* Specify alternate calibration file. */
	    if(strcasepfx(arg, "-f"))
	    {
		i++;
		if(i < argc)
		{
		    g_free(jc->calib_file);
		    jc->calib_file = (argv[i] != NULL) ?
			g_strdup(argv[i]) : NULL;
		}
		else
		{
		    g_printerr(
			"%s: Requires argument.\n",
			argv[i - 1]
		    );
		}
	    }
	    /* Specify startup device. */
	    else if(strcasepfx(arg, "-d"))
	    {
		i++;
		if(i < argc)
		{
		    GtkCombo *combo = (GtkCombo *)jc->js_device_combo;

		    if(combo != NULL)
			gtk_entry_set_text(
			    GTK_ENTRY(combo->entry),
			    argv[i]
		        );
		}
		else
		{
		    g_printerr(
			"%s: Requires argument.\n",
			argv[i - 1] 
		    );
		}
	    }
	}


	return(jc);
}
コード例 #3
0
static bool servicing(void *data, stream_t *stream)
{
	test_service_t *test = (test_service_t*)data;
	char buf[1024], hdr[256], *start, *end = NULL, *body = NULL, *type = NULL;
	struct tm tm;
	time_t t;
	ssize_t len, tot = 0;
	int nr = 0;

	start = buf;

	/* parse method and headers */
	while (end != start)
	{
		len = stream->read(stream, buf + tot, sizeof(buf) - tot, TRUE);
		ck_assert(len > 0);
		tot += len;

		while (TRUE)
		{
			end = memchr(start, '\n', tot);
			if (!end)
			{
				break;
			}
			*end = '\0';
			ck_assert(end > buf);
			ck_assert(*(--end) == '\r');
			*end = '\0';
			if (end == start)
			{
				body = end + strlen("\r\n");
				break;
			}
			switch (nr++)
			{
				case 0:
					snprintf(hdr, sizeof(hdr), "%s %s HTTP/1.%u",
							 test->meth, test->path, test->minor);
					ck_assert_str_eq(hdr, start);
					break;
				default:
					if (strcasepfx(start, "Content-Length: "))
					{
						ck_assert_int_eq(
							atoi(start + strlen("Content-Length: ")),
							test->req_len);
					}
					if (strcasepfx(start, "Content-Type: "))
					{
						type = start + strlen("Content-Type: ");
					}
					break;
			}
			start = end + strlen("\r\n");
		}
	}

	if (test->type)
	{
		ck_assert(type);
		ck_assert_str_eq(type, test->type);
	}

	/* request body */
	if (test->req_len)
	{
		ck_assert(stream->read_all(stream, buf + tot,
								   test->req_len - (tot - (body - buf))));
		ck_assert(memeq(body, test->req, test->req_len));
	}

	if (!test->code)
	{
		test->code = 200;
	}

	/* response headers */
	snprintf(buf, sizeof(buf), "HTTP/1.%u %u OK\r\n", test->minor, test->code);
	ck_assert(stream->write_all(stream, buf, strlen(buf)));

	/* if the response code indicates an error the following write operations
	 * might fail because the client already terminated the TCP connection */
#define may_fail(test, op) ck_assert(op || !HTTP_SUCCESS(test->code))

	t = time(NULL);
	gmtime_r(&t, &tm);
	strftime(buf, sizeof(buf), "%a, %d %b %Y %T %z", &tm);
	may_fail(test, stream->write_all(stream, buf, strlen(buf)));
	snprintf(buf, sizeof(buf), "Server: strongSwan unit test\r\n");
	may_fail(test, stream->write_all(stream, buf, strlen(buf)));

	/* rest of response headers */
	snprintf(buf, sizeof(buf), "Content-Type: text/plain\r\n");
	may_fail(test, stream->write_all(stream, buf, strlen(buf)));
	snprintf(buf, sizeof(buf), "Content-Length: %u\r\n", test->res_len);
	may_fail(test, stream->write_all(stream, buf, strlen(buf)));
	snprintf(buf, sizeof(buf), "Connection: close\r\n");
	may_fail(test, stream->write_all(stream, buf, strlen(buf)));
	snprintf(buf, sizeof(buf), "\r\n");
	may_fail(test, stream->write_all(stream, buf, strlen(buf)));

	/* response body */
	may_fail(test, stream->write_all(stream, test->res, test->res_len));
	return FALSE;
}