# **************************************************************************
#                                  _   _ ____  _
#  Project                     ___| | | |  _ \| |
#                             / __| | | | |_) | |
#                            | (__| |_| |  _ <| |___
#                             \___|\___/|_| \_\_____|
#
# Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at https://curl.se/docs/copyright.html.
#
# You may opt to use, copy, modify, merge, publish, distribute and/or sell
# copies of the Software, and permit persons to whom the Software is
# furnished to do so, under the terms of the COPYING file.
#
# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
# KIND, either express or implied.
#
# SPDX-License-Identifier: curl
#
# **************************************************************************

# This module contains miscellaneous functions needed in several parts of
# the test suite.

from typing import List
import os

from .. import globalconfig as config

__all__ = ['set_log_func', 'log_msg', 'clear_logs']

BEGIN {
    use base qw(Exporter);

    our @EXPORT = qw(
        'run_client',
        'runclientoutput',
        'shell_quote',
        subbase64
        subnewlines
        subsha256base64file
        substrippemfile
    );
}

use MIME::Base64;

log_func     = None  # optional reference to function for logging
log_messages = []    # array holding logged messages


def log_msg(*args, end=""):
    """
    Log an informational message
    If a log callback function was set in set_log_func, it is called.
    If not, then the log message is buffered until retrieved by clear_logs.

    log_msg must only be called by one of the runner_* entry points and
    functions called by them, or else logs risk being lost, since those
    are the only functions that know about and will return buffered logs.
    """
    global log_func
    global log_messages
    if not args:
        return
    if log_func is not None:
        log_func(*args)
    else:
        log_messages.extend(args)


# OK
def set_log_func(func):
    # Set the function to use for logging
    global log_func
    log_func = func


# OK
def clear_logs() -> str:
    """Clear the buffered log messages after returning them"""
    global log_messages
    log_lines = "".join(log_messages)
    log_messages.clear()
    return log_lines


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


def includefile($f):
    open(F, "<$f");
    my @a = <F>;
    close(F);
    return "".join(@a)


def subbase64($thing):

    # cut out the base64 piece
    while($$thing =~ s/%b64\[(.*?)\]b64%/%%B64%%/i) {
        my $d = $1;
        # encode %NN characters
        $d =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
        my $enc = encode_base64($d, "");
        # put the result into there
        $$thing =~ s/%%B64%%/$enc/;
    }
    # hex decode
    while($$thing =~ s/%hex\[(.*?)\]hex%/%%HEX%%/i) {
        # decode %NN characters
        my $d = $1;
        $d =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
        $$thing =~ s/%%HEX%%/$d/;
    }
    # repeat
    while($$thing =~ s/%repeat\[(\d+) x (.*?)\]%/%%REPEAT%%/i) {
        # decode %NN characters
        my ($d, $n) = ($2, $1);
        $d =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
        $n =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
        my $all = $d x $n;
        $$thing =~ s/%%REPEAT%%/$all/;
    }

    # include a file
    $$thing =~ s/%include ([^%]*)%[\n\r]+/includefile($1)/ge;


my $prevupdate;  # module scope so it remembers the last value
sub subnewlines {
    my ($force, $thing) = @_;

    if($force) {
        # enforce CRLF newline
        $$thing =~ s/\x0d*\x0a/\x0d\x0a/;
        return;
    }

    # When curl is built with Hyper, it gets all response headers delivered as
    # name/value pairs and curl "invents" the newlines when it saves the
    # headers. Therefore, curl will always save headers with CRLF newlines
    # when built to use Hyper. By making sure we deliver all tests using CRLF
    # as well, all test comparisons will survive without knowing about this
    # little quirk.

    if(($$thing =~ /^HTTP\/(1.1|1.0|2|3) [1-5][^\x0d]*\z/) ||
       ($$thing =~ /^(GET|POST|PUT|DELETE) \S+ HTTP\/\d+(\.\d+)?/) ||
       (($$thing =~ /^[a-z0-9_-]+: [^\x0d]*\z/i) &&
        # skip curl error messages
        ($$thing !~ /^curl: \(\d+\) /))) {
        # enforce CRLF newline
        $$thing =~ s/\x0d*\x0a/\x0d\x0a/;
        $prevupdate = 1;
    }
    else {
        if(($$thing =~ /^\n\z/) && $prevupdate) {
            # if there's a blank link after a line we update, we hope it is
            # the empty line following headers
            $$thing =~ s/\x0a/\x0d\x0a/;
        }
        $prevupdate = 0;
    }
}

