• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1""" Test suite for the code in msilib """
2import os
3import unittest
4from test.support import TESTFN, import_module, unlink
5msilib = import_module('msilib')
6import msilib.schema
7
8
9def init_database():
10    path = TESTFN + '.msi'
11    db = msilib.init_database(
12        path,
13        msilib.schema,
14        'Python Tests',
15        'product_code',
16        '1.0',
17        'PSF',
18    )
19    return db, path
20
21
22class MsiDatabaseTestCase(unittest.TestCase):
23
24    def test_view_fetch_returns_none(self):
25        db, db_path = init_database()
26        properties = []
27        view = db.OpenView('SELECT Property, Value FROM Property')
28        view.Execute(None)
29        while True:
30            record = view.Fetch()
31            if record is None:
32                break
33            properties.append(record.GetString(1))
34        view.Close()
35        db.Close()
36        self.assertEqual(
37            properties,
38            [
39                'ProductName', 'ProductCode', 'ProductVersion',
40                'Manufacturer', 'ProductLanguage',
41            ]
42        )
43        self.addCleanup(unlink, db_path)
44
45    def test_view_non_ascii(self):
46        db, db_path = init_database()
47        view = db.OpenView("SELECT 'ß-розпад' FROM Property")
48        view.Execute(None)
49        record = view.Fetch()
50        self.assertEqual(record.GetString(1), 'ß-розпад')
51        view.Close()
52        db.Close()
53        self.addCleanup(unlink, db_path)
54
55    def test_summaryinfo_getproperty_issue1104(self):
56        db, db_path = init_database()
57        try:
58            sum_info = db.GetSummaryInformation(99)
59            title = sum_info.GetProperty(msilib.PID_TITLE)
60            self.assertEqual(title, b"Installation Database")
61
62            sum_info.SetProperty(msilib.PID_TITLE, "a" * 999)
63            title = sum_info.GetProperty(msilib.PID_TITLE)
64            self.assertEqual(title, b"a" * 999)
65
66            sum_info.SetProperty(msilib.PID_TITLE, "a" * 1000)
67            title = sum_info.GetProperty(msilib.PID_TITLE)
68            self.assertEqual(title, b"a" * 1000)
69
70            sum_info.SetProperty(msilib.PID_TITLE, "a" * 1001)
71            title = sum_info.GetProperty(msilib.PID_TITLE)
72            self.assertEqual(title, b"a" * 1001)
73        finally:
74            db = None
75            sum_info = None
76            os.unlink(db_path)
77
78    def test_database_open_failed(self):
79        with self.assertRaises(msilib.MSIError) as cm:
80            msilib.OpenDatabase('non-existent.msi', msilib.MSIDBOPEN_READONLY)
81        self.assertEqual(str(cm.exception), 'open failed')
82
83    def test_database_create_failed(self):
84        db_path = os.path.join(TESTFN, 'test.msi')
85        with self.assertRaises(msilib.MSIError) as cm:
86            msilib.OpenDatabase(db_path, msilib.MSIDBOPEN_CREATE)
87        self.assertEqual(str(cm.exception), 'create failed')
88
89    def test_get_property_vt_empty(self):
90        db, db_path = init_database()
91        summary = db.GetSummaryInformation(0)
92        self.assertIsNone(summary.GetProperty(msilib.PID_SECURITY))
93        db.Close()
94        self.addCleanup(unlink, db_path)
95
96    def test_directory_start_component_keyfile(self):
97        db, db_path = init_database()
98        self.addCleanup(unlink, db_path)
99        self.addCleanup(db.Close)
100        self.addCleanup(msilib._directories.clear)
101        feature = msilib.Feature(db, 0, 'Feature', 'A feature', 'Python')
102        cab = msilib.CAB('CAB')
103        dir = msilib.Directory(db, cab, None, TESTFN, 'TARGETDIR',
104                               'SourceDir', 0)
105        dir.start_component(None, feature, None, 'keyfile')
106
107    def test_getproperty_uninitialized_var(self):
108        db, db_path = init_database()
109        self.addCleanup(unlink, db_path)
110        self.addCleanup(db.Close)
111        si = db.GetSummaryInformation(0)
112        with self.assertRaises(msilib.MSIError):
113            si.GetProperty(-1)
114
115
116class Test_make_id(unittest.TestCase):
117    #http://msdn.microsoft.com/en-us/library/aa369212(v=vs.85).aspx
118    """The Identifier data type is a text string. Identifiers may contain the
119    ASCII characters A-Z (a-z), digits, underscores (_), or periods (.).
120    However, every identifier must begin with either a letter or an
121    underscore.
122    """
123
124    def test_is_no_change_required(self):
125        self.assertEqual(
126            msilib.make_id("short"), "short")
127        self.assertEqual(
128            msilib.make_id("nochangerequired"), "nochangerequired")
129        self.assertEqual(
130            msilib.make_id("one.dot"), "one.dot")
131        self.assertEqual(
132            msilib.make_id("_"), "_")
133        self.assertEqual(
134            msilib.make_id("a"), "a")
135        #self.assertEqual(
136        #    msilib.make_id(""), "")
137
138    def test_invalid_first_char(self):
139        self.assertEqual(
140            msilib.make_id("9.short"), "_9.short")
141        self.assertEqual(
142            msilib.make_id(".short"), "_.short")
143
144    def test_invalid_any_char(self):
145        self.assertEqual(
146            msilib.make_id(".s\x82ort"), "_.s_ort")
147        self.assertEqual(
148            msilib.make_id(".s\x82o?*+rt"), "_.s_o___rt")
149
150
151if __name__ == '__main__':
152    unittest.main()
153