<commit_before>#!/usr/bin/env perl

use strict;
use warnings;

use Class::C3;
use MRO::Compat;

use IO::Socket::INET;
use IO::Handle;

# use Log::Log4perl;
# use POSIX ":sys_wait_h";
# use String::Diff;
# use Sys::Hostname;
# use YAML::Syck;
# use Cwd;
# use TAP::DOM;
# use Artemis::MCP::Net;

use Artemis::Schema::TestTools;
use Test::Fixture::DBIC::Schema;
use Artemis::Reports::Receiver::Daemon;

use Test::More;
use Test::Deep;

# -----------------------------------------------------------------------------------------------------------------
construct_fixture( schema  => testrundb_schema,  fixture => 't/fixtures/testrundb/testrun_with_preconditions.yml' );
construct_fixture( schema  => reportsdb_schema,  fixture => 't/fixtures/reportsdb/report.yml' );
construct_fixture( schema  => hardwaredb_schema, fixture => 't/fixtures/hardwaredb/systems.yml' );
# -----------------------------------------------------------------------------------------------------------------

ok(1);

$ENV{MX_DAEMON_STDOUT} ||= '/tmp/artemis_reports_receiver_daemon_test_'.(getpwuid($<) || "unknown").'-stdout.log';
$ENV{MX_DAEMON_STDERR} ||= '/tmp/artemis_reports_receiver_daemon_test_'.(getpwuid($<) || "unknown").'-stderr.log';


my $pid = fork();
if ($pid == 0) {

        my $EUID = `id -u`; chomp $EUID;
        my $EGID = `id -g`; chomp $EGID;
        my $receiver = new Artemis::Reports::Receiver
            (
             port    => 7359,
             pidfile => '/tmp/artemis-reports-receiver-daemon-test-'.(getpwuid($<) || "unknown").'.pid',
             user    => $EUID,
             group   => $EGID,
            );
        $receiver->run;
}
else
{
        sleep 3;
        my $sock = IO::Socket::INET->new( PeerAddr  => 'localhost',
                                          PeerPort  => '7359',
                                          Proto     => 'tcp',
                                          ReuseAddr => 1,
                                        ) or die $!;

        is(ref($sock), 'IO::Socket::INET', "socket created");

        eval {
                local $SIG{ALRM} = sub { die "Timeout!" };
                alarm (3);
                my $answer = <$sock>;
                diag $answer;
                like ($answer, qr/^Artemis::Reports::Receiver\. Protocol is TAP\. Your report id: \d+/, "receiver api");
                $sock->print( "1..2\nok 1 affe\nok 2 zomtec\n");
        };
        alarm(0);
        ok (!$@, "Read and write in time");

        kill 15, $pid;
        sleep 3;
        kill 9, $pid;
}

ok(1);

# END {
#         $receiver->stop();
# }

done_testing();<commit_msg>Reports-Receiver: [TEST] whether tap gets into db<commit_after>#!/usr/bin/env perl

use strict;
use warnings;

use Class::C3;
use MRO::Compat;

use IO::Socket::INET;
use IO::Handle;

# use Log::Log4perl;
# use POSIX ":sys_wait_h";
# use String::Diff;
# use Sys::Hostname;
# use YAML::Syck;
# use Cwd;
# use TAP::DOM;
# use Artemis::MCP::Net;

use Artemis::Schema::TestTools;
use Test::Fixture::DBIC::Schema;
use Artemis::Reports::Receiver::Daemon;
use Artemis::Model 'model';

use Test::More;
use Test::Deep;

# -----------------------------------------------------------------------------------------------------------------
construct_fixture( schema  => testrundb_schema,  fixture => 't/fixtures/testrundb/testrun_with_preconditions.yml' );
construct_fixture( schema  => reportsdb_schema,  fixture => 't/fixtures/reportsdb/report.yml' );
construct_fixture( schema  => hardwaredb_schema, fixture => 't/fixtures/hardwaredb/systems.yml' );
# -----------------------------------------------------------------------------------------------------------------

ok(1);

$ENV{MX_DAEMON_STDOUT} ||= '/tmp/artemis_reports_receiver_daemon_test_'.(getpwuid($<) || "unknown").'-stdout.log';
$ENV{MX_DAEMON_STDERR} ||= '/tmp/artemis_reports_receiver_daemon_test_'.(getpwuid($<) || "unknown").'-stderr.log';


my $pid = fork();
if ($pid == 0) {

        my $EUID = `id -u`; chomp $EUID;
        my $EGID = `id -g`; chomp $EGID;
        my $receiver = new Artemis::Reports::Receiver
            (
             port    => 7359,
             pidfile => '/tmp/artemis-reports-receiver-daemon-test-'.(getpwuid($<) || "unknown").'.pid',
             user    => $EUID,
             group   => $EGID,
            );
        $receiver->run;
}
else
{
        sleep 3;
        my $sock = IO::Socket::INET->new( PeerAddr  => 'localhost',
                                          PeerPort  => '7359',
                                          Proto     => 'tcp',
                                          ReuseAddr => 1,
                                        ) or die $!;

        is(ref($sock), 'IO::Socket::INET', "socket created");

        my $answer;
        my $taptxt = "1..2\nok 1 affe\nok 2 zomtec\n";
        eval {
                local $SIG{ALRM} = sub { die "Timeout!" };
                alarm (3);
                $answer = <$sock>;
                diag $answer;
                like ($answer, qr/^Artemis::Reports::Receiver\. Protocol is TAP\. Your report id: \d+/, "receiver api");
                my $success = $sock->print( $taptxt );
                close $sock; # must! --> triggers the daemon's post_processing hook
        };
        alarm(0);
        ok (!$@, "Read and write in time");

        if (my ($report_id) = $answer =~ m/^Artemis::Reports::Receiver\. Protocol is TAP\. Your report id: (\d+)/){
                my $report = model('ReportsDB')->resultset('Report')->find($report_id);
                is(ref($report), 'Artemis::Schema::ReportsDB::Result::Report', 'Find report in db');
                like($report->tap->tap, qr($taptxt), 'Tap found in db');
        } else {
                diag ('No report ID. Can not search for report');
        }
        kill 15, $pid;
        sleep 3;
        kill 9, $pid;
}

ok(1);

# END {
#         $receiver->stop();
# }

done_testing();
{'fraction_non_alphanumeric': 0.2339270568278202, 'fraction_numerical': 0.008481764206955046, 'mean_word_length': 1.6714997734481196, 'pattern_counts': {'":': 2, '<': 11, '<?xml version=': 0, '>': 46, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3489761', 'n_tokens_mistral': 1933, 'n_tokens_neox': 1813, 'n_words': 389}