blob: 8882afbbab4d83b9c492325da79863f35c5ab3a8 [file] [log] [blame]
Adnan Begovicd5fdee92015-11-13 15:28:21 -08001/*
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
17import java.io.File;
18import java.io.IOException;
19import java.util.ArrayList;
20import java.util.Collections;
21
22/**
23 * A verbose settings migration test
24 */
25class 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 Mortimer542742b2017-09-18 19:44:11 -070033 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 Begovicd5fdee92015-11-13 15:28:21 -080036
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 Mortimer542742b2017-09-18 19:44:11 -070077 SettingImageCommands legacyToLineageSettings =
Adnan Begovicd5fdee92015-11-13 15:28:21 -080078 new SettingImageCommands(SettingsConstants.SETTINGS_AUTHORITY);
79 //For each example setting in the table, add inserts
80 for (Setting setting : legacySystemSettings) {
Sam Mortimer542742b2017-09-18 19:44:11 -070081 legacyToLineageSettings.addInsert(SettingsConstants.SYSTEM, setting);
Adnan Begovicd5fdee92015-11-13 15:28:21 -080082 }
83 for (Setting setting : legacySecureSettings) {
Sam Mortimer542742b2017-09-18 19:44:11 -070084 legacyToLineageSettings.addInsert(SettingsConstants.SECURE, setting);
Adnan Begovicd5fdee92015-11-13 15:28:21 -080085 }
86 for (Setting setting : legacyGlobalSettings) {
Sam Mortimer542742b2017-09-18 19:44:11 -070087 legacyToLineageSettings.addInsert(SettingsConstants.GLOBAL, setting);
Adnan Begovicd5fdee92015-11-13 15:28:21 -080088 }
89 //Write them to the database for verification later
Sam Mortimer542742b2017-09-18 19:44:11 -070090 legacyToLineageSettings.execute();
Adnan Begovicd5fdee92015-11-13 15:28:21 -080091
92 //Force update
Adnan Begovica3baf9c2015-11-29 14:34:56 -080093 DebuggingCommands updateRom = new DebuggingCommands();
94 updateRom.addAdb(AdbCommand.Types.REBOOT_BOOTLOADER);
Adnan Begovicd5fdee92015-11-13 15:28:21 -080095 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 Begovica3baf9c2015-11-29 14:34:56 -0800101 updateRom.addAdb(AdbCommand.Types.CHECK_BOOT_COMPLETE);
Adnan Begovicd5fdee92015-11-13 15:28:21 -0800102 updateRom.execute();
103
104 //Requery
Sam Mortimer542742b2017-09-18 19:44:11 -0700105 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 Begovicd5fdee92015-11-13 15:28:21 -0800111
112 //Validate
113 System.out.println("\n\nValidating " + SettingsConstants.SYSTEM + "...");
Sam Mortimer542742b2017-09-18 19:44:11 -0700114 validate(legacySystemSettings, lineageSystemSettingList);
Adnan Begovicd5fdee92015-11-13 15:28:21 -0800115 System.out.println("\n\nValidating " + SettingsConstants.SECURE + "...");
Sam Mortimer542742b2017-09-18 19:44:11 -0700116 validate(legacySecureSettings, lineageSecureSettingList);
Adnan Begovicd5fdee92015-11-13 15:28:21 -0800117 System.out.println("\n\nValidating " + SettingsConstants.GLOBAL + "...");
Sam Mortimer542742b2017-09-18 19:44:11 -0700118 validate(legacyGlobalSettings, lineageGlobalSettingList);
Adnan Begovicd5fdee92015-11-13 15:28:21 -0800119 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 Mortimer542742b2017-09-18 19:44:11 -0700153 private static void validate(ArrayList<Setting> legacySettings, ArrayList<Setting> lineageSettings) {
Adnan Begovicd5fdee92015-11-13 15:28:21 -0800154 Collections.sort(legacySettings);
Sam Mortimer542742b2017-09-18 19:44:11 -0700155 Collections.sort(lineageSettings);
Adnan Begovicd5fdee92015-11-13 15:28:21 -0800156
Sam Mortimer542742b2017-09-18 19:44:11 -0700157 if (legacySettings.size() != lineageSettings.size()) {
Adnan Begovicd5fdee92015-11-13 15:28:21 -0800158 System.err.println("Warning: Size mismatch: " + " legacy "
Sam Mortimer542742b2017-09-18 19:44:11 -0700159 + legacySettings.size() + " lineage " + lineageSettings.size());
Adnan Begovicd5fdee92015-11-13 15:28:21 -0800160 }
161
162 for (int i = 0; i < legacySettings.size(); i++) {
163 Setting legacySetting = legacySettings.get(i);
Sam Mortimer542742b2017-09-18 19:44:11 -0700164 Setting lineageSetting = lineageSettings.get(i);
Adnan Begovicd5fdee92015-11-13 15:28:21 -0800165 int error = 0;
166
Sam Mortimer542742b2017-09-18 19:44:11 -0700167 System.out.println("Comparing: legacy " + legacySetting.getKey() + " and lineagesetting "
168 + lineageSetting.getKey());
Adnan Begovicd5fdee92015-11-13 15:28:21 -0800169
Sam Mortimer542742b2017-09-18 19:44:11 -0700170 if (!legacySetting.getKey().equals(lineageSetting.getKey())) {
Adnan Begovicd5fdee92015-11-13 15:28:21 -0800171 System.err.println(" Key mismatch: " + legacySetting.getKey() + " and "
Sam Mortimer542742b2017-09-18 19:44:11 -0700172 + lineageSetting.getKey());
Adnan Begovicd5fdee92015-11-13 15:28:21 -0800173 error = 1;
174 }
Sam Mortimer542742b2017-09-18 19:44:11 -0700175 if (!legacySetting.getKeyType().equals(lineageSetting.getKeyType())) {
Adnan Begovicd5fdee92015-11-13 15:28:21 -0800176 System.err.println(" Key type mismatch: " + legacySetting.getKeyType() + " and "
Sam Mortimer542742b2017-09-18 19:44:11 -0700177 + lineageSetting.getKeyType());
Adnan Begovicd5fdee92015-11-13 15:28:21 -0800178 error = 1;
179 }
180 if (legacySetting.getValue().length() > 0) {
Sam Mortimer542742b2017-09-18 19:44:11 -0700181 if (!legacySetting.getValue().equals(lineageSetting.getValue())) {
Adnan Begovicd5fdee92015-11-13 15:28:21 -0800182 System.err.println(" Value mismatch: " + legacySetting.getValue() + " and "
Sam Mortimer542742b2017-09-18 19:44:11 -0700183 + lineageSetting.getValue());
Adnan Begovicd5fdee92015-11-13 15:28:21 -0800184 error = 1;
185 }
186 }
Sam Mortimer542742b2017-09-18 19:44:11 -0700187 if (!legacySetting.getValueType().equals(lineageSetting.getValueType())) {
Adnan Begovicd5fdee92015-11-13 15:28:21 -0800188 System.err.println(" Value type mismatch: " + legacySetting.getValueType()
Sam Mortimer542742b2017-09-18 19:44:11 -0700189 + " and " + lineageSetting.getValueType());
Adnan Begovicd5fdee92015-11-13 15:28:21 -0800190 error = 1;
191 }
192
193 if (error > 0) {
194 System.exit(-1);
195 } else {
196 System.out.println("...OK");
197 }
198 }
199 }
200}