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