# Tests for the base64 module.                              -*- tcl -*- 
#
# This file contains a collection of tests for one or more of the Tcl
# built-in commands.  Sourcing this file into Tcl runs the tests and
# generates output for errors.  No output means no errors were found.
#
# Copyright (c) 1998-2000 by Ajuba Solutions.
# All rights reserved.
#
# RCS: @(#) $Id: base64.test,v 1.17 2011/11/09 04:31:24 andreas_kupries Exp $

# -------------------------------------------------------------------------

source [file join \
	[file dirname [file dirname [file join [pwd] [info script]]]] \
	devtools testutilities.tcl]

testsNeedTcl     8.2
testsNeedTcltest 1.0

testing {
    useLocal base64.tcl base64
}

# -------------------------------------------------------------------------

if {[catch {package present Trf}]} {
    puts "> pure Tcl"
    tcltest::testConstraint trf 0
} else {
    puts "> Trf based"
    tcltest::testConstraint trf 1
}

# -------------------------------------------------------------------------

test base64-1.1 {base64::encode} {
    base64::encode "this is a test\n"
} "dGhpcyBpcyBhIHRlc3QK"
test base64-1.2 {base64::encode wraps lines at 76 characters} {
    set str "The short red fox ran quickly through the green field "
    append str "and jumped over the tall brown bear\n"
    base64::encode $str
} "VGhlIHNob3J0IHJlZCBmb3ggcmFuIHF1aWNrbHkgdGhyb3VnaCB0aGUgZ3JlZW4gZmllbGQgYW5k
IGp1bXBlZCBvdmVyIHRoZSB0YWxsIGJyb3duIGJlYXIK"
test base64-1.3 {base64::encode with wrap length set to 60} {
    set str "The short red fox ran quickly through the green field "
    append str "and jumped over the tall brown bear\n"
    base64::encode -maxlen 60 $str
} "VGhlIHNob3J0IHJlZCBmb3ggcmFuIHF1aWNrbHkgdGhyb3VnaCB0aGUgZ3Jl
ZW4gZmllbGQgYW5kIGp1bXBlZCBvdmVyIHRoZSB0YWxsIGJyb3duIGJlYXIK"
test base64-1.4 {base64::encode with wrap length set to 0} {
    set str "The short red fox ran quickly through the green field "
    append str "and jumped over the tall brown bear\n"
    base64::encode -maxlen 0 $str
} "VGhlIHNob3J0IHJlZCBmb3ggcmFuIHF1aWNrbHkgdGhyb3VnaCB0aGUgZ3JlZW4gZmllbGQgYW5kIGp1bXBlZCBvdmVyIHRoZSB0YWxsIGJyb3duIGJlYXIK"
test base64-1.5 {base64::encode with wrap length set to 76, wrapchar to newline+space} {
    set str "The short red fox ran quickly through the green field "
    append str "and jumped over the tall brown bear\n"
    base64::encode -maxlen 76 -wrapchar "\n " $str
} "VGhlIHNob3J0IHJlZCBmb3ggcmFuIHF1aWNrbHkgdGhyb3VnaCB0aGUgZ3JlZW4gZmllbGQgYW5k
 IGp1bXBlZCBvdmVyIHRoZSB0YWxsIGJyb3duIGJlYXIK"
