Back to index

enigmail  1.4.3
unit-writemozinfo.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 from __future__ import with_statement
00003 import unittest
00004 import os, sys, time, tempfile
00005 from StringIO import StringIO
00006 
00007 sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
00008 
00009 from writemozinfo import build_dict, write_json, JsonValue, jsonify
00010 
00011 class TestBuildDict(unittest.TestCase):
00012     def testMissing(self):
00013         """
00014         Test that missing required values raises.
00015         """
00016         self.assertRaises(Exception, build_dict, {})
00017         self.assertRaises(Exception, build_dict, {'OS_TARGET':'foo'})
00018         self.assertRaises(Exception, build_dict, {'TARGET_CPU':'foo'})
00019         self.assertRaises(Exception, build_dict, {'MOZ_WIDGET_TOOLKIT':'foo'})
00020 
00021     def testWin(self):
00022         d = build_dict({'OS_TARGET':'WINNT',
00023                         'TARGET_CPU':'i386',
00024                         'MOZ_WIDGET_TOOLKIT':'windows'})
00025         self.assertEqual('win', d['os'])
00026         self.assertEqual('x86', d['processor'])
00027         self.assertEqual('windows', d['toolkit'])
00028         self.assertEqual(32, d['bits'])
00029 
00030     def testLinux(self):
00031         d = build_dict({'OS_TARGET':'Linux',
00032                         'TARGET_CPU':'i386',
00033                         'MOZ_WIDGET_TOOLKIT':'gtk2'})
00034         self.assertEqual('linux', d['os'])
00035         self.assertEqual('x86', d['processor'])
00036         self.assertEqual('gtk2', d['toolkit'])
00037         self.assertEqual(32, d['bits'])
00038 
00039         d = build_dict({'OS_TARGET':'Linux',
00040                         'TARGET_CPU':'x86_64',
00041                         'MOZ_WIDGET_TOOLKIT':'gtk2'})
00042         self.assertEqual('linux', d['os'])
00043         self.assertEqual('x86_64', d['processor'])
00044         self.assertEqual('gtk2', d['toolkit'])
00045         self.assertEqual(64, d['bits'])
00046 
00047     def testMac(self):
00048         d = build_dict({'OS_TARGET':'Darwin',
00049                         'TARGET_CPU':'i386',
00050                         'MOZ_WIDGET_TOOLKIT':'cocoa'})
00051         self.assertEqual('mac', d['os'])
00052         self.assertEqual('x86', d['processor'])
00053         self.assertEqual('cocoa', d['toolkit'])
00054         self.assertEqual(32, d['bits'])
00055 
00056         d = build_dict({'OS_TARGET':'Darwin',
00057                         'TARGET_CPU':'x86_64',
00058                         'MOZ_WIDGET_TOOLKIT':'cocoa'})
00059         self.assertEqual('mac', d['os'])
00060         self.assertEqual('x86_64', d['processor'])
00061         self.assertEqual('cocoa', d['toolkit'])
00062         self.assertEqual(64, d['bits'])
00063 
00064     def testMacUniversal(self):
00065         d = build_dict({'OS_TARGET':'Darwin',
00066                         'TARGET_CPU':'i386',
00067                         'MOZ_WIDGET_TOOLKIT':'cocoa',
00068                         'UNIVERSAL_BINARY': '1'})
00069         self.assertEqual('mac', d['os'])
00070         self.assertEqual('universal-x86-x86_64', d['processor'])
00071         self.assertEqual('cocoa', d['toolkit'])
00072         self.assertFalse('bits' in d)
00073 
00074         d = build_dict({'OS_TARGET':'Darwin',
00075                         'TARGET_CPU':'x86_64',
00076                         'MOZ_WIDGET_TOOLKIT':'cocoa',
00077                         'UNIVERSAL_BINARY': '1'})
00078         self.assertEqual('mac', d['os'])
00079         self.assertEqual('universal-x86-x86_64', d['processor'])
00080         self.assertEqual('cocoa', d['toolkit'])
00081         self.assertFalse('bits' in d)
00082 
00083     def testAndroid(self):
00084         d = build_dict({'OS_TARGET':'Android',
00085                         'TARGET_CPU':'arm',
00086                         'MOZ_WIDGET_TOOLKIT':'android'})
00087         self.assertEqual('android', d['os'])
00088         self.assertEqual('arm', d['processor'])
00089         self.assertEqual('android', d['toolkit'])
00090         self.assertEqual(32, d['bits'])
00091 
00092     def testX86(self):
00093         """
00094         Test that various i?86 values => x86.
00095         """
00096         d = build_dict({'OS_TARGET':'WINNT',
00097                         'TARGET_CPU':'i486',
00098                         'MOZ_WIDGET_TOOLKIT':'windows'})
00099         self.assertEqual('x86', d['processor'])
00100 
00101         d = build_dict({'OS_TARGET':'WINNT',
00102                         'TARGET_CPU':'i686',
00103                         'MOZ_WIDGET_TOOLKIT':'windows'})
00104         self.assertEqual('x86', d['processor'])
00105 
00106     def testARM(self):
00107         """
00108         Test that all arm CPU architectures => arm.
00109         """
00110         d = build_dict({'OS_TARGET':'Linux',
00111                         'TARGET_CPU':'arm',
00112                         'MOZ_WIDGET_TOOLKIT':'gtk2'})
00113         self.assertEqual('arm', d['processor'])
00114 
00115         d = build_dict({'OS_TARGET':'Linux',
00116                         'TARGET_CPU':'armv7',
00117                         'MOZ_WIDGET_TOOLKIT':'gtk2'})
00118         self.assertEqual('arm', d['processor'])
00119 
00120     def testUnknown(self):
00121         """
00122         Test that unknown values pass through okay.
00123         """
00124         d = build_dict({'OS_TARGET':'RandOS',
00125                         'TARGET_CPU':'cptwo',
00126                         'MOZ_WIDGET_TOOLKIT':'foobar'})
00127         self.assertEqual("randos", d["os"])
00128         self.assertEqual("cptwo", d["processor"])
00129         self.assertEqual("foobar", d["toolkit"])
00130         # unknown CPUs should not get a bits value
00131         self.assertFalse("bits" in d)
00132         
00133     def testDebug(self):
00134         """
00135         Test that debug values are properly detected.
00136         """
00137         d = build_dict({'OS_TARGET':'Linux',
00138                         'TARGET_CPU':'i386',
00139                         'MOZ_WIDGET_TOOLKIT':'gtk2'})
00140         self.assertEqual(False, d['debug'])
00141         
00142         d = build_dict({'OS_TARGET':'Linux',
00143                         'TARGET_CPU':'i386',
00144                         'MOZ_WIDGET_TOOLKIT':'gtk2',
00145                         'MOZ_DEBUG':'1'})
00146         self.assertEqual(True, d['debug'])
00147 
00148     def testCrashreporter(self):
00149         """
00150         Test that crashreporter values are properly detected.
00151         """
00152         d = build_dict({'OS_TARGET':'Linux',
00153                         'TARGET_CPU':'i386',
00154                         'MOZ_WIDGET_TOOLKIT':'gtk2'})
00155         self.assertEqual(False, d['crashreporter'])
00156         
00157         d = build_dict({'OS_TARGET':'Linux',
00158                         'TARGET_CPU':'i386',
00159                         'MOZ_WIDGET_TOOLKIT':'gtk2',
00160                         'MOZ_CRASHREPORTER':'1'})
00161         self.assertEqual(True, d['crashreporter'])
00162 
00163 class TestJsonValue(unittest.TestCase):
00164     def testNone(self):
00165         self.assertEqual("null", repr(JsonValue(None)))
00166         
00167     def testBool(self):
00168         self.assertEqual("true", repr(JsonValue(True)))
00169         self.assertEqual("false", repr(JsonValue(False)))
00170 
00171     def testStr(self):
00172         self.assertEqual("'abc'", repr(JsonValue("abc")))
00173 
00174     def testInt(self):
00175         self.assertEqual("100", repr(JsonValue(100)))
00176 
00177     def testInvalid(self):
00178         self.assertRaises(Exception, JsonValue, unicode("abc"))
00179         self.assertRaises(Exception, JsonValue, 123.45)
00180 
00181 def parse_json(j):
00182     """
00183     Awful hack to parse a restricted subset of JSON strings into Python dicts.
00184     """
00185     return eval(j, {'true':True,'false':False,'null':None})
00186 
00187 class TestJsonify(unittest.TestCase):
00188     """
00189     Test the jsonify function.
00190     """
00191     def testBasic(self):
00192         """
00193         Sanity check the set of accepted Python value types.
00194         """
00195         j = parse_json(jsonify({'a':True,'b':False,'c':None,'d':100,'e':"abc"}))
00196         self.assertEquals(True, j['a'])
00197         self.assertEquals(False, j['b'])
00198         self.assertEquals(None, j['c'])
00199         self.assertEquals(100, j['d'])
00200         self.assertEquals("abc", j['e'])
00201 
00202 class TestWriteJson(unittest.TestCase):
00203     """
00204     Test the write_json function.
00205     """
00206     def setUp(self):
00207         fd, self.f = tempfile.mkstemp()
00208         os.close(fd)
00209 
00210     def tearDown(self):
00211         os.unlink(self.f)
00212 
00213     def testBasic(self):
00214         """
00215         Test that writing to a file produces correct output.
00216         """
00217         write_json(self.f, env={'OS_TARGET':'WINNT',
00218                                 'TARGET_CPU':'i386',
00219                                 'MOZ_WIDGET_TOOLKIT':'windows'})
00220         with open(self.f) as f:
00221             d = parse_json(f.read())
00222             self.assertEqual('win', d['os'])
00223             self.assertEqual('x86', d['processor'])
00224             self.assertEqual('windows', d['toolkit'])
00225             self.assertEqual(32, d['bits'])
00226 
00227     def testFileObj(self):
00228         """
00229         Test that writing to a file-like object produces correct output.
00230         """
00231         s = StringIO()
00232         write_json(s, env={'OS_TARGET':'WINNT',
00233                            'TARGET_CPU':'i386',
00234                            'MOZ_WIDGET_TOOLKIT':'windows'})
00235         d = parse_json(s.getvalue())
00236         self.assertEqual('win', d['os'])
00237         self.assertEqual('x86', d['processor'])
00238         self.assertEqual('windows', d['toolkit'])
00239         self.assertEqual(32, d['bits'])
00240 
00241 if __name__ == '__main__':
00242     unittest.main()
00243