backupcompatibility.py 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645
  1. # -*- encoding: utf8 -*-
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2014 Marek Marczykowski-Górecki <marmarek@invisiblethingslab.com>
  6. #
  7. # This program is free software; you can redistribute it and/or
  8. # modify it under the terms of the GNU Lesser General Public License
  9. # as published by the Free Software Foundation; either version 2.1
  10. # of the License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Lesser General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Lesser General Public License
  18. # along with this program; if not, write to the Free Software
  19. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. #
  21. import functools
  22. import tempfile
  23. import unittest
  24. from distutils import spawn
  25. from multiprocessing import Queue
  26. import os
  27. import subprocess
  28. import logging
  29. try:
  30. import unittest.mock as mock
  31. except ImportError:
  32. import mock
  33. import re
  34. import multiprocessing
  35. import pkg_resources
  36. import sys
  37. import qubesadmin.backup.core2
  38. import qubesadmin.backup.core3
  39. import qubesadmin.firewall
  40. import qubesadmin.storage
  41. import qubesadmin.tests
  42. import qubesadmin.tests.backup
  43. MANGLED_SUBDIRS_R2 = {
  44. "test-work": "vm5",
  45. "test-template-clone": "vm9",
  46. "test-custom-template-appvm": "vm10",
  47. "test-standalonevm": "vm11",
  48. "test-testproxy": "vm12",
  49. "test-testhvm": "vm14",
  50. "test-net": "vm16",
  51. }
  52. MANGLED_SUBDIRS_R4 = {
  53. "test-work": "vm3",
  54. "test-fedora-25-clone": "vm7",
  55. "test-custom-template-appvm": "vm31",
  56. "test-standalonevm": "vm4",
  57. "test-proxy": "vm30",
  58. "test-hvm": "vm9",
  59. "test-net": "vm6",
  60. "test-d8test": "vm20",
  61. }
  62. APPTEMPLATE_R2B2 = '''
  63. [Desktop Entry]
  64. Name=%VMNAME%: {name}
  65. GenericName=%VMNAME%: {name}
  66. GenericName[ca]=%VMNAME%: Navegador web
  67. GenericName[cs]=%VMNAME%: Webový prohlížeč
  68. GenericName[es]=%VMNAME%: Navegador web
  69. GenericName[fa]=%VMNAME%: مرورر اینترنتی
  70. GenericName[fi]=%VMNAME%: WWW-selain
  71. GenericName[fr]=%VMNAME%: Navigateur Web
  72. GenericName[hu]=%VMNAME%: Webböngésző
  73. GenericName[it]=%VMNAME%: Browser Web
  74. GenericName[ja]=%VMNAME%: ウェブ・ブラウザ
  75. GenericName[ko]=%VMNAME%: 웹 브라우저
  76. GenericName[nb]=%VMNAME%: Nettleser
  77. GenericName[nl]=%VMNAME%: Webbrowser
  78. GenericName[nn]=%VMNAME%: Nettlesar
  79. GenericName[no]=%VMNAME%: Nettleser
  80. GenericName[pl]=%VMNAME%: Przeglądarka WWW
  81. GenericName[pt]=%VMNAME%: Navegador Web
  82. GenericName[pt_BR]=%VMNAME%: Navegador Web
  83. GenericName[sk]=%VMNAME%: Internetový prehliadač
  84. GenericName[sv]=%VMNAME%: Webbläsare
  85. Comment={comment}
  86. Comment[ca]=Navegueu per el web
  87. Comment[cs]=Prohlížení stránek World Wide Webu
  88. Comment[de]=Im Internet surfen
  89. Comment[es]=Navegue por la web
  90. Comment[fa]=صفحات شبه جهانی اینترنت را مرور نمایید
  91. Comment[fi]=Selaa Internetin WWW-sivuja
  92. Comment[fr]=Navigue sur Internet
  93. Comment[hu]=A világháló böngészése
  94. Comment[it]=Esplora il web
  95. Comment[ja]=ウェブを閲覧します
  96. Comment[ko]=웹을 돌아 다닙니다
  97. Comment[nb]=Surf på nettet
  98. Comment[nl]=Verken het internet
  99. Comment[nn]=Surf på nettet
  100. Comment[no]=Surf på nettet
  101. Comment[pl]=Przeglądanie stron WWW
  102. Comment[pt]=Navegue na Internet
  103. Comment[pt_BR]=Navegue na Internet
  104. Comment[sk]=Prehliadanie internetu
  105. Comment[sv]=Surfa på webben
  106. Exec=qvm-run -q --tray -a %VMNAME% '{command} %u'
  107. Categories=Network;WebBrowser;
  108. X-Qubes-VmName=%VMNAME%
  109. Icon=%VMDIR%/icon.png
  110. '''
  111. QUBESXML_R1 = '''<?xml version='1.0' encoding='UTF-8'?>
  112. <QubesVmCollection clockvm="2" default_fw_netvm="2" default_kernel="3.2.7-10" default_netvm="3" default_template="1" updatevm="3"><QubesTemplateVm conf_file="fedora-17-x64.conf" debug="False" default_user="user" dir_path="/var/lib/qubes/vm-templates/fedora-17-x64" include_in_backups="True" installed_by_rpm="True" internal="False" kernel="3.2.7-10" kernelopts="" label="gray" maxmem="4063" memory="400" name="fedora-17-x64" netvm_qid="3" pcidevs="[]" private_img="private.img" qid="1" root_img="root.img" services="{&apos;meminfo-writer&apos;: True}" template_qid="none" uses_default_kernel="True" uses_default_kernelopts="True" uses_default_netvm="True" vcpus="2" volatile_img="volatile.img" /><QubesNetVm conf_file="netvm.conf" debug="False" default_user="user" dir_path="/var/lib/qubes/servicevms/netvm" include_in_backups="True" installed_by_rpm="False" internal="False" kernel="3.2.7-10" kernelopts="iommu=soft swiotlb=2048" label="red" maxmem="4063" memory="200" name="netvm" netid="1" pcidevs="[&apos;00:19.0&apos;, &apos;03:00.0&apos;]" private_img="private.img" qid="2" root_img="root.img" services="{&apos;ntpd&apos;: False, &apos;meminfo-writer&apos;: False}" template_qid="1" uses_default_kernel="True" uses_default_kernelopts="True" vcpus="2" volatile_img="volatile.img" /><QubesProxyVm conf_file="firewallvm.conf" debug="False" default_user="user" dir_path="/var/lib/qubes/servicevms/firewallvm" include_in_backups="True" installed_by_rpm="False" internal="False" kernel="3.2.7-10" kernelopts="" label="green" maxmem="4063" memory="200" name="firewallvm" netid="2" netvm_qid="2" pcidevs="[]" private_img="private.img" qid="3" root_img="root.img" services="{&apos;meminfo-writer&apos;: True}" template_qid="1" uses_default_kernel="True" uses_default_kernelopts="True" vcpus="2" volatile_img="volatile.img" /><QubesAppVm conf_file="fedora-17-x64-dvm.conf" debug="False" default_user="user" dir_path="/var/lib/qubes/appvms/fedora-17-x64-dvm" include_in_backups="True" installed_by_rpm="False" internal="True" kernel="3.2.7-10" kernelopts="" label="gray" maxmem="4063" memory="400" name="fedora-17-x64-dvm" netvm_qid="3" pcidevs="[]" private_img="private.img" qid="4" root_img="root.img" services="{&apos;meminfo-writer&apos;: True}" template_qid="1" uses_default_kernel="True" uses_default_kernelopts="True" uses_default_netvm="True" vcpus="1" volatile_img="volatile.img" /><QubesAppVm conf_file="test-work.conf" debug="False" default_user="user" dir_path="/var/lib/qubes/appvms/test-work" include_in_backups="True" installed_by_rpm="False" internal="False" kernel="3.2.7-10" kernelopts="" label="green" maxmem="4063" memory="400" name="test-work" netvm_qid="3" pcidevs="[]" private_img="private.img" qid="5" root_img="root.img" services="{&apos;meminfo-writer&apos;: True}" template_qid="1" uses_default_kernel="True" uses_default_kernelopts="True" uses_default_netvm="True" vcpus="2" volatile_img="volatile.img" /><QubesAppVm conf_file="personal.conf" debug="False" default_user="user" dir_path="/var/lib/qubes/appvms/personal" include_in_backups="True" installed_by_rpm="False" internal="False" kernel="3.2.7-10" kernelopts="" label="yellow" maxmem="4063" memory="400" name="personal" netvm_qid="3" pcidevs="[]" private_img="private.img" qid="6" root_img="root.img" services="{&apos;meminfo-writer&apos;: True}" template_qid="1" uses_default_kernel="True" uses_default_kernelopts="True" uses_default_netvm="True" vcpus="2" volatile_img="volatile.img" /><QubesAppVm conf_file="banking.conf" debug="False" default_user="user" dir_path="/var/lib/qubes/appvms/banking" include_in_backups="True" installed_by_rpm="False" internal="False" kernel="3.2.7-10" kernelopts="" label="green" maxmem="4063" memory="400" name="banking" netvm_qid="3" pcidevs="[]" private_img="private.img" qid="7" root_img="root.img" services="{&apos;meminfo-writer&apos;: True}" template_qid="1" uses_default_kernel="True" uses_default_kernelopts="True" uses_default_netvm="True" vcpus="2" volatile_img="volatile.img" /><QubesAppVm conf_file="untrusted.conf" debug="False" default_user="user" dir_path="/var/lib/qubes/appvms/untrusted" include_in_backups="True" installed_by_rpm="False" internal="False" kernel="3.2.7-10" kernelopts="" label="red" maxmem="4063" memory="400" name="untrusted" netvm_qid="3" pcidevs="[]" private_img="private.img" qid="8" root_img="root.img" services="{&apos;meminfo-writer&apos;: True}" template_qid="1" uses_default_kernel="True" uses_default_kernelopts="True" uses_default_netvm="True" vcpus="2" volatile_img="volatile.img" /><QubesAppVm conf_file="test-standalonevm.conf" debug="False" default_user="user" dir_path="/var/lib/qubes/appvms/test-standalonevm" include_in_backups="True" installed_by_rpm="False" internal="False" kernel="None" kernelopts="" label="red" maxmem="4063" memory="400" name="test-standalonevm" netvm_qid="3" pcidevs="[]" private_img="private.img" qid="9" root_img="root.img" services="{&apos;meminfo-writer&apos;: True}" template_qid="none" uses_default_kernel="False" uses_default_kernelopts="True" uses_default_netvm="True" vcpus="2" volatile_img="volatile.img" /><QubesAppVm conf_file="test-testvm.conf" debug="False" default_user="user" dir_path="/var/lib/qubes/appvms/test-testvm" include_in_backups="True" installed_by_rpm="False" internal="False" kernel="3.2.7-10" kernelopts="" label="red" mac="00:16:3E:5E:6C:55" maxmem="4063" memory="400" name="test-testvm" netvm_qid="3" pcidevs="[]" private_img="private.img" qid="10" root_img="root.img" services="{&apos;meminfo-writer&apos;: True}" template_qid="1" uses_default_kernel="True" uses_default_kernelopts="True" uses_default_netvm="True" vcpus="2" volatile_img="volatile.img" /><QubesTemplateVm conf_file="test-template-clone.conf" debug="False" default_user="user" dir_path="/var/lib/qubes/vm-templates/test-template-clone" include_in_backups="True" installed_by_rpm="False" internal="False" kernel="3.2.7-10" kernelopts="" label="gray" maxmem="4063" memory="400" name="test-template-clone" netvm_qid="3" pcidevs="[]" private_img="private.img" qid="11" root_img="root.img" services="{&apos;meminfo-writer&apos;: True}" template_qid="none" uses_default_kernel="True" uses_default_kernelopts="True" uses_default_netvm="True" vcpus="2" volatile_img="volatile.img" /><QubesAppVm conf_file="test-custom-template-appvm.conf" debug="False" default_user="user" dir_path="/var/lib/qubes/appvms/test-custom-template-appvm" include_in_backups="True" installed_by_rpm="False" internal="False" kernel="3.2.7-10" kernelopts="" label="yellow" maxmem="4063" memory="400" name="test-custom-template-appvm" netvm_qid="3" pcidevs="[]" private_img="private.img" qid="12" root_img="root.img" services="{&apos;meminfo-writer&apos;: True}" template_qid="11" uses_default_kernel="True" uses_default_kernelopts="True" uses_default_netvm="True" vcpus="2" volatile_img="volatile.img" /><QubesProxyVm conf_file="test-testproxy.conf" debug="False" default_user="user" dir_path="/var/lib/qubes/servicevms/test-testproxy" include_in_backups="True" installed_by_rpm="False" internal="False" kernel="3.2.7-10" kernelopts="" label="yellow" maxmem="4063" memory="200" name="test-testproxy" netid="3" netvm_qid="2" pcidevs="[]" private_img="private.img" qid="13" root_img="root.img" services="{&apos;meminfo-writer&apos;: True}" template_qid="1" uses_default_kernel="True" uses_default_kernelopts="True" vcpus="2" volatile_img="volatile.img" /></QubesVmCollection>
  113. '''
  114. BACKUP_HEADER_R2 = '''version=3
  115. hmac-algorithm=SHA512
  116. crypto-algorithm=aes-256-cbc
  117. encrypted={encrypted}
  118. compressed={compressed}
  119. compression-filter=gzip
  120. '''
  121. BACKUP_HEADER_R4 = '''version=4
  122. hmac-algorithm=scrypt
  123. encrypted=True
  124. compressed={compressed}
  125. compression-filter=gzip
  126. backup-id=20161020T123455-1234
  127. '''
  128. parsed_qubes_xml_r2 = {
  129. 'domains': {
  130. 'dom0': {
  131. 'klass': 'AdminVM',
  132. 'label': 'black',
  133. 'properties': {},
  134. 'devices': {},
  135. 'tags': set(),
  136. 'features': {},
  137. 'template': None,
  138. 'backup_path': None,
  139. 'included_in_backup': False,
  140. },
  141. 'fedora-20-x64': {
  142. 'klass': 'TemplateVM',
  143. 'label': 'black',
  144. 'properties': {
  145. 'virt_mode': 'pv',
  146. 'maxmem': '1535',
  147. },
  148. 'devices': {},
  149. 'tags': set(),
  150. 'features': {'services.meminfo-writer': True},
  151. 'template': None,
  152. 'backup_path': None,
  153. 'included_in_backup': False,
  154. },
  155. 'netvm': {
  156. 'klass': 'AppVM',
  157. 'label': 'red',
  158. 'properties': {
  159. 'virt_mode': 'pv',
  160. 'maxmem': '1535',
  161. 'memory': '200',
  162. 'netvm': None,
  163. 'default_dispvm': 'disp-no-netvm',
  164. 'provides_network': True},
  165. 'devices': {
  166. 'pci': {
  167. ('dom0', '02_00.0'): {},
  168. ('dom0', '03_00.0'): {},
  169. }
  170. },
  171. 'tags': set(),
  172. 'features': {
  173. 'service.ntpd': False,
  174. 'services.meminfo-writer': False
  175. },
  176. 'template': 'fedora-20-x64',
  177. 'backup_path': None,
  178. 'included_in_backup': False,
  179. },
  180. 'firewallvm': {
  181. 'klass': 'AppVM',
  182. 'label': 'green',
  183. 'properties': {
  184. 'virt_mode': 'pv',
  185. 'maxmem': '1535',
  186. 'memory': '200',
  187. 'provides_network': True
  188. },
  189. 'devices': {},
  190. 'tags': set(),
  191. 'features': {'services.meminfo-writer': True},
  192. 'template': 'fedora-20-x64',
  193. 'backup_path': None,
  194. 'included_in_backup': False,
  195. },
  196. 'fedora-20-x64-dvm': {
  197. 'klass': 'AppVM',
  198. 'label': 'gray',
  199. 'properties': {
  200. 'virt_mode': 'pv',
  201. 'maxmem': '1535',
  202. 'vcpus': '1'
  203. },
  204. 'devices': {},
  205. 'tags': set(),
  206. 'features': {
  207. 'internal': True, 'services.meminfo-writer': True},
  208. 'template': 'fedora-20-x64',
  209. 'backup_path': None,
  210. 'included_in_backup': False,
  211. },
  212. 'banking': {
  213. 'klass': 'AppVM',
  214. 'label': 'green',
  215. 'properties': {'virt_mode': 'pv', 'maxmem': '1535'},
  216. 'devices': {},
  217. 'tags': set(),
  218. 'features': {'services.meminfo-writer': True},
  219. 'template': 'fedora-20-x64',
  220. 'backup_path': None,
  221. 'included_in_backup': False,
  222. },
  223. 'personal': {
  224. 'klass': 'AppVM',
  225. 'label': 'yellow',
  226. 'properties': {'virt_mode': 'pv', 'maxmem': '1535'},
  227. 'devices': {},
  228. 'tags': set(),
  229. 'features': {'services.meminfo-writer': True},
  230. 'template': 'fedora-20-x64',
  231. 'backup_path': None,
  232. 'included_in_backup': False,
  233. },
  234. 'untrusted': {
  235. 'klass': 'AppVM',
  236. 'label': 'red',
  237. 'properties': {
  238. 'virt_mode': 'pv',
  239. 'maxmem': '1535',
  240. 'netvm': 'test-testproxy',
  241. 'default_dispvm': 'disp-test-testproxy',
  242. },
  243. 'devices': {},
  244. 'tags': set(),
  245. 'features': {'services.meminfo-writer': True},
  246. 'template': 'fedora-20-x64',
  247. 'backup_path': None,
  248. 'included_in_backup': False,
  249. },
  250. 'testproxy2': {
  251. 'klass': 'AppVM',
  252. 'label': 'red',
  253. 'properties': {
  254. 'virt_mode': 'pv',
  255. 'maxmem': '1535',
  256. 'memory': '200',
  257. 'provides_network': True},
  258. 'devices': {},
  259. 'tags': set(),
  260. 'features': {'services.meminfo-writer': True},
  261. 'template': 'test-template-clone',
  262. 'backup_path': None,
  263. 'included_in_backup': False,
  264. },
  265. 'test-testproxy': {
  266. 'klass': 'AppVM',
  267. 'label': 'red',
  268. 'properties': {
  269. 'virt_mode': 'pv',
  270. 'maxmem': '1535',
  271. 'memory': '200',
  272. 'provides_network': True},
  273. 'devices': {},
  274. 'tags': set(),
  275. 'features': {'services.meminfo-writer': True},
  276. 'template': 'fedora-20-x64',
  277. 'backup_path': 'servicevms/test-testproxy',
  278. 'included_in_backup': True,
  279. },
  280. 'test-testhvm': {
  281. 'klass': 'StandaloneVM',
  282. 'label': 'purple',
  283. 'properties': {'virt_mode': 'hvm', 'memory': '512'},
  284. 'devices': {},
  285. 'tags': set(),
  286. 'features': {'services.meminfo-writer': False},
  287. 'template': None,
  288. 'backup_path': 'appvms/test-testhvm',
  289. 'included_in_backup': True,
  290. 'root_size': 2097664,
  291. },
  292. 'test-work': {
  293. 'klass': 'AppVM',
  294. 'label': 'green',
  295. 'properties': {'virt_mode': 'pv', 'maxmem': '1535'},
  296. 'devices': {},
  297. 'tags': set(),
  298. 'features': {'services.meminfo-writer': True},
  299. 'template': 'fedora-20-x64',
  300. 'backup_path': 'appvms/test-work',
  301. 'included_in_backup': True,
  302. },
  303. 'test-template-clone': {
  304. 'klass': 'TemplateVM',
  305. 'label': 'green',
  306. 'properties': {'virt_mode': 'pv', 'maxmem': '1535'},
  307. 'devices': {},
  308. 'tags': set(),
  309. 'features': {'services.meminfo-writer': True},
  310. 'template': None,
  311. 'backup_path': 'vm-templates/test-template-clone',
  312. 'included_in_backup': True,
  313. },
  314. 'test-custom-template-appvm': {
  315. 'klass': 'AppVM',
  316. 'label': 'yellow',
  317. 'properties': {'virt_mode': 'pv', 'maxmem': '1535'},
  318. 'devices': {},
  319. 'tags': set(),
  320. 'features': {'services.meminfo-writer': True},
  321. 'template': 'test-template-clone',
  322. 'backup_path': 'appvms/test-custom-template-appvm',
  323. 'included_in_backup': True,
  324. },
  325. 'test-standalonevm': {
  326. 'klass': 'StandaloneVM',
  327. 'label': 'blue',
  328. 'properties': {'virt_mode': 'pv', 'maxmem': '1535'},
  329. 'devices': {},
  330. 'tags': set(),
  331. 'features': {'services.meminfo-writer': True},
  332. 'template': None,
  333. 'backup_path': 'appvms/test-standalonevm',
  334. 'included_in_backup': True,
  335. 'root_size': 2097664,
  336. },
  337. 'test-net': {
  338. 'klass': 'AppVM',
  339. 'label': 'red',
  340. 'properties': {'virt_mode': 'pv',
  341. 'maxmem': '1535',
  342. 'memory': '200',
  343. 'netvm': None,
  344. 'default_dispvm': 'disp-no-netvm',
  345. 'provides_network': True},
  346. 'devices': {
  347. 'pci': {
  348. ('dom0', '02_00.0'): {},
  349. ('dom0', '03_00.0'): {},
  350. }
  351. },
  352. 'tags': set(),
  353. 'features': {
  354. 'service.ntpd': False,
  355. 'services.meminfo-writer': False
  356. },
  357. 'template': 'fedora-20-x64',
  358. 'backup_path': 'servicevms/test-net',
  359. 'included_in_backup': True,
  360. },
  361. 'disp-no-netvm': {
  362. 'klass': 'AppVM',
  363. 'label': 'red',
  364. 'properties': {
  365. 'netvm': None,
  366. 'dispvm_allowed': True,
  367. },
  368. 'devices': {},
  369. 'features': {},
  370. 'tags': set(),
  371. 'template': None, # default
  372. 'backup_path': None,
  373. 'included_in_backup': True,
  374. },
  375. 'disp-test-testproxy': {
  376. 'klass': 'AppVM',
  377. 'label': 'red',
  378. 'properties': {
  379. 'netvm': 'test-testproxy',
  380. 'dispvm_allowed': True,
  381. },
  382. 'devices': {},
  383. 'features': {},
  384. 'tags': set(),
  385. 'template': None, # default
  386. 'backup_path': None,
  387. 'included_in_backup': True,
  388. },
  389. },
  390. 'globals': {
  391. 'default_template': 'fedora-20-x64',
  392. 'default_kernel': '3.7.6-2',
  393. 'default_netvm': 'firewallvm',
  394. 'clockvm': 'netvm',
  395. 'updatevm': 'firewallvm'
  396. },
  397. }
  398. parsed_qubes_xml_v4 = {
  399. 'domains': {
  400. 'dom0': {
  401. 'klass': 'AdminVM',
  402. 'label': 'black',
  403. 'properties': {},
  404. 'devices': {},
  405. 'tags': set(),
  406. 'features': {},
  407. 'template': None,
  408. 'backup_path': None,
  409. 'included_in_backup': False,
  410. },
  411. 'fedora-25': {
  412. 'klass': 'TemplateVM',
  413. 'label': 'black',
  414. 'properties': {},
  415. 'devices': {},
  416. 'tags': {'created-by-test-work'},
  417. 'features': {
  418. 'gui': '1',
  419. 'qrexec': 'True',
  420. 'updates-available': False
  421. },
  422. 'template': None,
  423. 'backup_path': None,
  424. 'included_in_backup': False,
  425. },
  426. 'fedora-25-lvm': {
  427. 'klass': 'TemplateVM',
  428. 'label': 'black',
  429. 'properties': {
  430. 'maxmem': '4000',
  431. },
  432. 'devices': {},
  433. 'tags': set(),
  434. 'features': {},
  435. 'template': None,
  436. 'backup_path': None,
  437. 'included_in_backup': False,
  438. },
  439. 'debian-8': {
  440. 'klass': 'TemplateVM',
  441. 'label': 'black',
  442. 'properties': {},
  443. 'devices': {},
  444. 'tags': {'created-by-dom0'},
  445. 'features': {
  446. 'gui': '1',
  447. 'qrexec': 'True',
  448. 'updates-available': False},
  449. 'template': None,
  450. 'backup_path': None,
  451. 'included_in_backup': False,
  452. },
  453. 'sys-net': {
  454. 'klass': 'AppVM',
  455. 'label': 'red',
  456. 'properties': {
  457. 'virt_mode': 'hvm',
  458. 'kernelopts': 'nopat i8042.nokbd i8042.noaux',
  459. 'maxmem': '300',
  460. 'memory': '300',
  461. 'netvm': None,
  462. 'default_user': 'user',
  463. 'provides_network': 'True'},
  464. 'devices': {
  465. 'pci': {
  466. ('dom0', '02_00.0'): {},
  467. }
  468. },
  469. 'tags': set(),
  470. 'features': {
  471. 'service.clocksync': '1',
  472. 'service.meminfo-writer': False
  473. },
  474. 'template': 'fedora-25',
  475. 'backup_path': None,
  476. 'included_in_backup': False,
  477. },
  478. 'sys-firewall': {
  479. 'klass': 'AppVM',
  480. 'label': 'green',
  481. 'properties': {
  482. 'autostart': 'True',
  483. 'memory': '500',
  484. 'provides_network': 'True'
  485. },
  486. 'devices': {},
  487. 'tags': set(),
  488. 'features': {},
  489. 'template': 'fedora-25',
  490. 'backup_path': None,
  491. 'included_in_backup': False,
  492. },
  493. 'test-d8test': {
  494. 'klass': 'AppVM',
  495. 'label': 'gray',
  496. 'properties': {'debug': 'True', 'kernel': None},
  497. 'devices': {},
  498. 'tags': {'created-by-dom0'},
  499. 'features': {},
  500. 'template': 'debian-8',
  501. 'backup_path': 'appvms/test-d8test',
  502. 'included_in_backup': True,
  503. },
  504. 'fedora-25-dvm': {
  505. 'klass': 'AppVM',
  506. 'label': 'red',
  507. 'properties': {
  508. 'dispvm_allowed': 'True',
  509. 'vcpus': '1',
  510. },
  511. 'devices': {},
  512. 'tags': set(),
  513. 'features': {
  514. 'internal': '1', 'service.meminfo-writer': '1'},
  515. 'template': 'fedora-25',
  516. 'backup_path': None,
  517. 'included_in_backup': False,
  518. },
  519. 'fedora-25-clone-dvm': {
  520. 'klass': 'AppVM',
  521. 'label': 'red',
  522. 'properties': {
  523. 'vcpus': '1',
  524. 'dispvm_allowed': 'True',
  525. },
  526. 'devices': {},
  527. 'tags': set(),
  528. 'features': {
  529. 'internal': '1', 'service.meminfo-writer': '1'},
  530. 'template': 'test-fedora-25-clone',
  531. 'backup_path': None,
  532. 'included_in_backup': False,
  533. },
  534. 'vault': {
  535. 'klass': 'AppVM',
  536. 'label': 'black',
  537. 'properties': {'virt_mode': 'pv', 'maxmem': '1536', 'netvm': None},
  538. 'devices': {},
  539. 'tags': set(),
  540. 'features': {},
  541. 'template': 'fedora-25',
  542. 'backup_path': None,
  543. 'included_in_backup': False,
  544. },
  545. 'personal': {
  546. 'klass': 'AppVM',
  547. 'label': 'yellow',
  548. 'properties': {'netvm': 'sys-firewall'},
  549. 'devices': {},
  550. 'tags': set(),
  551. 'features': {
  552. 'feat1': '1',
  553. 'feat2': False,
  554. 'feat32': '1',
  555. 'featdis': False,
  556. 'xxx': '1'
  557. },
  558. 'template': 'fedora-25',
  559. 'backup_path': None,
  560. 'included_in_backup': False,
  561. },
  562. 'untrusted': {
  563. 'klass': 'AppVM',
  564. 'label': 'red',
  565. 'properties': {
  566. 'netvm': None,
  567. 'backup_timestamp': '1474318497',
  568. 'default_dispvm': 'fedora-25-clone-dvm',
  569. },
  570. 'devices': {},
  571. 'tags': set(),
  572. 'features': {'service.meminfo-writer': '1'},
  573. 'template': 'fedora-25',
  574. 'backup_path': None,
  575. 'included_in_backup': False,
  576. },
  577. 'sys-usb': {
  578. 'klass': 'AppVM',
  579. 'label': 'red',
  580. 'properties': {
  581. 'virt_mode': 'pv',
  582. 'autostart': 'True',
  583. 'maxmem': '400',
  584. 'provides_network': 'True',
  585. },
  586. 'devices': {},
  587. 'tags': set(),
  588. 'features': {
  589. 'service.meminfo-writer': False,
  590. 'service.network-manager': False,
  591. },
  592. 'template': 'fedora-25',
  593. 'backup_path': None,
  594. 'included_in_backup': False,
  595. },
  596. 'test-proxy': {
  597. 'klass': 'AppVM',
  598. 'label': 'red',
  599. 'properties': {'netvm': 'sys-net', 'provides_network': 'True'},
  600. 'devices': {},
  601. 'tags': {'created-by-dom0'},
  602. 'features': {},
  603. 'template': 'debian-8',
  604. 'backup_path': 'appvms/test-proxy',
  605. 'included_in_backup': True,
  606. },
  607. 'test-hvm': {
  608. 'klass': 'StandaloneVM',
  609. 'label': 'purple',
  610. 'properties': {'virt_mode': 'hvm', 'maxmem': '4000'},
  611. 'devices': {},
  612. 'tags': set(),
  613. 'features': {'service.meminfo-writer': False},
  614. 'template': None,
  615. 'backup_path': 'appvms/test-hvm',
  616. 'included_in_backup': True,
  617. 'root_size': 2097664,
  618. },
  619. 'test-work': {
  620. 'klass': 'AppVM',
  621. 'label': 'green',
  622. 'properties': {
  623. 'ip': '192.168.0.1',
  624. 'maxmem': '4000',
  625. 'memory': '400'},
  626. 'devices': {},
  627. 'tags': {'tag1', 'tag2'},
  628. 'features': {'service.meminfo-writer': '1'},
  629. 'template': 'fedora-25',
  630. 'backup_path': 'appvms/test-work',
  631. 'included_in_backup': True,
  632. },
  633. 'test-fedora-25-clone': {
  634. 'klass': 'TemplateVM',
  635. 'label': 'black',
  636. 'properties': {'maxmem': '4000'},
  637. 'devices': {},
  638. 'tags': set(),
  639. 'features': {'service.meminfo-writer': '1'},
  640. 'template': None,
  641. 'backup_path': 'vm-templates/test-fedora-25-clone',
  642. 'included_in_backup': True,
  643. },
  644. 'test-custom-template-appvm': {
  645. 'klass': 'AppVM',
  646. 'label': 'yellow',
  647. 'properties': {'debug': 'True', 'kernel': None},
  648. 'devices': {},
  649. 'tags': {'created-by-dom0'},
  650. 'features': {},
  651. 'template': 'test-fedora-25-clone',
  652. 'backup_path': 'appvms/test-custom-template-appvm',
  653. 'included_in_backup': True,
  654. },
  655. 'test-standalonevm': {
  656. 'klass': 'StandaloneVM',
  657. 'label': 'blue',
  658. 'properties': {'maxmem': '4000'},
  659. 'devices': {},
  660. 'tags': set(),
  661. 'features': {},
  662. 'template': None,
  663. 'backup_path': 'appvms/test-standalonevm',
  664. 'included_in_backup': True,
  665. 'root_size': 2097664,
  666. },
  667. 'test-net': {
  668. 'klass': 'AppVM',
  669. 'label': 'red',
  670. 'properties': {
  671. 'maxmem': '300',
  672. 'memory': '300',
  673. 'netvm': None,
  674. 'provides_network': 'True'
  675. },
  676. 'devices': {
  677. 'pci': {
  678. ('dom0', '03_00.0'): {},
  679. }
  680. },
  681. 'tags': set(),
  682. 'features': {
  683. 'service.ntpd': False,
  684. 'service.meminfo-writer': False
  685. },
  686. 'template': 'fedora-25',
  687. 'backup_path': 'appvms/test-net',
  688. 'included_in_backup': True,
  689. },
  690. },
  691. 'globals': {
  692. 'default_template': 'fedora-25',
  693. 'default_kernel': '4.9.31-17',
  694. 'default_netvm': 'sys-firewall',
  695. 'default_dispvm': 'fedora-25-dvm',
  696. #'default_fw_netvm': 'sys-net',
  697. 'clockvm': 'sys-net',
  698. 'updatevm': 'sys-firewall'
  699. },
  700. }
  701. class TC_00_QubesXML(qubesadmin.tests.QubesTestCase):
  702. def assertCorrectlyConverted(self, backup_app, expected_data):
  703. self.assertCountEqual(backup_app.domains.keys(),
  704. expected_data['domains'].keys())
  705. for vm in expected_data['domains']:
  706. self.assertEqual(backup_app.domains[vm].name, vm)
  707. self.assertEqual(backup_app.domains[vm].properties,
  708. expected_data['domains'][vm]['properties'], vm)
  709. for devtype in expected_data['domains'][vm]['devices']:
  710. self.assertEqual(backup_app.domains[vm].devices[devtype],
  711. expected_data['domains'][vm]['devices'][devtype], vm)
  712. self.assertEqual(backup_app.domains[vm].tags,
  713. expected_data['domains'][vm]['tags'], vm)
  714. self.assertEqual(backup_app.domains[vm].features,
  715. expected_data['domains'][vm]['features'], vm)
  716. self.assertEqual(backup_app.domains[vm].template,
  717. expected_data['domains'][vm]['template'], vm)
  718. self.assertEqual(backup_app.domains[vm].klass,
  719. expected_data['domains'][vm]['klass'], vm)
  720. self.assertEqual(backup_app.domains[vm].label,
  721. expected_data['domains'][vm]['label'], vm)
  722. self.assertEqual(backup_app.domains[vm].backup_path,
  723. expected_data['domains'][vm]['backup_path'], vm)
  724. self.assertEqual(backup_app.domains[vm].included_in_backup,
  725. expected_data['domains'][vm]['included_in_backup'], vm)
  726. self.assertEqual(backup_app.globals, expected_data['globals'])
  727. def test_000_qubes_xml_r2(self):
  728. xml_data = pkg_resources.resource_string(__name__, 'v3-qubes.xml')
  729. with tempfile.NamedTemporaryFile() as qubes_xml:
  730. qubes_xml.file.write(xml_data)
  731. backup_app = qubesadmin.backup.core2.Core2Qubes(qubes_xml.name)
  732. self.assertCorrectlyConverted(backup_app, parsed_qubes_xml_r2)
  733. def test_010_qubes_xml_r4(self):
  734. self.maxDiff = None
  735. xml_data = pkg_resources.resource_string(__name__, 'v4-qubes.xml')
  736. with tempfile.NamedTemporaryFile() as qubes_xml:
  737. qubes_xml.file.write(xml_data)
  738. backup_app = qubesadmin.backup.core3.Core3Qubes(qubes_xml.name)
  739. self.assertCorrectlyConverted(backup_app, parsed_qubes_xml_v4)
  740. # backup code use multiprocessing, synchronize with main process
  741. class AppProxy(object):
  742. def __init__(self, app, sync_queue):
  743. self._app = app
  744. self._sync_queue = sync_queue
  745. def qubesd_call(self, dest, method, arg=None, payload=None,
  746. payload_stream=None):
  747. if payload_stream:
  748. signature_bin = payload_stream.read(512)
  749. payload = signature_bin.split(b'\0', 1)[0]
  750. subprocess.call(['cat'], stdin=payload_stream,
  751. stdout=subprocess.DEVNULL)
  752. payload_stream.close()
  753. self._sync_queue.put((dest, method, arg, payload))
  754. return self._app.qubesd_call(dest, method, arg, payload)
  755. class MockVolume(qubesadmin.storage.Volume):
  756. def __init__(self, import_data_queue, *args, **kwargs):
  757. super(MockVolume, self).__init__(*args, **kwargs)
  758. self.app = AppProxy(self.app, import_data_queue)
  759. class MockFirewall(qubesadmin.firewall.Firewall):
  760. def __init__(self, import_data_queue, *args, **kwargs):
  761. super(MockFirewall, self).__init__(*args, **kwargs)
  762. self.vm.app = AppProxy(self.vm.app, import_data_queue)
  763. @unittest.skipUnless(os.environ.get('ENABLE_SLOW_TESTS', False),
  764. 'Set ENABLE_SLOW_TESTS=1 environment variable')
  765. class TC_10_BackupCompatibility(qubesadmin.tests.backup.BackupTestCase):
  766. storage_pool = None
  767. def tearDown(self):
  768. super(TC_10_BackupCompatibility, self).tearDown()
  769. def create_whitelisted_appmenus(self, filename):
  770. with open(filename, "w") as f:
  771. f.write("gnome-terminal.desktop\n")
  772. f.write("nautilus.desktop\n")
  773. f.write("firefox.desktop\n")
  774. f.write("mozilla-thunderbird.desktop\n")
  775. f.write("libreoffice-startcenter.desktop\n")
  776. def create_appmenus(self, dir, template, list):
  777. for name in list:
  778. with open(os.path.join(dir, name + ".desktop"), "w") as f:
  779. f.write(template.format(name=name, comment=name, command=name))
  780. def create_private_img(self, filename):
  781. signature = '/'.join(os.path.splitext(filename)[0].split('/')[-2:])
  782. self.create_sparse(filename, 2*2**20, signature=signature.encode())
  783. #subprocess.check_call(["/usr/sbin/mkfs.ext4", "-q", "-F", filename])
  784. def create_volatile_img(self, filename):
  785. self.create_sparse(filename, 11.5*2**20)
  786. # here used to be sfdisk call with "0,1024,S\n,10240,L\n" input,
  787. # but since sfdisk folks like to change command arguments in
  788. # incompatible way, have an partition table verbatim here
  789. ptable = (
  790. '\x00\x00\x00\x00\x00\x00\x00\x00\xab\x39\xd5\xd4\x00\x00\x20\x00'
  791. '\x00\x21\xaa\x82\x82\x28\x08\x00\x00\x00\x00\x00\x00\x20\xaa\x00'
  792. '\x82\x29\x15\x83\x9c\x79\x08\x00\x00\x20\x00\x00\x01\x40\x00\x00'
  793. '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
  794. '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xaa\x55'
  795. )
  796. with open(filename, 'r+') as f:
  797. f.seek(0x1b0)
  798. f.write(ptable)
  799. # TODO: mkswap
  800. def fullpath(self, name):
  801. return os.path.join(self.backupdir, name)
  802. def create_v1_files(self, r2b2=False):
  803. appmenus_list = [
  804. "firefox", "gnome-terminal", "evince", "evolution",
  805. "mozilla-thunderbird", "libreoffice-startcenter", "nautilus",
  806. "gedit", "gpk-update-viewer", "gpk-application"
  807. ]
  808. os.mkdir(self.fullpath("appvms"))
  809. os.mkdir(self.fullpath("servicevms"))
  810. os.mkdir(self.fullpath("vm-templates"))
  811. # normal AppVM, with firewall
  812. os.mkdir(self.fullpath("appvms/test-work"))
  813. self.create_whitelisted_appmenus(self.fullpath(
  814. "appvms/test-work/whitelisted-appmenus.list"))
  815. os.symlink("/usr/share/qubes/icons/green.png",
  816. self.fullpath("appvms/test-work/icon.png"))
  817. self.create_private_img(self.fullpath("appvms/test-work/private.img"))
  818. with open(self.fullpath("appvms/test-work/firewall.xml"), "wb") as \
  819. f_firewall:
  820. f_firewall.write(
  821. pkg_resources.resource_string(__name__, 'v3-firewall.xml'))
  822. # StandaloneVM
  823. os.mkdir(self.fullpath("appvms/test-standalonevm"))
  824. self.create_whitelisted_appmenus(self.fullpath(
  825. "appvms/test-standalonevm/whitelisted-appmenus.list"))
  826. os.symlink("/usr/share/qubes/icons/blue.png",
  827. self.fullpath("appvms/test-standalonevm/icon.png"))
  828. self.create_private_img(self.fullpath(
  829. "appvms/test-standalonevm/private.img"))
  830. self.create_sparse(
  831. self.fullpath("appvms/test-standalonevm/root.img"), 10*2**30)
  832. self.fill_image(self.fullpath("appvms/test-standalonevm/root.img"),
  833. 1024*1024, True,
  834. signature=b'test-standalonevm/root')
  835. os.mkdir(self.fullpath("appvms/test-standalonevm/apps.templates"))
  836. self.create_appmenus(self.fullpath("appvms/test-standalonevm/apps"
  837. ".templates"),
  838. APPTEMPLATE_R2B2,
  839. appmenus_list)
  840. os.mkdir(self.fullpath("appvms/test-standalonevm/kernels"))
  841. for k_file in ["initramfs", "vmlinuz", "modules.img"]:
  842. self.fill_image(self.fullpath("appvms/test-standalonevm/kernels/"
  843. + k_file), 1024*1024)
  844. # VM based on custom template
  845. subprocess.check_call(
  846. ["/bin/cp", "-a", self.fullpath("appvms/test-work"),
  847. self.fullpath("appvms/test-custom-template-appvm")])
  848. # override for correct signature
  849. self.create_private_img(
  850. self.fullpath("appvms/test-custom-template-appvm/private.img"))
  851. # HVM
  852. if r2b2:
  853. subprocess.check_call(
  854. ["/bin/cp", "-a", self.fullpath("appvms/test-standalonevm"),
  855. self.fullpath("appvms/test-testhvm")])
  856. # override for correct signature
  857. self.create_private_img(
  858. self.fullpath("appvms/test-testhvm/private.img"))
  859. self.fill_image(self.fullpath("appvms/test-testhvm/root.img"),
  860. 1024*1024, True,
  861. signature=b'test-testhvm/root')
  862. # ProxyVM
  863. os.mkdir(self.fullpath("servicevms/test-testproxy"))
  864. self.create_whitelisted_appmenus(self.fullpath(
  865. "servicevms/test-testproxy/whitelisted-appmenus.list"))
  866. self.create_private_img(
  867. self.fullpath("servicevms/test-testproxy/private.img"))
  868. # NetVM
  869. os.mkdir(self.fullpath("servicevms/test-net"))
  870. self.create_whitelisted_appmenus(self.fullpath(
  871. "servicevms/test-net/whitelisted-appmenus.list"))
  872. self.create_private_img(
  873. self.fullpath("servicevms/test-net/private.img"))
  874. # Custom template
  875. os.mkdir(self.fullpath("vm-templates/test-template-clone"))
  876. self.create_private_img(
  877. self.fullpath("vm-templates/test-template-clone/private.img"))
  878. self.create_sparse(self.fullpath(
  879. "vm-templates/test-template-clone/root-cow.img"), 10*2**20)
  880. self.create_sparse(self.fullpath(
  881. "vm-templates/test-template-clone/root.img"), 10*2**20)
  882. self.fill_image(self.fullpath(
  883. "vm-templates/test-template-clone/root.img"), 1*2**20, True,
  884. signature=b'test-template-clone/root')
  885. self.create_volatile_img(self.fullpath(
  886. "vm-templates/test-template-clone/volatile.img"))
  887. subprocess.check_call([
  888. "/bin/tar", "cS",
  889. "-f", self.fullpath(
  890. "vm-templates/test-template-clone/clean-volatile.img.tar"),
  891. "-C", self.fullpath("vm-templates/test-template-clone"),
  892. "volatile.img"])
  893. self.create_whitelisted_appmenus(self.fullpath(
  894. "vm-templates/test-template-clone/whitelisted-appmenus.list"))
  895. self.create_whitelisted_appmenus(self.fullpath(
  896. "vm-templates/test-template-clone/vm-whitelisted-appmenus.list"))
  897. if r2b2:
  898. self.create_whitelisted_appmenus(self.fullpath(
  899. "vm-templates/test-template-clone/netvm-whitelisted-appmenus"
  900. ".list"))
  901. os.symlink("/usr/share/qubes/icons/green.png",
  902. self.fullpath("vm-templates/test-template-clone/icon.png"))
  903. os.mkdir(
  904. self.fullpath("vm-templates/test-template-clone/apps.templates"))
  905. self.create_appmenus(
  906. self.fullpath("vm-templates/test-template-clone/apps.templates"),
  907. APPTEMPLATE_R2B2,
  908. appmenus_list)
  909. os.mkdir(self.fullpath("vm-templates/test-template-clone/apps"))
  910. self.create_appmenus(
  911. self.fullpath("vm-templates/test-template-clone/apps"),
  912. APPTEMPLATE_R2B2.replace("%VMNAME%", "test-template-clone")
  913. .replace("%VMDIR%", self.fullpath(
  914. "vm-templates/test-template-clone")),
  915. appmenus_list)
  916. def create_v4_files(self):
  917. appmenus_list = [
  918. "firefox", "gnome-terminal", "evince", "evolution",
  919. "mozilla-thunderbird", "libreoffice-startcenter", "nautilus",
  920. "gedit", "gpk-update-viewer", "gpk-application"
  921. ]
  922. os.mkdir(self.fullpath("appvms"))
  923. os.mkdir(self.fullpath("vm-templates"))
  924. # normal AppVMs
  925. for vm in ('test-work', 'test-d8test', 'test-proxy',
  926. 'test-custom-template-appvm', 'test-net'):
  927. os.mkdir(self.fullpath('appvms/{}'.format(vm)))
  928. self.create_whitelisted_appmenus(self.fullpath(
  929. 'appvms/{}/whitelisted-appmenus.list'.format(vm)))
  930. self.create_private_img(self.fullpath('appvms/{}/private.img'.format(
  931. vm)))
  932. # setup firewall only on one VM
  933. with open(self.fullpath("appvms/test-work/firewall.xml"), "wb") as \
  934. f_firewall:
  935. f_firewall.write(
  936. pkg_resources.resource_string(__name__, 'v4-firewall.xml'))
  937. # StandaloneVMs
  938. for vm in ('test-standalonevm', 'test-hvm'):
  939. os.mkdir(self.fullpath('appvms/{}'.format(vm)))
  940. self.create_whitelisted_appmenus(self.fullpath(
  941. 'appvms/{}/whitelisted-appmenus.list'.format(vm)))
  942. self.create_private_img(self.fullpath(
  943. 'appvms/{}/private.img'.format(vm)))
  944. self.create_sparse(
  945. self.fullpath('appvms/{}/root.img'.format(vm)), 10*2**30)
  946. self.fill_image(self.fullpath('appvms/{}/root.img'.format(vm)),
  947. 1024*1024, True,
  948. signature='{}/root'.format(vm).encode())
  949. # only for Linux one
  950. os.mkdir(self.fullpath('appvms/test-standalonevm/apps.templates'))
  951. self.create_appmenus(
  952. self.fullpath('appvms/test-standalonevm/apps.templates'),
  953. APPTEMPLATE_R2B2,
  954. appmenus_list)
  955. # Custom template
  956. os.mkdir(self.fullpath("vm-templates/test-fedora-25-clone"))
  957. self.create_private_img(
  958. self.fullpath("vm-templates/test-fedora-25-clone/private.img"))
  959. self.create_sparse(self.fullpath(
  960. "vm-templates/test-fedora-25-clone/root.img"), 10*2**20)
  961. self.fill_image(self.fullpath(
  962. "vm-templates/test-fedora-25-clone/root.img"), 1*2**20, True,
  963. signature=b'test-fedora-25-clone/root')
  964. self.create_volatile_img(self.fullpath(
  965. "vm-templates/test-fedora-25-clone/volatile.img"))
  966. self.create_whitelisted_appmenus(self.fullpath(
  967. "vm-templates/test-fedora-25-clone/whitelisted-appmenus.list"))
  968. self.create_whitelisted_appmenus(self.fullpath(
  969. "vm-templates/test-fedora-25-clone/vm-whitelisted-appmenus.list"))
  970. os.mkdir(
  971. self.fullpath("vm-templates/test-fedora-25-clone/apps.templates"))
  972. self.create_appmenus(
  973. self.fullpath("vm-templates/test-fedora-25-clone/apps.templates"),
  974. APPTEMPLATE_R2B2,
  975. appmenus_list)
  976. os.mkdir(self.fullpath("vm-templates/test-fedora-25-clone/apps"))
  977. self.create_appmenus(
  978. self.fullpath("vm-templates/test-fedora-25-clone/apps"),
  979. APPTEMPLATE_R2B2.replace("%VMNAME%", "test-fedora-25-clone")
  980. .replace("%VMDIR%", self.fullpath(
  981. "vm-templates/test-fedora-25-clone")),
  982. appmenus_list)
  983. def scrypt_encrypt(self, f_name, output_name=None, password='qubes',
  984. basedir=None):
  985. if basedir is None:
  986. basedir = self.backupdir
  987. if output_name is None:
  988. output_name = f_name + '.enc'
  989. if f_name == 'backup-header':
  990. scrypt_pass = 'backup-header!' + password
  991. else:
  992. scrypt_pass = '20161020T123455-1234!{}!{}'.format(f_name, password)
  993. p = subprocess.Popen(['scrypt', 'enc', '-P', '-t', '0.1',
  994. os.path.join(basedir, f_name), os.path.join(basedir, output_name)],
  995. stdin=subprocess.PIPE)
  996. p.communicate(scrypt_pass.encode())
  997. assert p.wait() == 0
  998. return output_name
  999. def calculate_hmac(self, f_name, algorithm="sha512", password="qubes"):
  1000. with open(self.fullpath(f_name), "r") as f_data:
  1001. with open(self.fullpath(f_name+".hmac"), "w") as f_hmac:
  1002. subprocess.check_call(
  1003. ["openssl", "dgst", "-"+algorithm, "-hmac", password],
  1004. stdin=f_data, stdout=f_hmac)
  1005. def append_backup_stream(self, f_name, stream, basedir=None):
  1006. if not basedir:
  1007. basedir = self.backupdir
  1008. subprocess.check_call(["tar", "-cO", "--posix", "-C", basedir,
  1009. f_name],
  1010. stdout=stream)
  1011. def handle_v3_file(self, f_name, subdir, stream, compressed=True,
  1012. encrypted=True):
  1013. # create inner archive
  1014. tar_cmdline = ["tar", "-Pc", '--sparse',
  1015. '-C', self.fullpath(os.path.dirname(f_name)),
  1016. '--xform', 's:^%s:%s\\0:' % (
  1017. os.path.basename(f_name),
  1018. subdir),
  1019. os.path.basename(f_name)
  1020. ]
  1021. if compressed:
  1022. tar_cmdline.insert(-1, "--use-compress-program=%s" % "gzip")
  1023. tar = subprocess.Popen(tar_cmdline, stdout=subprocess.PIPE)
  1024. if encrypted:
  1025. encryptor = subprocess.Popen(
  1026. ["openssl", "enc", "-e", "-aes-256-cbc", "-pass", "pass:qubes"],
  1027. stdin=tar.stdout,
  1028. stdout=subprocess.PIPE)
  1029. tar.stdout.close()
  1030. data = encryptor.stdout
  1031. else:
  1032. data = tar.stdout
  1033. stage1_dir = self.fullpath(os.path.join("stage1", subdir))
  1034. if not os.path.exists(stage1_dir):
  1035. os.makedirs(stage1_dir)
  1036. subprocess.check_call(["split", "--numeric-suffixes",
  1037. "--suffix-length=3",
  1038. "--bytes="+str(100*1024*1024), "-",
  1039. os.path.join(stage1_dir,
  1040. os.path.basename(f_name+"."))],
  1041. stdin=data)
  1042. data.close()
  1043. for part in sorted(os.listdir(stage1_dir)):
  1044. if not re.match(
  1045. r"^{}.[0-9][0-9][0-9]$".format(os.path.basename(f_name)),
  1046. part):
  1047. continue
  1048. part_with_dir = os.path.join(subdir, part)
  1049. self.calculate_hmac(os.path.join("stage1", part_with_dir))
  1050. self.append_backup_stream(part_with_dir, stream,
  1051. basedir=self.fullpath("stage1"))
  1052. self.append_backup_stream(part_with_dir+".hmac", stream,
  1053. basedir=self.fullpath("stage1"))
  1054. def handle_v4_file(self, f_name, subdir, stream, compressed=True):
  1055. # create inner archive
  1056. tar_cmdline = ["tar", "-Pc", '--sparse',
  1057. '-C', self.fullpath(os.path.dirname(f_name)),
  1058. '--xform', 's:^%s:%s\\0:' % (
  1059. os.path.basename(f_name),
  1060. subdir),
  1061. os.path.basename(f_name)
  1062. ]
  1063. if compressed:
  1064. tar_cmdline.insert(-1, "--use-compress-program=%s" % "gzip")
  1065. tar = subprocess.Popen(tar_cmdline, stdout=subprocess.PIPE)
  1066. data = tar.stdout
  1067. stage1_dir = self.fullpath(os.path.join("stage1", subdir))
  1068. if not os.path.exists(stage1_dir):
  1069. os.makedirs(stage1_dir)
  1070. subprocess.check_call(["split", "--numeric-suffixes",
  1071. "--suffix-length=3",
  1072. "--bytes="+str(100*1024*1024), "-",
  1073. os.path.join(stage1_dir,
  1074. os.path.basename(f_name+"."))],
  1075. stdin=data)
  1076. data.close()
  1077. for part in sorted(os.listdir(stage1_dir)):
  1078. if not re.match(
  1079. r"^{}.[0-9][0-9][0-9]$".format(os.path.basename(f_name)),
  1080. part):
  1081. continue
  1082. part_with_dir = os.path.join(subdir, part)
  1083. f_name = self.scrypt_encrypt(part_with_dir,
  1084. basedir=self.fullpath('stage1'))
  1085. self.append_backup_stream(f_name, stream,
  1086. basedir=self.fullpath("stage1"))
  1087. def create_v3_backup(self, encrypted=True, compressed=True):
  1088. """
  1089. Create "backup format 3" backup - used in R2 and R3.0
  1090. :param encrypted: Should the backup be encrypted
  1091. :param compressed: Should the backup be compressed
  1092. :return:
  1093. """
  1094. output = open(self.fullpath("backup.bin"), "w")
  1095. with open(self.fullpath("backup-header"), "w") as f:
  1096. f.write(BACKUP_HEADER_R2.format(
  1097. encrypted=str(encrypted),
  1098. compressed=str(compressed)
  1099. ))
  1100. self.calculate_hmac("backup-header")
  1101. self.append_backup_stream("backup-header", output)
  1102. self.append_backup_stream("backup-header.hmac", output)
  1103. with open(self.fullpath("qubes.xml"), "wb") as f:
  1104. qubesxml = pkg_resources.resource_string(__name__, 'v3-qubes.xml')
  1105. if encrypted:
  1106. for vmname, subdir in MANGLED_SUBDIRS_R2.items():
  1107. qubesxml = re.sub(r"[a-z-]*/{}".format(vmname).encode(),
  1108. subdir.encode(), qubesxml)
  1109. f.write(qubesxml)
  1110. else:
  1111. f.write(qubesxml)
  1112. self.handle_v3_file("qubes.xml", "", output, encrypted=encrypted,
  1113. compressed=compressed)
  1114. self.create_v1_files(r2b2=True)
  1115. for vm_type in ["appvms", "servicevms"]:
  1116. for vm_name in os.listdir(self.fullpath(vm_type)):
  1117. vm_dir = os.path.join(vm_type, vm_name)
  1118. for f_name in os.listdir(self.fullpath(vm_dir)):
  1119. if encrypted:
  1120. subdir = MANGLED_SUBDIRS_R2[vm_name]
  1121. else:
  1122. subdir = vm_dir
  1123. self.handle_v3_file(
  1124. os.path.join(vm_dir, f_name),
  1125. subdir+'/', output, encrypted=encrypted)
  1126. for vm_name in os.listdir(self.fullpath("vm-templates")):
  1127. vm_dir = os.path.join("vm-templates", vm_name)
  1128. if encrypted:
  1129. subdir = MANGLED_SUBDIRS_R2[vm_name]
  1130. else:
  1131. subdir = vm_dir
  1132. self.handle_v3_file(
  1133. os.path.join(vm_dir, "."),
  1134. subdir+'/', output, encrypted=encrypted)
  1135. output.close()
  1136. def create_v4_backup(self, compressed=True):
  1137. """
  1138. Create "backup format 4" backup - used in R4.0
  1139. :param compressed: Should the backup be compressed
  1140. :return:
  1141. """
  1142. output = open(self.fullpath("backup.bin"), "w")
  1143. with open(self.fullpath("backup-header"), "w") as f:
  1144. f.write(BACKUP_HEADER_R4.format(
  1145. compressed=str(compressed)
  1146. ))
  1147. self.scrypt_encrypt("backup-header", output_name='backup-header.hmac')
  1148. self.append_backup_stream("backup-header", output)
  1149. self.append_backup_stream("backup-header.hmac", output)
  1150. with open(self.fullpath("qubes.xml"), "wb") as f:
  1151. qubesxml = pkg_resources.resource_string(__name__, 'v4-qubes.xml')
  1152. for vmname, subdir in MANGLED_SUBDIRS_R4.items():
  1153. qubesxml = re.sub(
  1154. r'backup-path">[a-z-]*/{}'.format(vmname).encode(),
  1155. ('backup-path">' + subdir).encode(),
  1156. qubesxml)
  1157. f.write(qubesxml)
  1158. self.handle_v4_file("qubes.xml", "", output, compressed=compressed)
  1159. self.create_v4_files()
  1160. for vm_type in ["appvms", "vm-templates"]:
  1161. for vm_name in os.listdir(self.fullpath(vm_type)):
  1162. vm_dir = os.path.join(vm_type, vm_name)
  1163. for f_name in os.listdir(self.fullpath(vm_dir)):
  1164. subdir = MANGLED_SUBDIRS_R4[vm_name]
  1165. self.handle_v4_file(
  1166. os.path.join(vm_dir, f_name),
  1167. subdir+'/', output, compressed=compressed)
  1168. output.close()
  1169. def setup_expected_calls(self, parsed_qubes_xml, templates_map=None):
  1170. if templates_map is None:
  1171. templates_map = {}
  1172. extra_vm_list_lines = []
  1173. for name, vm in parsed_qubes_xml['domains'].items():
  1174. if not vm['included_in_backup']:
  1175. continue
  1176. if self.storage_pool:
  1177. self.app.expected_calls[
  1178. ('dom0', 'admin.vm.CreateInPool.' + vm['klass'],
  1179. templates_map.get(vm['template'], vm['template']),
  1180. 'name={} label={} pool={}'.format(
  1181. name, vm['label'], self.storage_pool).encode())] = \
  1182. b'0\0'
  1183. else:
  1184. self.app.expected_calls[
  1185. ('dom0', 'admin.vm.Create.' + vm['klass'],
  1186. templates_map.get(vm['template'], vm['template']),
  1187. 'name={} label={}'.format(name, vm['label']).encode())] =\
  1188. b'0\0'
  1189. extra_vm_list_lines.append(
  1190. '{} class={} state=Halted\n'.format(name, vm['klass']).encode())
  1191. if vm['backup_path']:
  1192. self.app.expected_calls[
  1193. (name, 'admin.vm.volume.List', None, None)] = \
  1194. b'0\0root\nprivate\nvolatile\n'
  1195. if vm['klass'] == 'AppVM':
  1196. self.app.expected_calls[
  1197. (name, 'admin.vm.volume.Info', 'root', None)] = \
  1198. b'0\0' \
  1199. b'pool=default\n' \
  1200. b'vid=' + name.encode() + b'/root\n' \
  1201. b'size=1024\n' \
  1202. b'usage=512\n' \
  1203. b'rw=False\n' \
  1204. b'snap_on_start=True\n' \
  1205. b'save_on_stop=False\n' \
  1206. b'source=\n' \
  1207. b'internal=True\n' \
  1208. b'revisions_to_keep=3\n'
  1209. else:
  1210. self.app.expected_calls[
  1211. (name, 'admin.vm.volume.Info', 'root', None)] = \
  1212. b'0\0' \
  1213. b'pool=default\n' \
  1214. b'vid=' + name.encode() + b'/root\n' \
  1215. b'size=1024\n' \
  1216. b'usage=512\n' \
  1217. b'rw=True\n' \
  1218. b'snap_on_start=False\n' \
  1219. b'save_on_stop=True\n' \
  1220. b'internal=True\n' \
  1221. b'revisions_to_keep=3\n'
  1222. self.app.expected_calls[
  1223. (name, 'admin.vm.volume.Resize', 'root',
  1224. str(vm.get('root_size', 2097664)).encode())] = \
  1225. b'0\0'
  1226. self.app.expected_calls[
  1227. (name, 'admin.vm.volume.Import', 'root',
  1228. name.encode() + b'/root')] = \
  1229. b'0\0'
  1230. self.app.expected_calls[
  1231. (name, 'admin.vm.volume.Info', 'private', None)] = \
  1232. b'0\0' \
  1233. b'pool=default\n' \
  1234. b'vid=' + name.encode() + b'/private\n' \
  1235. b'size=1024\n' \
  1236. b'usage=512\n' \
  1237. b'rw=True\n' \
  1238. b'snap_on_start=False\n' \
  1239. b'save_on_stop=True\n' \
  1240. b'revisions_to_keep=3\n'
  1241. self.app.expected_calls[
  1242. (name, 'admin.vm.volume.Resize', 'private', b'2097152')] = \
  1243. b'0\0'
  1244. self.app.expected_calls[
  1245. (name, 'admin.vm.volume.Import', 'private',
  1246. name.encode() + b'/private')] = \
  1247. b'0\0'
  1248. self.app.expected_calls[
  1249. (name, 'admin.vm.volume.Info', 'volatile', None)] = \
  1250. b'0\0' \
  1251. b'pool=default\n' \
  1252. b'vid=' + name.encode() + b'/root\n' \
  1253. b'size=1024\n' \
  1254. b'usage=512\n' \
  1255. b'rw=True\n' \
  1256. b'snap_on_start=False\n' \
  1257. b'save_on_stop=False\n' \
  1258. b'revisions_to_keep=3\n'
  1259. for prop, value in vm['properties'].items():
  1260. self.app.expected_calls[
  1261. (name, 'admin.vm.property.Set', prop,
  1262. str(value).encode() if value is not None else b'')] = b'0\0'
  1263. for bus, devices in vm['devices'].items():
  1264. for (backend_domain, ident), options in devices.items():
  1265. all_options = options.copy()
  1266. all_options['persistent'] = True
  1267. encoded_options = ' '.join('{}={}'.format(key, value) for
  1268. key, value in all_options.items()).encode()
  1269. self.app.expected_calls[
  1270. (name, 'admin.vm.device.{}.Attach'.format(bus),
  1271. '{}+{}'.format(backend_domain, ident),
  1272. encoded_options)] = b'0\0'
  1273. for feature, value in vm['features'].items():
  1274. if value is False:
  1275. value = ''
  1276. elif value is True:
  1277. value = '1'
  1278. self.app.expected_calls[
  1279. (name, 'admin.vm.feature.Set', feature,
  1280. str(value).encode())] = b'0\0'
  1281. for tag in vm['tags']:
  1282. self.app.expected_calls[
  1283. (name, 'admin.vm.tag.Set', tag, None)] = b'0\0'
  1284. if vm['backup_path']:
  1285. appmenus = (
  1286. b'gnome-terminal.desktop\n'
  1287. b'nautilus.desktop\n'
  1288. b'firefox.desktop\n'
  1289. b'mozilla-thunderbird.desktop\n'
  1290. b'libreoffice-startcenter.desktop\n'
  1291. )
  1292. self.app.expected_calls[
  1293. (name, 'appmenus', None, appmenus)] = b'0\0'
  1294. orig_admin_vm_list = self.app.expected_calls[
  1295. ('dom0', 'admin.vm.List', None, None)]
  1296. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  1297. [orig_admin_vm_list] + \
  1298. [orig_admin_vm_list + b''.join(extra_vm_list_lines)] * \
  1299. len(extra_vm_list_lines)
  1300. def mock_appmenus(self, queue, vm, stream):
  1301. queue.put((vm.name, 'appmenus', None, stream.read()))
  1302. def test_210_r2(self):
  1303. self.create_v3_backup(False)
  1304. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = (
  1305. b'0\0dom0 class=AdminVM state=Running\n'
  1306. b'fedora-25 class=TemplateVM state=Halted\n'
  1307. b'testvm class=AppVM state=Running\n'
  1308. )
  1309. self.app.expected_calls[
  1310. ('dom0', 'admin.property.Get', 'default_template', None)] = \
  1311. b'0\0default=no type=vm fedora-25'
  1312. self.setup_expected_calls(parsed_qubes_xml_r2, templates_map={
  1313. 'fedora-20-x64': 'fedora-25'
  1314. })
  1315. firewall_data = (
  1316. 'action=accept specialtarget=dns\n'
  1317. 'action=accept proto=icmp\n'
  1318. 'action=accept proto=tcp dstports=22-22\n'
  1319. 'action=accept proto=tcp dstports=9418-9418\n'
  1320. 'action=accept proto=tcp dst4=192.168.0.1/32 dstports=1234-1234\n'
  1321. 'action=accept proto=tcp dsthost=fedorahosted.org dstports=443-443\n'
  1322. 'action=accept proto=tcp dsthost=xenbits.xen.org dstports=80-80\n'
  1323. 'action=drop\n'
  1324. )
  1325. self.app.expected_calls[
  1326. ('test-work', 'admin.vm.firewall.Set', None,
  1327. firewall_data.encode())] = b'0\0'
  1328. self.app.expected_calls[
  1329. ('test-custom-template-appvm', 'admin.vm.firewall.Set', None,
  1330. firewall_data.encode())] = b'0\0'
  1331. qubesd_calls_queue = multiprocessing.Queue()
  1332. patches = [
  1333. mock.patch('qubesadmin.storage.Volume',
  1334. functools.partial(MockVolume, qubesd_calls_queue)),
  1335. mock.patch(
  1336. 'qubesadmin.backup.restore.BackupRestore._handle_appmenus_list',
  1337. functools.partial(self.mock_appmenus, qubesd_calls_queue)),
  1338. mock.patch(
  1339. 'qubesadmin.firewall.Firewall',
  1340. functools.partial(MockFirewall, qubesd_calls_queue)),
  1341. ]
  1342. for patch in patches:
  1343. patch.start()
  1344. try:
  1345. self.restore_backup(self.fullpath("backup.bin"), options={
  1346. 'use-default-template': True,
  1347. 'use-default-netvm': True,
  1348. })
  1349. finally:
  1350. for patch in patches:
  1351. patch.stop()
  1352. # retrieve calls from other multiprocess.Process instances
  1353. while not qubesd_calls_queue.empty():
  1354. call_args = qubesd_calls_queue.get()
  1355. self.app.qubesd_call(*call_args)
  1356. qubesd_calls_queue.close()
  1357. self.assertAllCalled()
  1358. def test_220_r2_encrypted(self):
  1359. self.create_v3_backup(True)
  1360. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = (
  1361. b'0\0dom0 class=AdminVM state=Running\n'
  1362. b'fedora-25 class=TemplateVM state=Halted\n'
  1363. b'testvm class=AppVM state=Running\n'
  1364. )
  1365. self.app.expected_calls[
  1366. ('dom0', 'admin.property.Get', 'default_template', None)] = \
  1367. b'0\0default=no type=vm fedora-25'
  1368. self.setup_expected_calls(parsed_qubes_xml_r2, templates_map={
  1369. 'fedora-20-x64': 'fedora-25'
  1370. })
  1371. firewall_data = (
  1372. 'action=accept specialtarget=dns\n'
  1373. 'action=accept proto=icmp\n'
  1374. 'action=accept proto=tcp dstports=22-22\n'
  1375. 'action=accept proto=tcp dstports=9418-9418\n'
  1376. 'action=accept proto=tcp dst4=192.168.0.1/32 dstports=1234-1234\n'
  1377. 'action=accept proto=tcp dsthost=fedorahosted.org dstports=443-443\n'
  1378. 'action=accept proto=tcp dsthost=xenbits.xen.org dstports=80-80\n'
  1379. 'action=drop\n'
  1380. )
  1381. self.app.expected_calls[
  1382. ('test-work', 'admin.vm.firewall.Set', None,
  1383. firewall_data.encode())] = b'0\0'
  1384. self.app.expected_calls[
  1385. ('test-custom-template-appvm', 'admin.vm.firewall.Set', None,
  1386. firewall_data.encode())] = b'0\0'
  1387. qubesd_calls_queue = multiprocessing.Queue()
  1388. patches = [
  1389. mock.patch('qubesadmin.storage.Volume',
  1390. functools.partial(MockVolume, qubesd_calls_queue)),
  1391. mock.patch(
  1392. 'qubesadmin.backup.restore.BackupRestore._handle_appmenus_list',
  1393. functools.partial(self.mock_appmenus, qubesd_calls_queue)),
  1394. mock.patch(
  1395. 'qubesadmin.firewall.Firewall',
  1396. functools.partial(MockFirewall, qubesd_calls_queue)),
  1397. ]
  1398. for patch in patches:
  1399. patch.start()
  1400. try:
  1401. self.restore_backup(self.fullpath("backup.bin"), options={
  1402. 'use-default-template': True,
  1403. 'use-default-netvm': True,
  1404. })
  1405. finally:
  1406. for patch in patches:
  1407. patch.stop()
  1408. # retrieve calls from other multiprocess.Process instances
  1409. while not qubesd_calls_queue.empty():
  1410. call_args = qubesd_calls_queue.get()
  1411. self.app.qubesd_call(*call_args)
  1412. qubesd_calls_queue.close()
  1413. self.assertAllCalled()
  1414. @unittest.skipUnless(spawn.find_executable('scrypt'),
  1415. "scrypt not installed")
  1416. def test_230_r4(self):
  1417. self.create_v4_backup(False)
  1418. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = (
  1419. b'0\0dom0 class=AdminVM state=Running\n'
  1420. b'fedora-25 class=TemplateVM state=Halted\n'
  1421. b'testvm class=AppVM state=Running\n'
  1422. b'sys-net class=AppVM state=Running\n'
  1423. )
  1424. self.app.expected_calls[
  1425. ('dom0', 'admin.property.Get', 'default_template', None)] = \
  1426. b'0\0default=no type=vm fedora-25'
  1427. self.app.expected_calls[
  1428. ('sys-net', 'admin.vm.property.Get', 'provides_network', None)] = \
  1429. b'0\0default=no type=bool True'
  1430. self.setup_expected_calls(parsed_qubes_xml_v4, templates_map={
  1431. 'debian-8': 'fedora-25'
  1432. })
  1433. firewall_data = (
  1434. 'action=accept specialtarget=dns\n'
  1435. 'action=accept proto=icmp\n'
  1436. 'action=accept proto=tcp dstports=22-22\n'
  1437. 'action=accept proto=tcp dsthost=www.qubes-os.org '
  1438. 'dstports=443-443\n'
  1439. 'action=accept proto=tcp dst4=192.168.0.0/24\n'
  1440. 'action=drop\n'
  1441. )
  1442. self.app.expected_calls[
  1443. ('test-work', 'admin.vm.firewall.Set', None,
  1444. firewall_data.encode())] = b'0\0'
  1445. qubesd_calls_queue = multiprocessing.Queue()
  1446. patches = [
  1447. mock.patch('qubesadmin.storage.Volume',
  1448. functools.partial(MockVolume, qubesd_calls_queue)),
  1449. mock.patch(
  1450. 'qubesadmin.backup.restore.BackupRestore._handle_appmenus_list',
  1451. functools.partial(self.mock_appmenus, qubesd_calls_queue)),
  1452. mock.patch(
  1453. 'qubesadmin.firewall.Firewall',
  1454. functools.partial(MockFirewall, qubesd_calls_queue)),
  1455. ]
  1456. for patch in patches:
  1457. patch.start()
  1458. try:
  1459. self.restore_backup(self.fullpath("backup.bin"), options={
  1460. 'use-default-template': True,
  1461. 'use-default-netvm': True,
  1462. })
  1463. finally:
  1464. for patch in patches:
  1465. patch.stop()
  1466. # retrieve calls from other multiprocess.Process instances
  1467. while not qubesd_calls_queue.empty():
  1468. call_args = qubesd_calls_queue.get()
  1469. self.app.qubesd_call(*call_args)
  1470. qubesd_calls_queue.close()
  1471. self.assertAllCalled()
  1472. @unittest.skipUnless(spawn.find_executable('scrypt'),
  1473. "scrypt not installed")
  1474. def test_230_r4_compressed(self):
  1475. self.create_v4_backup(True)
  1476. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = (
  1477. b'0\0dom0 class=AdminVM state=Running\n'
  1478. b'fedora-25 class=TemplateVM state=Halted\n'
  1479. b'testvm class=AppVM state=Running\n'
  1480. b'sys-net class=AppVM state=Running\n'
  1481. )
  1482. self.app.expected_calls[
  1483. ('dom0', 'admin.property.Get', 'default_template', None)] = \
  1484. b'0\0default=no type=vm fedora-25'
  1485. self.app.expected_calls[
  1486. ('sys-net', 'admin.vm.property.Get', 'provides_network', None)] = \
  1487. b'0\0default=no type=bool True'
  1488. self.setup_expected_calls(parsed_qubes_xml_v4, templates_map={
  1489. 'debian-8': 'fedora-25'
  1490. })
  1491. firewall_data = (
  1492. 'action=accept specialtarget=dns\n'
  1493. 'action=accept proto=icmp\n'
  1494. 'action=accept proto=tcp dstports=22-22\n'
  1495. 'action=accept proto=tcp dsthost=www.qubes-os.org '
  1496. 'dstports=443-443\n'
  1497. 'action=accept proto=tcp dst4=192.168.0.0/24\n'
  1498. 'action=drop\n'
  1499. )
  1500. self.app.expected_calls[
  1501. ('test-work', 'admin.vm.firewall.Set', None,
  1502. firewall_data.encode())] = b'0\0'
  1503. qubesd_calls_queue = multiprocessing.Queue()
  1504. patches = [
  1505. mock.patch('qubesadmin.storage.Volume',
  1506. functools.partial(MockVolume, qubesd_calls_queue)),
  1507. mock.patch(
  1508. 'qubesadmin.backup.restore.BackupRestore._handle_appmenus_list',
  1509. functools.partial(self.mock_appmenus, qubesd_calls_queue)),
  1510. mock.patch(
  1511. 'qubesadmin.firewall.Firewall',
  1512. functools.partial(MockFirewall, qubesd_calls_queue)),
  1513. ]
  1514. for patch in patches:
  1515. patch.start()
  1516. try:
  1517. self.restore_backup(self.fullpath("backup.bin"), options={
  1518. 'use-default-template': True,
  1519. 'use-default-netvm': True,
  1520. })
  1521. finally:
  1522. for patch in patches:
  1523. patch.stop()
  1524. # retrieve calls from other multiprocess.Process instances
  1525. while not qubesd_calls_queue.empty():
  1526. call_args = qubesd_calls_queue.get()
  1527. self.app.qubesd_call(*call_args)
  1528. qubesd_calls_queue.close()
  1529. self.assertAllCalled()
  1530. class TC_11_BackupCompatibilityIntoLVM(TC_10_BackupCompatibility):
  1531. storage_pool = 'some-pool'
  1532. def restore_backup(self, source=None, appvm=None, options=None,
  1533. expect_errors=None, manipulate_restore_info=None,
  1534. passphrase='qubes'):
  1535. if options is None:
  1536. options = {}
  1537. options['override_pool'] = self.storage_pool
  1538. super(TC_11_BackupCompatibilityIntoLVM, self).restore_backup(source,
  1539. appvm, options, expect_errors, manipulate_restore_info)