AnyEvent-MCP
view release on metacpan or search on metacpan
t/c02_config.t view on Meta::CPAN
ok $derivation->derive_secret (32) ne $derivation->derive_secret (32, ""),
"empty context field differs from an absent field";
ok $derivation->derive_secret (0, "test") eq "", "derive zero bytes";
ok length ($derivation->derive_secret (16320, "test")) == 16320, "derive maximum byte count";
eval { $derivation->derive_secret (-1, "test") };
ok $@ =~ /length must be an integer/, "negative derivation length rejected";
eval { $derivation->derive_secret (16321, "test") };
ok $@ =~ /length must be an integer/, "derivation cannot exceed the block counter";
eval { $derivation->derive_secret (1.5, "test") };
ok $@ =~ /length must be an integer/, "fractional derivation length rejected";
{
local $derivation->{section}{global}{secret} = "invalid";
eval { $derivation->derive_secret (32, "test") };
ok $@ =~ /missing or invalid secret/, "derivation rejects an invalid master secret";
}
my $automatic = "$dir/automatic/global.ini";
my $auto = AnyEvent::MCP::Config->new (file => $automatic)->load->ensure_secret;
ok -f $automatic && !((stat $automatic)[2] & 077), "automatic private creation";
ok $auto->credential ("token", "example") eq
AnyEvent::MCP::Config->new (file => $automatic)->load->credential ("token", "example"),
"automatic secret persists";
my $sections = AnyEvent::MCP::Config::parse_ini ("root = first\nroot = last\n[rule]\na=one\n[rule]\na=two\n", "test.ini");
ok @$sections == 3 && $sections->[0][0] eq "" && $sections->[0][1]{root} eq "last",
"INI unnamed section and last duplicate key win";
ok $sections->[1][0] eq "rule" && $sections->[1][1]{a} eq "one"
&& $sections->[2][0] eq "rule" && $sections->[2][1]{a} eq "two",
"INI parser preserves repeated section ordering";
my $roundtrip = AnyEvent::MCP::Config::parse_ini (AnyEvent::MCP::Config::serialize_ini $sections);
ok @$roundtrip == 3 && $roundtrip->[1][1]{a} eq "one" && $roundtrip->[2][1]{a} eq "two",
"INI serializer preserves repeated sections and their order";
my $merged = AnyEvent::MCP::Config::merge_sections ($sections, "global");
ok $merged->{global}{root} eq "last" && $merged->{rule}{a} eq "two",
"INI section merging maps unnamed section and uses last repeated section";
eval { AnyEvent::MCP::Config::serialize_ini [["rule", { broken => "x\ny" }]] };
ok $@ =~ /Invalid INI value/, "INI serializer rejects multiline values";
eval { AnyEvent::MCP::Config::serialize_ini [["rule", { "#ignored" => "value" }]] };
ok $@ =~ /Invalid INI key/, "INI serializer rejects keys which would parse as comments";
eval { AnyEvent::MCP::Config::parse_ini ("[rule]\na=b\nbroken\n", "bad.ini") };
ok $@ =~ /bad\.ini:3: invalid INI line/,
"INI errors report filename and line";
$sections = AnyEvent::MCP::Config::parse_ini "";
ok @$sections == 1 && $sections->[0][0] eq "" && !%{$sections->[0][1]},
"empty INI has an empty unnamed section";
my $unicode_ini = "[text]\nvalue = \x{e4}\x{1f431}\n";
$sections = AnyEvent::MCP::Config::parse_ini $unicode_ini;
ok $sections->[1][1]{value} eq "\x{e4}\x{1f431}",
"common INI parser handles Unicode text";
my $unicode_config = "$dir/unicode.ini";
my $lock = AnyEvent::MCP::file_lock $unicode_config;
AnyEvent::MCP::file_save $unicode_config, $unicode_ini;
close $lock;
ok AnyEvent::MCP::file_load ($unicode_config) eq $unicode_ini,
"shared UTF-8 text loader and saver round-trip non-ASCII characters";
open $fh, "<:raw", $unicode_config or die $!;
ok AnyEvent::MCP::file_load ($unicode_config, $fh) eq $unicode_ini,
"UTF-8 loader can read an already open filehandle without closing it";
close $fh;
my $invalid_utf8 = "$dir/invalid-utf8.ini";
my $invalid_lock = AnyEvent::MCP::file_lock $invalid_utf8;
AnyEvent::MCP::file_replace $invalid_utf8, "[text]\nvalue = \xff\n";
close $invalid_lock;
ok !eval { AnyEvent::MCP::file_load $invalid_utf8; 1 },
"shared UTF-8 text loader rejects invalid encoding";
my $cfg = AnyEvent::MCP::Config->new (file => $unicode_config)->load;
ok $cfg->{section}{text}{value} eq "\x{e4}\x{1f431}",
"global configuration uses the same shared UTF-8 INI parser";
ok $cfg->ensure_secret->{section}{global}{secret} eq
AnyEvent::MCP::Config->new (file => $unicode_config)->load->{section}{global}{secret},
"secret creation re-reads Unicode INI bytes and persists the generated secret";
my $partial = "$dir/partial.ini";
open $fh, ">", $partial or die $!;
print {$fh} "# keep this comment\n[mcp-fs]\nexports = /one, /two\n";
close $fh;
chmod 0600, $partial;
my $filled = AnyEvent::MCP::Config->new (file => $partial)->load->ensure_secret;
ok $filled->{section}{"mcp-fs"}{exports} eq "/one, /two", "preserve settings while adding secret";
open $fh, "<", $partial or die $!;
my $first = <$fh>;
close $fh;
ok $first eq "# keep this comment\n", "preserve configuration comments";
my $cached = AnyEvent::MCP::Config::global_config (file => $automatic);
ok $cached == AnyEvent::MCP::Config::global_config (), "global configuration cached";
{
local $ENV{HOME};
local $ENV{XDG_CONFIG_HOME};
ok (AnyEvent::MCP::Config->derive_secret (32, "test") eq $cached->derive_secret (32, "test"),
"class derivation reuses the cached global configuration");
}
ok $config->derive_secret (32, "test") ne AnyEvent::MCP::Config->derive_secret (32, "test"),
"explicit configuration derives from its own master secret";
{
local *File::Path::mkpath = sub { die "cannot create configuration directory\n" };
my $unwritten = AnyEvent::MCP::Config->new (file => "$dir/unsaved/global.ini")->load;
ok !-e $unwritten->{file}, "construct and load do not create a file";
eval { $unwritten->ensure_secret };
ok $@ =~ /cannot create configuration directory/, "secret persistence failure is fatal";
}
{
local *AnyEvent::MCP::random_bytes = sub { die "random source unavailable\n" };
eval { AnyEvent::MCP::Config->new (file => "$dir/no-random.ini")->load->ensure_secret };
ok $@ =~ /random source unavailable\nSupply a securely generated/, "randomness failure explains manual secret";
}
open $fh, ">", "$dir/invalid.ini" or die $!;
print {$fh} "[global]\nsecret = invalid\n";
close $fh;
chmod 0600, "$dir/invalid.ini";
eval { AnyEvent::MCP::Config->new (file => "$dir/invalid.ini")->load->ensure_secret };
ok $@ =~ /invalid secret/, "invalid supplied secret rejected";
ok !Pod::Checker::podchecker ("lib/AnyEvent/MCP.pm"), "global configuration POD";
( run in 0.444 second using v1.01-cache-2.11-cpan-234fa220139 )