testifacecache.py :  » Installer » Zero-Install » zeroinstall-injector-0.47 » tests » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Installer » Zero Install 
Zero Install » zeroinstall injector 0.47 » tests » testifacecache.py
#!/usr/bin/env python
from basetest import BaseTest
import sys, tempfile, os, time
import unittest
import data

sys.path.insert(0, '..')
from zeroinstall.injector import model,gpg,trust
from zeroinstall.injector.namespaces import config_site
from zeroinstall.injector.iface_cache import iface_cache,PendingFeed
from zeroinstall.support import basedir

class TestIfaceCache(BaseTest):
  def testList(self):
    self.assertEquals([], iface_cache.list_all_interfaces())
    iface_dir = basedir.save_cache_path(config_site, 'interfaces')
    file(os.path.join(iface_dir, 'http%3a%2f%2ffoo'), 'w').close()
    self.assertEquals(['http://foo'],
        iface_cache.list_all_interfaces())
    # TODO: test overrides

  def testCheckSigned(self):
    trust.trust_db.trust_key(
      '92429807C9853C0744A68B9AAE07828059A53CC1')
    iface = iface_cache.get_interface('http://foo')
    src = tempfile.TemporaryFile()

    # Unsigned
    src.write("hello")
    src.flush()
    src.seek(0)
    try:
      PendingFeed(iface.uri, src)
      assert 0
    except model.SafeException:
      pass

    stream = tempfile.TemporaryFile()
    stream.write(data.thomas_key)
    stream.seek(0)

    gpg.import_key(stream)

    # Signed
    src.seek(0)
    src.write(data.foo_signed)
    src.flush()
    src.seek(0)

    pending = PendingFeed(iface.uri, src)
    assert iface_cache.update_interface_if_trusted(iface, pending.sigs, pending.new_xml)

    self.assertEquals(['http://foo'],
        iface_cache.list_all_interfaces())

    self.assertEquals(1116788178,  iface.last_modified)

  def testXMLupdate(self):
    trust.trust_db.trust_key(
      '92429807C9853C0744A68B9AAE07828059A53CC1')
    stream = tempfile.TemporaryFile()
    stream.write(data.thomas_key)
    stream.seek(0)
    gpg.import_key(stream)

    iface = iface_cache.get_interface('http://foo')
    src = tempfile.TemporaryFile()
    src.write(data.foo_signed_xml)
    src.seek(0)
    pending = PendingFeed(iface.uri, src)
    assert iface_cache.update_interface_if_trusted(iface, pending.sigs, pending.new_xml)

    iface_cache.__init__()
    iface = iface_cache.get_interface('http://foo')
    assert iface.last_modified == 1154850229

    # mtimes are unreliable because copying often changes them -
    # check that we extract the time from the signature when upgrading
    upstream_dir = basedir.save_cache_path(config_site, 'interfaces')
    cached = os.path.join(upstream_dir, model.escape(iface.uri))
    os.utime(cached, None)

    iface_cache.__init__()
    iface = iface_cache.get_interface('http://foo')
    assert iface.last_modified > 1154850229

    src = tempfile.TemporaryFile()
    src.write(data.new_foo_signed_xml)
    src.seek(0)

    pending = PendingFeed(iface.uri, src)
    assert iface_cache.update_interface_if_trusted(iface, pending.sigs, pending.new_xml)

    # Can't 'update' to an older copy
    src = tempfile.TemporaryFile()
    src.write(data.foo_signed_xml)
    src.seek(0)
    try:
      pending = PendingFeed(iface.uri, src)
      assert iface_cache.update_interface_if_trusted(iface, pending.sigs, pending.new_xml)

      assert 0
    except model.SafeException:
      pass

  def testTimes(self):
    stream = tempfile.TemporaryFile()
    stream.write(data.thomas_key)
    stream.seek(0)
    gpg.import_key(stream)

    upstream_dir = basedir.save_cache_path(config_site, 'interfaces')
    cached = os.path.join(upstream_dir, model.escape('http://foo'))

    stream = file(cached, 'w')
    stream.write(data.foo_signed_xml)
    stream.close()

    signed = iface_cache._get_signature_date('http://foo')
    assert signed == None

    trust.trust_db.trust_key(
      '92429807C9853C0744A68B9AAE07828059A53CC1')

    signed = iface_cache._get_signature_date('http://foo')
    assert signed == 1154850229

    stream = file(cached, 'w+')
    stream.seek(0)
    stream.write('Hello')
    stream.close()

    # When the signature is invalid, we just return None.
    # This is because versions < 0.22 used to corrupt the signatue
    # by adding an attribute to the XML
    signed = iface_cache._get_signature_date('http://foo')
    assert signed == None

  def testCheckAttempt(self):
    self.assertEquals(None, iface_cache.get_last_check_attempt("http://foo/bar.xml"))

    start_time = time.time() - 5  # Seems to be some odd rounding here
    iface_cache.mark_as_checking("http://foo/bar.xml")
    last_check = iface_cache.get_last_check_attempt("http://foo/bar.xml")

    assert last_check is not None
    assert last_check >= start_time, (last_check, start_time)

    self.assertEquals(None, iface_cache.get_last_check_attempt("http://foo/bar2.xml"))


if __name__ == '__main__':
  unittest.main()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.