summaryrefslogtreecommitdiff
path: root/contrib/test-rpc.pl
blob: 9bae2fdc36bace2b321ef20f6d26d039f52b907a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/perl
#
# Author: T. Zoerner
#
# Testing RPC support
#
# This program is in the public domain and can be used and
# redistributed without restrictions.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

use blib;
use warnings;
use strict;
use Quota;

# ----------------------------------------------------------------------------
##
## insert your test case constants here:
##
my $my_uid = $>;
(my $my_gid = $)) =~ s/ .*//;

my $path         = "/mnt";
my $remote_path  = "/data/tmp/qtest";
my $remote_host  = "localhost";
my $unused_port  = 29875;  # for RPC error testing
my $unknown_host = "UNKNOWN_HOSTNAME";  # for RPC error testing
my $dogrp        = 0;
my $ugid         = ($dogrp ? $my_gid : $my_uid);
my $other_ugid   = ($dogrp ? 2001 : 32000);  # for permission test when not run by admin

my $n_uid_gid= ($dogrp ? "GID" : "UID");  # for use in print output

# ----------------------------------------------------------------------------

sub print_quota_result
{
  my ($bc,$bs,$bh,$bt,$fc,$fs,$fh,$ft) = @_;

  if (defined $bc) {
    if ($bt) {
      my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($bt);
      $bt = sprintf("%04d-%02d-%02d/%02d:%02d", $year+1900,$mon+1,$mday,$hour,$min) if $bt;
    }
    if ($ft) {
      my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($ft);
      $ft = sprintf("%04d-%02d-%02d/%02d:%02d", $year+1900,$mon+1,$mday,$hour,$min) if $ft;
    }

    print "Query results: $bc ($bs,$bh,$bt) $fc ($fs,$fh,$ft)\n";
  }
  else {
    print "Query failed: ". Quota::strerr() ."\n";
  }
}

# ----------------------------------------------------------------------------

print ">>> stage 1: test locally mounted NFS fs: $path\n";
my $dev = Quota::getqcarg($path);
die "Failed to get device for path $path\n" unless $dev;
print "Using device/argument \"$dev\"\n";

print "Query quotas for $n_uid_gid $ugid...\n";
my @qtup = Quota::query($dev, $ugid, $dogrp);
print_quota_result(@qtup);

if (@qtup) {
  print ">>> stage 1b: Repeat with TCP\n";
  Quota::rpcpeer(0, 1);
  my @qtup2 = Quota::query($dev, $ugid, $dogrp);
  print_quota_result(@qtup2);
  if (!@qtup || (@qtup ne @qtup2)) {
    print("ERROR - results not equal\n");
  }
  Quota::rpcpeer(0, 0);

  print ">>> stage 1c: Repeat with explicit authentication\n";
  Quota::rpcauth($my_uid, $my_gid, "localhost");
  @qtup2 = Quota::query($dev, $ugid, $dogrp);
  print_quota_result(@qtup2);
  if (!@qtup || (@qtup ne @qtup2)) {
    print("ERROR - results not equal\n");
  }
  Quota::rpcauth();
}

# -------------------------------------------------------------------------

print ">>> state 2: repeat with different $n_uid_gid $other_ugid...\n";
@qtup = Quota::query($dev, $other_ugid, $dogrp);
print_quota_result(@qtup);

print ">>> stage 2b: Same with fake authentication\n";
Quota::rpcauth(($dogrp ? $my_uid : $other_ugid),
               ($dogrp ? $other_ugid : $my_gid),
               "localhost");
@qtup = Quota::query($dev, $other_ugid, $dogrp);
print_quota_result(@qtup);
Quota::rpcauth();

# -------------------------------------------------------------------------

print ">>> stage 3: force use of RPC to $remote_host:$remote_path\n";
print "Query quotas for $n_uid_gid $ugid\n";
@qtup = Quota::rpcquery($remote_host, $remote_path, $ugid, $dogrp);
print_quota_result(@qtup);

# -------------------------------------------------------------------------

print ">>> stage 4: force use of inactive remote port...\n";
Quota::rpcpeer($unused_port, 1, 2000);
@qtup = Quota::rpcquery($remote_host, $remote_path, $ugid, $dogrp);
if (!@qtup) {
  print "[Failure is expexted] "; # no newline
}
print_quota_result(@qtup);
Quota::rpcpeer();

print ">>> stage 4b: force use of non-existing remote host...\n";
@qtup = Quota::rpcquery($unknown_host, $remote_path, $ugid, $dogrp);
if (!@qtup) {
  print "[Failure is expexted] "; # no newline
}
print_quota_result(@qtup);