###############################################################################
#
# socket3.eagle --
#
# Extensible Adaptable Generalized Logic Engine (Eagle)
#
# Copyright (c) 2007-2012 by Joe Mistachkin.  All rights reserved.
#
# See the file "license.terms" for information on usage and redistribution of
# this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
# RCS: @(#) $Id: $
#
###############################################################################

source [file join [file normalize [file dirname [info script]]] prologue.eagle]

###############################################################################

runTest {test socket-3.1 {open/puts/read/close socket} -setup {
  unset -nocomplain z
} -body {
  if {[isEagle]} then {
    set s [socket -sendtimeout $test_timeout $test_host 80]
  } else {
    set s [socket $test_host 80]
  }

  fconfigure $s -translation binary
  set t [time {
    puts -nonewline $s "GET / HTTP/1.1\r\nHOST: $test_host\r\n\r\n"
    flush $s
  }]
  tputs $test_channel [appendArgs "---- done sending to \"" \
      $test_host "\" in " $t \n]

  set t [time {
    #
    # NOTE: It seems this read can return zero bytes in some cases (i.e. deep
    #       down, the ReadByte method on the NetworkStream object returns -1,
    #       meaning end-of-stream, immediately).  This only seems to happen
    #       in the .NET Framework 2.0 RTM on Windows 2000.  There may be some
    #       underlying bug that prevents the data from being sent or received
    #       properly via a NetworkStream.
    #
    set z [read $s 999999]
  }]
  tputs $test_channel [appendArgs "---- done receiving from \"" \
      $test_host "\" in " $t \n]

  close $s; unset s
  set z
} -cleanup {
  if {[info exists s]} then {catch {close $s}}
  unset -nocomplain s t z
} -constraints {command.socket compile.NETWORK network_eagle.to} -match regexp \
-result {(?s)<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4\.01 Transitional//EN"\
"http://www\.w3\.org/TR/html4/loose\.dtd">.*</html>}}

###############################################################################

runTest {test socket-3.2 {Dns using AsyncCallback} -setup {
  proc asyncCallback { ar } {
    set result [list]

    try {
      set ipHostEntry [object invoke -alias System.Net.Dns \
          EndGetHostEntry $ar]

      if {[string length $ipHostEntry] > 0} then {
        lappend result [$ipHostEntry HostName]
        set addresses [$ipHostEntry AddressList]

        object foreach -alias address $addresses {
          #
          # HACK: Check for an IPv4 address and add it to the result.
          #
          if {[string length $address] >= 0} then {
            set address2 [$address ToString]

            if {[regexp -- {^\d+\.\d+\.\d+\.\d+$} $address2]} then {
              lappend result $address2
            }
          }
        }
      }
    } finally {
      unset -nocomplain ar; # dispose
      after $::test_timeout; set ::result $result
    }
  }
} -body {
  set ar [object invoke System.Net.Dns BeginGetHostEntry $test_host \
      asyncCallback null]

  after 0 nop; vwait ::result; set ::result
} -cleanup {
  rename asyncCallback ""

  catch {object removecallback asyncCallback}

  unset -nocomplain ::result
  unset -nocomplain ar; # dispose
} -constraints {eagle compile.NETWORK network_eagle.to} -time true \
-match regexp -result [appendArgs ^ [string map [list . \\.] $test_host] " " \
\\d+\\.\\d+\\.\\d+\\.\\d+\$]}

###############################################################################

runTest {test socket-3.3 {uri upload -inline} -setup {
  #
  # HACK: We do not want to bother using HTTPS for this test.
  #
  set uri [string map [list https: http:] \
      [file join $test_repository test_env]]

  set request [list var1 val1 var2 val2]
} -body {
  set response [uri upload -inline -data $request $uri]

  expr {[regexp -line -- {^var1 = val1  <br />$} $response] && \
      [regexp -line -- {^var2 = val2  <br />$} $response]}
} -cleanup {
  unset -nocomplain response request uri
} -constraints {eagle compile.NETWORK network_eagle.to fossil_repository\
variable_test_repository} -time true -result {True}}

###############################################################################

source [file join [file normalize [file dirname [info script]]] epilogue.eagle]
