1
0
mirror of https://github.com/libssh2/libssh2.git synced 2026-10-28 17:01:54 +03:00
Files
libssh2/scripts/spacecheck.pl
Viktor Szakats f5821e600d tests: move trusted CA private keys off the test server
They are unused on the server and meant to stay on the issuer machine.
Only the trusted public CA keys are needed on the server.

Reported by Copilot
Bug: https://github.com/libssh2/libssh2/pull/2496#discussion_r3702522974

Closes #2497
2026-08-03 11:32:06 +02:00

297 lines
7.6 KiB
Perl
Executable File

#!/usr/bin/env perl
#***************************************************************************
# _ _ ____ _
# Project ___| | | | _ \| |
# / __| | | | |_) | |
# | (__| |_| | _ <| |___
# \___|\___/|_| \_\_____|
#
# Copyright (C) Viktor Szakats
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at https://curl.se/docs/copyright.html.
#
# You may opt to use, copy, modify, merge, publish, distribute and/or sell
# copies of the Software, and permit persons to whom the Software is
# furnished to do so, under the terms of the COPYING file.
#
# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
# KIND, either express or implied.
#
# SPDX-License-Identifier: curl
#
###########################################################################
use strict;
use warnings;
use File::Basename;
my @tabs = (
'Makefile\.[a-z]+$',
'm4/libssh2-link\.m4$',
);
my @mixed_eol = (
);
my @need_crlf = (
);
my @trailing_ws = (
'tests/keys/.+\.pub$',
);
my @double_empty_lines = (
);
my @longline = (
'^libssh2-style\.el$',
'tests/keys/.+\.pub$',
'tests/openssh_server/authorized_keys$',
'tests/openssh_server/ca_user_keys.pub$',
'tests/openssh_server/sshd_config$',
);
my @non_ascii = (
'AUTHORS',
'RELEASE-NOTES',
);
sub fn_match {
my ($filename, @masklist) = @_;
foreach my $mask (@masklist) {
if($filename =~ $mask) {
return 1;
}
}
return 0;
}
sub eol_detect {
my ($cr, $lf) = @_;
if($cr > 0 && $lf == 0) {
return 'cr';
}
elsif($cr == 0 && $lf > 0) {
return 'lf';
}
elsif($cr == 0 && $lf == 0) {
return 'bin';
}
elsif($cr == $lf) {
return 'crlf';
}
return '';
}
my $max_repeat_space = 79;
my $max_line_len = 192;
my $max_lines = 10000;
my $max_path_len = 64;
my $max_filename_len = 48;
my $issues = 0;
open(my $git_ls_files, '-|', 'git', 'ls-files') or die "Failed running git ls-files: $!";
while(my $filename = <$git_ls_files>) {
chomp $filename;
my @err = ();
if(length($filename) > $max_path_len) {
push @err, sprintf('long (%d > %d) path', length($filename), $max_path_len);
}
my $bn = basename($filename);
if(length($bn) > $max_filename_len) {
push @err, sprintf('long (%d > %d) filename', length($bn), $max_filename_len);
}
if($filename !~ /^[A-Za-z0-9\/._-]+$/) {
push @err, sprintf("filename contains character(s) outside [A-Za-z0-9/._-]");
}
open(my $fh, '<', $filename) or die "Cannot open '$filename': $!";
my $content = do { local $/; <$fh> };
close $fh;
if(!fn_match($filename, @tabs) &&
$content =~ /\t/) {
push @err, 'content: has tab';
}
my $cnt_cr = () = $content =~ /\r/g;
my $cnt_lf = () = $content =~ /\n/g;
my $eol = eol_detect($cnt_cr, $cnt_lf);
if($eol eq '') {
push @err, 'content: has mixed EOL types';
}
if($eol ne 'crlf' &&
fn_match($filename, @need_crlf)) {
push @err, 'content: must use CRLF EOL for this file type';
}
if($eol ne 'lf' && $content ne '' &&
!fn_match($filename, @need_crlf)) {
push @err, 'content: must use LF EOL for this file type';
}
if($cnt_cr > $max_lines) {
push @err, sprintf('content: too many lines (%d > %d)', $cnt_cr, $max_lines);
}
elsif($cnt_lf > $max_lines) {
push @err, sprintf('content: too many lines (%d > %d)', $cnt_lf, $max_lines);
}
if(!fn_match($filename, @trailing_ws) &&
$content =~ /[ \t]\n/) {
my $line;
for my $l (split(/\n/, $content)) {
$line++;
if($l =~ /[ \t]$/) {
push @err, "line $line: trailing whitespace";
}
}
}
if($content ne '' &&
$content !~ /\n\z/) {
push @err, 'content: has no EOL at EOF';
}
if($content =~ /\n\n\z/ ||
$content =~ /\r\n\r\n\z/) {
push @err, 'content: has multiple EOL at EOF';
}
if((!fn_match($filename, @double_empty_lines) &&
($content =~ /\n\n\n/ ||
$content =~ /\r\n\r\n\r\n/)) ||
$content =~ />\n\n\n+[<#]/) {
my $line = 0;
my $blank = 0;
for my $l (split(/\n/, $content)) {
chomp $l;
$line++;
if($l =~ /^$/) {
if($blank) {
my $lineno = sprintf('duplicate empty line @ line %d', $line);
push @err, $lineno;
}
$blank = 1;
}
else {
$blank = 0;
}
}
}
if($content =~ /\n\n\n\n/ ||
$content =~ /\r\n\r\n\r\n\r\n/) {
my $line = 0;
my $blank = 0;
for my $l (split(/\n/, $content)) {
chomp $l;
$line++;
if($l =~ /^$/) {
if($blank > 1) {
my $lineno = sprintf('3 or more consecutive empty lines @ line %d', $line);
push @err, $lineno;
}
$blank++;
}
else {
$blank = 0;
}
}
}
if(!fn_match($filename, @longline)) {
my $line = 0;
for my $l (split(/\n/, $content)) {
$line++;
if(length($l) > $max_line_len) {
push @err, sprintf('line %d: long (%d > %d) line', $line, length($l), $max_line_len);
}
}
}
my $line = 0;
for my $l (split(/\n/, $content)) {
$line++;
if($l =~ /( {$max_repeat_space,})/) {
push @err, sprintf('line %d: repeat spaces (%d >= %d)', $line, length($1), $max_repeat_space);
}
}
my $search = $content;
my $linepos = 0;
while($search =~ /[^ ] "\n *" [^ ]/) {
my $part = substr($search, 0, $+[0]);
$search = substr($search, $+[0]);
my $line = ($part =~ tr/\n//);
push @err, sprintf('line %d: double spaces in folded string', $linepos + $line);
$linepos += $line;
}
$search = $content;
$linepos = 0;
while($search =~ /\n\n *}\n/) {
my $part = substr($search, 0, $+[0] - 1);
$search = substr($search, $+[0]);
my $line = ($part =~ tr/\n//);
push @err, sprintf("line %d: '}' preceded by empty line", $linepos + $line);
$linepos += $line + 1;
}
$search = $content;
$linepos = 0;
while($search =~ /\n\{\n\n/) {
my $part = substr($search, 0, $+[0]);
$search = substr($search, $+[0]);
my $line = ($part =~ tr/\n//);
push @err, sprintf("line %d: top-level '{' followed by empty line", $linepos + $line);
$linepos += $line;
}
if($content =~ /([\x00-\x08\x0b\x0c\x0e-\x1f\x7f])/) {
push @err, 'content: has binary contents';
}
if(!fn_match($filename, @non_ascii) &&
($content =~ /([\x80-\xff]+)/)) {
my $non = $1;
my $hex;
for my $e (split(//, $non)) {
$hex .= sprintf('%s%02x', $hex ? ' ': '', ord($e));
}
my $line;
for my $l (split(/\n/, $content)) {
$line++;
if($l =~ /([\x80-\xff]+)/) {
push @err, "line $line: has non-ASCII: '$non' ($hex)";
}
}
}
if(@err) {
$issues++;
foreach my $err (@err) {
print "$filename: $err\n";
}
}
}
close $git_ls_files;
if($issues) {
exit 1;
}