Exemple #1
0
static int
test_one_codec (const char *codec, int flags)
{
	bool fail = false;

	printf("Codec %s:\n", codec);

	// Skip if this codec is not supported:
	if (!codec_supported(flags)) {
		puts("  skipping");
		return false;
	}

	// Test vectors:
	struct {
		const char *in;
		const char *out;
	} vec[] = {

		// These are the test vectors from RFC4648:
		{ "",		""         },
		{ "f",		"Zg=="     },
		{ "fo",		"Zm8="     },
		{ "foo",	"Zm9v"     },
		{ "foob",	"Zm9vYg==" },
		{ "fooba",	"Zm9vYmE=" },
		{ "foobar",	"Zm9vYmFy" },

		// The first paragraph from Moby Dick,
		// to test the SIMD codecs with larger blocksize:
		{ moby_dick_plain, moby_dick_base64 },
	};

	for (size_t i = 0; i < sizeof(vec) / sizeof(vec[0]); i++) {

		// Encode plain string, check against output:
		fail |= assert_enc(flags, vec[i].in, vec[i].out);

		// Decode the output string, check if we get the input:
		fail |= assert_dec(flags, vec[i].out, vec[i].in);

		// Do a roundtrip on the inputs and the outputs:
		fail |= assert_roundtrip(flags, vec[i].in);
		fail |= assert_roundtrip(flags, vec[i].out);
	}

	fail |= test_char_table(flags);
	fail |= test_streaming(flags);
	fail |= test_invalid_dec_input(flags);

	if (!fail)
		puts("  all tests passed.");

	return fail;
}
Exemple #2
0
int
main ()
{
	unsigned int i, flags;
	int ret = 0;
	#ifdef _MSC_VER
		READ_INPUT_FILE("moby_dick_base64.txt", moby_dick_base64_txt);
		READ_INPUT_FILE("moby_dick_plain.txt", moby_dick_plain_txt);
	#endif
	/* Loop over all codecs: */
	for (i = 0; codecs[i]; i++)
	{
		fail = 0;
		flags = (1 << i);

		/* Is this codec supported? */
		if (!codec_supported(flags)) {
			printf("Codec %s:\n  skipping\n", codecs[i]);
			continue;
		}
		printf("Codec %s:\n", codecs[i]);

		/* These are the test vectors from RFC4648: */
		assert_enc(flags, "", "");
		assert_enc(flags, "f", "Zg==");
		assert_enc(flags, "fo", "Zm8=");
		assert_enc(flags, "foo", "Zm9v");
		assert_enc(flags, "foob", "Zm9vYg==");
		assert_enc(flags, "fooba", "Zm9vYmE=");
		assert_enc(flags, "foobar", "Zm9vYmFy");

		/* And their inverse: */
		assert_dec(flags, "", "");
		assert_dec(flags, "Zg==", "f");
		assert_dec(flags, "Zm8=", "fo");
		assert_dec(flags, "Zm9v", "foo");
		assert_dec(flags, "Zm9vYg==", "foob");
		assert_dec(flags, "Zm9vYmE=", "fooba");
		assert_dec(flags, "Zm9vYmFy", "foobar");

		/* The first paragraph from Moby Dick,
		 * to test the SIMD codecs with larger blocksize: */
		assert_enc_len(flags,
			_binary_moby_dick_plain_txt_start,
			_binary_moby_dick_plain_txt_end - _binary_moby_dick_plain_txt_start,
			_binary_moby_dick_base64_txt_start,
			_binary_moby_dick_base64_txt_end - _binary_moby_dick_base64_txt_start
		);
		assert_dec_len(flags,
			_binary_moby_dick_base64_txt_start,
			_binary_moby_dick_base64_txt_end - _binary_moby_dick_base64_txt_start,
			_binary_moby_dick_plain_txt_start,
			_binary_moby_dick_plain_txt_end - _binary_moby_dick_plain_txt_start
		);

		assert_roundtrip(flags, "");
		assert_roundtrip(flags, "f");
		assert_roundtrip(flags, "fo");
		assert_roundtrip(flags, "foo");
		assert_roundtrip(flags, "foob");
		assert_roundtrip(flags, "fooba");
		assert_roundtrip(flags, "foobar");

		assert_roundtrip(flags, "");
		assert_roundtrip(flags, "Zg==");
		assert_roundtrip(flags, "Zm8=");
		assert_roundtrip(flags, "Zm9v");
		assert_roundtrip(flags, "Zm9vYg==");
		assert_roundtrip(flags, "Zm9vYmE=");
		assert_roundtrip(flags, "Zm9vYmFy");

		test_char_table(flags);

		test_streaming(flags);

		if (fail == 0) {
			printf("  all tests passed.\n");
		}
		ret |= fail;
	}
	#ifdef _MSC_VER
	free(_binary_moby_dick_base64_txt_start);
	free(_binary_moby_dick_plain_txt_start);
	#endif
	return ret;
}