1 /* 2 * Copyright (C) 2019 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.protolog; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertNull; 21 22 import android.os.Build; 23 import android.platform.test.annotations.Presubmit; 24 25 import com.google.common.truth.Truth; 26 27 import org.junit.Assume; 28 import org.junit.Before; 29 import org.junit.Test; 30 import org.junit.runner.RunWith; 31 import org.junit.runners.JUnit4; 32 33 import perfetto.protos.ProtologCommon; 34 35 import java.io.File; 36 import java.io.IOException; 37 38 @Presubmit 39 @RunWith(JUnit4.class) 40 public class ProtoLogViewerConfigReaderTest { 41 private static final String TEST_GROUP_NAME = "MY_TEST_GROUP"; 42 private static final String TEST_GROUP_TAG = "TEST"; 43 44 private static final String OTHER_TEST_GROUP_NAME = "MY_OTHER_TEST_GROUP"; 45 private static final String OTHER_TEST_GROUP_TAG = "OTHER_TEST"; 46 47 private static final byte[] TEST_VIEWER_CONFIG = 48 perfetto.protos.Protolog.ProtoLogViewerConfig.newBuilder() 49 .addGroups( 50 perfetto.protos.Protolog.ProtoLogViewerConfig.Group.newBuilder() 51 .setId(1) 52 .setName(TEST_GROUP_NAME) 53 .setTag(TEST_GROUP_TAG) 54 ).addGroups( 55 perfetto.protos.Protolog.ProtoLogViewerConfig.Group.newBuilder() 56 .setId(2) 57 .setName(OTHER_TEST_GROUP_NAME) 58 .setTag(OTHER_TEST_GROUP_TAG) 59 ).addMessages( 60 perfetto.protos.Protolog.ProtoLogViewerConfig.MessageData.newBuilder() 61 .setMessageId(1) 62 .setMessage("My Test Log Message 1 %b") 63 .setLevel(ProtologCommon.ProtoLogLevel.PROTOLOG_LEVEL_DEBUG) 64 .setGroupId(1) 65 ).addMessages( 66 perfetto.protos.Protolog.ProtoLogViewerConfig.MessageData.newBuilder() 67 .setMessageId(2) 68 .setMessage("My Test Log Message 2 %b") 69 .setLevel(ProtologCommon.ProtoLogLevel.PROTOLOG_LEVEL_VERBOSE) 70 .setGroupId(1) 71 ).addMessages( 72 perfetto.protos.Protolog.ProtoLogViewerConfig.MessageData.newBuilder() 73 .setMessageId(3) 74 .setMessage("My Test Log Message 3 %b") 75 .setLevel(ProtologCommon.ProtoLogLevel.PROTOLOG_LEVEL_WARN) 76 .setGroupId(1) 77 ).addMessages( 78 perfetto.protos.Protolog.ProtoLogViewerConfig.MessageData.newBuilder() 79 .setMessageId(4) 80 .setMessage("My Test Log Message 4 %b") 81 .setLevel(ProtologCommon.ProtoLogLevel.PROTOLOG_LEVEL_ERROR) 82 .setGroupId(2) 83 ).addMessages( 84 perfetto.protos.Protolog.ProtoLogViewerConfig.MessageData.newBuilder() 85 .setMessageId(5) 86 .setMessage("My Test Log Message 5 %b") 87 .setLevel(ProtologCommon.ProtoLogLevel.PROTOLOG_LEVEL_WTF) 88 .setGroupId(2) 89 ).build().toByteArray(); 90 91 private final ViewerConfigInputStreamProvider mViewerConfigInputStreamProvider = 92 () -> new AutoClosableProtoInputStream(TEST_VIEWER_CONFIG); 93 94 private ProtoLogViewerConfigReader mConfig; 95 96 @Before before()97 public void before() { 98 mConfig = new ProtoLogViewerConfigReader(mViewerConfigInputStreamProvider); 99 } 100 101 @Test getViewerString_notLoaded()102 public void getViewerString_notLoaded() { 103 assertNull(mConfig.getViewerString(1)); 104 } 105 106 @Test loadViewerConfig()107 public void loadViewerConfig() { 108 mConfig.loadViewerConfig(new String[] { TEST_GROUP_NAME }); 109 assertEquals("My Test Log Message 1 %b", mConfig.getViewerString(1)); 110 assertEquals("My Test Log Message 2 %b", mConfig.getViewerString(2)); 111 assertEquals("My Test Log Message 3 %b", mConfig.getViewerString(3)); 112 assertNull(mConfig.getViewerString(4)); 113 assertNull(mConfig.getViewerString(5)); 114 } 115 116 @Test unloadViewerConfig()117 public void unloadViewerConfig() { 118 mConfig.loadViewerConfig(new String[] { TEST_GROUP_NAME, OTHER_TEST_GROUP_NAME }); 119 mConfig.unloadViewerConfig(new String[] { TEST_GROUP_NAME }); 120 assertNull(mConfig.getViewerString(1)); 121 assertNull(mConfig.getViewerString(2)); 122 assertNull(mConfig.getViewerString(3)); 123 assertEquals("My Test Log Message 4 %b", mConfig.getViewerString(4)); 124 assertEquals("My Test Log Message 5 %b", mConfig.getViewerString(5)); 125 126 mConfig.unloadViewerConfig(new String[] { OTHER_TEST_GROUP_NAME }); 127 assertNull(mConfig.getViewerString(4)); 128 assertNull(mConfig.getViewerString(5)); 129 } 130 131 @Test viewerConfigIsOnDevice()132 public void viewerConfigIsOnDevice() { 133 Assume.assumeFalse(Build.FINGERPRINT.contains("robolectric")); 134 135 final String[] viewerConfigPaths; 136 if (android.tracing.Flags.perfettoProtologTracing()) { 137 viewerConfigPaths = new String[] { 138 "/system_ext/etc/wmshell.protolog.pb", 139 "/system/etc/core.protolog.pb", 140 }; 141 } else { 142 viewerConfigPaths = new String[] { 143 "/system_ext/etc/wmshell.protolog.json.gz", 144 "/system/etc/protolog.conf.json.gz", 145 }; 146 } 147 148 for (final var viewerConfigPath : viewerConfigPaths) { 149 File f = new File(viewerConfigPath); 150 151 Truth.assertWithMessage(f.getAbsolutePath() + " exists").that(f.exists()).isTrue(); 152 } 153 154 } 155 156 @Test loadUnloadAndReloadViewerConfig()157 public void loadUnloadAndReloadViewerConfig() { 158 loadViewerConfig(); 159 unloadViewerConfig(); 160 loadViewerConfig(); 161 unloadViewerConfig(); 162 } 163 164 @Test testMessageHashIsAvailableInFile()165 public void testMessageHashIsAvailableInFile() throws IOException { 166 Truth.assertThat(mConfig.messageHashIsAvailableInFile(1)).isTrue(); 167 Truth.assertThat(mConfig.messageHashIsAvailableInFile(2)).isTrue(); 168 Truth.assertThat(mConfig.messageHashIsAvailableInFile(3)).isTrue(); 169 Truth.assertThat(mConfig.messageHashIsAvailableInFile(4)).isTrue(); 170 Truth.assertThat(mConfig.messageHashIsAvailableInFile(5)).isTrue(); 171 Truth.assertThat(mConfig.messageHashIsAvailableInFile(6)).isFalse(); 172 } 173 } 174