aboutsummaryrefslogtreecommitdiff
path: root/v_windows/v/old/vlib/encoding/base64/base64_test.v
blob: e461330b9281a3572bdd0a8a76388310cbf18196 (plain)
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
import encoding.base64

struct TestPair {
	decoded string
	encoded string
}

const (
	pairs    = [
		// RFC 3548 examples
		TestPair{'\x14\xfb\x9c\x03\xd9\x7e', 'FPucA9l+'},
		TestPair{'\x14\xfb\x9c\x03\xd9', 'FPucA9k='},
		TestPair{'\x14\xfb\x9c\x03', 'FPucAw=='},
		// RFC 4648 examples
		TestPair{'', ''},
		TestPair{'f', 'Zg=='},
		TestPair{'fo', 'Zm8='},
		TestPair{'foo', 'Zm9v'},
		TestPair{'foob', 'Zm9vYg=='},
		TestPair{'fooba', 'Zm9vYmE='},
		TestPair{'foobar', 'Zm9vYmFy'},
		// Wikipedia examples
		TestPair{'sure.', 'c3VyZS4='},
		TestPair{'sure', 'c3VyZQ=='},
		TestPair{'sur', 'c3Vy'},
		TestPair{'su', 'c3U='},
		TestPair{'leasure.', 'bGVhc3VyZS4='},
		TestPair{'easure.', 'ZWFzdXJlLg=='},
		TestPair{'asure.', 'YXN1cmUu'},
		TestPair{'sure.', 'c3VyZS4='},
	]

	man_pair = TestPair{'Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.', 'TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4='}
)

fn test_decode() {
	assert base64.decode(man_pair.encoded) == man_pair.decoded.bytes()

	// Test for incorrect padding.
	assert base64.decode('aGk') == ''.bytes()
	assert base64.decode('aGk=') == 'hi'.bytes()
	assert base64.decode('aGk==') == ''.bytes()

	for i, p in pairs {
		got := base64.decode(p.encoded)
		if got != p.decoded.bytes() {
			eprintln('pairs[$i]: expected = $p.decoded, got = $got')
			assert false
		}
	}
}

fn test_decode_str() {
	assert base64.decode_str(man_pair.encoded) == man_pair.decoded

	// Test for incorrect padding.
	assert base64.decode_str('aGk') == ''
	assert base64.decode_str('aGk=') == 'hi'
	assert base64.decode_str('aGk==') == ''

	for i, p in pairs {
		got := base64.decode_str(p.encoded)
		if got != p.decoded {
			eprintln('pairs[$i]: expected = $p.decoded, got = $got')
			assert false
		}
	}
}

fn test_encode() {
	assert base64.encode(man_pair.decoded.bytes()) == man_pair.encoded

	for i, p in pairs {
		got := base64.encode(p.decoded.bytes())
		if got != p.encoded {
			eprintln('pairs[$i]: expected = $p.encoded, got = $got')
			assert false
		}
	}
}

fn test_encode_str() {
	assert base64.encode_str(man_pair.decoded) == man_pair.encoded

	for i, p in pairs {
		got := base64.encode_str(p.decoded)
		if got != p.encoded {
			eprintln('pairs[$i]: expected = $p.encoded, got = $got')
			assert false
		}
	}
}

fn test_url_encode() {
	test := base64.url_encode('Hello Base64Url encoding!'.bytes())
	assert test == 'SGVsbG8gQmFzZTY0VXJsIGVuY29kaW5nIQ'
}

fn test_url_encode_str() {
	test := base64.url_encode_str('Hello Base64Url encoding!')
	assert test == 'SGVsbG8gQmFzZTY0VXJsIGVuY29kaW5nIQ'
}

fn test_url_decode() {
	test := base64.url_decode('SGVsbG8gQmFzZTY0VXJsIGVuY29kaW5nIQ')
	assert test == 'Hello Base64Url encoding!'.bytes()
}

fn test_url_decode_str() {
	test := base64.url_decode_str('SGVsbG8gQmFzZTY0VXJsIGVuY29kaW5nIQ')
	assert test == 'Hello Base64Url encoding!'
}

fn test_encode_null_byte() {
	assert base64.encode([byte(`A`), 0, `C`]) == 'QQBD'
}

fn test_encode_null_byte_str() {
	// While this works, bytestr() does a memcpy
	s := [byte(`A`), 0, `C`].bytestr()
	assert base64.encode_str(s) == 'QQBD'
}

fn test_decode_null_byte() {
	assert base64.decode('QQBD') == [byte(`A`), 0, `C`]
}

fn test_decode_null_byte_str() {
	// While this works, bytestr() does a memcpy
	s := [byte(`A`), 0, `C`].bytestr()
	assert base64.decode_str('QQBD') == s
}