xref: /freebsd/contrib/pam-krb5/tests/style/obsolete-strings-t (revision bf6873c5786e333d679a7838d28812febf479a8a)
1*bf6873c5SCy Schubert#!/usr/bin/perl
2*bf6873c5SCy Schubert#
3*bf6873c5SCy Schubert# Check for obsolete strings in source files.
4*bf6873c5SCy Schubert#
5*bf6873c5SCy Schubert# Examine all source files in a distribution for obsolete strings and report
6*bf6873c5SCy Schubert# on files that fail this check.  This catches various transitions I want to
7*bf6873c5SCy Schubert# do globally in all my packages, like changing my personal URLs to https.
8*bf6873c5SCy Schubert#
9*bf6873c5SCy Schubert# The canonical version of this file is maintained in the rra-c-util package,
10*bf6873c5SCy Schubert# which can be found at <https://www.eyrie.org/~eagle/software/rra-c-util/>.
11*bf6873c5SCy Schubert#
12*bf6873c5SCy Schubert# Copyright 2016, 2018-2020 Russ Allbery <eagle@eyrie.org>
13*bf6873c5SCy Schubert#
14*bf6873c5SCy Schubert# Permission is hereby granted, free of charge, to any person obtaining a
15*bf6873c5SCy Schubert# copy of this software and associated documentation files (the "Software"),
16*bf6873c5SCy Schubert# to deal in the Software without restriction, including without limitation
17*bf6873c5SCy Schubert# the rights to use, copy, modify, merge, publish, distribute, sublicense,
18*bf6873c5SCy Schubert# and/or sell copies of the Software, and to permit persons to whom the
19*bf6873c5SCy Schubert# Software is furnished to do so, subject to the following conditions:
20*bf6873c5SCy Schubert#
21*bf6873c5SCy Schubert# The above copyright notice and this permission notice shall be included in
22*bf6873c5SCy Schubert# all copies or substantial portions of the Software.
23*bf6873c5SCy Schubert#
24*bf6873c5SCy Schubert# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25*bf6873c5SCy Schubert# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26*bf6873c5SCy Schubert# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
27*bf6873c5SCy Schubert# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28*bf6873c5SCy Schubert# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29*bf6873c5SCy Schubert# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30*bf6873c5SCy Schubert# DEALINGS IN THE SOFTWARE.
31*bf6873c5SCy Schubert#
32*bf6873c5SCy Schubert# SPDX-License-Identifier: MIT
33*bf6873c5SCy Schubert
34*bf6873c5SCy Schubertuse 5.010;
35*bf6873c5SCy Schubertuse strict;
36*bf6873c5SCy Schubertuse warnings;
37*bf6873c5SCy Schubert
38*bf6873c5SCy Schubertuse lib "$ENV{C_TAP_SOURCE}/tap/perl";
39*bf6873c5SCy Schubert
40*bf6873c5SCy Schubertuse Test::RRA qw(skip_unless_author);
41*bf6873c5SCy Schubertuse Test::RRA::Automake qw(all_files automake_setup);
42*bf6873c5SCy Schubert
43*bf6873c5SCy Schubertuse File::Basename qw(basename);
44*bf6873c5SCy Schubertuse Test::More;
45*bf6873c5SCy Schubert
46*bf6873c5SCy Schubert# Bad patterns to search for.
47*bf6873c5SCy Schubertmy @BAD_REGEXES = (qr{ http:// \S+ [.]eyrie[.]org }xms);
48*bf6873c5SCy Schubertmy @BAD_STRINGS = qw(rra@stanford.edu RRA_MAINTAINER_TESTS);
49*bf6873c5SCy Schubert
50*bf6873c5SCy Schubert# File names to exclude from this check.
51*bf6873c5SCy Schubertmy %EXCLUDE
52*bf6873c5SCy Schubert  = map { $_ => 1 } qw(NEWS changelog obsolete-strings.t obsolete-strings-t);
53*bf6873c5SCy Schubert
54*bf6873c5SCy Schubert# Only run this test for the package author, since it doesn't indicate any
55*bf6873c5SCy Schubert# user-noticable flaw in the package itself.
56*bf6873c5SCy Schubertskip_unless_author('Obsolete strings tests');
57*bf6873c5SCy Schubert
58*bf6873c5SCy Schubert# Set up Automake testing.
59*bf6873c5SCy Schubertautomake_setup();
60*bf6873c5SCy Schubert
61*bf6873c5SCy Schubert# Check a single file for one of the bad patterns.
62*bf6873c5SCy Schubert#
63*bf6873c5SCy Schubert# $path - Path to the file
64*bf6873c5SCy Schubert#
65*bf6873c5SCy Schubert# Returns: undef
66*bf6873c5SCy Schubertsub check_file {
67*bf6873c5SCy Schubert    my ($path) = @_;
68*bf6873c5SCy Schubert    my $filename = basename($path);
69*bf6873c5SCy Schubert
70*bf6873c5SCy Schubert    # Ignore excluded and binary files.
71*bf6873c5SCy Schubert    return if $EXCLUDE{$filename};
72*bf6873c5SCy Schubert    return if !-T $path;
73*bf6873c5SCy Schubert
74*bf6873c5SCy Schubert    # Scan the file.
75*bf6873c5SCy Schubert    open(my $fh, '<', $path) or BAIL_OUT("Cannot open $path");
76*bf6873c5SCy Schubert    while (defined(my $line = <$fh>)) {
77*bf6873c5SCy Schubert        for my $regex (@BAD_REGEXES) {
78*bf6873c5SCy Schubert            if ($line =~ $regex) {
79*bf6873c5SCy Schubert                ok(0, "$path contains $regex");
80*bf6873c5SCy Schubert                close($fh) or BAIL_OUT("Cannot close $path");
81*bf6873c5SCy Schubert                return;
82*bf6873c5SCy Schubert            }
83*bf6873c5SCy Schubert        }
84*bf6873c5SCy Schubert        for my $string (@BAD_STRINGS) {
85*bf6873c5SCy Schubert            if (index($line, $string) != -1) {
86*bf6873c5SCy Schubert                ok(0, "$path contains $string");
87*bf6873c5SCy Schubert                close($fh) or BAIL_OUT("Cannot close $path");
88*bf6873c5SCy Schubert                return;
89*bf6873c5SCy Schubert            }
90*bf6873c5SCy Schubert        }
91*bf6873c5SCy Schubert    }
92*bf6873c5SCy Schubert    close($fh) or BAIL_OUT("Cannot close $path");
93*bf6873c5SCy Schubert    ok(1, $path);
94*bf6873c5SCy Schubert    return;
95*bf6873c5SCy Schubert}
96*bf6873c5SCy Schubert
97*bf6873c5SCy Schubert# Scan every file for any of the bad patterns or strings.  We don't declare a
98*bf6873c5SCy Schubert# plan since we skip a lot of files and don't want to precalculate the file
99*bf6873c5SCy Schubert# list.
100*bf6873c5SCy Schubertmy @paths = all_files();
101*bf6873c5SCy Schubertfor my $path (@paths) {
102*bf6873c5SCy Schubert    check_file($path);
103*bf6873c5SCy Schubert}
104*bf6873c5SCy Schubertdone_testing();
105