146c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme/*
246c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme * Copyright (C) 2016 The Android Open Source Project
346c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme *
446c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme * Licensed under the Apache License, Version 2.0 (the "License");
546c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme * you may not use this file except in compliance with the License.
646c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme * You may obtain a copy of the License at
746c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme *
846c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme *      http://www.apache.org/licenses/LICENSE-2.0
946c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme *
1046c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme * Unless required by applicable law or agreed to in writing, software
1146c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme * distributed under the License is distributed on an "AS IS" BASIS,
1246c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1346c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme * See the License for the specific language governing permissions and
1446c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme * limitations under the License.
1546c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme */
1646c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Lemepackage android.net;
1746c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme
1846c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Lemeimport static android.net.NetworkPolicyManager.MASK_ALL_NETWORKS;
1946c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Lemeimport static android.net.NetworkPolicyManager.MASK_METERED_NETWORKS;
2046c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Lemeimport static android.net.NetworkPolicyManager.RULE_ALLOW_ALL;
2146c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Lemeimport static android.net.NetworkPolicyManager.RULE_ALLOW_METERED;
2246c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Lemeimport static android.net.NetworkPolicyManager.RULE_NONE;
2346c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Lemeimport static android.net.NetworkPolicyManager.RULE_REJECT_ALL;
2446c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Lemeimport static android.net.NetworkPolicyManager.RULE_REJECT_METERED;
2546c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Lemeimport static android.net.NetworkPolicyManager.RULE_TEMPORARY_ALLOW_METERED;
2646c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Lemeimport static android.net.NetworkPolicyManager.uidRulesToString;
2746c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme
2846c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Lemeimport junit.framework.TestCase;
2946c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme
3046c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Lemepublic class NetworkPolicyManagerTest extends TestCase {
3146c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme
3246c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme    public void testUidRulesToString() {
3346c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme        uidRulesToStringTest(RULE_NONE, "0 (NONE)");
3446c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme        uidRulesToStringTest(RULE_ALLOW_METERED, "1 (ALLOW_METERED)");
3546c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme        uidRulesToStringTest(RULE_TEMPORARY_ALLOW_METERED, "2 (TEMPORARY_ALLOW_METERED)");
3646c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme        uidRulesToStringTest(RULE_REJECT_METERED, "4 (REJECT_METERED)");
3746c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme        uidRulesToStringTest(RULE_ALLOW_ALL, "32 (ALLOW_ALL)");
3846c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme        uidRulesToStringTest(RULE_REJECT_ALL, "64 (REJECT_ALL)");
3946c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme
4046c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme        uidRulesToStringTest(RULE_ALLOW_METERED | RULE_ALLOW_ALL,
4146c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme                "33 (ALLOW_METERED|ALLOW_ALL)");
4246c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme        uidRulesToStringTest(RULE_ALLOW_METERED | RULE_REJECT_ALL,
4346c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme                "65 (ALLOW_METERED|REJECT_ALL)");
4446c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme        uidRulesToStringTest(RULE_TEMPORARY_ALLOW_METERED | RULE_ALLOW_ALL,
4546c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme                "34 (TEMPORARY_ALLOW_METERED|ALLOW_ALL)");
4646c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme        uidRulesToStringTest(RULE_TEMPORARY_ALLOW_METERED | RULE_REJECT_ALL,
4746c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme                "66 (TEMPORARY_ALLOW_METERED|REJECT_ALL)");
4846c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme        uidRulesToStringTest(RULE_REJECT_METERED | RULE_ALLOW_ALL,
4946c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme                "36 (REJECT_METERED|ALLOW_ALL)");
5046c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme        uidRulesToStringTest(RULE_REJECT_METERED | RULE_REJECT_ALL,
5146c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme                "68 (REJECT_METERED|REJECT_ALL)");
5246c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme    }
5346c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme
5446c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme    private void uidRulesToStringTest(int uidRules, String expected) {
5546c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme        final String actual = uidRulesToString(uidRules);
5646c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme        assertEquals("Wrong string for uidRules " + uidRules, expected, actual);
5746c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme    }
5846c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme
5946c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme    public void testMeteredNetworksMask() {
6046c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme        assertEquals(RULE_NONE, MASK_METERED_NETWORKS
6146c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme                & RULE_NONE);
6246c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme
6346c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme        assertEquals(RULE_ALLOW_METERED, MASK_METERED_NETWORKS
6446c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme                & RULE_ALLOW_METERED);
6546c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme        assertEquals(RULE_ALLOW_METERED, MASK_METERED_NETWORKS
6646c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme                & (RULE_ALLOW_METERED | RULE_ALLOW_ALL));
6746c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme        assertEquals(RULE_ALLOW_METERED, MASK_METERED_NETWORKS
6846c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme                & (RULE_ALLOW_METERED | RULE_REJECT_ALL));
6946c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme
7046c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme        assertEquals(RULE_TEMPORARY_ALLOW_METERED, MASK_METERED_NETWORKS
7146c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme                & RULE_TEMPORARY_ALLOW_METERED);
7246c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme        assertEquals(RULE_TEMPORARY_ALLOW_METERED, MASK_METERED_NETWORKS
7346c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme                & (RULE_TEMPORARY_ALLOW_METERED | RULE_ALLOW_ALL));
7446c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme        assertEquals(RULE_TEMPORARY_ALLOW_METERED, MASK_METERED_NETWORKS
7546c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme                & (RULE_TEMPORARY_ALLOW_METERED | RULE_REJECT_ALL));
7646c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme
7746c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme        assertEquals(RULE_REJECT_METERED, MASK_METERED_NETWORKS
7846c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme                & RULE_REJECT_METERED);
7946c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme        assertEquals(RULE_REJECT_METERED, MASK_METERED_NETWORKS
8046c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme                & (RULE_REJECT_METERED | RULE_ALLOW_ALL));
8146c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme        assertEquals(RULE_REJECT_METERED, MASK_METERED_NETWORKS
8246c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme                & (RULE_REJECT_METERED | RULE_REJECT_ALL));
8346c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme    }
8446c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme
8546c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme    public void testAllNetworksMask() {
8646c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme        assertEquals(RULE_NONE, MASK_ALL_NETWORKS
8746c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme                & RULE_NONE);
8846c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme
8946c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme        assertEquals(RULE_ALLOW_ALL, MASK_ALL_NETWORKS
9046c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme                & RULE_ALLOW_ALL);
9146c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme        assertEquals(RULE_ALLOW_ALL, MASK_ALL_NETWORKS
9246c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme                & (RULE_ALLOW_ALL | RULE_ALLOW_METERED));
9346c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme        assertEquals(RULE_ALLOW_ALL, MASK_ALL_NETWORKS
9446c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme                & (RULE_ALLOW_ALL | RULE_TEMPORARY_ALLOW_METERED));
9546c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme        assertEquals(RULE_ALLOW_ALL, MASK_ALL_NETWORKS
9646c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme                & (RULE_ALLOW_ALL | RULE_REJECT_METERED));
9746c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme
9846c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme        assertEquals(RULE_REJECT_ALL, MASK_ALL_NETWORKS
9946c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme                & RULE_REJECT_ALL);
10046c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme        assertEquals(RULE_REJECT_ALL, MASK_ALL_NETWORKS
10146c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme                & (RULE_REJECT_ALL | RULE_ALLOW_METERED));
10246c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme        assertEquals(RULE_REJECT_ALL, MASK_ALL_NETWORKS
10346c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme                & (RULE_REJECT_ALL | RULE_TEMPORARY_ALLOW_METERED));
10446c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme        assertEquals(RULE_REJECT_ALL, MASK_ALL_NETWORKS
10546c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme                & (RULE_REJECT_ALL | RULE_REJECT_METERED));
10646c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme    }
10746c4fc357ce8e686b6e7c2adda573e9870a8221fFelipe Leme}
108