# -*- tcl -*-
# Commands covered:  base64
#
# This file contains a collection of tests for one or more of the trf
# commands of the TRF extension. Sourcing this file into Tcl runs the
# tests and generates output for errors.  No output means no errors were
# found.
#
# Copyright (c) 1995 Andreas Kupries (andreas_kupries@users.sourceforge.net)
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
# $Id: base64.test,v 1.5 2004/02/18 19:07:18 andreas_kupries Exp $

if {[string compare test [info procs test]] == 1} then {source defs}


test base64-2.0 {base64, ignore illegal characters} {
    set msg [base64 -mode decode aGV!bG8=]
} {he[}


foreach {index string fullencode encode} {
    1 hello {aGVsbG8=
} aGVsbG8=
2 hell  {aGVsbA==
} aGVsbA
} {
    test base64-3.$index {base64, encode string} {
	base64 -mode encode $string
    } $fullencode	;#{}

    test base64-4.$index {base64, decode string} {
	base64 -mode decode $encode
    } $string	;#{}

    # redundant tests following
    test base64-5.$index {base64, encode/decode identity} {
	base64 -mode decode [base64 -mode encode $string]
    } $string	;#{}

    test base64-6.$index {base64, decode/encode identity} {
	base64 -mode encode [base64 -mode decode $fullencode]
    } $fullencode	;#{}
}



test base64-7.1 {base64, partial conversion for attachments} {
    puts -nonewline [set ma [memchan]] aGVsbA
    seek $ma 0
    base64 -mode encode -attach $ma

    set data [read $ma]
    close $ma
    list [string length $data] $data
} {4 hell}


test base64-7.2 {base64, partial conversion for attachments} {
    puts -nonewline [set ma [memchan]] hello
    seek $ma 0

    base64 -mode decode -attach $ma

    set data [read $ma]
    close $ma
    list [string length $data] $data
} {9 {aGVsbG8=
}}

test base64-7.3 {base64, original data, no transform} {
    puts -nonewline [set ma [memchan]] aGVsbA
    seek $ma 0
    set data [read $ma]
    close $ma
    list [string length $data] $data
} {6 aGVsbA}

test base64-7.4 {base64, original data, no transform} {
    puts -nonewline [set ma [memchan]] hello
    seek $ma 0
    set data [read $ma]
    close $ma
    list [string length $data] $data
} {5 hello}



set p [makeFile {aGVsbA} foo]
test base64-7.5 {base64, partial conversion for attachments} {
    set ma [open $p r+]
    base64 -mode encode -attach $ma

    set data [read $ma]
    close $ma
    list [string length $data] $data
} {4 hell}

set p [makeFile {hell} foo]
test base64-7.6 {base64, partial conversion for attachments} {
    set ma [open $p r+]
    base64 -mode decode -attach $ma

    set data [read $ma]
    close $ma
    list [string length $data] $data
} {9 {aGVsbAo=
}}
