-
Notifications
You must be signed in to change notification settings - Fork 0
/
Base64Suite.scala
95 lines (81 loc) · 2.23 KB
/
Base64Suite.scala
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
package jimm.util
import java.io._
import org.scalatest.FunSuite
/**
* Tests the base64 encoder/decoder.
*
* @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
* @see Base64
*/
class Base64Suite extends FunSuite {
test("empty") {
roundTrip("")
}
test("simple") {
roundTrip("This is a test. It is only a test.")
}
test("binary") {
// Don't bother trying 16-bit characters the String class messes up
// the binary representations due to UTF-8 encoding.
roundTrip("\u0000\u0001\u0002\u00ff\u00dc")
}
def roundTrip(text:String) {
// Encode string
var in = new ByteArrayInputStream(text.getBytes())
var out = new ByteArrayOutputStream()
try {
Base64.encode(in, out)
}
catch {
case ioe: IOException => fail(ioe.toString())
}
// Test encoded string
var encoded = out.toString()
if (text.length() > 0)
assert(encoded.length() > text.length())
// Decode
in = new ByteArrayInputStream(encoded.getBytes())
out = new ByteArrayOutputStream()
try {
Base64.decode(in, out)
}
catch {
case ioe: IOException => fail(ioe.toString())
}
// Compare decoded with original
val roundTripResult = out.toString()
assert(roundTripResult === text)
}
test("spaces in encoding") {
val text = "This is a test. It is only a test."
// Encode string
var in = new ByteArrayInputStream(text.getBytes())
var out = new ByteArrayOutputStream()
try {
Base64.encode(in, out)
}
catch {
case ioe: IOException => fail(ioe.toString())
}
// Test encoded string
var encoded = out.toString()
if (text.length() > 0)
assert(encoded.length() > text.length())
// Add whitespace to beginning, middle, and end of string. They should be
// ignored
encoded = "\n " + encoded.substring(0, 5) + "\n " +
encoded.substring(5) + " \n "
// Decode
in = new ByteArrayInputStream(encoded.getBytes())
out = new ByteArrayOutputStream()
try {
Base64.decode(in, out)
}
catch {
case ioe: IOException => fail(ioe.toString())
}
// Compare decoded with original
val roundTripResult = out.toString()
assert(text === roundTripResult)
}
}