| 1 | #!/usr/bin/env perl | ||||||||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use Mojolicious::Lite; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Mojo::File 'path'; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Mojo::Util 'steady_time'; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use DOCSIS::ConfigFile qw( decode_docsis encode_docsis ); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use File::Basename; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use File::Spec::Functions qw( catdir catfile ); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use FindBin; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use YAML::XS; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | BEGIN { unshift @INC, "$FindBin::Bin/../lib" } | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use App::docsisious; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||
| 14 | 1 | ▶ | my $STORAGE = $ENV{DOCSIS_STORAGE} || ($ENV{HOME} ? catdir $ENV{HOME}, '.docsisious' : ''); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$ENV{'DOCSIS_STORAGE'} || ($ENV{'HOME'} ? catdir($ENV{'HOME'}, '.docsisious') : '')
MC/DC0%0/2
$ENV{'DOCSIS_STORAGE'} || ($ENV{'HOME'} ? catdir($ENV{'HOME'}, '.docsisious') : '')
$ENV{'DOCSIS_STORAGE'}
($ENV{'HOME'} ? catdir($ENV{'HOME'}, '.docsisious') : '')
Truth table33%1/3
A || B
A= $ENV{'DOCSIS_STORAGE'}B= ($ENV{'HOME'} ? catdir($ENV{'HOME'}, '.docsisious') : '')
| |||||||||||||||||||||||||||||||||||
| 15 | 1 | ▶ | die "DOCSIS_STORAGE=/path/to/docsis/files need to be set" unless $STORAGE; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 16 | 1 | ▶ | mkdir $STORAGE or die "mkdir $STORAGE: $!" unless -d $STORAGE; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%2/4
-d $STORAGE || (mkdir($STORAGE) || die("mkdir ${STORAGE}: $!"))
mkdir $STORAGE or die "mkdir ${STORAGE}: $!"
MC/DC0%0/2
-d $STORAGE || (mkdir($STORAGE) || die("mkdir ${STORAGE}: $!"))
-d $STORAGE
mkdir $STORAGE
Truth table33%1/3
A || B
A= -d $STORAGEB= mkdir $STORAGE
| |||||||||||||||||||||||||||||||||||
| 17 | 1 | app->log->info("Will store DOCSIS config files in $STORAGE"); | |||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||
| 19 | 1 | get '/' => {layout => 'default'} => 'editor'; | |||||||||||||||||||||||||||||||||
| 20 | 1 | get '/css/docsis/:version' => [format => ['css']] => {template => 'css/docsis'}; | |||||||||||||||||||||||||||||||||
| 21 | 1 | get '/js/docsis/:version' => [format => ['js']] => {template => 'js/docsis'}; | |||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||
| 23 | 2 | ▶ | my $download = sub { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=18.2
| |||||||||||||||||||||||||||||||||||
| 24 | 2 | my $c = shift; | |||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||
| 26 | eval { | ||||||||||||||||||||||||||||||||||
| 27 | 2 | my $config = YAML::XS::Load($c->param('config')); | |||||||||||||||||||||||||||||||||
| 28 | 2 | delete $config->{$_} for qw( CmMic CmtsMic ); | |||||||||||||||||||||||||||||||||
| 29 | 2 | my $binary = encode_docsis $config, {shared_secret => $c->param('shared_secret')}; | |||||||||||||||||||||||||||||||||
| 30 | 2 | ▶ | my $filename = $c->param('filename') || $config->{filename} || sprintf '%s.bin', $c->paste_id; | ||||||||||||||||||||||||||||||||
|
Condition66%4/6
$c->param('filename') || $config->{'filename'} || sprintf('%s.bin', $c->paste_id)
$c->param("filename") or $config->{"filename"}
MC/DC0%0/3
$c->param('filename') || $config->{'filename'} || sprintf('%s.bin', $c->paste_id)
$c->param("filename")
$config->{"filename"}
sprintf('%s.bin', $c->paste_id)
Truth table50%2/4
A or B || C
A= $c->param("filename")B= $config->{"filename"}C= sprintf('%s.bin', $c->paste_id)
| |||||||||||||||||||||||||||||||||||
| 31 | 2 | $filename =~ s![^\w\.-]!-!g; | |||||||||||||||||||||||||||||||||
| 32 | 2 | $c->res->headers->content_disposition("attachment; filename=$filename"); | |||||||||||||||||||||||||||||||||
| 33 | 2 | $c->render(data => $binary, format => 'binary'); | |||||||||||||||||||||||||||||||||
| 34 | 2 | ▶ | } or do { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 35 | 0 | my $err = $@; | |||||||||||||||||||||||||||||||||
| 36 | 0 | chomp $err; | |||||||||||||||||||||||||||||||||
| 37 | 0 | $c->app->log->warn("Could not encode_docsis: $err"); | |||||||||||||||||||||||||||||||||
| 38 | 0 | $err =~ s! at \S+.*$!!s; | |||||||||||||||||||||||||||||||||
| 39 | 0 | $c->render(error => "Cannot encode config file: $err", status => 400); | |||||||||||||||||||||||||||||||||
| 40 | }; | ||||||||||||||||||||||||||||||||||
| 41 | 1 | }; | |||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||
| 43 | 4 | ▶ | my $save = sub { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||
| 44 | 4 | my $c = shift; | |||||||||||||||||||||||||||||||||
| 45 | 4 | my $id = $c->paste_id; | |||||||||||||||||||||||||||||||||
| 46 | 4 | my $config = $c->param('config'); | |||||||||||||||||||||||||||||||||
| 47 | 4 | my %data; | |||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||
| 49 | eval { | ||||||||||||||||||||||||||||||||||
| 50 | 4 | ▶ | die 'Empty config' unless length $config; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 51 | 3 | $data{filename} = $c->param('filename'); | |||||||||||||||||||||||||||||||||
| 52 | 3 | $data{shared_secret} = $c->param('shared_secret'); | |||||||||||||||||||||||||||||||||
| 53 | 3 | $data{ts} = Mojo::Date->new->to_datetime; | |||||||||||||||||||||||||||||||||
| 54 | 3 | $data{config} = YAML::XS::Load($config); | |||||||||||||||||||||||||||||||||
| 55 | 3 | path($STORAGE, "$id.yaml")->spurt(YAML::XS::Dump(\%data)); | |||||||||||||||||||||||||||||||||
| 56 | 3 | $c->redirect_to('edit' => id => $id); | |||||||||||||||||||||||||||||||||
| 57 | 4 | ▶ | } or do { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 58 | 1 | my $err = $@; | |||||||||||||||||||||||||||||||||
| 59 | 1 | chomp $err; | |||||||||||||||||||||||||||||||||
| 60 | 1 | $err =~ s! at \S+.*$!!s; | |||||||||||||||||||||||||||||||||
| 61 | 1 | $c->app->log->debug("Cannot encode config file: $err"); | |||||||||||||||||||||||||||||||||
| 62 | 1 | $c->render(error => "Cannot encode config file: $err", status => 400); | |||||||||||||||||||||||||||||||||
| 63 | }; | ||||||||||||||||||||||||||||||||||
| 64 | 1 | }; | |||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||
| 66 | 1 | ▶ | my $upload = sub { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=8.3
| |||||||||||||||||||||||||||||||||||
| 67 | 1 | my $c = shift; | |||||||||||||||||||||||||||||||||
| 68 | 1 | my $binary = $c->req->upload('binary'); | |||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||
| 70 | eval { | ||||||||||||||||||||||||||||||||||
| 71 | 1 | my $config = decode_docsis($binary->slurp); | |||||||||||||||||||||||||||||||||
| 72 | 1 | $c->param(config => YAML::XS::Dump($config)); | |||||||||||||||||||||||||||||||||
| 73 | 1 | $c->param(filename => basename $binary->filename); | |||||||||||||||||||||||||||||||||
| 74 | 1 | ▶ | } or do { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 75 | 0 | my $err = $@; | |||||||||||||||||||||||||||||||||
| 76 | 0 | chomp $err; | |||||||||||||||||||||||||||||||||
| 77 | 0 | $err =~ s! at \S+.*$!!s; | |||||||||||||||||||||||||||||||||
| 78 | 0 | $c->render(error => "Cannot decode config file: $err", status => 400); | |||||||||||||||||||||||||||||||||
| 79 | }; | ||||||||||||||||||||||||||||||||||
| 80 | 1 | }; | |||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||
| 82 | get( | ||||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | '/edit/example' => {layout => 'default', template => 'editor'} => sub { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||
| 84 | 0 | my $c = shift; | |||||||||||||||||||||||||||||||||
| 85 | 0 | $c->param(config => Mojo::Loader::data_section(__PACKAGE__, 'example.yaml')); | |||||||||||||||||||||||||||||||||
| 86 | 0 | $c->param(filename => 'example.bin'); | |||||||||||||||||||||||||||||||||
| 87 | }, | ||||||||||||||||||||||||||||||||||
| 88 | 1 | 'example', | |||||||||||||||||||||||||||||||||
| 89 | ); | ||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||
| 91 | get( | ||||||||||||||||||||||||||||||||||
| 92 | 1 | ▶ | '/edit/:id' => {layout => 'default', template => 'editor'} => sub { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 93 | 1 | my $c = shift; | |||||||||||||||||||||||||||||||||
| 94 | 1 | my $id = $c->paste_id; | |||||||||||||||||||||||||||||||||
| 95 | 1 | my $data = YAML::XS::Load(path($STORAGE, "$id.yaml")->slurp); | |||||||||||||||||||||||||||||||||
| 96 | 1 | $c->param(config => YAML::XS::Dump($data->{config})); | |||||||||||||||||||||||||||||||||
| 97 | 1 | $c->param(filename => $data->{filename}); | |||||||||||||||||||||||||||||||||
| 98 | 1 | $c->param(shared_secret => $data->{shared_secret}); | |||||||||||||||||||||||||||||||||
| 99 | }, | ||||||||||||||||||||||||||||||||||
| 100 | 1 | 'edit' | |||||||||||||||||||||||||||||||||
| 101 | ); | ||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||
| 103 | post( | ||||||||||||||||||||||||||||||||||
| 104 | 8 | ▶ | '/' => {layout => 'default', template => 'editor'} => sub { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||
| 105 | 8 | my $c = shift; | |||||||||||||||||||||||||||||||||
| 106 | 8 | ▶ | return $c->$save if $c->param('save'); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 107 | 4 | ▶ | return $c->$download if $c->param('download'); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 108 | 2 | ▶ | return $c->$upload if $c->req->upload('binary'); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 109 | 1 | return $c->render(text => "Either download, binary or save need to be present.\n", status => 400); | |||||||||||||||||||||||||||||||||
| 110 | }, | ||||||||||||||||||||||||||||||||||
| 111 | 1 | 'save' | |||||||||||||||||||||||||||||||||
| 112 | ); | ||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||
| 114 | 6 | ▶ | helper paste_id => sub { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.5
| |||||||||||||||||||||||||||||||||||
| 115 | 6 | my $c = shift; | |||||||||||||||||||||||||||||||||
| 116 | 6 | ▶ | my $id = $c->param('id') || Mojo::Util::md5_sum($^T . $$ . steady_time); | ||||||||||||||||||||||||||||||||
|
Condition66%2/3
$c->param('id') || Mojo::Util::md5_sum($^T . $$ . &steady_time())
MC/DC0%0/2
$c->param('id') || Mojo::Util::md5_sum($^T . $$ . &steady_time())
$c->param('id')
Mojo::Util::md5_sum($^T . $$ . &steady_time())
Truth table66%2/3
A || B
A= $c->param('id')B= Mojo::Util::md5_sum($^T . $$ . &steady_time())
| |||||||||||||||||||||||||||||||||||
| 117 | 6 | ▶ | die "Invalid id: $id" unless $id =~ /^\w+$/; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 118 | 6 | ▶ | die "Invalid id: $id" if $id eq 'example'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 119 | 6 | return $id; | |||||||||||||||||||||||||||||||||
| 120 | 1 | }; | |||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||
| 122 | 1 | app->home(Mojo::Home->new(App::docsisious->HOME)); | |||||||||||||||||||||||||||||||||
| 123 | 1 | app->static->paths([app->home->child('public')]); | |||||||||||||||||||||||||||||||||
| 124 | 1 | app->defaults(error => '', VERSION => App::docsisious->VERSION); | |||||||||||||||||||||||||||||||||
| 125 | 1 | app->defaults->{VERSION} =~ s!\W+!-!g; | |||||||||||||||||||||||||||||||||
| 126 | 1 | app->start; | |||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||
| 128 | __DATA__ | ||||||||||||||||||||||||||||||||||