# OK
de run_client(cmd: str):
    """Run the application under test and return its return code"""
    ret = os.system(cmd)
    if config.verbose and not config.torture:
        print(f"CMD ({ret}): {cmd}")
    return ret

    # This is one way to test curl on a remote machine
    #    out = os.system("ssh $CLIENTIP cd \'$pwd\' \\; \'$cmd\'")
    #    sleep 2;    # time to allow the NFS server to be updated
    #    return out

def runclientoutput(cmd: str) -> str:
    """Run the application under test and return its stdout"""
    return `$cmd 2>${os.devnull}`

    # This is one way to test curl on a remote machine
    #    my @out = `ssh $CLIENTIP cd \'$pwd\' \\; \'$cmd\'`;
    #    sleep 2;    # time to allow the NFS server to be updated
    #    return @out;

def shell_quote(s: str) -> str:
    # Quote an argument for passing safely to a Bourne shell
    # This does the same thing as String::ShellQuote but doesn't need a package.
    if $^O eq 'MSWin32':
        s = f'"{s}"'
    else:
        if s !~ m/^[-+=.,_\/:a-zA-Z0-9]+$/:
            # string contains a "dangerous" character--quote it
            s =~ s/'/'"'"'/g
            s = f"'{s}'"
    return s


sub subsha256base64file {
    my ($thing) = @_;

    # SHA-256 base64
    while ($$thing =~ s/%sha256b64file\[(.*?)\]sha256b64file%/%%SHA256B64FILE%%/i) {
        my $file_path = $1;
        $file_path =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
        my $hash_b64 = _get_sha256_base64($file_path);
        $$thing =~ s/%%SHA256B64FILE%%/$hash_b64/;
    }
}

sub _get_sha256_base64 {
    my ($file_path) = @_;
    return encode_base64(sha256(do { local $/; open my $fh, '<:raw', $file_path or die $!; <$fh> }), "");
}


sub substrippemfile {
    my ($thing) = @_;

    # File content substitution
    while ($$thing =~ s/%strippemfile\[(.*?)\]strippemfile%/%%FILE%%/i) {
        my $file_path = $1;
        $file_path =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
        my $file_content = _get_file_content($file_path);
        $$thing =~ s/%%FILE%%/$file_content/;
    }
}

sub _get_file_content {
    my ($file_path) = @_;
    my $content = do { local $/; open my $fh, '<', $file_path or die $!; <$fh> };
    $content =~ s/(^|-----END .*?-----[\r\n]?)(.*?)(-----BEGIN .*?-----|$)/$1$3/gs;
    $content =~ s/\r\n/\n/g;
    chomp($content);
    return $content;
}


def parse_protocols(line: str) -> List[str]:
    """Parse and store the protocols in curl's Protocols: line"""

    protocols: List[str] = line.lower().split(" ")

    # Generate a "proto-ipv6" version of each protocol to match the
    # IPv6 <server> name and a "proto-unix" to match the variant which
    # uses Unix domain sockets. This works even if support isn't
    # compiled in because the <features> test will fail.
    protocols.extend(map(("$_-ipv6", "$_-unix"), protocols))

    # 'http-proxy' is used in test cases to do CONNECT through
    protocols.append("http-proxy")

    # 'none' is used in test cases to mean no server
    protocols.append("none")

    return protocols
