1# Before `make install' is performed this script should be runnable with
2# `make test'. After `make install' it should work as `perl test.pl'
3
4######################### We start with some black magic to print on failure.
5
6# Change 1..1 below to 1..last_test_to_print .
7# (It may become useful if the test is moved to ./t subdirectory.)
8
9BEGIN { $| = 1; print "1..22\n"; }
10END {print "not ok 1\n" unless $loaded;}
11#use lib qw (../../blib/lib blib/lib);
12use lib qw (blib/lib);
13require Crypt::License::Notice;
14$loaded = 1;
15print "ok 1\n";
16
17######################### End of black magic.
18
19# Insert your test code below (better if it prints "ok 13"
20# (correspondingly "not ok 13") depending on the success of chunk 13
21# of the test code):
22
23my $test = 2;
24my $tmp = './';
25my $lf = 'License.tmp';
26my $user = `/usr/bin/id -un`;
27chomp $user;
28my $tgt = "$user.bln";
29my $notice = "./notice.tmp";
30
31unlink $lf if (-e $lf);
32unlink $notice if (-e $notice);
33unlink "$tmp/$tgt" if (-e "$tmp/$tgt");
34
35my $License_data = q|
36 just some stuff to put in a file
37
38 ID:	: 54321
39 JUNK:	: a little junk
40|;
41
42my $expected_txt = q|From: root
43To: monkey_see@monkey.do
44Subject: LICENSE EXPIRATION
45
46 ID:| . (split('ID:', $License_data))[1] . "\n";;
47
48my $ptr = {
49	'path'	=> do {$_ = `/bin/pwd`; chomp;$_} . "/$lf",
50	'expires'	=> 12345,	# will write tmp file if detected
51	'TMPDIR'	=> $tmp,
52	'ACTION'	=> "/bin/cat > $notice",
53	'TO'		=> 'monkey_see@monkey.do',
54	'INTERVALS'	=> '2w,4d,8h,16m,32s,64',
55};
56
57@expectedI = (1209600,345600,28800,960,64,32);
58
59#### FAIL tests first
60
61# there is no license file
62print "did not detect missing License file\nnot " if Crypt::License::Notice->check($ptr);
63&OK_test;
64
65# write a temporary license file
66unless (open(LF,">$ptr->{path}")) {
67  print "could not open $lf for write\nnot ";
68} else {
69  print LF $License_data;
70  close LF;
71}
72&OK_test;
73
74$tmp = $ptr->{path};
75delete $ptr->{path};
76print "did not detect missing 'path' hash key\nnot " if Crypt::License::Notice->check($ptr);
77&OK_test;
78
79$ptr->{path} = $tmp;
80
81delete $ptr->{expires};
82
83# there is no expiration
84print "did not detect missing 'expires' hash key\nnot " if Crypt::License::Notice->check($ptr);
85&OK_test;
86
87# expiration is zero
88$ptr->{expires} = 0;
89print "did not detect zero 'expires' hash value\nnot " if Crypt::License::Notice->check($ptr);
90&OK_test;
91
92# expiration is greater than any check value
93$ptr->{expires} = $expectedI[0] +1;
94print "did not check expiration $ptr->{expires}\nnot " unless (@_ = Crypt::License::Notice->check($ptr));
95&OK_test;
96
97# unexpected notice tracking file found
98print "unexpected tracking file $ptr->{TMPDIR}/$user.bln for $ptr->{expires}\nnot " if (-e "$ptr->{TMPDIR}/$user.bln");
99&OK_test;
100
101# unexpected notice found
102print "created unexpected notice for $ptr->{expires}\nnot " if (-e $notice);
103&OK_test;
104
105# wrong number of check arguments
106$tmp = 0;
107++$tmp unless @_ == @expectedI;		# same number of arguments
108print "wrong number of check arguments @_\nnot " if $tmp;
109&OK_test;
110
111$tmp = 0;
112foreach(0..$#_) {
113  ++$tmp unless $_[$_] == $expectedI[$_];
114}
115print "check times @_ do not agree with expected values\nnot " if $tmp;
116&OK_test;
117
118# detect illegal character
119$tmp = $ptr->{INTERVALS};
120$ptr->{INTERVALS} = '12x34';
121eval{Crypt::License::Notice->check($ptr)};
122print "did not detect illegal character string '$ptr->{INTERVALS}'\nnot " unless $@;
123
124&OK_test;
125$ptr->{INTERVALS} = $tmp;
126
127#### PASS tests
128
129# create tracking file and notice, test each epoch
130
131$ptr->{INTERVALS} = '5,2';
132
133my ($ctime, $prev);
134foreach my $chk (5,2) {
135# check that abutting timeout is NOT found
136  &no_find($chk) unless $chk == 5;	# skip first back check
137# check that overflow value is found
138  $ptr->{expires} = $chk;
139  $prev = &next_sec(time);
140  Crypt::License::Notice->check($ptr);
141  $_ = `/bin/cat $notice`;
142  print "$chk, notice text does not match expected\nnot " unless $_ eq $expected_txt;
143  &OK_test;
144  $ctime = (stat("$ptr->{TMPDIR}/$user.bln"))[10];
145  print "ctime $ctime is not now $prev\nnot " unless $ctime == $prev;
146  &OK_test;
147  unlink $notice;
148}
149# check 0 -- expired is not found ever
150&no_find(0);
151&no_find(0);
152
153unlink "$ptr->{TMPDIR}/$user.bln" if ( -e "$ptr->{TMPDIR}/$user.bln");	# clean up
154
155sub next_sec {
156  my ($then) = @_;
157  do { select(undef,undef,undef,0.1); $now = time } while ( $then == $now );	# wait for epoch
158  $now;
159}
160
161sub OK_test {
162  print "ok $test\n";
163  ++$test;
164}
165
166sub no_find {
167  my $chk = $_[0];
168  $ptr->{expires} = $chk + 1;
169  do { $tmp = &next_sec(time) } while ( $tmp < $prev + $ptr->{expires});
170# wait for next epoch
171  Crypt::License::Notice->check($ptr);
172  print "$chk , unexpected notice found for check $ptr->{expires}\nnot " if (-e $notice);
173  &OK_test;
174  $ctime = (stat("$ptr->{TMPDIR}/$user.bln"))[10];
175  print "ctime $ctime should be $prev\nnot " unless $ctime == $prev;
176  &OK_test;
177}
178