| OLD | NEW |
| (Empty) |
| 1 #!/usr/bin/env python | |
| 2 # Copyright (c) 2012 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 '''Unit tests for grit.format.policy_templates.writers.reg_writer''' | |
| 8 | |
| 9 | |
| 10 import os | |
| 11 import sys | |
| 12 if __name__ == '__main__': | |
| 13 sys.path.append(os.path.join(os.path.dirname(__file__), '../../../..')) | |
| 14 | |
| 15 import unittest | |
| 16 | |
| 17 from grit.format.policy_templates.writers import writer_unittest_common | |
| 18 | |
| 19 | |
| 20 class RegWriterUnittest(writer_unittest_common.WriterUnittestCommon): | |
| 21 '''Unit tests for RegWriter.''' | |
| 22 | |
| 23 NEWLINE = '\r\n' | |
| 24 | |
| 25 def CompareOutputs(self, output, expected_output): | |
| 26 '''Compares the output of the reg_writer with its expected output. | |
| 27 | |
| 28 Args: | |
| 29 output: The output of the reg writer as returned by grit. | |
| 30 expected_output: The expected output. | |
| 31 | |
| 32 Raises: | |
| 33 AssertionError: if the two strings are not equivalent. | |
| 34 ''' | |
| 35 self.assertEquals( | |
| 36 output.strip(), | |
| 37 expected_output.strip()) | |
| 38 | |
| 39 def testEmpty(self): | |
| 40 # Test the handling of an empty policy list. | |
| 41 grd = self.PrepareTest( | |
| 42 '{' | |
| 43 ' "policy_definitions": [],' | |
| 44 ' "placeholders": [],' | |
| 45 ' "messages": {}' | |
| 46 '}') | |
| 47 output = self.GetOutput(grd, 'fr', {'_chromium': '1', }, 'reg', 'en') | |
| 48 expected_output = 'Windows Registry Editor Version 5.00' | |
| 49 self.CompareOutputs(output, expected_output) | |
| 50 | |
| 51 def testEmptyVersion(self): | |
| 52 # Test the handling of an empty policy list. | |
| 53 grd = self.PrepareTest( | |
| 54 '{' | |
| 55 ' "policy_definitions": [],' | |
| 56 ' "placeholders": [],' | |
| 57 ' "messages": {}' | |
| 58 '}') | |
| 59 output = self.GetOutput( | |
| 60 grd, 'fr', {'_chromium': '1', 'version': '39.0.0.0' }, 'reg', 'en') | |
| 61 expected_output = ('Windows Registry Editor Version 5.00\r\n' | |
| 62 '; chromium version: 39.0.0.0\r\n') | |
| 63 self.CompareOutputs(output, expected_output) | |
| 64 | |
| 65 def testMainPolicy(self): | |
| 66 # Tests a policy group with a single policy of type 'main'. | |
| 67 grd = self.PrepareTest( | |
| 68 '{' | |
| 69 ' "policy_definitions": [' | |
| 70 ' {' | |
| 71 ' "name": "MainPolicy",' | |
| 72 ' "type": "main",' | |
| 73 ' "features": { "can_be_recommended": True },' | |
| 74 ' "caption": "",' | |
| 75 ' "desc": "",' | |
| 76 ' "supported_on": ["chrome.win:8-"],' | |
| 77 ' "example_value": True' | |
| 78 ' },' | |
| 79 ' ],' | |
| 80 ' "placeholders": [],' | |
| 81 ' "messages": {},' | |
| 82 '}') | |
| 83 output = self.GetOutput(grd, 'fr', {'_google_chrome' : '1'}, 'reg', 'en') | |
| 84 expected_output = self.NEWLINE.join([ | |
| 85 'Windows Registry Editor Version 5.00', | |
| 86 '', | |
| 87 '[HKEY_LOCAL_MACHINE\\Software\\Policies\\Google\\Chrome]', | |
| 88 '"MainPolicy"=dword:00000001', | |
| 89 '', | |
| 90 '[HKEY_LOCAL_MACHINE\\Software\\Policies\\Google\\Chrome\\Recommended]', | |
| 91 '"MainPolicy"=dword:00000001']) | |
| 92 self.CompareOutputs(output, expected_output) | |
| 93 | |
| 94 def testRecommendedMainPolicy(self): | |
| 95 # Tests a policy group with a single policy of type 'main'. | |
| 96 grd = self.PrepareTest( | |
| 97 '{' | |
| 98 ' "policy_definitions": [' | |
| 99 ' {' | |
| 100 ' "name": "MainPolicy",' | |
| 101 ' "type": "main",' | |
| 102 ' "features": {' | |
| 103 ' "can_be_recommended": True,' | |
| 104 ' "can_be_mandatory": False ' | |
| 105 ' },' | |
| 106 ' "caption": "",' | |
| 107 ' "desc": "",' | |
| 108 ' "supported_on": ["chrome.win:8-"],' | |
| 109 ' "example_value": True' | |
| 110 ' },' | |
| 111 ' ],' | |
| 112 ' "placeholders": [],' | |
| 113 ' "messages": {},' | |
| 114 '}') | |
| 115 output = self.GetOutput(grd, 'fr', {'_google_chrome' : '1'}, 'reg', 'en') | |
| 116 expected_output = self.NEWLINE.join([ | |
| 117 'Windows Registry Editor Version 5.00', | |
| 118 '', | |
| 119 '[HKEY_LOCAL_MACHINE\\Software\\Policies\\Google\\Chrome\\Recommended]', | |
| 120 '"MainPolicy"=dword:00000001']) | |
| 121 self.CompareOutputs(output, expected_output) | |
| 122 | |
| 123 def testStringPolicy(self): | |
| 124 # Tests a policy group with a single policy of type 'string'. | |
| 125 grd = self.PrepareTest( | |
| 126 '{' | |
| 127 ' "policy_definitions": [' | |
| 128 ' {' | |
| 129 ' "name": "StringPolicy",' | |
| 130 ' "type": "string",' | |
| 131 ' "caption": "",' | |
| 132 ' "desc": "",' | |
| 133 ' "supported_on": ["chrome.win:8-"],' | |
| 134 ' "example_value": "hello, world! \\\" \\\\"' | |
| 135 ' },' | |
| 136 ' ],' | |
| 137 ' "placeholders": [],' | |
| 138 ' "messages": {},' | |
| 139 '}') | |
| 140 output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'reg', 'en') | |
| 141 expected_output = self.NEWLINE.join([ | |
| 142 'Windows Registry Editor Version 5.00', | |
| 143 '', | |
| 144 '[HKEY_LOCAL_MACHINE\\Software\\Policies\\Chromium]', | |
| 145 '"StringPolicy"="hello, world! \\\" \\\\"']) | |
| 146 self.CompareOutputs(output, expected_output) | |
| 147 | |
| 148 def testIntPolicy(self): | |
| 149 # Tests a policy group with a single policy of type 'int'. | |
| 150 grd = self.PrepareTest( | |
| 151 '{' | |
| 152 ' "policy_definitions": [' | |
| 153 ' {' | |
| 154 ' "name": "IntPolicy",' | |
| 155 ' "type": "int",' | |
| 156 ' "caption": "",' | |
| 157 ' "desc": "",' | |
| 158 ' "supported_on": ["chrome.win:8-"],' | |
| 159 ' "example_value": 26' | |
| 160 ' },' | |
| 161 ' ],' | |
| 162 ' "placeholders": [],' | |
| 163 ' "messages": {},' | |
| 164 '}') | |
| 165 output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'reg', 'en') | |
| 166 expected_output = self.NEWLINE.join([ | |
| 167 'Windows Registry Editor Version 5.00', | |
| 168 '', | |
| 169 '[HKEY_LOCAL_MACHINE\\Software\\Policies\\Chromium]', | |
| 170 '"IntPolicy"=dword:0000001a']) | |
| 171 self.CompareOutputs(output, expected_output) | |
| 172 | |
| 173 def testIntEnumPolicy(self): | |
| 174 # Tests a policy group with a single policy of type 'int-enum'. | |
| 175 grd = self.PrepareTest( | |
| 176 '{' | |
| 177 ' "policy_definitions": [' | |
| 178 ' {' | |
| 179 ' "name": "EnumPolicy",' | |
| 180 ' "type": "int-enum",' | |
| 181 ' "caption": "",' | |
| 182 ' "desc": "",' | |
| 183 ' "items": [' | |
| 184 ' {"name": "ProxyServerDisabled", "value": 0, "caption": ""},' | |
| 185 ' {"name": "ProxyServerAutoDetect", "value": 1, "caption": ""},' | |
| 186 ' ],' | |
| 187 ' "supported_on": ["chrome.win:8-"],' | |
| 188 ' "example_value": 1' | |
| 189 ' },' | |
| 190 ' ],' | |
| 191 ' "placeholders": [],' | |
| 192 ' "messages": {},' | |
| 193 '}') | |
| 194 output = self.GetOutput(grd, 'fr', {'_google_chrome': '1'}, 'reg', 'en') | |
| 195 expected_output = self.NEWLINE.join([ | |
| 196 'Windows Registry Editor Version 5.00', | |
| 197 '', | |
| 198 '[HKEY_LOCAL_MACHINE\\Software\\Policies\\Google\\Chrome]', | |
| 199 '"EnumPolicy"=dword:00000001']) | |
| 200 self.CompareOutputs(output, expected_output) | |
| 201 | |
| 202 def testStringEnumPolicy(self): | |
| 203 # Tests a policy group with a single policy of type 'string-enum'. | |
| 204 grd = self.PrepareTest( | |
| 205 '{' | |
| 206 ' "policy_definitions": [' | |
| 207 ' {' | |
| 208 ' "name": "EnumPolicy",' | |
| 209 ' "type": "string-enum",' | |
| 210 ' "caption": "",' | |
| 211 ' "desc": "",' | |
| 212 ' "items": [' | |
| 213 ' {"name": "ProxyServerDisabled", "value": "one",' | |
| 214 ' "caption": ""},' | |
| 215 ' {"name": "ProxyServerAutoDetect", "value": "two",' | |
| 216 ' "caption": ""},' | |
| 217 ' ],' | |
| 218 ' "supported_on": ["chrome.win:8-"],' | |
| 219 ' "example_value": "two"' | |
| 220 ' },' | |
| 221 ' ],' | |
| 222 ' "placeholders": [],' | |
| 223 ' "messages": {},' | |
| 224 '}') | |
| 225 output = self.GetOutput(grd, 'fr', {'_google_chrome': '1'}, 'reg', 'en') | |
| 226 expected_output = self.NEWLINE.join([ | |
| 227 'Windows Registry Editor Version 5.00', | |
| 228 '', | |
| 229 '[HKEY_LOCAL_MACHINE\\Software\\Policies\\Google\\Chrome]', | |
| 230 '"EnumPolicy"="two"']) | |
| 231 self.CompareOutputs(output, expected_output) | |
| 232 | |
| 233 def testListPolicy(self): | |
| 234 # Tests a policy group with a single policy of type 'list'. | |
| 235 grd = self.PrepareTest( | |
| 236 '{' | |
| 237 ' "policy_definitions": [' | |
| 238 ' {' | |
| 239 ' "name": "ListPolicy",' | |
| 240 ' "type": "list",' | |
| 241 ' "caption": "",' | |
| 242 ' "desc": "",' | |
| 243 ' "supported_on": ["chrome.linux:8-"],' | |
| 244 ' "example_value": ["foo", "bar"]' | |
| 245 ' },' | |
| 246 ' ],' | |
| 247 ' "placeholders": [],' | |
| 248 ' "messages": {},' | |
| 249 '}') | |
| 250 output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'reg', 'en') | |
| 251 expected_output = self.NEWLINE.join([ | |
| 252 'Windows Registry Editor Version 5.00', | |
| 253 '', | |
| 254 '[HKEY_LOCAL_MACHINE\\Software\\Policies\\Chromium\\ListPolicy]', | |
| 255 '"1"="foo"', | |
| 256 '"2"="bar"']) | |
| 257 | |
| 258 def testStringEnumListPolicy(self): | |
| 259 # Tests a policy group with a single policy of type 'string-enum-list'. | |
| 260 grd = self.PrepareTest( | |
| 261 '{' | |
| 262 ' "policy_definitions": [' | |
| 263 ' {' | |
| 264 ' "name": "ListPolicy",' | |
| 265 ' "type": "string-enum-list",' | |
| 266 ' "caption": "",' | |
| 267 ' "desc": "",' | |
| 268 ' "items": [' | |
| 269 ' {"name": "ProxyServerDisabled", "value": "foo",' | |
| 270 ' "caption": ""},' | |
| 271 ' {"name": "ProxyServerAutoDetect", "value": "bar",' | |
| 272 ' "caption": ""},' | |
| 273 ' ],' | |
| 274 ' "supported_on": ["chrome.linux:8-"],' | |
| 275 ' "example_value": ["foo", "bar"]' | |
| 276 ' },' | |
| 277 ' ],' | |
| 278 ' "placeholders": [],' | |
| 279 ' "messages": {},' | |
| 280 '}') | |
| 281 output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'reg', 'en') | |
| 282 expected_output = self.NEWLINE.join([ | |
| 283 'Windows Registry Editor Version 5.00', | |
| 284 '', | |
| 285 '[HKEY_LOCAL_MACHINE\\Software\\Policies\\Chromium\\ListPolicy]', | |
| 286 '"1"="foo"', | |
| 287 '"2"="bar"']) | |
| 288 | |
| 289 def testDictionaryPolicy(self): | |
| 290 # Tests a policy group with a single policy of type 'dict'. | |
| 291 example = { | |
| 292 'bool': True, | |
| 293 'dict': { | |
| 294 'a': 1, | |
| 295 'b': 2, | |
| 296 }, | |
| 297 'int': 10, | |
| 298 'list': [1, 2, 3], | |
| 299 'string': 'abc', | |
| 300 } | |
| 301 grd = self.PrepareTest( | |
| 302 '{' | |
| 303 ' "policy_definitions": [' | |
| 304 ' {' | |
| 305 ' "name": "DictionaryPolicy",' | |
| 306 ' "type": "dict",' | |
| 307 ' "caption": "",' | |
| 308 ' "desc": "",' | |
| 309 ' "supported_on": ["chrome.win:8-"],' | |
| 310 ' "example_value": ' + str(example) + | |
| 311 ' },' | |
| 312 ' ],' | |
| 313 ' "placeholders": [],' | |
| 314 ' "messages": {},' | |
| 315 '}') | |
| 316 output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'reg', 'en') | |
| 317 expected_output = self.NEWLINE.join([ | |
| 318 'Windows Registry Editor Version 5.00', | |
| 319 '', | |
| 320 '[HKEY_LOCAL_MACHINE\\Software\\Policies\\Chromium]', | |
| 321 '"DictionaryPolicy"="{"bool": true, "dict": {"a": 1, ' | |
| 322 '"b": 2}, "int": 10, "list": [1, 2, 3], "string": "abc"}"']) | |
| 323 self.CompareOutputs(output, expected_output) | |
| 324 | |
| 325 def testNonSupportedPolicy(self): | |
| 326 # Tests a policy that is not supported on Windows, so it shouldn't | |
| 327 # be included in the .REG file. | |
| 328 grd = self.PrepareTest( | |
| 329 '{' | |
| 330 ' "policy_definitions": [' | |
| 331 ' {' | |
| 332 ' "name": "NonWindowsPolicy",' | |
| 333 ' "type": "list",' | |
| 334 ' "caption": "",' | |
| 335 ' "desc": "",' | |
| 336 ' "supported_on": ["chrome.mac:8-"],' | |
| 337 ' "example_value": ["a"]' | |
| 338 ' },' | |
| 339 ' ],' | |
| 340 ' "placeholders": [],' | |
| 341 ' "messages": {},' | |
| 342 '}') | |
| 343 output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'reg', 'en') | |
| 344 expected_output = self.NEWLINE.join([ | |
| 345 'Windows Registry Editor Version 5.00']) | |
| 346 self.CompareOutputs(output, expected_output) | |
| 347 | |
| 348 def testPolicyGroup(self): | |
| 349 # Tests a policy group that has more than one policies. | |
| 350 grd = self.PrepareTest( | |
| 351 '{' | |
| 352 ' "policy_definitions": [' | |
| 353 ' {' | |
| 354 ' "name": "Group1",' | |
| 355 ' "type": "group",' | |
| 356 ' "caption": "",' | |
| 357 ' "desc": "",' | |
| 358 ' "policies": [{' | |
| 359 ' "name": "Policy1",' | |
| 360 ' "type": "list",' | |
| 361 ' "caption": "",' | |
| 362 ' "desc": "",' | |
| 363 ' "supported_on": ["chrome.win:8-"],' | |
| 364 ' "example_value": ["a", "b"]' | |
| 365 ' },{' | |
| 366 ' "name": "Policy2",' | |
| 367 ' "type": "string",' | |
| 368 ' "caption": "",' | |
| 369 ' "desc": "",' | |
| 370 ' "supported_on": ["chrome.win:8-"],' | |
| 371 ' "example_value": "c"' | |
| 372 ' }],' | |
| 373 ' },' | |
| 374 ' ],' | |
| 375 ' "placeholders": [],' | |
| 376 ' "messages": {},' | |
| 377 '}') | |
| 378 output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'reg', 'en') | |
| 379 expected_output = self.NEWLINE.join([ | |
| 380 'Windows Registry Editor Version 5.00', | |
| 381 '', | |
| 382 '[HKEY_LOCAL_MACHINE\\Software\\Policies\\Chromium]', | |
| 383 '"Policy2"="c"', | |
| 384 '', | |
| 385 '[HKEY_LOCAL_MACHINE\\Software\\Policies\\Chromium\\Policy1]', | |
| 386 '"1"="a"', | |
| 387 '"2"="b"']) | |
| 388 self.CompareOutputs(output, expected_output) | |
| 389 | |
| 390 | |
| 391 if __name__ == '__main__': | |
| 392 unittest.main() | |
| OLD | NEW |