test base64-1.6 {base64::encode, errors} {
    list [catch {base64::encode} msg] $msg
} [list 1 "wrong # args: should be \"base64::encode ?-maxlen maxlen? ?-wrapchar wrapchar? string\""]
test base64-1.7 {base64::encode, errors} {
    list [catch {base64::encode -maxlen foo} msg] $msg
} [list 1 "value for \"-maxlen\" missing"]
test base64-1.8 {base64::encode, errors} {
    list [catch {base64::encode -maxlen foo bar} msg] $msg
} [list 1 "expected integer but got \"foo\""]
test base64-1.9 {base64::encode, errors} {
    list [catch {base64::encode -maxlen foo -wrapchar bar} msg] $msg
} [list 1 "value for \"-wrapchar\" missing"]
test base64-1.10 {base64::encode, errors} {
    list [catch {base64::encode -foo bar} msg] $msg
} [list 1 "unknown option \"-foo\": must be -maxlen or -wrapchar"]
test base64-1.11 {base64::encode with bogus wrap length (< 0)} {
    set str "The short red fox ran quickly through the green field "
    append str "and jumped over the tall brown bear\n"
    list [catch { base64::encode -maxlen -3 $str } msg] $msg
} {1 {expected positive integer but got "-3"}}
test base64-1.12 {base64::encode with bogus wrap length (non-numeric)} {
    set str "The short red fox ran quickly through the green field "
    append str "and jumped over the tall brown bear\n"
    list [catch { base64::encode -maxlen foo $str } msg] $msg
} {1 {expected integer but got "foo"}}
test base64-1.13 {base64::encode with bogus wrap length (non-integer)} {
    set str "The short red fox ran quickly through the green field "
    append str "and jumped over the tall brown bear\n"
    list [catch { base64::encode -maxlen 1.5 $str } msg] $msg
} {1 {expected integer but got "1.5"}}
test base64-1.14 {base64::encode with wrap length set to 20} {
    set str "The short red fox ran quickly through the green field "
    append str "and jumped over the tall brown bear\n"
    base64::encode -maxlen 20 $str
} "VGhlIHNob3J0IHJlZCBm
b3ggcmFuIHF1aWNrbHkg
dGhyb3VnaCB0aGUgZ3Jl
ZW4gZmllbGQgYW5kIGp1
bXBlZCBvdmVyIHRoZSB0
YWxsIGJyb3duIGJlYXIK"
test base64-1.15 {base64::encode with wrap length set to 23 (prime)} {
    set str "The short red fox ran quickly through the green field "
    append str "and jumped over the tall brown bear\n"
    base64::encode -maxlen 23 $str
} "VGhlIHNob3J0IHJlZCBmb3g
gcmFuIHF1aWNrbHkgdGhyb3
VnaCB0aGUgZ3JlZW4gZmllb
GQgYW5kIGp1bXBlZCBvdmVy
IHRoZSB0YWxsIGJyb3duIGJ
lYXIK"


test base64-2.1 {base64::decode} {
    base64::decode "dGhpcyBpcyBhIHRlc3QK"
} "this is a test\n"
test base64-2.2 {base64::decode ignores newlines} {
    set str "VGhlIHNob3J0IHJlZCBmb3ggcmFuIHF1aWNrbHkgdGhyb3VnaCB0aGUgZ3Jl\n"
    append str "ZW4gZmllbGQgYW5kIGp1bXBlZCBvdmVyIHRoZSB0YWxsIGJyb3duIGJlYXIK"
    base64::decode $str
} "The short red fox ran quickly through the green field and jumped over the tall brown bear\n"
test base64-2.3 {base64::decode handles equal sign padding} {
    # decode the encoding of a string that will be padded in the encoding with
    # one padding char
    base64::decode [base64::encode "01234"]
} "01234"
test base64-2.4 {base64::decode handles equal sign padding} {
    # decode the encoding of a string that will be padded in the encoding with
    # two padding chars
    base64::decode [base64::encode "0123"]
} "0123"


test base64-2.5 {base64::decode} {
    base64::decode ""
} ""
test base64-2.6 {base64::decode} {
    base64::decode " "
} ""


test base64-3.1 {base64 identity test} {
	base64::decode [base64::encode "this is a test"]
} "this is a test"
test base64-3.2 {base64 identity test} {
    # This test fails on version 1.5 because of the format %04x bug
    # when handling the last characters
    set x \f\xee
    set y [base64::decode [base64::encode $x]]
    string compare $x $y
} 0

# For trf a known bug.
test base64-4.0 {base64 -- sf bug 2976290} {!trf} {
    list [catch {
	::base64::decode s=GQMRAk5WXhsABh0NEx4RXBocBVgBHQMXHRgEFltMQENQXEFOExJVQ0RAQERUQ0dAEhYEExVIRRVVFENWKxMKABsPGBI6LRoYLhsEFhsXGFkXEwZXGQMIHg==
    } msg] $msg
} {1 {Not enough data to process padding}}

testsuiteCleanup
return
