[TC Fixed] Index out of range
[platform/core/dotnet/launcher.git] / tests / TCs / 6_TOOL / TOOL.py
1 #!/usr/bin/env python3
2 import os, subprocess, sys, argparse
3 sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
4
5 from time import sleep
6 from Utils import *
7
8
9 module_name = "TOOL"
10
11 # The `dotnettool` works normally.
12 def TC_01():
13     raw = cmd(f"shell find /usr/bin/ -name dotnettool")
14     if "/usr/bin/dotnettool" not in raw:
15         return "FAIL : The dotnettool works normally"
16
17     raw = cmd(f"shell dotnettool")
18     if "Dotnet Tool Version: 1.0" not in raw:
19         return "FAIL : The dotnettool works normally"
20
21     return "PASS"
22
23 # The `native image` is generated normally.
24 def TC_02():
25     cmd(f"shell dotnettool --ni-system")
26
27     raw = cmd(f"shell find {RUNTIME_DIR} -name {SPC_DLL}.Backup")
28     if f"{RUNTIME_DIR}{SPC_DLL}.Backup" not in raw:
29         return "FAIL : Create the platform native image"
30
31     raw = cmd(f"shell find {RUNTIME_DIR} -name *.ni.dll")
32     lines1 = [l for l in raw.splitlines()]
33     raw = cmd(f"shell find {RUNTIME_DIR} -name *.dll -not -name *.ni.dll")
34     lines2 = [l for l in raw.splitlines()]
35     if (len(lines1) + 2) != len(lines2):
36         return "FAIL : The number of .dll and .ni.dll must match"
37
38     raw = cmd(f"shell find {FRAMEWORK_DIR} -name *.ni.dll")
39     lines1 = [l for l in raw.splitlines()]
40     raw = cmd(f"shell find {FRAMEWORK_DIR} -name *.dll -not -name *.ni.dll")
41     lines2 = [l for l in raw.splitlines() if "/ref/" not in l]
42     if len(lines1) != len(lines2):
43         return "FAIL : The number of .dll and .ni.dll must match"
44
45     for ni in lines1:
46         is_same = False
47         for dll in lines2:
48             if Path(ni).name.replace(".ni.dll", "") == Path(dll).name.replace(".dll", ""):
49                 is_same = True
50                 break
51         if not is_same:
52             return "FAIL : The file name of .dll and .ni.dll must match in the platform"
53
54     return "PASS"
55
56 # Remove the `platform` native image.
57 def TC_03():
58     cmd(f"shell dotnettool --ni-reset-system")
59
60     raw = cmd(f"shell find {RUNTIME_DIR} -name {SPC_DLL}.Backup")
61     if f"{RUNTIME_DIR}{SPC_DLL}.Backup" in raw:
62         return "FAIL : Remove the platform native image"
63
64     return "PASS"
65
66 # Create native image for `System.Private.CoreLib.dll`.
67 def TC_04():
68     raw = cmd(f"shell find {RUNTIME_DIR} -name {SPC_DLL}.Backup")
69     if f"{RUNTIME_DIR}{SPC_DLL}.Backup" in raw:
70         cmd(f"shell rm {RUNTIME_DIR}{SPC_DLL}")
71         cmd(f"shell mv {RUNTIME_DIR}{SPC_DLL}.Backup {RUNTIME_DIR}{SPC_DLL}")
72
73     raw = cmd(f"shell dotnettool --ni-dll {RUNTIME_DIR}{SPC_DLL}")
74     if f"{SPC_DLL} (FNV)" not in raw and \
75        "System.Private.CoreLib.ni.dll generated successfully." not in raw:
76         return f"FAIL : Create native image for {SPC_DLL}"
77
78     raw = cmd(f"shell find {RUNTIME_DIR} -name {SPC_DLL}.Backup")
79     if f"{RUNTIME_DIR}{SPC_DLL}.Backup" not in raw:
80         return f"FAIL : Create native image for {SPC_DLL}"
81
82     return "PASS"
83
84 # The file name of `.dll` and `.ni.dll` must match in the framework.
85 def TC_05():
86     cmd(f"shell dotnettool --ni-dir {FRAMEWORK_DIR}")
87     raw = cmd(f"shell find {FRAMEWORK_DIR} -name *.ni.dll")
88     lines1 = [l for l in raw.splitlines()]
89     raw = cmd(f"shell find {FRAMEWORK_DIR} -name *.dll -not -name *.ni.dll")
90     lines2 = [l for l in raw.splitlines() if "/ref/" not in l]
91     if len(lines1) != len(lines2):
92         return "FAIL : The number of .dll and .ni.dll must match"
93
94     for ni in lines1:
95         is_same = False
96         for dll in lines2:
97             if Path(ni).name.replace(".ni.dll", "") == Path(dll).name.replace(".dll", ""):
98                 is_same = True
99                 break
100         if not is_same:
101             return "FAIL : The file name of .dll and .ni.dll must match in the {FRAMEWORK_DIR}"
102
103     return "PASS"
104
105 # The `.ni.dll` files should not exist in the framework.
106 def TC_06():
107     cmd(f"shell dotnettool --ni-reset-dir {FRAMEWORK_DIR}")
108     raw = cmd(f"shell find {FRAMEWORK_DIR} -name *.ni.dll")
109     lines = [l for l in raw.splitlines()]
110     if len(lines) != 0:
111         return "FAIL : The .ni.dll files should not exist"
112
113     return "PASS"
114
115 # Create native image for Tizen.dll in `R2R` mode.
116 def TC_07():
117     raw = cmd(f"shell dotnettool --r2r --ni-dll {FRAMEWORK_DIR}Tizen.dll")
118     if "Tizen.dll (R2R)" not in raw and \
119        "Tizen.ni.dll generated successfully." not in raw:
120         return "FAIL : Create native image for Tizen.dll in R2R mode"
121
122     return "PASS"
123
124 # The `Launcher_TC_TOOL_01` application does not have native image.
125 def TC_08():
126     sln_name = "Launcher_TC_TOOL_01.Tizen"
127
128     tpk_path = get_tpk_path(tpk_list, f"{sln_name}")
129     if tpk_path == None:
130         return f"FAIL : Get the tpk path for {sln_name}"
131
132     if "OK" not in app_install(f"{tpk_path}"):
133         return f"FAIL : Install the application for {tpk_path}"
134
135     pkg_id = f"org.tizen.example.Launcher_TC_TOOL_01.Tizen"
136
137     root_path = get_root_path(f"{pkg_id}")
138     if root_path == "None":
139         return f"FAIL : Get the root path for {pkg_id}"
140
141     raw = cmd(f"shell find {root_path}/bin/ -name .native_image")
142     if ".native_image" not in raw:
143         return "FAIL : The .native_image folder should exist"
144
145     cmd(f"shell dotnettool --ni-reset-pkg {pkg_id}")
146
147     raw = cmd(f"shell ls {root_path}/bin/.native_image")
148     if "No such file or directory" not in raw:
149         return "FAIL : The .native_image folder should not exist"
150
151     raw = cmd(f"shell find {root_path}/bin/ -name *.ni.dll")
152     lines = [l for l in raw.splitlines()]
153     if len(lines) != 0:
154         return "FAIL : The .ni.dll files should not exist"
155
156     return "PASS"
157
158 # The `Launcher_TC_TOOL_02` application generates native image.
159 def TC_09():
160     sln_name = "Launcher_TC_TOOL_02.Tizen"
161
162     tpk_path = get_tpk_path(tpk_list, f"{sln_name}")
163     if tpk_path == None:
164         return f"FAIL : Get the tpk path for {sln_name}"
165
166     if "OK" not in app_install(f"{tpk_path}"):
167         return f"FAIL : Install the application for {tpk_path}"
168
169     pkg_id = f"org.tizen.example.Launcher_TC_TOOL_02.Tizen"
170
171     root_path = get_root_path(f"{pkg_id}")
172     if root_path == "None":
173         return f"FAIL : Get the root path for {pkg_id}"
174
175     raw = cmd(f"shell ls {root_path}/bin/.native_image")
176     if "No such file or directory" not in raw:
177         return "FAIL : The .native_image folder not should exist"
178
179     cmd(f"shell dotnettool --ni-pkg {pkg_id}")
180
181     raw = cmd(f"shell find {root_path}/bin/ -name .native_image")
182     if ".native_image" not in raw:
183         return "FAIL : The .native_image folder should exist"
184
185     raw = cmd(f"shell find {root_path}/bin/.native_image/ -name *.ni.dll")
186     lines1 = [l for l in raw.splitlines()]
187     raw = cmd(f"shell find {root_path}/bin/ -maxdepth 1 -name *.dll -not -name *.ni.dll")
188     lines2 = [l for l in raw.splitlines()]
189     if len(lines1) != len(lines2):
190         return "FAIL : The number of .dll and .ni.dll must match in the application"
191
192     for ni in lines1:
193         is_same = False
194         for dll in lines2:
195             if Path(ni).name.replace(".ni.dll", "") == Path(dll).name.replace(".dll", ""):
196                 is_same = True
197                 break
198         if not is_same:
199             return "FAIL : The file name of .dll and .ni.dll must match in the application"
200
201     return "PASS"
202
203 # The `prefer_dotnet_aot` metadata value of `true` will regenerates the native image in all .NET applications.
204 def TC_10():
205     sln_name = "Launcher_TC_TOOL_03.Tizen"
206
207     tpk_path = get_tpk_path(tpk_list, f"{sln_name}")
208     if tpk_path == None:
209         return f"FAIL : Get the tpk path for {sln_name}"
210
211     if "OK" not in app_install(f"{tpk_path}"):
212         return f"FAIL : Install the application for {tpk_path}"
213
214     cmd(f"shell dotnettool --ni-regen-all-app")
215
216     raw = subprocess.run((f"sdb -s {serial} shell pkginfo --metadata-flt").split(), stdout=subprocess.PIPE, input=f"http://tizen.org/metadata/prefer_dotnet_aot\ntrue\n", encoding="utf-8").stdout
217     lines1 = [l for l in raw.splitlines() if "appid" in l]
218     lines2 = []
219     for app in lines1:
220         pkg_id = app.split(": ")[1]
221         raw = subprocess.run((f"sdb -s {serial} shell pkginfo --pkg {pkg_id}").split(), stdout=subprocess.PIPE, encoding="utf-8").stdout
222         lines2 = [l for l in raw.splitlines() if "root_path" in l]
223
224     for path in lines2:
225         root_path = path.split(": ")[1]
226         raw = cmd(f"shell find {root_path}/bin/.native_image/ -name *.ni.dll")
227         lines3 = [l for l in raw.splitlines()]
228         raw = cmd(f"shell find {root_path}/bin/ -maxdepth 1 -name *.dll -not -name *.ni.dll")
229         lines4 = [l for l in raw.splitlines()]
230         if len(lines3) != len(lines4):
231             return "FAIL : The number of .dll and .ni.dll must match in the application"
232
233         for ni in lines3:
234             is_same = False
235             for dll in lines4:
236                 if Path(ni).name.replace(".ni.dll", "") == Path(dll).name.replace(".dll", ""):
237                     is_same = True
238                     break
239             if not is_same:
240                 return "FAIL : The file name of .dll and .ni.dll must match in the application"
241
242     return "PASS"
243
244 # The `prefer_nuget_cache` metadata value of `true` will regenerates the native image in the `TAC`.
245 def TC_11():
246     sln_name = "Launcher_TC_TOOL_04.Tizen"
247
248     tpk_path = get_tpk_path(tpk_list, f"{sln_name}")
249     if tpk_path == None:
250         return f"FAIL : Get the tpk path for {sln_name}"
251
252     if "OK" not in app_install(f"{tpk_path}"):
253         return f"FAIL : Install the application for {tpk_path}"
254
255     raw = cmd(f"shell dotnettool --tac-regen-all")
256     if ".dll (FNV)" not in raw and \
257        ".ni.dll generated successfully." not in raw:
258         return "FAIL : Create native image for TAC"
259
260     return "PASS"
261
262 # The `Launcher_TC_TOOL_05` application must not have `TAC` applied.
263 def TC_12():
264     sln_name = "Launcher_TC_TOOL_05.Tizen"
265
266     tpk_path = get_tpk_path(tpk_list, f"{sln_name}")
267     if tpk_path == None:
268         return f"FAIL : Get the tpk path for {sln_name}"
269
270     if "OK" not in app_install(f"{tpk_path}"):
271         return f"FAIL : Install the application for {tpk_path}"
272
273     pkg_id = f"org.tizen.example.Launcher_TC_TOOL_05.Tizen"
274
275     root_path = get_root_path(f"{pkg_id}")
276     if root_path == "None":
277         return f"FAIL : Get the root path for {pkg_id}"
278
279     raw = cmd(f"shell find {root_path}/bin/ -name .tac_symlink")
280     if ".tac_symlink" not in raw:
281         return "FAIL : The .tac_symlink folder should exist"
282
283     raw = cmd(f"shell find {root_path}/bin/.tac_symlink/ -name *.dll -not -name *.ni.dll")
284     lines1 = [l for l in raw.splitlines()]
285     raw = cmd(f"shell find {DOTNET_DIR}Xamarin.Forms/4.6.0.967/ -name *.dll -not -name *.ni.dll")
286     lines2 = [l for l in raw.splitlines()]
287     if len(lines1) != len(lines2):
288         return "FAIL : The number of .dll in the .tac_symlink and .dll in the TAC must match"
289
290     raw = cmd(f"shell ls -alZ {root_path}/bin/.tac_symlink/*.dll")
291     lines = [l for l in raw.splitlines() if ".ni.dll" not in l]
292     for dll in lines:
293         origin_path = dll.split("->")[1].strip()
294         raw = cmd(f"shell ls -alZ {origin_path}")
295         if "No such file or directory" in raw:
296             return "FAIL : The original file of the symbolic link must exist"
297
298     cmd(f"shell dotnettool --tac-disable-pkg {pkg_id}")
299
300     raw = cmd(f"shell find {root_path}/bin/ -name .tac_symlink")
301     if ".tac_symlink" in raw:
302         return "FAIL : The .tac_symlink folder should not exist"
303
304     raw = cmd(f"shell find {root_path}/bin/ -maxdepth 1 -name *.dll -not -name *.ni.dll")
305     lines = [l for l in raw.splitlines()]
306     if len(lines) != 6:
307         return "FAIL : The number of .dll must exist correctly in the bin folder"
308
309     return "PASS"
310
311 # The `Launcher_TC_TOOL_06` application must have `TAC` applied.
312 def TC_13():
313     sln_name = "Launcher_TC_TOOL_06.Tizen"
314
315     tpk_path = get_tpk_path(tpk_list, f"{sln_name}")
316     if tpk_path == None:
317         return f"FAIL : Get the tpk path for {sln_name}"
318
319     if "OK" not in app_install(f"{tpk_path}"):
320         return f"FAIL : Install the application for {tpk_path}"
321
322     pkg_id = f"org.tizen.example.Launcher_TC_TOOL_06.Tizen"
323
324     root_path = get_root_path(f"{pkg_id}")
325     if root_path == "None":
326         return f"FAIL : Get the root path for {pkg_id}"
327
328     cmd(f"shell dotnettool --tac-disable-pkg {pkg_id}")
329
330     raw = cmd(f"shell find {root_path}/bin/ -name .tac_symlink")
331     if ".tac_symlink" in raw:
332         return "FAIL : The .tac_symlink folder should not exist"
333
334     raw = cmd(f"shell find {root_path}/bin/ -maxdepth 1 -name *.dll -not -name *.ni.dll")
335     lines = [l for l in raw.splitlines()]
336     if len(lines) != 6:
337         return "FAIL : The number of .dll must exist correctly in the bin folder"
338
339     cmd(f"shell dotnettool --tac-enable-pkg {pkg_id}")
340
341     raw = cmd(f"shell find {root_path}/bin/ -name .tac_symlink")
342     if ".tac_symlink" not in raw:
343         return "FAIL : The .tac_symlink folder should exist"
344
345     raw = cmd(f"shell find {root_path}/bin/.tac_symlink/ -name *.dll -not -name *.ni.dll")
346     lines1 = [l for l in raw.splitlines()]
347     raw = cmd(f"shell find {DOTNET_DIR}Xamarin.Forms/4.6.0.967/ -name *.dll -not -name *.ni.dll")
348     lines2 = [l for l in raw.splitlines()]
349     if len(lines1) != len(lines2):
350         return "FAIL : The number of .dll in the .tac_symlink and .dll in the TAC must match"
351
352     raw = cmd(f"shell ls -alZ {root_path}/bin/.tac_symlink/*.dll")
353     lines = [l for l in raw.splitlines() if ".ni.dll" not in l]
354     for dll in lines:
355         origin_path = dll.split("->")[1].strip()
356         raw = cmd(f"shell ls -alZ {origin_path}")
357         if "No such file or directory" in raw:
358             return "FAIL : The original file of the symbolic link must exist"
359
360     return "PASS"
361
362 # The `DB` of the restored `TAC` and `TLC` must be a valid value.
363 def TC_14():
364     sln_name = "Launcher_TC_TOOL_07.Tizen"
365
366     tpk_path = get_tpk_path(tpk_list, f"{sln_name}")
367     if tpk_path == None:
368         return f"FAIL : Get the tpk path for {sln_name}"
369
370     if "OK" not in app_install(f"{tpk_path}"):
371         return f"FAIL : Install the application for {tpk_path}"
372
373     cmd(f"shell rm {DOTNET_DIR}.TAC.App.list.db*")
374     cmd(f"shell rm {DOTNET_DIR}.TLC.App.list.db*")
375
376     cmd(f"shell dotnettool --tac-restore-db")
377
378     pkg_id = f"org.tizen.example.Launcher_TC_TOOL_07.Tizen"
379
380     raw = subprocess.run((f"sdb -s {serial} shell sqlite3 {DOTNET_DIR}.TAC.App.list.db").split(), stdout=subprocess.PIPE, input=f"select * from TAC;\n.q\n", encoding="utf-8").stdout
381     lines = [l for l in raw.splitlines() if f"{pkg_id}" in l]
382     for rcd in lines:
383         if ("SkiaSharp/2.80.2" not in rcd) and \
384            ("Xamarin.Forms/4.6.0.967" not in rcd):
385             return "FAIL : TAC database must have a valid value"
386
387     raw = subprocess.run((f"sdb -s {serial} shell sqlite3 {DOTNET_DIR}.TLC.App.list.db").split(), stdout=subprocess.PIPE, input=f"select * from TLC;\n.q\n", encoding="utf-8").stdout
388     lines = [l for l in raw.splitlines() if f"{pkg_id}" in l]
389     for rcd in lines:
390         if "libSkiaSharp.so..8e2fcc43f9c49b2de7b6212ff5ed914b319bc92f125715b9fe1f35786df82f98" not in rcd:
391             return "FAIL : TLC database must have a valid value"
392
393     return "PASS"
394
395
396 #def TC_15():
397 #dotnettool --resolve-all-app
398
399 #def TC_16():
400 #dotnettool --ibc-dir
401
402 #def TC_17():
403 #dotnettool --instrument
404
405 #def TC_18():
406 #dotnettool --compatibility
407
408 #def TC_19():
409 #dotnettool --verbose
410
411 # Run the test
412 def run():
413     cmd(f"root on")
414     cmd(f"shell mount -o remount,rw /")
415
416     global tpk_list
417     tpk_list = search_tpk(f"{module_name}")
418
419     p = run_tc_array(module_name, tc_array)
420     f = len(tc_array) - p
421     r = round(((p / len(tc_array)) * 100), 2)
422     print(f"--- {module_name} TCT Result ---\nFAIL : [{f}] / PASS : [{p}] - [{r}%]\n")
423
424 # Uninstall the application and restore to original state
425 def clean():
426     cmd(f"uninstall org.tizen.example.Launcher_TC_TOOL_01.Tizen")
427     cmd(f"uninstall org.tizen.example.Launcher_TC_TOOL_02.Tizen")
428     cmd(f"uninstall org.tizen.example.Launcher_TC_TOOL_03.Tizen")
429     cmd(f"uninstall org.tizen.example.Launcher_TC_TOOL_04.Tizen")
430     cmd(f"uninstall org.tizen.example.Launcher_TC_TOOL_05.Tizen")
431     cmd(f"uninstall org.tizen.example.Launcher_TC_TOOL_06.Tizen")
432     cmd(f"uninstall org.tizen.example.Launcher_TC_TOOL_07.Tizen")
433
434     cmd(f"shell rm {FRAMEWORK_DIR}Tizen.ni.dll")
435
436 # Main entry point
437 def main():
438     parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter)
439     parser.add_argument("TC_NUMBER", type=str, nargs="*", help="Individual excution")
440     args = parser.parse_args()
441
442     global tc_array
443     if args.TC_NUMBER and "TC_" in args.TC_NUMBER[0]:
444         tc_array = []
445         for tc_num in args.TC_NUMBER:
446             if tc_num not in funcMap:
447                 print(f"There is no {tc_num} test.")
448                 exit(1)
449             else:
450                 tc_array.append(funcMap[tc_num])
451     else:
452         tc_array = [TC_01, TC_02, TC_03, TC_04, TC_05, TC_06, TC_07, TC_08, TC_09, TC_10, TC_11, TC_12, TC_13, TC_14]
453
454     global serial
455     if len(sys.argv) >= 2 and "TC_" not in sys.argv[1]:
456         serial = read_serial(sys.argv[1])
457     else:
458         serial = read_serial(None)
459
460     if serial is None:
461         print("No connected device(s).")
462         exit(1)
463
464     device = get_device_type()
465     print(f"=== Dotnet-Launcher [{device}] Test Case - ({module_name}) ===")
466
467     run()
468     clean()
469
470
471 funcMap = {
472 'TC_01': TC_01, 'TC_02': TC_02, 'TC_03': TC_03, 'TC_04': TC_04, 'TC_05': TC_05, 'TC_06': TC_06, 'TC_07': TC_07,
473 'TC_08': TC_08, 'TC_09': TC_09, 'TC_10': TC_10, 'TC_11': TC_11, 'TC_12': TC_12, 'TC_13': TC_13, 'TC_14': TC_14,
474 'TOOL_TC_01': TC_01, 'TOOL_TC_02': TC_02, 'TOOL_TC_03': TC_03, 'TOOL_TC_04': TC_04, 'TOOL_TC_05': TC_05, 'TOOL_TC_06': TC_06, 'TOOL_TC_07': TC_07,
475 'TOOL_TC_08': TC_08, 'TOOL_TC_09': TC_09, 'TOOL_TC_10': TC_10, 'TOOL_TC_11': TC_11, 'TOOL_TC_12': TC_12, 'TOOL_TC_13': TC_13, 'TOOL_TC_14': TC_14
476 }
477
478
479 if __name__ == "__main__":
480     try:
481         main()
482     except KeyboardInterrupt:
483         print("\nExit (Pressed Ctrl+C)")
484         exit(1)