| 1 |
/* |
| 2 |
* junixsocket |
| 3 |
* |
| 4 |
* Copyright 2009-2022 Christian Kohlschütter |
| 5 |
* |
| 6 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 |
* you may not use this file except in compliance with the License. |
| 8 |
* You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, software |
| 13 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
* See the License for the specific language governing permissions and |
| 16 |
* limitations under the License. |
| 17 |
*/ |
| 18 |
package org.newsclub.net.unix.rmi; |
| 19 |
|
| 20 |
import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
| 21 |
import static org.junit.jupiter.api.Assertions.assertEquals; |
| 22 |
import static org.junit.jupiter.api.Assertions.assertNull; |
| 23 |
|
| 24 |
import java.io.IOException; |
| 25 |
import java.rmi.NotBoundException; |
| 26 |
|
| 27 |
import org.junit.jupiter.api.Test; |
| 28 |
import org.newsclub.net.unix.AFSocketCapability; |
| 29 |
import org.newsclub.net.unix.AFUNIXSocketCredentials; |
| 30 |
|
| 31 |
/** |
| 32 |
* Verifies that peer credentials are properly set when communicating over RMI. |
| 33 |
* |
| 34 |
* @author Christian Kohlschütter |
| 35 |
*/ |
| 36 |
@AFSocketCapabilityRequirement({ |
| 37 |
AFSocketCapability.CAPABILITY_UNIX_DOMAIN, AFSocketCapability.CAPABILITY_PEER_CREDENTIALS}) |
| 38 |
public class RMIPeerCredentialsTest extends TestBase { |
| 39 |
public RMIPeerCredentialsTest() throws IOException { |
| 40 |
super(); |
| 41 |
} |
| 42 |
|
| 43 |
@Test |
| 44 |
public void testRemotePeerCredentials() throws NotBoundException, IOException { |
| 45 |
TestService svc = lookupTestService(); |
| 46 |
|
| 47 |
// CPD-OFF |
| 48 |
AFUNIXSocketCredentials clientCreds = svc.remotePeerCredentials(); |
| 49 |
AFUNIXSocketCredentials serverCreds = RemotePeerInfo.remotePeerCredentials(svc); |
| 50 |
|
| 51 |
if (serverCreds != AFUNIXSocketCredentials.SAME_PROCESS) { // NOPMD |
| 52 |
assertEquals(clientCreds, serverCreds, |
| 53 |
"Since our tests run in the same process, the peer credentials must be identical"); |
| 54 |
assertEquals(clientCreds.toString(), serverCreds.toString(), |
| 55 |
"Since our tests run in the same process, the peer credentials must be identical"); |
| 56 |
assertEquals(clientCreds.getGid(), serverCreds.getGid(), |
| 57 |
"Since our tests run in the same process, the peer credentials must be identical"); |
| 58 |
assertArrayEquals(clientCreds.getGids(), serverCreds.getGids(), |
| 59 |
"Since our tests run in the same process, the peer credentials must be identical"); |
| 60 |
assertEquals(clientCreds.getUid(), serverCreds.getUid(), |
| 61 |
"Since our tests run in the same process, the peer credentials must be identical"); |
| 62 |
assertEquals(clientCreds.getPid(), serverCreds.getPid(), |
| 63 |
"Since our tests run in the same process, the peer credentials must be identical"); |
| 64 |
assertEquals(clientCreds.getUUID(), serverCreds.getUUID(), |
| 65 |
"Since our tests run in the same process, the peer credentials must be identical"); |
| 66 |
} |
| 67 |
// CPD-ON |
| 68 |
|
| 69 |
assertNull(AFUNIXSocketCredentials.remotePeerCredentials(), |
| 70 |
"AFUNIXSocketCredentials.remotePeerCredentials should be null when called outside of an RMI service"); |
| 71 |
} |
| 72 |
} |