Class-Shared

 view release on metacpan or  search on metacpan

t/logic.t  view on Meta::CPAN

			. $Class::Shared::UNIX_TRUST_REASON,
		fe_unix_tls => 'Class::Shared::Frontend->new: a unix socket needs no TLS: '
			. $Class::Shared::UNIX_TRUST_REASON,
		fe_two_tokens  => 'Class::Shared::Frontend->new: give token or token_file, not both',
		fe_tcp_both    => 'Class::Shared::Frontend->new: a TCP backend needs both host and port',
		fe_socket_both => 'Class::Shared::Frontend->new: use either socket or host/port, not both',
		fe_tls_pair    => 'Class::Shared::Frontend->new: a TCP backend needs tls_cert and tls_key: '
			. 'the backend will not accept a client it cannot verify',
		fe_tls_trust => 'Class::Shared::Frontend->new: a TCP backend needs tls_ca or tls_fingerprint: '
			. 'without one it cannot verify the backend',
		fe_tls_both => 'Class::Shared::Frontend->new: give tls_ca or tls_fingerprint, not both: '
			. 'a CA and a pin are alternatives',
	},
);

$config{class_name_re} = qr/\A[A-Za-z_][A-Za-z0-9_]*(?:::[A-Za-z_][A-Za-z0-9_]*)*\z/;
$config{fingerprint_re} = qr/\A(?:sha256\$)?(?:(?:[0-9A-Fa-f]{2}:){31}[0-9A-Fa-f]{2}|[0-9A-Fa-f]{64})\z/;
$config{cwd}      = Cwd::getcwd();
$config{env_dir}  = $ENV{CLASS_SHARED_DIR};
$config{env_xdg}  = $ENV{XDG_RUNTIME_DIR};
$config{max_frame} = $Class::Shared::Protocol::MAX_FRAME;

my $tmp = File::Temp->newdir('class-shared-logic-XXXXXX', TMPDIR => 1, CLEANUP => 1);

my $hex64 = join('', map { sprintf '%02x', $_ } 1 .. $config{sha256_bytes});
my @pairs = $hex64 =~ /(..)/g;
my $colon64 = join($config{colon_separator}, @pairs);
my $pin = $config{sha256_prefix} . ($config{pin_byte} x $config{sha256_bytes});

# ---------------------------------------------------------------- helpers

sub dies_with {
	my ($code, $expected, $name) = @_;

	throws_ok { $code->() } qr/\A\Q$expected\E\n\z/, $name;

	return;
}

sub dies_like {
	my ($code, $pattern, $name) = @_;

	throws_ok { $code->() } $pattern, $name;

	return;
}

sub write_bytes {
	my ($name, $content) = @_;
	my $path = File::Spec->catfile("$tmp", $name);

	open(my $fh, '>:raw', $path) or die "cannot write $path: $!";
	print {$fh} $content or die "cannot write $path: $!";
	close($fh) or die "cannot write $path: $!";

	return $path;
}

sub read_mode {
	my ($path) = @_;
	my @stat = stat($path) or return;

	return $stat[2] & 07777;
}

sub make_dir_with_mode {
	my ($name, $mode) = @_;
	my $dir = File::Spec->catdir("$tmp", $name);

	mkdir($dir, $config{create_mode}) or die "cannot make $dir: $!";
	chmod($mode, $dir) or die "cannot chmod $dir: $!";

	return $dir;
}

# The Z grammar for CLASSNAME, used as a separate oracle: the code checks the
# characters and then each word, so agreement on every short string shows the
# two-step check computes the same set.
sub class_oracle {
	my ($value) = @_;

	return $value =~ $config{class_name_re} ? 1 : 0;
}

sub fingerprint_oracle_value {
	my ($value) = @_;

	my $rest = $value =~ /\Asha256\$(.*)\z/s ? $1 : $value;
	$rest =~ s/://g;

	return $config{sha256_prefix} . lc $rest;
}

# TakeFrame's status algebra: one of -1, 0, 1; a message exactly when 1, an
# error exactly when -1.
sub assert_take_law {
	my ($status, $message, $error, $label) = @_;

	ok($status == -1 || $status == 0 || $status == 1, "$label: status is in {-1, 0, 1}");
	is(defined($message) ? 1 : 0, $status == 1 ? 1 : 0, "$label: a message iff status 1");
	is(defined($error) ? 1 : 0, $status == -1 ? 1 : 0, "$label: an error iff status -1");

	return;
}

sub frame_for {
	my ($body, $secret) = @_;

	return defined $secret
		? pack('N', $config{digest_size} + length $body)
			. Digest::SHA::hmac_sha256($body, $secret) . $body
		: pack('N', length $body) . $body;
}

# A proof that needs the platform asks first and returns at once when the
# answer is no: plan skip_all says why, and the return stops the proof before
# it touches anything, so nothing after the 1..0 line can run.
sub skipped_without_unix {
	my ($what) = @_;

	return 0 if $has_unix_sockets;



( run in 1.182 second using v1.01-cache-2.11-cpan-eb134014566 )