1/* 2 * Copyright (C) 2011 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 17package com.android.server; 18 19import static com.android.server.NativeDaemonConnector.appendEscaped; 20import static com.android.server.NativeDaemonConnector.makeCommand; 21 22import android.test.AndroidTestCase; 23import android.test.suitebuilder.annotation.MediumTest; 24 25import com.android.server.NativeDaemonConnector.SensitiveArg; 26 27/** 28 * Tests for {@link NativeDaemonConnector}. 29 */ 30@MediumTest 31public class NativeDaemonConnectorTest extends AndroidTestCase { 32 private static final String TAG = "NativeDaemonConnectorTest"; 33 34 public void testArgumentNormal() throws Exception { 35 final StringBuilder builder = new StringBuilder(); 36 37 builder.setLength(0); 38 appendEscaped(builder, ""); 39 assertEquals("", builder.toString()); 40 41 builder.setLength(0); 42 appendEscaped(builder, "foo"); 43 assertEquals("foo", builder.toString()); 44 45 builder.setLength(0); 46 appendEscaped(builder, "foo\"bar"); 47 assertEquals("foo\\\"bar", builder.toString()); 48 49 builder.setLength(0); 50 appendEscaped(builder, "foo\\bar\\\"baz"); 51 assertEquals("foo\\\\bar\\\\\\\"baz", builder.toString()); 52 } 53 54 public void testArgumentWithSpaces() throws Exception { 55 final StringBuilder builder = new StringBuilder(); 56 57 builder.setLength(0); 58 appendEscaped(builder, "foo bar"); 59 assertEquals("\"foo bar\"", builder.toString()); 60 61 builder.setLength(0); 62 appendEscaped(builder, "foo\"bar\\baz foo"); 63 assertEquals("\"foo\\\"bar\\\\baz foo\"", builder.toString()); 64 } 65 66 public void testArgumentWithUtf() throws Exception { 67 final StringBuilder builder = new StringBuilder(); 68 69 builder.setLength(0); 70 appendEscaped(builder, "caf\u00E9 c\u00F6ffee"); 71 assertEquals("\"caf\u00E9 c\u00F6ffee\"", builder.toString()); 72 } 73 74 public void testSensitiveArgs() throws Exception { 75 final StringBuilder rawBuilder = new StringBuilder(); 76 final StringBuilder logBuilder = new StringBuilder(); 77 78 rawBuilder.setLength(0); 79 logBuilder.setLength(0); 80 makeCommand(rawBuilder, logBuilder, 1, "foo", "bar", "baz"); 81 assertEquals("1 foo bar baz\0", rawBuilder.toString()); 82 assertEquals("1 foo bar baz", logBuilder.toString()); 83 84 rawBuilder.setLength(0); 85 logBuilder.setLength(0); 86 makeCommand(rawBuilder, logBuilder, 1, "foo", new SensitiveArg("bar"), "baz"); 87 assertEquals("1 foo bar baz\0", rawBuilder.toString()); 88 assertEquals("1 foo [scrubbed] baz", logBuilder.toString()); 89 90 rawBuilder.setLength(0); 91 logBuilder.setLength(0); 92 makeCommand(rawBuilder, logBuilder, 1, "foo", new SensitiveArg("foo bar"), "baz baz", 93 new SensitiveArg("wat")); 94 assertEquals("1 foo \"foo bar\" \"baz baz\" wat\0", rawBuilder.toString()); 95 assertEquals("1 foo [scrubbed] \"baz baz\" [scrubbed]", logBuilder.toString()); 96 } 97} 98