Skip to content

Commit

Permalink
Add tests for new CPAN libs
Browse files Browse the repository at this point in the history
  • Loading branch information
sdepassio committed Feb 27, 2025
1 parent a05387e commit f06bc1c
Show file tree
Hide file tree
Showing 4 changed files with 215 additions and 0 deletions.
41 changes: 41 additions & 0 deletions tests/cpan-libraries/crypt-argon2.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/perl

use strict;
use warnings;
use Crypt::Argon2 qw/argon2d_raw argon2i_raw argon2id_raw argon2_pass argon2_verify/;

# Password to hash
my $password = 'my_secure_password';
my $salt = 'random_salt';
my $iterations = 3;
my $memory_cost = 32 * 1024; # in KB
my $parallelism = 1;
my $hash_length = 32;

# Hash with Argon2d
my $argon2d_hash = argon2d_raw($password, $salt, $iterations, $memory_cost, $parallelism, $hash_length);
print("Argon2d hash: " . unpack("H*", $argon2d_hash) . "\n");
# Hash with Argon2i
my $argon2i_hash = argon2i_raw($password, $salt, $iterations, $memory_cost, $parallelism, $hash_length);
print("Argon2i hash: " . unpack("H*", $argon2i_hash) . "\n");
# Hash with Argon2id
my $argon2id_hash = argon2id_raw($password, $salt, $iterations, $memory_cost, $parallelism, $hash_length);
print("Argon2id hash: " . unpack("H*", $argon2id_hash) . "\n");

# Encode password with Argon2d
my $argon2d_encoded = argon2_pass('argon2d', $password, $salt, $iterations, $memory_cost, $parallelism, $hash_length);
print "Argon2d encoded: $argon2d_encoded\n";
# Encode password with Argon2i
my $argon2i_encoded = argon2_pass('argon2i', $password, $salt, $iterations, $memory_cost, $parallelism, $hash_length);
print "Argon2i encoded: $argon2i_encoded\n";
# Encode password with Argon2id
my $argon2id_encoded = argon2_pass('argon2id', $password, $salt, $iterations, $memory_cost, $parallelism, $hash_length);
print "Argon2id encoded: $argon2id_encoded\n";

# Verify password with Argon2d
# print argon2d_verify($argon2d_encoded1, $password) ? "Argon2d password is correct.\n" : "Argon2d password is incorrect.\n";
argon2_verify($argon2d_encoded, $password) ? print "Argon2d password is correct.\n" : print "Argon2d password is incorrect.\n";
# Verify password with Argon2i
argon2_verify($argon2i_encoded, $password) ? print "Argon2i password is correct.\n" : print "Argon2i password is incorrect.\n";
# Verify password with Argon2id
argon2_verify($argon2id_encoded, $password) ? print "Argon2id password is correct.\n" : print "Argon2id password is incorrect.\n";
32 changes: 32 additions & 0 deletions tests/cpan-libraries/json-path.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/perl
use strict;
use warnings;
use JSON::Path;

# Sample Perl data structure
my $data = {
store => {
book => [
{ category => "reference", author => "Nigel Rees", title => "Sayings of the Century", price => 8.95 },
{ category => "fiction", author => "Evelyn Waugh", title => "Sword of Honour", price => 12.99 },
{ category => "fiction", author => "Herman Melville", title => "Moby Dick", isbn => "0-553-21311-3", price => 8.99 },
{ category => "fiction", author => "J. R. R. Tolkien", title => "The Lord of the Rings", isbn => "0-395-19395-8", price => 22.99 }
],
bicycle => {
color => "red",
price => 19.95
}
}
};

# Create a JSON::Path object
my $jpath = JSON::Path->new('$.store.book[*].author');

# Find all authors
my @authors = $jpath->values($data);

# Print authors
print "Authors:\n";
foreach my $author (@authors) {
print "$author\n";
}
111 changes: 111 additions & 0 deletions tests/cpan-libraries/libssh-session.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#!/usr/bin/perl
use strict;
use warnings;
use Libssh::Session qw(:all);
use POSIX qw(WIFEXITED WEXITSTATUS);

