File: generator_utils_tests.py

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (130 lines) | stat: -rwxr-xr-x 4,985 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env vpython3
# Copyright 2020 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""
Unit tests for generator_utils.py
"""

import os
import unittest
import generator_utils

# Absolute path to chrome/src.
SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
SRC_DIR = os.path.abspath(os.path.join(SCRIPT_DIR, "../../.."))
TESTS_DIR = os.path.join(SCRIPT_DIR, "test_data")


class ParserTest(unittest.TestCase):
  TSV_CONTENTS = [[
      u"unique_id_A", u"", u"sender_A", u"description_A", u"trigger_A",
      u"data_A", u"destination_A", u"cookies_allowed_A", u"cookies_store_A",
      u"settings_A", u"chrome_policy_A", u"", u"source_file_A",
      u"id_hash_code_A", u"content_hash_code_A"],
    [
      u"unique_id_B", u"", u"sender_B", u"description_B", u"trigger_B",
      u"data_B", u"destination_B", u"cookies_allowed_B", u"cookies_store_B",
      u"settings_B", u"chrome_policy_B", u"", u"source_file_B",
      u"id_hash_code_B", u"content_hash_code_B"],
    [
      u"unique_id_C", u"", u"sender_C", u"description_C", u"trigger_C",
      u"data_C", u"destination_C", u"cookies_allowed_C", u"cookies_store_C",
      u"settings_C", u"chrome_policy_C", u"", u"source_file_C",
      u"id_hash_code_C", u"content_hash_code_C"]
  ]

  ANNOTATIONS_MAPPING = {
      "unique_id_A":
      generator_utils.TrafficAnnotation(
          **{
              "unique_id": "unique_id_A",
              "description": "description_A",
              "trigger": "trigger_A",
              "data": "data_A",
              "settings": "settings_A",
              "policy": "chrome_policy_A"
          }),
      "unique_id_B":
      generator_utils.TrafficAnnotation(
          **{
              "unique_id": "unique_id_B",
              "description": "description_B",
              "trigger": "trigger_B",
              "data": "data_B",
              "settings": "settings_B",
              "policy": "chrome_policy_B"
          }),
      "unique_id_C":
      generator_utils.TrafficAnnotation(
          **{
              "unique_id": "unique_id_C",
              "description": "description_C",
              "trigger": "trigger_C",
              "data": "data_C",
              "settings": "settings_C",
              "policy": "chrome_policy_C"
          })
  }

  PLACEHOLDERS = [
    {"type": generator_utils.Placeholder.GROUP, "name": "Group A"},
    {"type": generator_utils.Placeholder.SENDER, "name": "Sender 1"},
    {
      "type": generator_utils.Placeholder.ANNOTATION,
      "traffic_annotation": ANNOTATIONS_MAPPING["unique_id_A"]},
    {"type": generator_utils.Placeholder.SENDER, "name": "Sender 2"},
    {
      "type": generator_utils.Placeholder.ANNOTATION,
      "traffic_annotation": ANNOTATIONS_MAPPING["unique_id_B"]},
    {"type": generator_utils.Placeholder.GROUP, "name": "Group C"},
    {"type": generator_utils.Placeholder.SENDER, "name": "Sender 3"},
    {
      "type": generator_utils.Placeholder.ANNOTATION,
      "traffic_annotation": ANNOTATIONS_MAPPING["unique_id_C"]}
  ]

  # Document formatted according to fake_grouping.xml
  DOC_JSON = generator_utils.extract_body(target="all",
                                          json_file_path=os.path.join(
                                              TESTS_DIR, "fake_doc.json"))

  def test_load_tsv_file(self):
    self.assertEqual(self.TSV_CONTENTS, generator_utils.load_tsv_file(
      os.path.join(SRC_DIR,
      "tools/traffic_annotation/scripts/test_data/fake_annotations.tsv"),
      False))

  def test_map_annotations(self):
    self.assertEqual(self.ANNOTATIONS_MAPPING,
                     generator_utils.map_annotations(self.TSV_CONTENTS))

  def test_xml_parser_build_placeholders(self):
    xml_parser = generator_utils.XMLParser(
        os.path.join(TESTS_DIR, "fake_grouping.xml"), self.ANNOTATIONS_MAPPING)
    self.assertEqual(self.PLACEHOLDERS, xml_parser.build_placeholders())

  def test_find_first_index(self):
    first_index = generator_utils.find_first_index(self.DOC_JSON)
    self.assertEqual(1822, first_index)

  def test_find_last_index(self):
    last_index = generator_utils.find_last_index(self.DOC_JSON)
    self.assertEqual(2066, last_index)

  def test_find_chrome_browser_version(self):
    current_version = generator_utils.find_chrome_browser_version(self.DOC_JSON)
    self.assertEqual("86.0.4187.0", current_version)

  def test_find_bold_ranges(self):
    expected_bold_ranges = [(1843, 1855), (1859, 1867), (1871, 1876),
                            (1880, 1889), (1893, 1900), (1918, 1930),
                            (1934, 1942), (1968, 1975), (1946, 1951),
                            (1955, 1964), (2001, 2013), (2017, 2025),
                            (2029, 2034), (2038, 2047), (2051, 2058)]
    bold_ranges = generator_utils.find_bold_ranges(self.DOC_JSON)
    self.assertItemsEqual(expected_bold_ranges, bold_ranges)


if __name__ == "__main__":
  unittest.main()