1#!/usr/bin/python2 2# Copyright 2020 The Chromium OS 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 6import mock 7import unittest 8 9import common 10from autotest_lib.server.cros.servo.topology import topology_constants as stc 11from autotest_lib.server.cros.servo.topology import servo_topology 12 13# pylint: disable=missing-docstring 14 15 16class ConvertDataToStringTests(unittest.TestCase): 17 def test_expect_empty_when_data_is_none(self): 18 data = servo_topology._convert_topology_to_string(None) 19 self.assertEqual(data, '') 20 21 def test_expect_empty_when_data_is_empty(self): 22 data = servo_topology._convert_topology_to_string({}) 23 self.assertEqual(data, '') 24 25 def test_expect_string_when_data_is_provided(self): 26 expected_string = ( 27 'eyJtYWluIjp7InVzYl9odWJfcG9ydCI6IjYuNC4xIiwic2VyaWFsIjoiQzE' 28 '5MDMxNDU1OTEhQCMkJV4qKClcIic6OycnXFw/L2BcIiwiLCJ0eXBlIjoic2' 29 'Vydm9fdjQiLCJzeXNmc19wcm9kdWN0IjoiU2Vydm8gVjQifSwiY2hpbGRyZ' 30 'W4iOlt7InVzYl9odWJfcG9ydCI6IjYuNC4yIiwic2VyaWFsIjoiMDY4MUQw' 31 'M0EtOTJEQ0NENjQiLCJ0eXBlIjoiY2NkX2NyNTAiLCJzeXNmc19wcm9kdWN' 32 '0IjoiQ3I1MCJ9XX0=') 33 data = { 34 "main": { 35 "usb_hub_port": "6.4.1", 36 "serial": "C1903145591!@#$%^*()\"':;''\\?/`\",", 37 "type": "servo_v4", 38 "sysfs_product": "Servo V4" 39 }, 40 "children": [{ 41 "usb_hub_port": "6.4.2", 42 "serial": "0681D03A-92DCCD64", 43 "type": "ccd_cr50", 44 "sysfs_product": "Cr50" 45 }] 46 } 47 48 result = servo_topology._convert_topology_to_string(data) 49 self.assertEqual(result, expected_string) 50 51 def test_expect_string_when_data_is_provided2(self): 52 expected_string = ( 53 'eyJtYWluIjp7InVzYl9odWJfcG9ydCI6IiIsInNlcmlhbCI6IiIsInR5cGU' 54 'iOiIiLCJzeXNmc19wcm9kdWN0IjoiIn0sImNoaWxkcmVuIjpbXX0=') 55 data = { 56 "main": { 57 "usb_hub_port": "", 58 "serial": "", 59 "type": "", 60 "sysfs_product": "" 61 }, 62 "children": [] 63 } 64 65 result = servo_topology._convert_topology_to_string(data) 66 self.assertEqual(result, expected_string) 67 68 69class ParseStringToDataTests(unittest.TestCase): 70 def test_expect_none_when_string_empty(self): 71 data = servo_topology._parse_string_as_topology("") 72 self.assertEqual(data, None) 73 74 def test_expect_empty_data_when_string_empty_instance(self): 75 data = servo_topology._parse_string_as_topology('{}') 76 self.assertEqual(data, None) 77 78 def test_expect_data_when_string_is_provided(self): 79 b64_data = ( 80 'eyJtYWluIjp7InVzYl9odWJfcG9ydCI6IjYuNC4xIiwic2VyaWFsIjoiQzE5' 81 'MDMxNDU1OTEhQCMkJV4mKigpXCInOjsnJ1xcPy9gflwiLCIsInR5cGUiOiJz' 82 'ZXJ2b192NCIsInN5c2ZzX3Byb2R1Y3QiOiJTZXJ2byBWNCJ9LCJjaGlsZHJl' 83 'biI6W3sidXNiX2h1Yl9wb3J0IjoiNi40LjIiLCJzZXJpYWwiOiIwNjgxRDAz' 84 'QS05MkRDQ0Q2NCIsInR5cGUiOiJjY2RfY3I1MCIsInN5c2ZzX3Byb2R1Y3Qi' 85 'OiJDcjUwIn1dfQ==') 86 data = { 87 "main": { 88 "usb_hub_port": "6.4.1", 89 "serial": "C1903145591!@#$%^&*()\"':;''\\?/`~\",", 90 "type": "servo_v4", 91 "sysfs_product": "Servo V4" 92 }, 93 "children": [{ 94 "usb_hub_port": "6.4.2", 95 "serial": "0681D03A-92DCCD64", 96 "type": "ccd_cr50", 97 "sysfs_product": "Cr50" 98 }] 99 } 100 101 result = servo_topology._parse_string_as_topology(b64_data) 102 self.assertEqual(result, data) 103 104 def test_expect_data_when_string_is_provided2(self): 105 b64_data = ( 106 'eyJtYWluIjp7InVzYl9odWJfcG9ydCI6IiIsInNlcmlhbCI6IiIsInR5cGU' 107 'iOiIiLCJzeXNmc19wcm9kdWN0IjoiIn0sImNoaWxkcmVuIjpbXX0=') 108 data = { 109 "main": { 110 "usb_hub_port": "", 111 "serial": "", 112 "type": "", 113 "sysfs_product": "" 114 }, 115 "children": [] 116 } 117 118 result = servo_topology._parse_string_as_topology(b64_data) 119 self.assertEqual(result, data) 120 121 122if __name__ == '__main__': 123 unittest.main() 124