From cdcecc282d552a51f8eee1212343d9a12aeb8d2e Mon Sep 17 00:00:00 2001 From: j4kuuu Date: Thu, 11 Jun 2026 16:59:33 +0200 Subject: [PATCH] status: verify TLS on the check.torproject.org request --- lib/Nipe/Component/Utils/Status.pm | 6 ++-- tests/status.t | 50 ++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 tests/status.t diff --git a/lib/Nipe/Component/Utils/Status.pm b/lib/Nipe/Component/Utils/Status.pm index b5596ff..258d125 100644 --- a/lib/Nipe/Component/Utils/Status.pm +++ b/lib/Nipe/Component/Utils/Status.pm @@ -7,11 +7,11 @@ package Nipe::Component::Utils::Status { Readonly my $SUCCESS_CODE => 200; - our $VERSION = '0.0.4'; + our $VERSION = '0.0.5'; sub new { my $api_check = 'https://check.torproject.org/api/ip'; - my $request = HTTP::Tiny -> new -> get($api_check); + my $request = HTTP::Tiny -> new(verify_SSL => 1) -> get($api_check); if ($request -> {status} == $SUCCESS_CODE) { my $data = decode_json($request -> {content}); @@ -26,4 +26,4 @@ package Nipe::Component::Utils::Status { } } -1; \ No newline at end of file +1; diff --git a/tests/status.t b/tests/status.t new file mode 100644 index 0000000..e2bc230 --- /dev/null +++ b/tests/status.t @@ -0,0 +1,50 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use Test::More; +use Test::MockModule; + +use lib './lib'; + +BEGIN { + use_ok('Nipe::Component::Utils::Status'); +} + +my $http_mock = Test::MockModule -> new('HTTP::Tiny'); + +subtest 'reports tor when IsTor is true' => sub { + plan tests => 2; + + $http_mock -> mock('get', sub { + return { status => 200, content => '{"IsTor":true,"IP":"185.220.101.1"}' }; + }); + + my $output = Nipe::Component::Utils::Status -> new(); + ok(index($output, 'Status: true') >= 0, 'status is true'); + ok(index($output, '185.220.101.1') >= 0, 'ip is shown'); +}; + +subtest 'reports not tor when IsTor is false' => sub { + plan tests => 1; + + $http_mock -> mock('get', sub { + return { status => 200, content => '{"IsTor":false,"IP":"8.8.8.8"}' }; + }); + + my $output = Nipe::Component::Utils::Status -> new(); + ok(index($output, 'Status: false') >= 0, 'status is false'); +}; + +subtest 'reports an error when the request fails' => sub { + plan tests => 1; + + $http_mock -> mock('get', sub { + return { status => 599, content => q{} }; + }); + + my $output = Nipe::Component::Utils::Status -> new(); + ok(index($output, 'not possible to establish a connection') >= 0, 'error branch'); +}; + +done_testing();