VAPID
view release on metacpan or search on metacpan
t/01-test.t view on Meta::CPAN
use Test::More;
use MIME::Base64 ();
use Crypt::PK::ECC ();
use VAPID qw/all/;
ok(my ($pub, $priv) = generate_vapid_keys());
ok(validate_subject('mailto:thisusedtobeanemail@gmail.com'));
ok(validate_public_key($pub));
ok(validate_private_key($priv));
ok(validate_expiration(time + 60));
ok(my $header = generate_vapid_header(
'https://fcm.googleapis.com',
'mailto:thisusedtobeanemail@gmail.com',
$pub,
$priv,
time + 60
));
# Test validate_subscription
my $valid_subscription = {
endpoint => 'https://fcm.googleapis.com/fcm/send/test-endpoint',
keys => {
p256dh => $pub,
auth => 'dGVzdGF1dGhrZXkxMjM0NQ'
}
};
ok(validate_subscription($valid_subscription), 'validate_subscription with valid subscription');
# Test invalid subscriptions
eval { validate_subscription() };
ok($@ =~ /No subscription/, 'validate_subscription dies without subscription');
eval { validate_subscription('not a hash') };
ok($@ =~ /must be a hash/, 'validate_subscription dies with non-hash');
eval { validate_subscription({}) };
ok($@ =~ /must have an endpoint/, 'validate_subscription dies without endpoint');
eval { validate_subscription({ endpoint => 'https://example.com' }) };
ok($@ =~ /must have keys/, 'validate_subscription dies without keys');
eval { validate_subscription({ endpoint => 'https://example.com', keys => {} }) };
ok($@ =~ /must have a p256dh/, 'validate_subscription dies without p256dh');
eval { validate_subscription({ endpoint => 'https://example.com', keys => { p256dh => 'test' } }) };
ok($@ =~ /must have an auth/, 'validate_subscription dies without auth');
# Test encrypt_payload
my ($enc_pub, $enc_priv) = generate_vapid_keys();
my $test_subscription = {
endpoint => 'https://fcm.googleapis.com/fcm/send/test',
keys => {
p256dh => $enc_pub,
auth => 'dGVzdGF1dGhrZXkxMjM0NQ'
}
};
ok(my $encrypted = encrypt_payload('Test message', $test_subscription), 'encrypt_payload works');
ok(!ref $encrypted, 'encrypt_payload returns the body as one string');
is(length($encrypted), 86 + length('Test message') + 1 + 16,
'header + plaintext + delimiter + tag');
is(substr($encrypted, 16, 4), pack('N', 4096), 'the record size is in the header');
is(unpack('C', substr($encrypted, 20, 1)), 65, 'and the key id length');
# Test encrypt_payload validation
eval { encrypt_payload() };
ok($@ =~ /No payload/, 'encrypt_payload dies without payload');
( run in 1.245 second using v1.01-cache-2.11-cpan-8379b9b8ec2 )