1#!/usr/bin/env python
2
3# Copyright (c) 2012 Google Inc. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7"""
8Make sure buffer security check setting is extracted properly.
9"""
10
11import TestGyp
12
13import sys
14
15if sys.platform == 'win32':
16  test = TestGyp.TestGyp(formats=['msvs', 'ninja'])
17
18  CHDIR = 'compiler-flags'
19  test.run_gyp('buffer-security-check.gyp', chdir=CHDIR)
20  test.build('buffer-security-check.gyp', chdir=CHDIR)
21
22  def GetDisassemblyOfMain(exe):
23    # The standard library uses buffer security checks independent of our
24    # buffer security settings, so we extract just our code (i.e. main()) to
25    # check against.
26    full_path = test.built_file_path(exe, chdir=CHDIR)
27    output = test.run_dumpbin('/disasm', full_path)
28    result = []
29    in_main = False
30    for line in output.splitlines():
31      if line == '_main:':
32        in_main = True
33      elif in_main:
34        # Disassembly of next function starts.
35        if line.startswith('_'):
36          break
37        result.append(line)
38    return '\n'.join(result)
39
40  # Buffer security checks are on by default, make sure security_cookie
41  # appears in the disassembly of our code.
42  if 'security_cookie' not in GetDisassemblyOfMain('test_bsc_unset.exe'):
43    test.fail_test()
44
45  # Explicitly on.
46  if 'security_cookie' not in GetDisassemblyOfMain('test_bsc_on.exe'):
47    test.fail_test()
48
49  # Explicitly off, shouldn't be a reference to the security cookie.
50  if 'security_cookie' in GetDisassemblyOfMain('test_bsc_off.exe'):
51    test.fail_test()
52
53  test.pass_test()
54