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