16cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot/*
26cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot * Copyright (C) 2009 The Android Open Source Project
36cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot *
46cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot * Licensed under the Apache License, Version 2.0 (the "License");
56cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot * you may not use this file except in compliance with the License.
66cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot * You may obtain a copy of the License at
76cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot *
86cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot *      http://www.apache.org/licenses/LICENSE-2.0
96cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot *
106cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot * Unless required by applicable law or agreed to in writing, software
116cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot * distributed under the License is distributed on an "AS IS" BASIS,
126cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
136cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot * See the License for the specific language governing permissions and
146cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot * limitations under the License.
156cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot */
166cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot
176cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabotpackage com.android.framework.permission.tests;
186cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot
196cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabotimport java.util.ArrayList;
206cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot
216cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabotimport android.telephony.SmsManager;
226cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabotimport android.test.AndroidTestCase;
236cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabotimport android.test.suitebuilder.annotation.SmallTest;
246cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot
256cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot/**
266cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot * Verify that SmsManager apis cannot be called without required permissions.
276cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot */
286cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabotpublic class SmsManagerPermissionTest extends AndroidTestCase {
296cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot
306cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot    private static final String MSG_CONTENTS = "hi";
316cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot    private static final short DEST_PORT = (short)1004;
326cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot    private static final String DEST_NUMBER = "4567";
336cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot    private static final String SRC_NUMBER = "1234";
346cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot
356cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot    /**
366cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot     * Verify that SmsManager.sendTextMessage requires permissions.
376cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot     * <p>Tests Permission:
386cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot     *   {@link android.Manifest.permission#SEND_SMS}.
396cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot     */
406cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot    @SmallTest
416cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot    public void testSendTextMessage() {
426cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot        try {
436cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot            SmsManager.getDefault().sendTextMessage(SRC_NUMBER, DEST_NUMBER, MSG_CONTENTS, null,
446cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot                    null);
456cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot            fail("SmsManager.sendTextMessage did not throw SecurityException as expected");
466cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot        } catch (SecurityException e) {
476cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot            // expected
486cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot        }
496cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot    }
506cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot
516cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot    /**
526cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot     * Verify that SmsManager.sendDataMessage requires permissions.
536cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot     * <p>Tests Permission:
546cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot     *   {@link android.Manifest.permission#SEND_SMS}.
556cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot     */
566cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot    @SmallTest
576cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot    public void testSendDataMessage() {
586cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot        try {
596cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot            SmsManager.getDefault().sendDataMessage(SRC_NUMBER, DEST_NUMBER, DEST_PORT,
606cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot                    MSG_CONTENTS.getBytes(), null, null);
616cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot            fail("SmsManager.sendDataMessage did not throw SecurityException as expected");
626cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot        } catch (SecurityException e) {
636cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot            // expected
646cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot        }
656cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot    }
666cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot
676cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot    /**
686cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot     * Verify that SmsManager.sendMultipartMessage requires permissions.
696cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot     * <p>Tests Permission:
706cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot     *   {@link android.Manifest.permission#SEND_MMS}.
716cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot     */
726cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot    @SmallTest
736cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot    public void testSendMultipartMessage() {
746cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot        try {
756cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot            ArrayList<String> msgParts = new ArrayList<String>(2);
766cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot            msgParts.add(MSG_CONTENTS);
776cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot            msgParts.add("foo");
786cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot            SmsManager.getDefault().sendMultipartTextMessage(SRC_NUMBER, DEST_NUMBER, msgParts,
796cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot                    null, null);
806cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot            fail("SmsManager.sendMultipartTextMessage did not throw SecurityException as expected");
816cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot        } catch (SecurityException e) {
826cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot            // expected
836cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot        }
846cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot    }
856cdd1c8a60f56eabbdb09e4c1231975c96715388Brett Chabot}
86