addon-sdk/source/python-lib/cuddlefish/tests/test_preflight.py

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 # This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 # License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
michael@0 4
michael@0 5
michael@0 6 import os, shutil
michael@0 7 import simplejson as json
michael@0 8 import unittest
michael@0 9 import hashlib
michael@0 10 import base64
michael@0 11 from cuddlefish import preflight
michael@0 12 from StringIO import StringIO
michael@0 13
michael@0 14 class Util(unittest.TestCase):
michael@0 15 def get_basedir(self):
michael@0 16 return os.path.join(".test_tmp", self.id())
michael@0 17 def make_basedir(self):
michael@0 18 basedir = self.get_basedir()
michael@0 19 if os.path.isdir(basedir):
michael@0 20 here = os.path.abspath(os.getcwd())
michael@0 21 assert os.path.abspath(basedir).startswith(here) # safety
michael@0 22 shutil.rmtree(basedir)
michael@0 23 os.makedirs(basedir)
michael@0 24 return basedir
michael@0 25
michael@0 26 def test_base62(self):
michael@0 27 for i in range(1000):
michael@0 28 h = hashlib.sha1(str(i)).digest()
michael@0 29 s1 = base64.b64encode(h, "AB").strip("=")
michael@0 30 s2 = base64.b64encode(h).strip("=").replace("+","A").replace("/","B")
michael@0 31 self.failUnlessEqual(s1, s2)
michael@0 32
michael@0 33 def write(self, config):
michael@0 34 basedir = self.get_basedir()
michael@0 35 fn = os.path.join(basedir, "package.json")
michael@0 36 open(fn,"w").write(config)
michael@0 37 def read(self):
michael@0 38 basedir = self.get_basedir()
michael@0 39 fn = os.path.join(basedir, "package.json")
michael@0 40 return open(fn,"r").read()
michael@0 41
michael@0 42 def get_cfg(self):
michael@0 43 cfg = json.loads(self.read())
michael@0 44 if "name" not in cfg:
michael@0 45 # the cfx parser always provides a name, even if package.json
michael@0 46 # doesn't contain one
michael@0 47 cfg["name"] = "pretend name"
michael@0 48 return cfg
michael@0 49
michael@0 50 def parse(self, keydata):
michael@0 51 fields = {}
michael@0 52 fieldnames = []
michael@0 53 for line in keydata.split("\n"):
michael@0 54 if line.strip():
michael@0 55 k,v = line.split(":", 1)
michael@0 56 k = k.strip() ; v = v.strip()
michael@0 57 fields[k] = v
michael@0 58 fieldnames.append(k)
michael@0 59 return fields, fieldnames
michael@0 60
michael@0 61 def test_preflight(self):
michael@0 62 basedir = self.make_basedir()
michael@0 63 fn = os.path.join(basedir, "package.json")
michael@0 64
michael@0 65 # empty config is not ok: need id (name is automatically supplied)
michael@0 66 config_orig = "{}"
michael@0 67 self.write(config_orig)
michael@0 68 out = StringIO()
michael@0 69 cfg = self.get_cfg()
michael@0 70 config_was_ok, modified = preflight.preflight_config(cfg, fn,
michael@0 71 stderr=out)
michael@0 72 self.failUnlessEqual(config_was_ok, False)
michael@0 73 self.failUnlessEqual(modified, True)
michael@0 74 backup_fn = os.path.join(basedir, "package.json.backup")
michael@0 75 config_backup = open(backup_fn,"r").read()
michael@0 76 self.failUnlessEqual(config_backup, config_orig)
michael@0 77 config = json.loads(self.read())
michael@0 78 self.failIf("name" in config)
michael@0 79 self.failUnless("id" in config)
michael@0 80 self.failUnless(config["id"].startswith("jid1-"), config["id"])
michael@0 81 self.failUnlessEqual(out.getvalue().strip(),
michael@0 82 "No 'id' in package.json: creating a new ID for you.")
michael@0 83 os.unlink(backup_fn)
michael@0 84
michael@0 85 # just a name? we add the id
michael@0 86 config_orig = '{"name": "my-awesome-package"}'
michael@0 87 self.write(config_orig)
michael@0 88 out = StringIO()
michael@0 89 cfg = self.get_cfg()
michael@0 90 config_was_ok, modified = preflight.preflight_config(cfg, fn,
michael@0 91 stderr=out)
michael@0 92 self.failUnlessEqual(config_was_ok, False)
michael@0 93 self.failUnlessEqual(modified, True)
michael@0 94 backup_fn = os.path.join(basedir, "package.json.backup")
michael@0 95 config_backup = open(backup_fn,"r").read()
michael@0 96 self.failUnlessEqual(config_backup, config_orig)
michael@0 97 config = json.loads(self.read())
michael@0 98 self.failUnlessEqual(config["name"], "my-awesome-package")
michael@0 99 self.failUnless("id" in config)
michael@0 100 self.failUnless(config["id"].startswith("jid1-"), config["id"])
michael@0 101 jid = str(config["id"])
michael@0 102 self.failUnlessEqual(out.getvalue().strip(),
michael@0 103 "No 'id' in package.json: creating a new ID for you.")
michael@0 104 os.unlink(backup_fn)
michael@0 105
michael@0 106 # name and valid id? great! ship it!
michael@0 107 config2 = '{"name": "my-awesome-package", "id": "%s"}' % jid
michael@0 108 self.write(config2)
michael@0 109 out = StringIO()
michael@0 110 cfg = self.get_cfg()
michael@0 111 config_was_ok, modified = preflight.preflight_config(cfg, fn,
michael@0 112 stderr=out)
michael@0 113 self.failUnlessEqual(config_was_ok, True)
michael@0 114 self.failUnlessEqual(modified, False)
michael@0 115 config2a = self.read()
michael@0 116 self.failUnlessEqual(config2a, config2)
michael@0 117 self.failUnlessEqual(out.getvalue().strip(), "")
michael@0 118
michael@0 119 # name and anonymous ID? without asking to see its papers, ship it
michael@0 120 config3 = '{"name": "my-old-skool-package", "id": "anonid0-deadbeef"}'
michael@0 121 self.write(config3)
michael@0 122 out = StringIO()
michael@0 123 cfg = self.get_cfg()
michael@0 124 config_was_ok, modified = preflight.preflight_config(cfg, fn,
michael@0 125 stderr=out)
michael@0 126 self.failUnlessEqual(config_was_ok, True)
michael@0 127 self.failUnlessEqual(modified, False)
michael@0 128 config3a = self.read()
michael@0 129 self.failUnlessEqual(config3a, config3)
michael@0 130 self.failUnlessEqual(out.getvalue().strip(), "")
michael@0 131
michael@0 132 # name and old-style ID? with nostalgic trepidation, ship it
michael@0 133 config4 = '{"name": "my-old-skool-package", "id": "foo@bar.baz"}'
michael@0 134 self.write(config4)
michael@0 135 out = StringIO()
michael@0 136 cfg = self.get_cfg()
michael@0 137 config_was_ok, modified = preflight.preflight_config(cfg, fn,
michael@0 138 stderr=out)
michael@0 139 self.failUnlessEqual(config_was_ok, True)
michael@0 140 self.failUnlessEqual(modified, False)
michael@0 141 config4a = self.read()
michael@0 142 self.failUnlessEqual(config4a, config4)
michael@0 143 self.failUnlessEqual(out.getvalue().strip(), "")
michael@0 144
michael@0 145
michael@0 146 if __name__ == '__main__':
michael@0 147 unittest.main()

mercurial