3
3
import static org .dcache .rquota .QuotaVfs .GROUP_QUOTA ;
4
4
import static org .dcache .rquota .QuotaVfs .USER_QUOTA ;
5
5
import static org .junit .Assert .*;
6
+ import static org .mockito .ArgumentMatchers .any ;
7
+ import static org .mockito .ArgumentMatchers .anyString ;
6
8
import static org .mockito .Mockito .mock ;
9
+ import static org .mockito .Mockito .when ;
10
+ import org .dcache .nfs .ExportTable ;
11
+ import org .dcache .nfs .FsExport ;
7
12
import org .dcache .nfs .util .UnixSubjects ;
8
13
import org .dcache .oncrpc4j .rpc .RpcAuth ;
9
14
import org .dcache .oncrpc4j .rpc .RpcCall ;
15
+ import org .dcache .oncrpc4j .rpc .RpcTransport ;
10
16
import org .dcache .rquota .xdr .ext_getquota_args ;
17
+ import org .dcache .rquota .xdr .ext_setquota_args ;
11
18
import org .dcache .rquota .xdr .getquota_rslt ;
12
19
import org .dcache .rquota .xdr .qr_status ;
20
+ import org .dcache .rquota .xdr .setquota_rslt ;
13
21
import org .junit .Before ;
14
22
import org .junit .Test ;
15
- import org .mockito .Mockito ;
23
+
24
+ import java .net .InetSocketAddress ;
25
+ import java .net .UnknownHostException ;
16
26
17
27
public class QuotaSvcTest {
18
28
19
29
private QuotaVfs quotaVfs ;
20
30
private QuotaSvc quotaSvc ;
21
31
private RpcCall call ;
22
32
private RpcAuth auth ;
33
+ private ExportTable exportTable ;
34
+ private RpcTransport transport ;
23
35
24
36
@ Before
25
37
public void setUp () {
26
38
quotaVfs = mock (QuotaVfs .class );
27
- quotaSvc = new QuotaSvc (quotaVfs );
39
+ exportTable = mock (ExportTable .class );
40
+ quotaSvc = new QuotaSvc (quotaVfs , exportTable );
28
41
call = mock (RpcCall .class );
29
42
auth = mock (RpcAuth .class );
43
+ when (call .getCredential ()).thenReturn (auth );
44
+
45
+ transport = mock (RpcTransport .class );
46
+ when (transport .getRemoteSocketAddress ()).thenReturn (new InetSocketAddress (0 ));
47
+ when (call .getTransport ()).thenReturn (transport );
30
48
}
31
49
32
50
@ Test
@@ -35,8 +53,7 @@ public void testGetQuotaWrongUser() {
35
53
args .gqa_id = 1 ;
36
54
args .gqa_type = USER_QUOTA ;
37
55
38
- Mockito .when (call .getCredential ()).thenReturn (auth );
39
- Mockito .when (auth .getSubject ()).thenReturn (UnixSubjects .toSubject (2 , 2 ));
56
+ when (auth .getSubject ()).thenReturn (UnixSubjects .toSubject (2 , 2 ));
40
57
getquota_rslt result = quotaSvc .RQUOTAPROC_GETQUOTA_2 (call , args );
41
58
assertEquals (qr_status .Q_EPERM , result .status );
42
59
}
@@ -47,8 +64,7 @@ public void testGetQuotaWrongGroup() {
47
64
args .gqa_id = 1 ;
48
65
args .gqa_type = GROUP_QUOTA ;
49
66
50
- Mockito .when (call .getCredential ()).thenReturn (auth );
51
- Mockito .when (auth .getSubject ()).thenReturn (UnixSubjects .toSubject (2 , 2 ));
67
+ when (auth .getSubject ()).thenReturn (UnixSubjects .toSubject (2 , 2 ));
52
68
getquota_rslt result = quotaSvc .RQUOTAPROC_GETQUOTA_2 (call , args );
53
69
assertEquals (qr_status .Q_EPERM , result .status );
54
70
}
@@ -59,8 +75,7 @@ public void testGetUserQuota() {
59
75
args .gqa_id = 1 ;
60
76
args .gqa_type = USER_QUOTA ;
61
77
62
- Mockito .when (call .getCredential ()).thenReturn (auth );
63
- Mockito .when (auth .getSubject ()).thenReturn (UnixSubjects .toSubject (1 , 1 ));
78
+ when (auth .getSubject ()).thenReturn (UnixSubjects .toSubject (1 , 1 ));
64
79
getquota_rslt result = quotaSvc .RQUOTAPROC_GETQUOTA_2 (call , args );
65
80
assertEquals (qr_status .Q_OK , result .status );
66
81
}
@@ -71,9 +86,64 @@ public void testGetGroupQuota() {
71
86
args .gqa_id = 1 ;
72
87
args .gqa_type = GROUP_QUOTA ;
73
88
74
- Mockito .when (call .getCredential ()).thenReturn (auth );
75
- Mockito .when (auth .getSubject ()).thenReturn (UnixSubjects .toSubject (1 , 1 ));
89
+ when (auth .getSubject ()).thenReturn (UnixSubjects .toSubject (1 , 1 ));
76
90
getquota_rslt result = quotaSvc .RQUOTAPROC_GETQUOTA_2 (call , args );
77
91
assertEquals (qr_status .Q_OK , result .status );
78
92
}
93
+
94
+ @ Test
95
+ public void testSetQuotaNoExport () {
96
+ ext_setquota_args args = new ext_setquota_args ();
97
+ args .sqa_id = 1 ;
98
+ args .sqa_type = GROUP_QUOTA ;
99
+
100
+ when (exportTable .getExport (anyString (), any ())).thenReturn (null );
101
+ when (auth .getSubject ()).thenReturn (UnixSubjects .toSubject (0 , 0 ));
102
+ setquota_rslt result = quotaSvc .RQUOTAPROC_SETQUOTA_2 (call , args );
103
+ assertEquals (qr_status .Q_EPERM , result .status );
104
+ }
105
+
106
+ @ Test
107
+ public void testSetQuotaNotRoot () {
108
+ ext_setquota_args args = new ext_setquota_args ();
109
+ args .sqa_id = 1 ;
110
+ args .sqa_type = GROUP_QUOTA ;
111
+
112
+ when (exportTable .getExport (anyString (), any ())).thenReturn (mock (FsExport .class ));
113
+ when (auth .getSubject ()).thenReturn (UnixSubjects .toSubject (2 , 2 ));
114
+ setquota_rslt result = quotaSvc .RQUOTAPROC_SETQUOTA_2 (call , args );
115
+ assertEquals (qr_status .Q_EPERM , result .status );
116
+ }
117
+
118
+ @ Test
119
+ public void testSetQuotaRootSquashed () throws UnknownHostException {
120
+ ext_setquota_args args = new ext_setquota_args ();
121
+ args .sqa_id = 1 ;
122
+ args .sqa_type = GROUP_QUOTA ;
123
+
124
+ when (exportTable .getExport (anyString (), any ())).thenReturn (new FsExport .FsExportBuilder ()
125
+ .notTrusted ()
126
+ .forClient ("*" )
127
+ .build ("/" ));
128
+
129
+ when (auth .getSubject ()).thenReturn (UnixSubjects .toSubject (0 , 0 ));
130
+ setquota_rslt result = quotaSvc .RQUOTAPROC_SETQUOTA_2 (call , args );
131
+ assertEquals (qr_status .Q_EPERM , result .status );
132
+ }
133
+
134
+ @ Test
135
+ public void testSetQuotaAsRoot () throws UnknownHostException {
136
+ ext_setquota_args args = new ext_setquota_args ();
137
+ args .sqa_id = 1 ;
138
+ args .sqa_type = GROUP_QUOTA ;
139
+
140
+ when (exportTable .getExport (anyString (), any ())).thenReturn (new FsExport .FsExportBuilder ()
141
+ .trusted ()
142
+ .forClient ("*" )
143
+ .build ("/" ));
144
+
145
+ when (auth .getSubject ()).thenReturn (UnixSubjects .toSubject (0 , 0 ));
146
+ setquota_rslt result = quotaSvc .RQUOTAPROC_SETQUOTA_2 (call , args );
147
+ assertEquals (qr_status .Q_OK , result .status );
148
+ }
79
149
}
0 commit comments