~ubuntu-branches/ubuntu/precise/enigmail/precise-security

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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
from __future__ import with_statement
import unittest

from StringIO import StringIO
import os
import sys
import os.path
from mozunit import main, MockedOpen

from Preprocessor import Preprocessor

def NamedIO(name, content):
  with open(name, 'w') as f:
    f.write(content)
  return name

class TestPreprocessor(unittest.TestCase):
  """
  Unit tests for the Context class
  """

  def setUp(self):
    self.pp = Preprocessor()
    self.pp.out = StringIO()

  def test_conditional_if_0(self):
    f = NamedIO("conditional_if_0.in", """#if 0
FAIL
#else
PASS
#endif
""")
    self.pp.do_include(f)
    self.assertEqual(self.pp.out.getvalue(), "PASS\n")

  def test_no_marker(self):
    no_marker = """#if 0
PASS
#endif
"""
    f = NamedIO("no_marker.in", no_marker)
    self.pp.setMarker(None)
    self.pp.do_include(f)
    self.assertEqual(self.pp.out.getvalue(), no_marker)

  def test_string_value(self):
    f = NamedIO("string_value.in", """#define FOO STRING
#if FOO
string value is true
#else
string value is false
#endif
""")
    self.pp.do_include(f)
    self.assertEqual(self.pp.out.getvalue(), "string value is false\n")
  
  def test_number_value(self):
    f = NamedIO("string_value.in", """#define FOO 1
#if FOO
number value is true
#else
number value is false
#endif
""")
    self.pp.do_include(f)
    self.assertEqual(self.pp.out.getvalue(), "number value is true\n")
  
  def test_conditional_if_0_elif_1(self):
    f = NamedIO('conditional_if_0_elif_1.in', '''#if 0
#elif 1
PASS
#else
FAIL
#endif
''')
    self.pp.do_include(f)
    self.assertEqual(self.pp.out.getvalue(), "PASS\n")
  
  def test_conditional_if_1(self):
    f = NamedIO('conditional_if_1.in', '''#if 1
PASS
#else
FAILE
#endif
''')
    self.pp.do_include(f)
    self.assertEqual(self.pp.out.getvalue(), "PASS\n")
  
  def test_conditional_if_1_elif_1_else(self):
    f = NamedIO('conditional_if_1_elif_1_else.in', '''#if 1
PASS
#elif 1
FAIL
#else
FAIL
#endif
''')
    self.pp.do_include(f)
    self.assertEqual(self.pp.out.getvalue(), "PASS\n")
  
  def test_conditional_if_1_if_1(self):
    f = NamedIO('conditional_if_1_if_1.in', '''#if 1
#if 1
PASS
#else
FAIL
#endif
#else
FAIL
#endif
''')
    self.pp.do_include(f)
    self.assertEqual(self.pp.out.getvalue(), "PASS\n")
  
  def test_conditional_not_0(self):
    f = NamedIO('conditional_not_0.in', '''#if !0
PASS
#else
FAIL
#endif
''')
    self.pp.do_include(f)
    self.assertEqual(self.pp.out.getvalue(), "PASS\n")
  
  def test_conditional_not_1(self):
    f = NamedIO('conditional_not_1.in', '''#if !1
FAIL
#else
PASS
#endif
''')
    self.pp.do_include(f)
    self.assertEqual(self.pp.out.getvalue(), "PASS\n")
  
  def test_conditional_not_emptyval(self):
    f = NamedIO('conditional_not_emptyval.in', '''#define EMPTYVAL
#if !EMPTYVAL
FAIL
#else
PASS
#endif
#if EMPTYVAL
PASS
#else
FAIL
#endif
''')
    self.pp.do_include(f)
    self.assertEqual(self.pp.out.getvalue(), "PASS\nPASS\n")
  
  def test_conditional_not_nullval(self):
    f = NamedIO('conditional_not_nullval.in', '''#define NULLVAL 0
#if !NULLVAL
PASS
#else
FAIL
#endif
''')
    self.pp.do_include(f)
    self.assertEqual(self.pp.out.getvalue(), "PASS\n")
  
  def test_expand(self):
    f = NamedIO('expand.in', '''#define ASVAR AS
#expand P__ASVAR__S
''')
    self.pp.do_include(f)
    self.assertEqual(self.pp.out.getvalue(), "PASS\n")

  def test_undef_defined(self):
    f = NamedIO('undef_defined.in', '''#define BAR
#undef BAR
BAR
''')
    self.pp.do_include(f)
    self.assertEqual(self.pp.out.getvalue(), "BAR\n")

  def test_undef_undefined(self):
    f = NamedIO('undef_undefined.in', '''#undef VAR
''')
    self.pp.do_include(f)
    self.assertEqual(self.pp.out.getvalue(), "")
  
  def test_filter_attemptSubstitution(self):
    f = NamedIO('filter_attemptSubstitution.in', '''#filter attemptSubstitution
@PASS@
#unfilter attemptSubstitution
''')
    self.pp.do_include(f)
    self.assertEqual(self.pp.out.getvalue(), "@PASS@\n")
  
  def test_filter_emptyLines(self):
    f = NamedIO('filter_emptyLines.in', '''lines with a

blank line
#filter emptyLines
lines with

no blank lines
#unfilter emptyLines
yet more lines with

blank lines
''')
    self.pp.do_include(f)
    self.assertEqual(self.pp.out.getvalue(), '''lines with a

blank line
lines with
no blank lines
yet more lines with

blank lines
''')
  
  def test_filter_slashslash(self):
    f = NamedIO('filter_slashslash.in', '''#filter slashslash
PASS//FAIL  // FAIL
#unfilter slashslash
PASS // PASS
''')
    self.pp.do_include(f)
    self.assertEqual(self.pp.out.getvalue(), "PASS\nPASS // PASS\n")
  
  def test_filter_spaces(self):
    f = NamedIO('filter_spaces.in', '''#filter spaces
You should see two nice ascii tables
 +-+-+-+
 | |   |     |
 +-+-+-+
#unfilter spaces
+-+---+
| |   |
+-+---+ 
''')
    self.pp.do_include(f)
    self.assertEqual(self.pp.out.getvalue(), """You should see two nice ascii tables
+-+-+-+
| | | |
+-+-+-+
+-+---+
| |   |
+-+---+ 
""")
  
  def test_filter_substitution(self):
    f = NamedIO('filter_substitution.in', '''#define VAR ASS
#filter substitution
P@VAR@
#unfilter substitution
''')
    self.pp.do_include(f)
    self.assertEqual(self.pp.out.getvalue(), "PASS\n")

  def test_error(self):
    f = NamedIO('error.in', '''#error spit this message out
''')
    caught_msg = None
    try:
      self.pp.do_include(f)
    except Preprocessor.Error, e:
      caught_msg = e.args[0][-1]
    self.assertEqual(caught_msg, 'spit this message out')
  
  def test_javascript_line(self):
    f = NamedIO('javascript_line.js.in', '''// Line 1
#if 0
// line 3
#endif
// line 5
# comment
// line 7
// line 8
// line 9
# another comment
// line 11
#define LINE 1
// line 13, given line number overwritten with 2
''')
    self.pp.do_include(f)
    out = """// Line 1
//@line 5 "CWDjavascript_line.js.in"
// line 5
//@line 7 "CWDjavascript_line.js.in"
// line 7
// line 8
// line 9
//@line 11 "CWDjavascript_line.js.in"
// line 11
//@line 2 "CWDjavascript_line.js.in"
// line 13, given line number overwritten with 2
"""
    out = out.replace('CWD', os.getcwd() + os.path.sep)
    self.assertEqual(self.pp.out.getvalue(), out)
  
  def test_literal(self):
    f = NamedIO('literal.in', '''#literal PASS
''')
    self.pp.do_include(f)
    self.assertEqual(self.pp.out.getvalue(), "PASS\n")
  
  def test_var_directory(self):
    f = NamedIO('var_directory.in', '''#ifdef DIRECTORY
PASS
#else
FAIL
#endif
''')
    self.pp.do_include(f)
    self.assertEqual(self.pp.out.getvalue(), "PASS\n")
  
  def test_var_file(self):
    f = NamedIO('var_file.in', '''#ifdef FILE
PASS
#else
FAIL
#endif
''')
    self.pp.do_include(f)
    self.assertEqual(self.pp.out.getvalue(), "PASS\n")
  
  def test_var_if_0(self):
    f = NamedIO('var_if_0.in', '''#define VAR 0
#if VAR
FAIL
#else
PASS
#endif
''')
    self.pp.do_include(f)
    self.assertEqual(self.pp.out.getvalue(), "PASS\n")
  
  def test_var_if_0_elifdef(self):
    f = NamedIO('var_if_0_elifdef.in', '''#if 0
#elifdef FILE
PASS
#else
FAIL
#endif
''')
    self.pp.do_include(f)
    self.assertEqual(self.pp.out.getvalue(), "PASS\n")
  
  def test_var_if_0_elifndef(self):
    f = NamedIO('var_if_0_elifndef.in', '''#if 0
#elifndef VAR
PASS
#else
FAIL
#endif
''')
    self.pp.do_include(f)
    self.assertEqual(self.pp.out.getvalue(), "PASS\n")
  
  def test_var_ifdef_0(self):
    f = NamedIO('var_ifdef_0.in', '''#define VAR 0
#ifdef VAR
PASS
#else
FAIL
#endif
''')
    self.pp.do_include(f)
    self.assertEqual(self.pp.out.getvalue(), "PASS\n")
  
  def test_var_ifdef_undef(self):
    f = NamedIO('var_ifdef_undef.in', '''#define VAR 0
#undef VAR
#ifdef VAR
FAIL
#else
PASS
#endif
''')
    self.pp.do_include(f)
    self.assertEqual(self.pp.out.getvalue(), "PASS\n")
  
  def test_var_ifndef_0(self):
    f = NamedIO('var_ifndef_0.in', '''#define VAR 0
#ifndef VAR
FAIL
#else
PASS
#endif
''')
    self.pp.do_include(f)
    self.assertEqual(self.pp.out.getvalue(), "PASS\n")
  
  def test_var_ifndef_undef(self):
    f = NamedIO('var_ifndef_undef.in', '''#define VAR 0
#undef VAR
#ifndef VAR
PASS
#else
FAIL
#endif
''')
    self.pp.do_include(f)
    self.assertEqual(self.pp.out.getvalue(), "PASS\n")
  
  def test_var_line(self):
    f = NamedIO('var_line.in', '''#ifdef LINE
PASS
#else
FAIL
#endif
''')
    self.pp.do_include(f)
    self.assertEqual(self.pp.out.getvalue(), "PASS\n")

  def test_lineEndings(self):
    f = NamedIO('lineEndings.in', '''first
#literal second
''')
    self.pp.setLineEndings('cr')
    self.pp.do_include(f)
    self.assertEqual(self.pp.out.getvalue(), "first\rsecond\r")

  def test_filterDefine(self):
    f = NamedIO('filterDefine.in', '''#filter substitution
#define VAR AS
#define VAR2 P@VAR@
@VAR2@S
''')
    self.pp.do_include(f)
    self.assertEqual(self.pp.out.getvalue(), "PASS\n")

  def test_number_value_equals(self):
    f = NamedIO("number_value_equals.in", """#define FOO 1000
#if FOO == 1000
number value is equal
#else
number value is not equal
#endif
""")
    self.pp.do_include(f)
    self.assertEqual(self.pp.out.getvalue(), "number value is equal\n")

  def test_number_value_equals_defines(self):
    f = NamedIO("number_value_equals_defines.in", """#if FOO == 1000
number value is equal
#else
number value is not equal
#endif
""")
    self.pp.handleCommandLine(["-DFOO=1000"])
    self.pp.do_include(f)
    self.assertEqual(self.pp.out.getvalue(), "number value is equal\n")

  def test_octal_value_equals(self):
    f = NamedIO("octal_value_equals.in", """#define FOO 0100
#if FOO == 0100
octal value is equal
#else
octal value is not equal
#endif
""")
    self.pp.do_include(f)
    self.assertEqual(self.pp.out.getvalue(), "octal value is equal\n")

  def test_octal_value_equals_defines(self):
    f = NamedIO("octal_value_equals_defines.in", """#if FOO == 0100
octal value is equal
#else
octal value is not equal
#endif
""")
    self.pp.handleCommandLine(["-DFOO=0100"])
    self.pp.do_include(f)
    self.assertEqual(self.pp.out.getvalue(), "octal value is equal\n")

  def test_value_quoted_expansion(self):
    """
    Quoted values on the commandline don't currently have quotes stripped.
    Pike says this is for compat reasons.
    """
    f = NamedIO("value_quoted_expansion.in", """#filter substitution
@FOO@
""")
    self.pp.handleCommandLine(['-DFOO="ABCD"'])
    self.pp.do_include(f)
    self.assertEqual(self.pp.out.getvalue(), '"ABCD"\n')

  def test_octal_value_quoted_expansion(self):
    f = NamedIO("value_quoted_expansion.in", """#filter substitution
@FOO@
""")
    self.pp.handleCommandLine(['-DFOO="0100"'])
    self.pp.do_include(f)
    self.assertEqual(self.pp.out.getvalue(), '"0100"\n')

  def test_number_value_not_equals_quoted_defines(self):
    f = NamedIO("number_value_not_equals_quoted_defines.in", """#if FOO == 1000
number value is equal
#else
number value is not equal
#endif
""")
    self.pp.handleCommandLine(['-DFOO="1000"'])
    self.pp.do_include(f)
    self.assertEqual(self.pp.out.getvalue(), "number value is not equal\n")

  def test_octal_value_not_equals_quoted_defines(self):
    f = NamedIO("octal_value_not_equals_quoted_defines.in", """#if FOO == 0100
octal value is equal
#else
octal value is not equal
#endif
""")
    self.pp.handleCommandLine(['-DFOO="0100"'])
    self.pp.do_include(f)
    self.assertEqual(self.pp.out.getvalue(), "octal value is not equal\n")

  def test_undefined_variable(self):
    f = NamedIO("undefined_variable.in", """#filter substitution
@foo@
""")
    try:
      self.pp.do_include(f)
    except Preprocessor.Error, exception:
      self.assertEqual(exception.key, 'UNDEFINED_VAR')
    else:
      self.fail("Expected a Preprocessor.Error")

  def test_include(self):
    with MockedOpen({"foo/test": """#define foo foobarbaz
#include @inc@
@bar@
""",
                     "bar": """#define bar barfoobaz
@foo@
"""}):
      f = NamedIO("include.in", """#filter substitution
#define inc ../bar
#include foo/test""")
      self.pp.do_include(f)
      self.assertEqual(self.pp.out.getvalue(), """foobarbaz
barfoobaz
""")

  def test_include_missing_file(self):
    f = NamedIO("include_missing_file.in", "#include foo")
    try:
      self.pp.do_include(f)
    except Preprocessor.Error, exception:
      self.assertEqual(exception.key, 'FILE_NOT_FOUND')
    else:
      self.fail("Expected a Preprocessor.Error")

  def test_include_undefined_variable(self):
    f = NamedIO("include_undefined_variable.in", """#filter substitution
#include @foo@
""")
    try:
      self.pp.do_include(f)
    except Preprocessor.Error, exception:
      self.assertEqual(exception.key, 'UNDEFINED_VAR')
    else:
      self.fail("Expected a Preprocessor.Error")

  def test_include_literal_at(self):
    with MockedOpen({"@foo@": "#define foo foobarbaz"}):
      f = NamedIO("include_literal_at.in", """#include @foo@
#filter substitution
@foo@
""")
      self.pp.do_include(f)
      self.assertEqual(self.pp.out.getvalue(), """foobarbaz
""")

  def test_command_line_literal_at(self):
    with MockedOpen({"@foo@.in": """@foo@
"""}):
      self.pp.handleCommandLine(['-Fsubstitution', '-Dfoo=foobarbaz', '@foo@.in'])
      self.assertEqual(self.pp.out.getvalue(), """foobarbaz
""")

if __name__ == '__main__':
  main()