This file is indexed.

/usr/share/pyshared/gcmclient/test.py is in python-gcm-client 0.1.4-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
if __name__ == '__main__':
    import os.path, sys
    sys.path.append(os.path.dirname(os.path.dirname(__file__)))

import unittest
import json
import pickle
from gcmclient.gcm import *


class GCMClientTest(unittest.TestCase):
    """ API tests. """

    def setUp(self):
        self.gcm = GCM('my_api_key')

    def test_plain_text(self):
        msg = PlainTextMessage('target_device',
                {
                    'str': 'string',
                    'int': 90,
                    'bool': True,
                }, collapse_key='collapse.key',
                   time_to_live=90,
                   delay_while_idle=True,
                   dry_run=True)

        headers = {}
        data = msg._prepare(headers)

        # will be URL encoded by requests
        ex_data = {'collapse_key': 'collapse.key',
                    'time_to_live': 90,
                    'delay_while_idle': True,
                    'dry_run': True,
                    'registration_id': 'target_device',
                    'data.bool': True,
                    'data.int': 90,
                    'data.str': 'string',
                   }

        ex_headers = {'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'}

        self.assertEqual(data, ex_data)
        self.assertEqual(headers, ex_headers)

        # responses
        rsp = msg._parse_response("id=1:2342\nregistration_id=32\n")
        ex_rsp = {
            'canonicals': {'target_device': '32'},
            'failed': {},
            'not_registered': [],
            'success': {'target_device': '1:2342'},
        }
        self.assertEqual(rsp, ex_rsp)

        rsp = msg._parse_response("Error=InvalidRegistration")
        ex_rsp = {
            'canonicals': {},
            'failed': {'target_device': 'InvalidRegistration'},
            'not_registered': [],
            'success': {}
        }
        self.assertEqual(rsp, ex_rsp)

        # retry (must be ['target_device'], but we test here the difference)
        msg2 = msg._retry(['target_device_retry'])
        self.assertEqual(msg2.registration_id, 'target_device_retry')
        self.assertEqual(msg2.options, msg.options)
        self.assertEqual(msg2.data, msg.data)

        # pickle
        pmsg = pickle.dumps(msg)
        pmsg = pickle.loads(pmsg)
        self.assertEqual(pmsg.registration_id, msg.registration_id)
        self.assertEqual(pmsg.options, msg.options)
        self.assertEqual(pmsg.data, msg.data)

        pstate = msg.__getstate__()
        pmsg = PlainTextMessage(**pstate)
        self.assertEqual(pmsg.registration_id, msg.registration_id)
        self.assertEqual(pmsg.options, msg.options)
        self.assertEqual(pmsg.data, msg.data)

    def test_json_message(self):
        msg = JSONMessage(['A', 'B', 'C', 'D', 'E'],
                {
                    'str': 'string',
                    'int': 90,
                    'bool': True,
                }, collapse_key='collapse.key',
                   time_to_live=90,
                   delay_while_idle=True,
                   dry_run=True)

        headers = {}
        data = msg._prepare(headers)

        # will be URL encoded by requests
        ex_data = json.dumps({'collapse_key': 'collapse.key',
                    'time_to_live': 90,
                    'delay_while_idle': True,
                    'dry_run': True,
                    'registration_ids': ['A', 'B', 'C', 'D', 'E'],
                    'data': {
                        'str': 'string',
                        'int': 90,
                        'bool': True,
                    }
                   })

        ex_headers = {'Content-Type': 'application/json'}

        self.assertEqual(data, ex_data)
        self.assertEqual(headers, ex_headers)

        # responses
        response = json.dumps({ "multicast_id": 1,
          "success": 2,
          "failure": 3,
          "canonical_ids": 1,
          "results": [
            { "message_id": "1:0408" },
            { "error": "Unavailable" },
            { "error": "InvalidRegistration" },
            { "message_id": "1:2342", "registration_id": "32" },
            { "error": "NotRegistered"}
          ]
        })

        rsp = msg._parse_response(response)
        ex_rsp = {
            'multicast_id': 1,
            'canonicals': {'D': u'32'},
            'failed': {'C': u'InvalidRegistration'},
            'not_registered': ['E'],
            'success': {'A': u'1:0408', 'D': u'1:2342'},
            'unavailable': ['B']
        }
        self.assertEqual(rsp, ex_rsp)

        # retry (must be ['target_device'], but we test here the difference)
        msg2 = msg._retry(['B'])
        self.assertEqual(msg2.registration_ids, ['B'])
        self.assertEqual(msg2.options, msg.options)
        self.assertEqual(msg2.data, msg.data)

        # pickle
        pmsg = pickle.dumps(msg)
        pmsg = pickle.loads(pmsg)
        self.assertEqual(pmsg.registration_ids, msg.registration_ids)
        self.assertEqual(pmsg.options, msg.options)
        self.assertEqual(pmsg.data, msg.data)

        pstate = msg.__getstate__()
        pmsg = JSONMessage(**pstate)
        self.assertEqual(pmsg.registration_ids, msg.registration_ids)
        self.assertEqual(pmsg.options, msg.options)
        self.assertEqual(pmsg.data, msg.data)

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