# Install SSH server
if (-f "/etc/debian_version") {
system("apt-get update") == 0
or die "Échec de apt-get update: $?";
system("apt-get install -y openssh-server") == 0
or die "Échec de l'installation: $?";
} elsif (-f "/etc/redhat-release") {
system("dnf install -y openssh-server") == 0
or die "Échec de l'installation: $?";
} else {
die "Système d'exploitation non supporté";
}

mkdir("/var/run/sshd") unless -d "/var/run/sshd";

system("ssh-keygen -A") == 0
or die "Échec de génération des clés SSH: $?";

if (getpwnam("testuser")) {
print "L'utilisateur testuser existe déjà\n";
} else {
system("useradd -m testuser") == 0
or die "Échec de création de l'utilisateur: $?";
}

system("echo 'testuser:testpassword' | chpasswd") == 0
or die "Échec de configuration du mot de passe: $?";

# SSH configuration
open(my $fh, '>', '/etc/ssh/sshd_config')
or die "Impossible d'ouvrir sshd_config: $!";
print $fh "Port 2222\n";
print $fh "PermitRootLogin no\n";
print $fh "AllowUsers testuser\n";
print $fh "PasswordAuthentication yes\n";
close($fh);

# Start SSH server
my $pid = fork();
die "Fork failed: $!" unless defined $pid;

if ($pid == 0) {
exec("/usr/sbin/sshd", "-D")
or die "Impossible de démarrer sshd: $!";
}

# Wait and check the port
sleep(5);

# Check the port with Perl
use IO::Socket::INET;
my $sock = IO::Socket::INET->new(
PeerAddr => '127.0.0.1',
PeerPort => 2222,
Proto => 'tcp'
);

die "Le port SSH 2222 n'est pas en écoute" unless $sock;
$sock->close();

# Connection test with Libssh::Session
eval {
my $session = Libssh::Session->new();
$session->options(
host => "127.0.0.1",
port => 2222,
user => "testuser",
LogVerbosity => 1,
PrintError => 1,
Timeout => 10
);

print "Tentative de connexion...\n";
$session->connect() == SSH_OK or die "Échec de connexion: " . $session->get_error();

print "Tentative d'authentification...\n";
$session->auth_password(password => "testpassword") == SSH_AUTH_SUCCESS or die "Échec d'authentification: " . $session->get_error();

print "Test de connexion SSH réussi\n";
$session->disconnect();
};
if ($@) {
kill 'TERM', $pid;
die "Test échoué: $@";
}

# Nettoyage
kill 'TERM', $pid;
waitpid($pid, 0);

# On différencie les serveurs el et debian
if (-f "/etc/debian_version") {
system("apt-get autoremove -y --purge openssh-server") == 0
or die "Échec de la désinstallation: $?";
} elsif (-f "/etc/redhat-release") {
system("dnf autoremove --setopt=keepcache=True -y openssh-server") == 0
or die "Échec de la désinstallation: $?";
} else {
die "Système d'exploitation non supporté";
}

system("userdel -r testuser") == 0
or die "Échec de la suppression de l'utilisateur: $?";

print "Test et nettoyage terminés avec succès\n";
31 changes: 31 additions & 0 deletions tests/cpan-libraries/net-curl.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/perl

use strict;
use warnings;
use Net::Curl::Easy qw(:constants);

# URL à récupérer
my $url = 'https://www.centreon.com';

# Création d'un nouvel objet Curl
my $curl = Net::Curl::Easy->new();

# Configuration de l'URL
$curl->setopt(CURLOPT_URL, $url);

# Variable pour stocker le contenu de la page
my $response_body;

# Configuration de l'option pour écrire la réponse dans la variable
$curl->setopt(CURLOPT_WRITEDATA, \$response_body);

# Exécution de la requête
eval {
$curl->perform();
};
die "Unable to fetch URL $url: $@" if $@;

# Affichage du contenu de la page
print "Response body:\n$response_body\n";

print "Test completed successfully.\n";

0 comments on commit f06bc1c

Please sign in to comment.