File: test_base64.c

package info (click to toggle)
burp 3.1.4-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,684 kB
  • sloc: ansic: 50,989; sh: 3,612; cpp: 2,859; makefile: 868
file content (94 lines) | stat: -rw-r--r-- 1,850 bytes parent folder | download | duplicates (3)
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
#include <check.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include "test.h"
#include "../src/base64.h"

struct data
{
        int64_t value;
	const char *where;
	int len;
};

static struct data p[] = {
	{ 0,		"A", 1 },
	{ 1,		"B", 1 },
	{ 2,		"C", 1 },
	{ 10000,	"CcQ", 3 },
	{ -1,		"-B", 2 },
	{ -2,		"-C", 2 },
	{ -10000,	"-CcQ", 4 },
	{ 0x7FFFFFFFFFFFFFFF,	"H//////////", 11 },  // int64 max
	{ -0x7FFFFFFFFFFFFFFF,	"-H//////////", 12 }, // int64 min
	{ 1239054578,	"BJ2njy", 6 },
	{ -1239054578,	"-BJ2njy", 7 },
	{ 999999999999,	"OjUpQ//", 7 },
	{ 16510910,	"++++", 4 },
	// Stuff taken from a genuine manifest file.
	{ 16877,	"EHt", 3 },
	{ 22,		"W", 1 },
	{ 4096,		"BAA", 3 },
	{ 8,		"I", 1 },
	{ 1404621582,	"BTuNMO", 6 },
	{ 1403256802,	"BTo//i", 6 },
	{ 9,		"J", 1 },
};

START_TEST(test_to_base64)
{
	base64_init();
	FOREACH(p)
	{
		char where[32];
		fail_unless(to_base64(p[i].value, where)==p[i].len);
		fail_unless(!strcmp(p[i].where, where));
	}
}
END_TEST

static struct data extra_from[] = {
	// Test some strange input.
	{ 0,		"", 0 },
	{ 10000,	" C$cQ", 5 },
	{ 10000,	"C$cQ", 4 },
	{ 10000,	"C$cQ 82394", 4 },
	{ 10000,	"Cc€Q 82394", 6 },
	{ 9184233123957407608,	"812908312903012923901239081232394", 33 }
};

static void do_from_base64(struct data *d, size_t s)
{
	unsigned int i;
	base64_init();
	for(i=0; i<s; i++)
	{
		int64_t value=0;
		fail_unless(from_base64(&value, d[i].where)==d[i].len);
		fail_unless(value==d[i].value);
	}
}

START_TEST(test_from_base64)
{
	do_from_base64(p, ARR_LEN(p));
	do_from_base64(extra_from, ARR_LEN(extra_from));
}
END_TEST

Suite *suite_base64(void)
{
	Suite *s;
	TCase *tc_core;

	s=suite_create("base64");

	tc_core=tcase_create("Core");

	tcase_add_test(tc_core, test_to_base64);
	tcase_add_test(tc_core, test_from_base64);
	suite_add_tcase(s, tc_core);

	return s;
}