1 /*
2  * Copyright (C) 2008 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 android.app.activity;
18 
19 import android.app.AlarmManager;
20 import android.content.Context;
21 import android.test.AndroidTestCase;
22 
23 import androidx.test.filters.LargeTest;
24 
25 import java.util.TimeZone;
26 
27 public class SetTimeZonePermissionsTest extends AndroidTestCase {
28 
29     private String[] mZones;
30     private String mCurrentZone;
31     private AlarmManager mAlarm;
32 
33     @Override
setUp()34     protected void setUp() throws Exception {
35         super.setUp();
36 
37         mZones = TimeZone.getAvailableIDs();
38         mCurrentZone = TimeZone.getDefault().getID();
39         mAlarm = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
40     }
41 
42     /**
43      * Verify that non-system processes cannot set the time zone.
44      */
45     @LargeTest
testSetTimeZonePermissions()46     public void testSetTimeZonePermissions() {
47         /**
48          * Attempt to set several predefined time zones, verifying that the system
49          * system default time zone has not actually changed from its prior state
50          * after each attempt.
51          */
52         int max = (mZones.length > 10) ? mZones.length : 10;
53         assertTrue("No system-defined time zones - test invalid", max > 0);
54 
55         for (int i = 0; i < max; i++) {
56             String tz = mZones[i];
57             try {
58                 mAlarm.setTimeZone(tz);
59             } catch (SecurityException se) {
60                 // Expected failure; no need to handle specially since we're
61                 // about to assert that the test invariant holds: no change
62                 // to the system time zone.
63             }
64 
65             String newZone = TimeZone.getDefault().getID();
66             assertEquals("AlarmManager.setTimeZone() succeeded despite lack of permission",
67                     mCurrentZone,
68                     newZone);
69         }
70     }
71 }
72