Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/tests/test_meid.doctest
blob: 1cb551a23bb7cd7796363c5f779016993271e87f (plain)
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
test_meid.doctest - more detailed doctests for stdnum.meid module

Copyright (C) 2010, 2011, 2013 Arthur de Jong

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA


This file contains more detailed doctests for the stdnum.meid module. It
tries to test more corner cases and detailed functionality that is not
really useful as module documentation.

>>> from stdnum import meid


IMEI numbers without the software version (but optionally with a check
digit) should be valid numbers:

>>> meid.validate('49-015420-323751')
'49015420323751'
>>> meid.validate('35-209900-176148-1')
'35209900176148'
>>> meid.validate('35-209900-176148-2')
Traceback (most recent call last):
    ...
InvalidChecksum: ...


MEIDs can be represented as HEX strings (with and without check digit):

>>> meid.validate('AF 01 23 45 0A BC DE')
'AF0123450ABCDE'
>>> meid.validate('AF 01 23 45 0A BC DE C')
'AF0123450ABCDE'
>>> meid.validate('AF 01 23 45 0A BC DE D')
Traceback (most recent call last):
    ...
InvalidChecksum: ...


Also, MEIDs can be represented in decimal format (with and without check digit):

>>> meid.validate('29360 87365 0070 3710')
'AF0123450ABCDE'
>>> meid.validate('29360 87365 0070 3710 0')
'AF0123450ABCDE'
>>> meid.validate('29360 87365 0070 3710 1')
Traceback (most recent call last):
    ...
InvalidChecksum: ...


The validate() method should be fairly robust against invalid junk passed:

>>> meid.validate('29360 ABCDE 0070 3710')
Traceback (most recent call last):
    ...
InvalidFormat: ...
>>> meid.validate('GF 01 23 45 0A BC DE')
Traceback (most recent call last):
    ...
InvalidFormat: ...

Decimal format MEIDs with manufacturer code exceeding 32-bits should fail.

>>> meid.validate('99999 99999 0070 3710 4')
Traceback (most recent call last):
    ...
InvalidComponent: ...

Decimal format MEIDs with serial number exceeding 24-bits should fail.

>>> meid.validate('29360 87365 9999 9999 4')
Traceback (most recent call last):
    ...
InvalidComponent: ...


The compact method should convert to HEX if needed and can optionally leave
the check digit intact.

>>> meid.compact('49-015420-323751')
'49015420323751'
>>> meid.compact('35-209900-176148-2')
'35209900176148'
>>> meid.compact('35-209900-176148-2', strip_check_digit=False)
'352099001761482'
>>> meid.compact('af 01 23 45 0a bc de')
'AF0123450ABCDE'
>>> meid.compact('AF 01 23 45 0A BC DE C')
'AF0123450ABCDE'
>>> meid.compact('AF 01 23 45 0A BC DE C', strip_check_digit=False)
'AF0123450ABCDEC'
>>> meid.compact('29360 87365 0070 3710')
'AF0123450ABCDE'
>>> meid.compact('29360 87365 0070 3710 0')
'AF0123450ABCDE'
>>> meid.compact('29360 87365 0070 3710 0', strip_check_digit=False)
'AF0123450ABCDEC'


The format() function can add the check digit if needed. It should leave
alone existing check digits (even invalid ones).

>>> meid.format('35-209900-176148-2')
'35 20 99 00 17 61 48 2'
>>> meid.format('35-209900-176148')
'35 20 99 00 17 61 48'
>>> meid.format('35-209900-176148', add_check_digit=True)
'35 20 99 00 17 61 48 1'
>>> meid.format('af0123450abcDE')
'AF 01 23 45 0A BC DE'
>>> meid.format('af0123450abcDEC', add_check_digit=True)
'AF 01 23 45 0A BC DE C'


The format() function can also convert to decimal, recalculating the check
digit if needed (conversion will silently correct incorrect check digits):

>>> meid.format('35-209900-176148', format='dec')
'08913 28768 0153 2232'
>>> meid.format('35-209900-176148', format='dec', add_check_digit=True)
'08913 28768 0153 2232 3'
>>> meid.format('35-209900-176148-9', format='dec')
'08913 28768 0153 2232 3'
>>> meid.format('af0123450abcDE', format='dec')
'29360 87365 0070 3710'
>>> meid.format('af0123450abcDE', format='dec', add_check_digit=True)
'29360 87365 0070 3710 0'
>>> meid.format('af0123450abcDEC', format='dec')
'29360 87365 0070 3710 0'
>>> meid.format('293608736500703710', format='dec')
'29360 87365 0070 3710'
>>> meid.format('293608736500703710', format='dec', add_check_digit=True)
'29360 87365 0070 3710 0'
>>> meid.format('2936087365007037106', format='dec')
'29360 87365 0070 3710 6'


The format() function can also convert to hex, recalculating the check
digit if needed (conversion will silently correct incorrect check digits):

>>> meid.format('35-209900-176148', format='hex')
'35 20 99 00 17 61 48'
>>> meid.format('35-209900-176148', format='hex', add_check_digit=True)
'35 20 99 00 17 61 48 1'
>>> meid.format('35-209900-176148-9', format='hex')
'35 20 99 00 17 61 48 9'
>>> meid.format('af0123450abcDE', format='hex')
'AF 01 23 45 0A BC DE'
>>> meid.format('af0123450abcDE', format='hex', add_check_digit=True)
'AF 01 23 45 0A BC DE C'
>>> meid.format('af0123450abcDEF', format='hex')
'AF 01 23 45 0A BC DE F'
>>> meid.format('293608736500703710', format='hex')
'AF 01 23 45 0A BC DE'
>>> meid.format('293608736500703710', format='hex', add_check_digit=True)
'AF 01 23 45 0A BC DE C'
>>> meid.format('2936087365007037106', format='hex')
'AF 01 23 45 0A BC DE C'

The conversion function should work regardless of the check digit and
whether decimal or hex representation is used.

>>> meid.to_pseudo_esn('AF 01 23 45 0A BC DE C')
'8016B128'
>>> meid.to_pseudo_esn('29360 87365 0070 3710')
'8016B128'