Adnan Begovic | d5fdee9 | 2015-11-13 15:28:21 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The CyanogenMod 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 | import java.io.File; |
| 18 | import java.io.IOException; |
| 19 | import java.util.ArrayList; |
| 20 | import java.util.Collections; |
| 21 | |
| 22 | /** |
| 23 | * A verbose settings migration test |
| 24 | */ |
| 25 | class MigrationTest { |
| 26 | private static final String ARGUMENT_SETTINGS = "--settings"; |
| 27 | private static final String ARGUMENT_BOOT_IMG = "--bootimg"; |
| 28 | private static final String ARGUMENT_SYSTEM_IMG = "--systemimg"; |
| 29 | private static final String ARGUMENT_PREFIX = "--"; |
| 30 | |
| 31 | public static final boolean DEBUG = true; |
| 32 | |
Sam Mortimer | 542742b | 2017-09-18 19:44:11 -0700 | [diff] [blame] | 33 | private static ArrayList<Setting> lineageSystemSettingList = new ArrayList<Setting>(); |
| 34 | private static ArrayList<Setting> lineageSecureSettingList = new ArrayList<Setting>(); |
| 35 | private static ArrayList<Setting> lineageGlobalSettingList = new ArrayList<Setting>(); |
Adnan Begovic | d5fdee9 | 2015-11-13 15:28:21 -0800 | [diff] [blame] | 36 | |
| 37 | private static ArrayList<Setting> legacySystemSettings = new ArrayList<Setting>(); |
| 38 | private static ArrayList<Setting> legacySecureSettings = new ArrayList<Setting>(); |
| 39 | private static ArrayList<Setting> legacyGlobalSettings = new ArrayList<Setting>(); |
| 40 | |
| 41 | private static Tokenizer tokenizer; |
| 42 | |
| 43 | public static void main(String[] args) throws IOException { |
| 44 | if (args.length < 1) { |
| 45 | showUsage(); |
| 46 | System.exit(-1); |
| 47 | } |
| 48 | tokenizer = new Tokenizer(args); |
| 49 | |
| 50 | String settingFileName = null; |
| 51 | String bootImage = null; |
| 52 | String systemImage = null; |
| 53 | for (String argument; (argument = tokenizer.nextArg())!= null;) { |
| 54 | if (ARGUMENT_SETTINGS.equals(argument)) { |
| 55 | settingFileName = argumentValueRequired(argument); |
| 56 | } else if (ARGUMENT_BOOT_IMG.equals(argument)) { |
| 57 | bootImage = argumentValueRequired(argument); |
| 58 | } else if (ARGUMENT_SYSTEM_IMG.equals(argument)) { |
| 59 | systemImage = argumentValueRequired(argument); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | if (!new File(settingFileName).exists()) { |
| 64 | System.err.print("Invalid file provided " + settingFileName); |
| 65 | System.exit(-1); |
| 66 | } |
| 67 | |
| 68 | SettingImageCommands legacySettings = |
| 69 | new SettingImageCommands(SettingsConstants.SETTINGS_AUTHORITY); |
| 70 | legacySettings.addRead(settingFileName, SettingsConstants.SYSTEM, legacySystemSettings); |
| 71 | legacySettings.addRead(settingFileName, SettingsConstants.SECURE, legacySecureSettings); |
| 72 | legacySettings.addRead(settingFileName, SettingsConstants.GLOBAL, legacyGlobalSettings); |
| 73 | |
| 74 | //Read settings |
| 75 | legacySettings.execute(); |
| 76 | |
Sam Mortimer | 542742b | 2017-09-18 19:44:11 -0700 | [diff] [blame] | 77 | SettingImageCommands legacyToLineageSettings = |
Adnan Begovic | d5fdee9 | 2015-11-13 15:28:21 -0800 | [diff] [blame] | 78 | new SettingImageCommands(SettingsConstants.SETTINGS_AUTHORITY); |
| 79 | //For each example setting in the table, add inserts |
| 80 | for (Setting setting : legacySystemSettings) { |
Sam Mortimer | 542742b | 2017-09-18 19:44:11 -0700 | [diff] [blame] | 81 | legacyToLineageSettings.addInsert(SettingsConstants.SYSTEM, setting); |
Adnan Begovic | d5fdee9 | 2015-11-13 15:28:21 -0800 | [diff] [blame] | 82 | } |
| 83 | for (Setting setting : legacySecureSettings) { |
Sam Mortimer | 542742b | 2017-09-18 19:44:11 -0700 | [diff] [blame] | 84 | legacyToLineageSettings.addInsert(SettingsConstants.SECURE, setting); |
Adnan Begovic | d5fdee9 | 2015-11-13 15:28:21 -0800 | [diff] [blame] | 85 | } |
| 86 | for (Setting setting : legacyGlobalSettings) { |
Sam Mortimer | 542742b | 2017-09-18 19:44:11 -0700 | [diff] [blame] | 87 | legacyToLineageSettings.addInsert(SettingsConstants.GLOBAL, setting); |
Adnan Begovic | d5fdee9 | 2015-11-13 15:28:21 -0800 | [diff] [blame] | 88 | } |
| 89 | //Write them to the database for verification later |
Sam Mortimer | 542742b | 2017-09-18 19:44:11 -0700 | [diff] [blame] | 90 | legacyToLineageSettings.execute(); |
Adnan Begovic | d5fdee9 | 2015-11-13 15:28:21 -0800 | [diff] [blame] | 91 | |
| 92 | //Force update |
Adnan Begovic | a3baf9c | 2015-11-29 14:34:56 -0800 | [diff] [blame] | 93 | DebuggingCommands updateRom = new DebuggingCommands(); |
| 94 | updateRom.addAdb(AdbCommand.Types.REBOOT_BOOTLOADER); |
Adnan Begovic | d5fdee9 | 2015-11-13 15:28:21 -0800 | [diff] [blame] | 95 | updateRom.addFastboot(FastbootCommand.Types.FASTBOOT_DEVICES, null); |
| 96 | updateRom.addFastboot(FastbootCommand.Types.FASTBOOT_FLASH, |
| 97 | new String[]{"boot", bootImage}); |
| 98 | updateRom.addFastboot(FastbootCommand.Types.FASTBOOT_FLASH, |
| 99 | new String[]{"system", systemImage}); |
| 100 | updateRom.addFastboot(FastbootCommand.Types.FASTBOOT_REBOOT, null); |
Adnan Begovic | a3baf9c | 2015-11-29 14:34:56 -0800 | [diff] [blame] | 101 | updateRom.addAdb(AdbCommand.Types.CHECK_BOOT_COMPLETE); |
Adnan Begovic | d5fdee9 | 2015-11-13 15:28:21 -0800 | [diff] [blame] | 102 | updateRom.execute(); |
| 103 | |
| 104 | //Requery |
Sam Mortimer | 542742b | 2017-09-18 19:44:11 -0700 | [diff] [blame] | 105 | SettingImageCommands lineageSettingImage = |
| 106 | new SettingImageCommands(SettingsConstants.LINEAGESETTINGS_AUTHORITY); |
| 107 | lineageSettingImage.addQuery(SettingsConstants.SYSTEM, lineageSystemSettingList); |
| 108 | lineageSettingImage.addQuery(SettingsConstants.SECURE, lineageSecureSettingList); |
| 109 | lineageSettingImage.addQuery(SettingsConstants.GLOBAL, lineageGlobalSettingList); |
| 110 | lineageSettingImage.execute(); |
Adnan Begovic | d5fdee9 | 2015-11-13 15:28:21 -0800 | [diff] [blame] | 111 | |
| 112 | //Validate |
| 113 | System.out.println("\n\nValidating " + SettingsConstants.SYSTEM + "..."); |
Sam Mortimer | 542742b | 2017-09-18 19:44:11 -0700 | [diff] [blame] | 114 | validate(legacySystemSettings, lineageSystemSettingList); |
Adnan Begovic | d5fdee9 | 2015-11-13 15:28:21 -0800 | [diff] [blame] | 115 | System.out.println("\n\nValidating " + SettingsConstants.SECURE + "..."); |
Sam Mortimer | 542742b | 2017-09-18 19:44:11 -0700 | [diff] [blame] | 116 | validate(legacySecureSettings, lineageSecureSettingList); |
Adnan Begovic | d5fdee9 | 2015-11-13 15:28:21 -0800 | [diff] [blame] | 117 | System.out.println("\n\nValidating " + SettingsConstants.GLOBAL + "..."); |
Sam Mortimer | 542742b | 2017-09-18 19:44:11 -0700 | [diff] [blame] | 118 | validate(legacyGlobalSettings, lineageGlobalSettingList); |
Adnan Begovic | d5fdee9 | 2015-11-13 15:28:21 -0800 | [diff] [blame] | 119 | System.exit(0); |
| 120 | } |
| 121 | |
| 122 | private static void showUsage() { |
| 123 | System.err.println("Usage: MigrationTest --settings [example setting file] " |
| 124 | + "--bootimg [image]" |
| 125 | + "--systemimg [image]"); |
| 126 | } |
| 127 | |
| 128 | private static class Tokenizer { |
| 129 | private final String[] mArgs; |
| 130 | private int mNextArg; |
| 131 | |
| 132 | public Tokenizer(String[] args) { |
| 133 | mArgs = args; |
| 134 | } |
| 135 | |
| 136 | private String nextArg() { |
| 137 | if (mNextArg < mArgs.length) { |
| 138 | return mArgs[mNextArg++]; |
| 139 | } else { |
| 140 | return null; |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | private static String argumentValueRequired(String argument) { |
| 146 | String value = tokenizer.nextArg(); |
| 147 | if (value == null || value.length() == 0 || value.startsWith(ARGUMENT_PREFIX)) { |
| 148 | throw new IllegalArgumentException("No value for argument: " + argument); |
| 149 | } |
| 150 | return value; |
| 151 | } |
| 152 | |
Sam Mortimer | 542742b | 2017-09-18 19:44:11 -0700 | [diff] [blame] | 153 | private static void validate(ArrayList<Setting> legacySettings, ArrayList<Setting> lineageSettings) { |
Adnan Begovic | d5fdee9 | 2015-11-13 15:28:21 -0800 | [diff] [blame] | 154 | Collections.sort(legacySettings); |
Sam Mortimer | 542742b | 2017-09-18 19:44:11 -0700 | [diff] [blame] | 155 | Collections.sort(lineageSettings); |
Adnan Begovic | d5fdee9 | 2015-11-13 15:28:21 -0800 | [diff] [blame] | 156 | |
Sam Mortimer | 542742b | 2017-09-18 19:44:11 -0700 | [diff] [blame] | 157 | if (legacySettings.size() != lineageSettings.size()) { |
Adnan Begovic | d5fdee9 | 2015-11-13 15:28:21 -0800 | [diff] [blame] | 158 | System.err.println("Warning: Size mismatch: " + " legacy " |
Sam Mortimer | 542742b | 2017-09-18 19:44:11 -0700 | [diff] [blame] | 159 | + legacySettings.size() + " lineage " + lineageSettings.size()); |
Adnan Begovic | d5fdee9 | 2015-11-13 15:28:21 -0800 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | for (int i = 0; i < legacySettings.size(); i++) { |
| 163 | Setting legacySetting = legacySettings.get(i); |
Sam Mortimer | 542742b | 2017-09-18 19:44:11 -0700 | [diff] [blame] | 164 | Setting lineageSetting = lineageSettings.get(i); |
Adnan Begovic | d5fdee9 | 2015-11-13 15:28:21 -0800 | [diff] [blame] | 165 | int error = 0; |
| 166 | |
Sam Mortimer | 542742b | 2017-09-18 19:44:11 -0700 | [diff] [blame] | 167 | System.out.println("Comparing: legacy " + legacySetting.getKey() + " and lineagesetting " |
| 168 | + lineageSetting.getKey()); |
Adnan Begovic | d5fdee9 | 2015-11-13 15:28:21 -0800 | [diff] [blame] | 169 | |
Sam Mortimer | 542742b | 2017-09-18 19:44:11 -0700 | [diff] [blame] | 170 | if (!legacySetting.getKey().equals(lineageSetting.getKey())) { |
Adnan Begovic | d5fdee9 | 2015-11-13 15:28:21 -0800 | [diff] [blame] | 171 | System.err.println(" Key mismatch: " + legacySetting.getKey() + " and " |
Sam Mortimer | 542742b | 2017-09-18 19:44:11 -0700 | [diff] [blame] | 172 | + lineageSetting.getKey()); |
Adnan Begovic | d5fdee9 | 2015-11-13 15:28:21 -0800 | [diff] [blame] | 173 | error = 1; |
| 174 | } |
Sam Mortimer | 542742b | 2017-09-18 19:44:11 -0700 | [diff] [blame] | 175 | if (!legacySetting.getKeyType().equals(lineageSetting.getKeyType())) { |
Adnan Begovic | d5fdee9 | 2015-11-13 15:28:21 -0800 | [diff] [blame] | 176 | System.err.println(" Key type mismatch: " + legacySetting.getKeyType() + " and " |
Sam Mortimer | 542742b | 2017-09-18 19:44:11 -0700 | [diff] [blame] | 177 | + lineageSetting.getKeyType()); |
Adnan Begovic | d5fdee9 | 2015-11-13 15:28:21 -0800 | [diff] [blame] | 178 | error = 1; |
| 179 | } |
| 180 | if (legacySetting.getValue().length() > 0) { |
Sam Mortimer | 542742b | 2017-09-18 19:44:11 -0700 | [diff] [blame] | 181 | if (!legacySetting.getValue().equals(lineageSetting.getValue())) { |
Adnan Begovic | d5fdee9 | 2015-11-13 15:28:21 -0800 | [diff] [blame] | 182 | System.err.println(" Value mismatch: " + legacySetting.getValue() + " and " |
Sam Mortimer | 542742b | 2017-09-18 19:44:11 -0700 | [diff] [blame] | 183 | + lineageSetting.getValue()); |
Adnan Begovic | d5fdee9 | 2015-11-13 15:28:21 -0800 | [diff] [blame] | 184 | error = 1; |
| 185 | } |
| 186 | } |
Sam Mortimer | 542742b | 2017-09-18 19:44:11 -0700 | [diff] [blame] | 187 | if (!legacySetting.getValueType().equals(lineageSetting.getValueType())) { |
Adnan Begovic | d5fdee9 | 2015-11-13 15:28:21 -0800 | [diff] [blame] | 188 | System.err.println(" Value type mismatch: " + legacySetting.getValueType() |
Sam Mortimer | 542742b | 2017-09-18 19:44:11 -0700 | [diff] [blame] | 189 | + " and " + lineageSetting.getValueType()); |
Adnan Begovic | d5fdee9 | 2015-11-13 15:28:21 -0800 | [diff] [blame] | 190 | error = 1; |
| 191 | } |
| 192 | |
| 193 | if (error > 0) { |
| 194 | System.exit(-1); |
| 195 | } else { |
| 196 | System.out.println("...OK"); |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | } |