#
# Commands covered: nacl::stream
#

#
# (c) 2016 Alexander Schoepe
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#

if {[lsearch [namespace children] ::tcltest] == -1} {
  package require tcltest 2
  namespace import -force ::tcltest::*
}

package require nacl

if {[lsearch -exact [nacl::randombytes names] urandom] > -1} {
  nacl::randombytes source urandom
}

set k1 [binary decode hex 1b27556473e985d462cd51197a9a46c76009549eac6474f206c4ee0844f68389]
set n1 [binary decode hex 69696ee955b62b73cd62bda875fc73d68219e0036b7a0b37]
set h1 [binary decode hex 662b9d0e3463029156069b12f918691a98f7dfb2ca0393c96bbfc6b1fbd630a2]
set s1 [binary decode hex eea6a7251c1e72916d11c2cb214d3c252539121d8e234e652d651fa4c8cff880]
set m1 [binary decode hex be075fc53c81f2d5cf141316ebeb0c7b5228c52a4c62cbd44b66849b64244ffce5ecbaaf33bd751a1ac728d45e6c61296cdc3c01233561f41db66cce314adb310e3be8250c46f06dceea3a7fa1348057e2f6556ad6b1318a024a838f21af1fde048977eb48f59ffd4924ca1c60902e52f0a089bc76897040e082f937763848645e0705]
set c1 [binary decode hex 8e993b9f48681273c29650ba32fc76ce48332ea7164d96a4476fb8c531a1186ac0dfc17c98dce87b4da7f011ec48c97271d2c20f9b928fe2270d6fb863d51738b48eeee314a7cc8ab932164548e526ae90224368517acfeabd6bb3732bc0e9da99832b61ca01b6de56244a9e88d5f9b37973f622a43d14a6599b1f654cb45a74e355a5]
set m2 "testing...\n"
set c2 [binary decode hex 9ac3d451757015bf433fc8]

test stream-1.1 {stream takes args} -body {
  nacl::stream
} -returnCodes error -result "wrong # args: should be \"nacl::stream ?command? ...\""

test stream-1.2 {stream info} {
  list [catch {nacl::stream info} l] $l
} {0 {cipher+ 0 nonce 24 public-key 32}}

test stream-1.3 {stream generate 4194304} {
  list [nacl::stream generate stream 4194304 $n1 $k1] [nacl::hash -sha256 hash $stream] $hash
} [list 0 0 $h1]

test stream-1.4 {stream generate 32} {
  list [nacl::stream generate stream 32 $n1 $k1] $stream
} [list 0 $s1]

test stream-1.5 {stream 163} {
  list [nacl::stream cipher [string repeat \x00 32]$m1 $n1 $k1] [string range $cipher 32 end]
} [list 0 $c1]

test stream-1.6 {stream encrypt} {
  list [nacl::stream cipher $m2 $n1 $k1] $cipher
} [list 0 $c2]

test stream-1.7 {stream decrypt} {
  list [nacl::stream message $c2 $n1 $k1] $message
} [list 0 $m2]

# cleanup
::tcltest::cleanupTests
return
