1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.internal.net; 18 19 import static android.system.OsConstants.AF_INET; 20 import static android.system.OsConstants.AF_INET6; 21 import static android.system.OsConstants.AF_UNIX; 22 import static android.system.OsConstants.EPERM; 23 import static android.system.OsConstants.SOCK_DGRAM; 24 import static android.system.OsConstants.SOCK_STREAM; 25 26 import static junit.framework.Assert.assertEquals; 27 28 import static org.junit.Assert.fail; 29 30 import android.os.Build; 31 import android.system.ErrnoException; 32 import android.system.Os; 33 34 import androidx.test.filters.SmallTest; 35 36 import com.android.testutils.DevSdkIgnoreRule; 37 import com.android.testutils.DevSdkIgnoreRunner; 38 39 import libcore.io.IoUtils; 40 41 import org.junit.Test; 42 import org.junit.runner.RunWith; 43 44 @RunWith(DevSdkIgnoreRunner.class) 45 @SmallTest 46 @DevSdkIgnoreRule.IgnoreUpTo(Build.VERSION_CODES.R) 47 public class NetworkUtilsInternalTest { 48 expectSocketSuccess(String msg, int domain, int type)49 private static void expectSocketSuccess(String msg, int domain, int type) { 50 try { 51 IoUtils.closeQuietly(Os.socket(domain, type, 0)); 52 } catch (ErrnoException e) { 53 fail(msg + e.getMessage()); 54 } 55 } 56 expectSocketPemissionError(String msg, int domain, int type)57 private static void expectSocketPemissionError(String msg, int domain, int type) { 58 try { 59 IoUtils.closeQuietly(Os.socket(domain, type, 0)); 60 fail(msg); 61 } catch (ErrnoException e) { 62 assertEquals(msg, e.errno, EPERM); 63 } 64 } 65 expectHasNetworking()66 private static void expectHasNetworking() { 67 expectSocketSuccess("Creating a UNIX socket should not have thrown ErrnoException", 68 AF_UNIX, SOCK_STREAM); 69 expectSocketSuccess("Creating a AF_INET socket shouldn't have thrown ErrnoException", 70 AF_INET, SOCK_DGRAM); 71 expectSocketSuccess("Creating a AF_INET6 socket shouldn't have thrown ErrnoException", 72 AF_INET6, SOCK_DGRAM); 73 } 74 expectNoNetworking()75 private static void expectNoNetworking() { 76 expectSocketSuccess("Creating a UNIX socket should not have thrown ErrnoException", 77 AF_UNIX, SOCK_STREAM); 78 expectSocketPemissionError( 79 "Creating a AF_INET socket should have thrown ErrnoException(EPERM)", 80 AF_INET, SOCK_DGRAM); 81 expectSocketPemissionError( 82 "Creating a AF_INET6 socket should have thrown ErrnoException(EPERM)", 83 AF_INET6, SOCK_DGRAM); 84 } 85 86 @Test testSetAllowNetworkingForProcess()87 public void testSetAllowNetworkingForProcess() { 88 expectHasNetworking(); 89 NetworkUtilsInternal.setAllowNetworkingForProcess(false); 90 expectNoNetworking(); 91 NetworkUtilsInternal.setAllowNetworkingForProcess(true); 92 expectHasNetworking(); 93 } 94 } 95