示例#1
0
  def test_encodeAsGoogle(self):
    """Verify that data can be properly hidden as a google search
    string

    Dependencies: decodeAsEnglish, decodeAsCookie
    
    """
    testData = ['some text', 'another test', 'still more text',
                'an example that is liable to introduce cookies'
                '- scrumptous, scrumptous cookies']
    for datum in testData:
      testOutput = urlEncode.encodeAsGoogle(datum)
      #assert that cookies were created if needed
      if len(datum) > 40:
        self.assertNotEqual(testOutput['cookie'], [])
      #assert that the url is in the correct form
      pattern = 'http://www.google.com/search\?q=(?P<data>[a-zA-Z+]+)'
      matches = re.match(pattern, testOutput['url'])
      self.assertIsNotNone(matches)
      #assert that the url and cookies correctly decode
      words = matches.group('data')
      words = words.split('+')
      decoded = urlEncode.decodeAsEnglish(words)
      for cookie in testOutput['cookie']:
        decoded += urlEncode.decodeAsCookie(cookie)
      self.assertEqual(datum, decoded)
示例#2
0
  def test_encodeAsBaidu(self):
    """
    Verify that the Baidu encoding actually looks like it should

    Dependencies: decodeAsEnglish, decodeAsCookie

    """
    testData = ['this is a string', 'some-data-that-appears-mildly'
                '-long-and-hopefully-_exceeds40chars',
                'another string']
    for datum in testData:
      testOutput = urlEncode.encodeAsBaidu(datum)
      #assert that cookies were created if needed
      if len(datum) > 40:
        self.assertNotEqual(testOutput['cookie'], [])
      #assert that the url is in the correct form
      pattern = 'http://www.baidu.com/s\?wd=(?P<data>[\S+]+)'
      matches = re.match(pattern, testOutput['url'])
      self.assertIsNotNone(matches)
      #assert that the url and cookies correctly decode
      words = matches.group('data')
      words = words.split('+')
      decoded = urlEncode.decodeAsEnglish(words)
      for cookie in testOutput['cookie']:
        decoded += urlEncode.decodeAsCookie(cookie)
      self.assertEqual(datum, decoded)
示例#3
0
  def test_encodeAsMarket(self):
    """Verify that data are correctly stored in the url market form"""

    testData = ['some text', 'more text',
                'even more text that seems moderately longish']
    for datum in testData:
        output = urlEncode.encodeAsMarket(datum)
        testOutput = output['url']
        #verify that the output is in the correct form
        pattern = 'http://[a-zA-Z0-9.]*[.]com\?qs=(?P<hash>[0-9a-fA-F]*)'
        matches = re.match(pattern, testOutput)
        self.assertNotEqual(matches, None)
        #verify that the output is actually the hex representation of
        #the data
        hashPart = matches.group('hash')
        self.assertEqual(len(hashPart), 80)
        #as with the real code in urlEncode, this is a one liner to
        #convert the data length counter from hex into an integer that
        #we can use later
        dataLen = int(hashPart[:2], 16)
        testDatum = hashPart[2:dataLen+2]
        testDatum = binascii.unhexlify(testDatum)
        for cookie in output['cookie']:
          testDatum += urlEncode.decodeAsCookie(cookie)
        self.assertEqual(testDatum, datum)
示例#4
0
 def test_decodeAsCookie(self):
   """Test that cookies can be correctly decoded"""
   testData = ['something', 'whisper, whisper','longer text than this'
               'a much longer text than you expected', 'tiny']
   for datum in testData:
     if len(datum) < 10 and len(datum) > 5:
       keyLen = 3
     elif len(datum) <= 5:
       key = 'keyForPadding' #longer than 10 chars, so no need to escape
       key = urlsafe_b64encode(key)
       key = key.replace('=', '+')
       value = urlsafe_b64encode(datum)
       cookie = 'Cookie: ' + key + '=' + value
       self.assertEqual(urlEncode.decodeAsCookie(cookie), datum)
       break
     else:
       keyLen = randint(3, 10)
     key = datum[:keyLen]
     value = datum[keyLen:]
     key = urlsafe_b64encode(key)
     key = key.replace('=', '+')
     value = urlsafe_b64encode(value)
     cookie = 'Cookie: ' + key + '=' + value
     self.assertEqual(urlEncode.decodeAsCookie(cookie), datum)
示例#5
0
  def test_decodeAsGoogle(self):
    """Verify that encoded text can be decoded again
    
    Dependencies: encodeAsGoogle

    """
    testData = ['some text', 'another test', 'still more text',
                'an example that is liable to introduce cookies'
                '- scrumptous, scrumptous cookies']
    for datum in testData:
      testOutput = urlEncode.encodeAsGoogle(datum)
    cookieData = []
    for cookie in testOutput['cookie']:
      cookieData.append(urlEncode.decodeAsCookie(cookie))
    cookieData = ''.join(cookieData)
    urlData = urlEncode.decodeAsGoogle(testOutput['url'])
    self.assertEqual(datum, urlData + cookieData)
示例#6
0
  def test_decodeAsBaidu(self):
    """
    Verify that we correctly decode Baidu urls

    Note: we are just testing that we can decode what we encode. We
    are relying on the test_encodeAsBaidu functionality to make sure
    that the encoding correctly hides the data

    Dependencies: this function depends on encodeAsBaidu and decodeAsCookie

    """
    testData = ['this is a string', 'some-data-that-appears-mildly'
                '-long-and-hopefully-_exceeds40chars',
                'another string']
    for datum in testData:
      testOutput = urlEncode.encodeAsBaidu(datum)
      cookieData = []
      for cookie in testOutput['cookie']:
        cookieData.append(urlEncode.decodeAsCookie(cookie))
      cookieData = ''.join(cookieData)
      urlData = urlEncode.decodeAsBaidu(testOutput['url'])
      self.assertEqual(datum, urlData + cookieData)
示例#7
0
  def test_encodeAsCookies(self):
    """
    Test that data is correctly inserted into multiple cookies and
    can be correctly retrieved
    """

    testData = ['by jove, this string seems like it may continue for'
               'eternity, possibly to the end of time',
               'this is a shorter message, but still longish',
               'some messages are not too long',
               'some messages can become very verbose and by the intrinsic'
               'nature of their character, they are forced to become'
               'eloquent prose which drags on for lines and lines',
               'Remember, remember, the 5th of November, the gunpowder'
               'treason and plot. I can think of no reason why the 5th of'
               'november should ever be forgot']
    for datum in testData:
      testOutput = urlEncode.encodeAsCookies(datum)
      data = []
      for cookie in testOutput:
        data.append(urlEncode.decodeAsCookie(cookie))
      data = ''.join(data)
      self.assertEqual(data, datum)