OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/python2.4 |
| 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. |
| 5 |
| 6 |
| 7 """Unittests for grit.format.policy_templates.writers.admx_writer.""" |
| 8 |
| 9 |
| 10 import os |
| 11 import sys |
| 12 import unittest |
| 13 if __name__ == '__main__': |
| 14 sys.path.append(os.path.join(os.path.dirname(sys.argv[0]), '../../../..')) |
| 15 |
| 16 |
| 17 from grit.format.policy_templates.writers import admx_writer |
| 18 from grit.format.policy_templates.writers import xml_writer_base_unittest |
| 19 from xml.dom import minidom |
| 20 |
| 21 |
| 22 class AdmxWriterTest(xml_writer_base_unittest.XmlWriterBaseTest): |
| 23 |
| 24 def _CreateDocumentElement(self): |
| 25 dom_impl = minidom.getDOMImplementation('') |
| 26 doc = dom_impl.createDocument(None, 'root', None) |
| 27 return doc.documentElement |
| 28 |
| 29 def setUp(self): |
| 30 # Writer configuration. This dictionary contains parameter used by the ADMX |
| 31 # Writer |
| 32 config = { |
| 33 'win_group_policy_class': 'TestClass', |
| 34 'win_supported_os': 'SUPPORTED_TESTOS', |
| 35 'win_reg_key_name': 'Software\\Policies\\Test', |
| 36 'win_category_path': ['test_category'], |
| 37 'admx_namespace': 'ADMXWriter.Test.Namespace', |
| 38 'admx_prefix': 'test_prefix' |
| 39 } |
| 40 self.writer = admx_writer.GetWriter(config) |
| 41 self.writer.Init() |
| 42 |
| 43 def _GetPoliciesElement(self, doc): |
| 44 node_list = doc.getElementsByTagName('policies') |
| 45 self.assertTrue(node_list.length == 1) |
| 46 return node_list.item(0) |
| 47 |
| 48 def _GetCategoriesElement(self, doc): |
| 49 node_list = doc.getElementsByTagName('categories') |
| 50 self.assertTrue(node_list.length == 1) |
| 51 return node_list.item(0) |
| 52 |
| 53 def testEmpty(self): |
| 54 self.writer.BeginTemplate() |
| 55 self.writer.EndTemplate() |
| 56 |
| 57 output = self.writer.GetTemplateText() |
| 58 expected_output = ( |
| 59 '<?xml version="1.0" ?>\n' |
| 60 '<policyDefinitions revision="1.0" schemaVersion="1.0">\n' |
| 61 ' <policyNamespaces>\n' |
| 62 ' <target namespace="ADMXWriter.Test.Namespace"' |
| 63 ' prefix="test_prefix"/>\n' |
| 64 ' <using namespace="Microsoft.Policies.Windows" prefix="windows"/>\n' |
| 65 ' </policyNamespaces>\n' |
| 66 ' <resources minRequiredRevision="1.0"/>\n' |
| 67 ' <supportedOn>\n' |
| 68 ' <definitions>\n' |
| 69 ' <definition displayName="' |
| 70 '$(string.SUPPORTED_TESTOS)" name="SUPPORTED_TESTOS"/>\n' |
| 71 ' </definitions>\n' |
| 72 ' </supportedOn>\n' |
| 73 ' <categories>\n' |
| 74 ' <category displayName="$(string.test_category)"' |
| 75 ' name="test_category"/>\n' |
| 76 ' </categories>\n' |
| 77 ' <policies/>\n' |
| 78 '</policyDefinitions>') |
| 79 self.AssertXMLEquals(output, expected_output) |
| 80 |
| 81 def testEmptyPolicyGroup(self): |
| 82 empty_policy_group = { |
| 83 'name': 'PolicyGroup', |
| 84 'policies': [] |
| 85 } |
| 86 # Initialize writer to write a policy group. |
| 87 self.writer.BeginTemplate() |
| 88 # Write policy group |
| 89 self.writer.BeginPolicyGroup(empty_policy_group) |
| 90 self.writer.EndPolicyGroup() |
| 91 |
| 92 output = self.GetXMLOfChildren(self._GetPoliciesElement(self.writer._doc)) |
| 93 expected_output = '' |
| 94 self.AssertXMLEquals(output, expected_output) |
| 95 |
| 96 output = self.GetXMLOfChildren( |
| 97 self._GetCategoriesElement(self.writer._doc)) |
| 98 expected_output = ( |
| 99 '<category displayName="$(string.test_category)"' |
| 100 ' name="test_category"/>\n' |
| 101 '<category displayName="$(string.PolicyGroup_group)"' |
| 102 ' name="PolicyGroup">\n' |
| 103 ' <parentCategory ref="test_category"/>\n' |
| 104 '</category>') |
| 105 |
| 106 self.AssertXMLEquals(output, expected_output) |
| 107 |
| 108 def testPolicyGroup(self): |
| 109 empty_policy_group = { |
| 110 'name': 'PolicyGroup', |
| 111 'policies': [ |
| 112 {'name': 'PolicyStub2', |
| 113 'type': 'main'}, |
| 114 {'name': 'PolicyStub1', |
| 115 'type': 'main'}, |
| 116 ] |
| 117 } |
| 118 # Initialize writer to write a policy group. |
| 119 self.writer.BeginTemplate() |
| 120 # Write policy group |
| 121 self.writer.BeginPolicyGroup(empty_policy_group) |
| 122 self.writer.EndPolicyGroup() |
| 123 |
| 124 output = self.GetXMLOfChildren(self._GetPoliciesElement(self.writer._doc)) |
| 125 expected_output = '' |
| 126 self.AssertXMLEquals(output, expected_output) |
| 127 |
| 128 output = self.GetXMLOfChildren( |
| 129 self._GetCategoriesElement(self.writer._doc)) |
| 130 expected_output = ( |
| 131 '<category displayName="$(string.test_category)"' |
| 132 ' name="test_category"/>\n' |
| 133 '<category displayName="$(string.PolicyGroup_group)"' |
| 134 ' name="PolicyGroup">\n' |
| 135 ' <parentCategory ref="test_category"/>\n' |
| 136 '</category>') |
| 137 self.AssertXMLEquals(output, expected_output) |
| 138 |
| 139 |
| 140 def _initWriterForPolicy(self, writer, policy): |
| 141 '''Initializes the writer to write the given policy next. |
| 142 ''' |
| 143 policy_group = { |
| 144 'name': 'PolicyGroup', |
| 145 'policies': [policy] |
| 146 } |
| 147 writer.BeginTemplate() |
| 148 writer.BeginPolicyGroup(policy_group) |
| 149 |
| 150 def testMainPolicy(self): |
| 151 main_policy = { |
| 152 'name': 'DummyMainPolicy', |
| 153 'type': 'main', |
| 154 } |
| 155 |
| 156 self._initWriterForPolicy(self.writer, main_policy) |
| 157 |
| 158 self.writer.WritePolicy(main_policy) |
| 159 |
| 160 output = self.GetXMLOfChildren(self._GetPoliciesElement(self.writer._doc)) |
| 161 expected_output = ( |
| 162 '<policy class="TestClass" displayName="$(string.DummyMainPolicy)"' |
| 163 ' explainText="$(string.DummyMainPolicy_Explain)"' |
| 164 ' key="Software\\Policies\\Test" name="DummyMainPolicy"' |
| 165 ' presentation="$(presentation.DummyMainPolicy)"' |
| 166 ' valueName="DummyMainPolicy">\n' |
| 167 ' <parentCategory ref="PolicyGroup"/>\n' |
| 168 ' <supportedOn ref="SUPPORTED_TESTOS"/>\n' |
| 169 ' <enabledValue>\n' |
| 170 ' <decimal value="1"/>\n' |
| 171 ' </enabledValue>\n' |
| 172 ' <disabledValue>\n' |
| 173 ' <decimal value="0"/>\n' |
| 174 ' </disabledValue>\n' |
| 175 '</policy>') |
| 176 |
| 177 self.AssertXMLEquals(output, expected_output) |
| 178 |
| 179 def testStringPolicy(self): |
| 180 string_policy = { |
| 181 'name': 'SampleStringPolicy', |
| 182 'type': 'string', |
| 183 } |
| 184 self._initWriterForPolicy(self.writer, string_policy) |
| 185 |
| 186 self.writer.WritePolicy(string_policy) |
| 187 output = self.GetXMLOfChildren(self._GetPoliciesElement(self.writer._doc)) |
| 188 expected_output = ( |
| 189 '<policy class="TestClass" displayName="$(string.SampleStringPolicy)"' |
| 190 ' explainText="$(string.SampleStringPolicy_Explain)"' |
| 191 ' key="Software\\Policies\\Test" name="SampleStringPolicy"' |
| 192 ' presentation="$(presentation.SampleStringPolicy)">\n' |
| 193 ' <parentCategory ref="PolicyGroup"/>\n' |
| 194 ' <supportedOn ref="SUPPORTED_TESTOS"/>\n' |
| 195 ' <elements>\n' |
| 196 ' <text id="SampleStringPolicy" valueName="SampleStringPolicy"/>\n' |
| 197 ' </elements>\n' |
| 198 '</policy>') |
| 199 self.AssertXMLEquals(output, expected_output) |
| 200 |
| 201 def testIntPolicy(self): |
| 202 int_policy = { |
| 203 'name': 'SampleIntPolicy', |
| 204 'type': 'int', |
| 205 } |
| 206 self._initWriterForPolicy(self.writer, int_policy) |
| 207 |
| 208 self.writer.WritePolicy(int_policy) |
| 209 output = self.GetXMLOfChildren(self._GetPoliciesElement(self.writer._doc)) |
| 210 expected_output = ( |
| 211 '<policy class="TestClass" displayName="$(string.SampleIntPolicy)"' |
| 212 ' explainText="$(string.SampleIntPolicy_Explain)"' |
| 213 ' key="Software\\Policies\\Test" name="SampleIntPolicy"' |
| 214 ' presentation="$(presentation.SampleIntPolicy)">\n' |
| 215 ' <parentCategory ref="PolicyGroup"/>\n' |
| 216 ' <supportedOn ref="SUPPORTED_TESTOS"/>\n' |
| 217 ' <elements>\n' |
| 218 ' <decimal id="SampleIntPolicy" valueName="SampleIntPolicy"/>\n' |
| 219 ' </elements>\n' |
| 220 '</policy>') |
| 221 self.AssertXMLEquals(output, expected_output) |
| 222 |
| 223 def testIntEnumPolicy(self): |
| 224 enum_policy = { |
| 225 'name': 'SampleEnumPolicy', |
| 226 'type': 'int-enum', |
| 227 'items': [ |
| 228 {'name': 'item_1', 'value': 0}, |
| 229 {'name': 'item_2', 'value': 1}, |
| 230 ] |
| 231 } |
| 232 |
| 233 self._initWriterForPolicy(self.writer, enum_policy) |
| 234 self.writer.WritePolicy(enum_policy) |
| 235 output = self.GetXMLOfChildren(self._GetPoliciesElement(self.writer._doc)) |
| 236 expected_output = ( |
| 237 '<policy class="TestClass" displayName="$(string.SampleEnumPolicy)"' |
| 238 ' explainText="$(string.SampleEnumPolicy_Explain)"' |
| 239 ' key="Software\\Policies\\Test" name="SampleEnumPolicy"' |
| 240 ' presentation="$(presentation.SampleEnumPolicy)">\n' |
| 241 ' <parentCategory ref="PolicyGroup"/>\n' |
| 242 ' <supportedOn ref="SUPPORTED_TESTOS"/>\n' |
| 243 ' <elements>\n' |
| 244 ' <enum id="SampleEnumPolicy" valueName="SampleEnumPolicy">\n' |
| 245 ' <item displayName="$(string.item_1)">\n' |
| 246 ' <value>\n' |
| 247 ' <decimal value="0"/>\n' |
| 248 ' </value>\n' |
| 249 ' </item>\n' |
| 250 ' <item displayName="$(string.item_2)">\n' |
| 251 ' <value>\n' |
| 252 ' <decimal value="1"/>\n' |
| 253 ' </value>\n' |
| 254 ' </item>\n' |
| 255 ' </enum>\n' |
| 256 ' </elements>\n' |
| 257 '</policy>') |
| 258 self.AssertXMLEquals(output, expected_output) |
| 259 |
| 260 def testStringEnumPolicy(self): |
| 261 enum_policy = { |
| 262 'name': 'SampleEnumPolicy', |
| 263 'type': 'string-enum', |
| 264 'items': [ |
| 265 {'name': 'item_1', 'value': 'one'}, |
| 266 {'name': 'item_2', 'value': 'two'}, |
| 267 ] |
| 268 } |
| 269 |
| 270 # This test is different than the others because it also tests that space |
| 271 # usage inside <string> nodes is correct. |
| 272 dom_impl = minidom.getDOMImplementation('') |
| 273 self.writer._doc = dom_impl.createDocument(None, 'policyDefinitions', None) |
| 274 self.writer._active_policies_elem = self.writer._doc.documentElement |
| 275 self.writer._active_policy_group_name = 'PolicyGroup' |
| 276 self.writer.WritePolicy(enum_policy) |
| 277 output = self.writer.GetTemplateText() |
| 278 expected_output = ( |
| 279 '<?xml version="1.0" ?>\n' |
| 280 '<policyDefinitions>\n' |
| 281 ' <policy class="TestClass" displayName="$(string.SampleEnumPolicy)"' |
| 282 ' explainText="$(string.SampleEnumPolicy_Explain)"' |
| 283 ' key="Software\\Policies\\Test" name="SampleEnumPolicy"' |
| 284 ' presentation="$(presentation.SampleEnumPolicy)">\n' |
| 285 ' <parentCategory ref="PolicyGroup"/>\n' |
| 286 ' <supportedOn ref="SUPPORTED_TESTOS"/>\n' |
| 287 ' <elements>\n' |
| 288 ' <enum id="SampleEnumPolicy" valueName="SampleEnumPolicy">\n' |
| 289 ' <item displayName="$(string.item_1)">\n' |
| 290 ' <value>\n' |
| 291 ' <string>one</string>\n' |
| 292 ' </value>\n' |
| 293 ' </item>\n' |
| 294 ' <item displayName="$(string.item_2)">\n' |
| 295 ' <value>\n' |
| 296 ' <string>two</string>\n' |
| 297 ' </value>\n' |
| 298 ' </item>\n' |
| 299 ' </enum>\n' |
| 300 ' </elements>\n' |
| 301 ' </policy>\n' |
| 302 '</policyDefinitions>') |
| 303 self.AssertXMLEquals(output, expected_output) |
| 304 |
| 305 def testListPolicy(self): |
| 306 list_policy = { |
| 307 'name': 'SampleListPolicy', |
| 308 'type': 'list', |
| 309 } |
| 310 self._initWriterForPolicy(self.writer, list_policy) |
| 311 self.writer.WritePolicy(list_policy) |
| 312 output = self.GetXMLOfChildren(self._GetPoliciesElement(self.writer._doc)) |
| 313 expected_output = ( |
| 314 '<policy class="TestClass" displayName="$(string.SampleListPolicy)"' |
| 315 ' explainText="$(string.SampleListPolicy_Explain)"' |
| 316 ' key="Software\\Policies\\Test" name="SampleListPolicy"' |
| 317 ' presentation="$(presentation.SampleListPolicy)">\n' |
| 318 ' <parentCategory ref="PolicyGroup"/>\n' |
| 319 ' <supportedOn ref="SUPPORTED_TESTOS"/>\n' |
| 320 ' <elements>\n' |
| 321 ' <list id="SampleListPolicyDesc"' |
| 322 ' key="Software\Policies\Test\SampleListPolicy" valuePrefix=""/>\n' |
| 323 ' </elements>\n' |
| 324 '</policy>') |
| 325 |
| 326 self.AssertXMLEquals(output, expected_output) |
| 327 |
| 328 def testPlatform(self): |
| 329 # Test that the writer correctly chooses policies of platform Windows. |
| 330 self.assertTrue(self.writer.IsPolicySupported({ |
| 331 'supported_on': [ |
| 332 {'platforms': ['win', 'zzz']}, {'platforms': ['aaa']} |
| 333 ] |
| 334 })) |
| 335 self.assertFalse(self.writer.IsPolicySupported({ |
| 336 'supported_on': [ |
| 337 {'platforms': ['mac', 'linux']}, {'platforms': ['aaa']} |
| 338 ] |
| 339 })) |
| 340 |
| 341 |
| 342 if __name__ == '__main__': |
| 343 unittest.main() |
OLD | NEW |