~ubuntu-branches/ubuntu/raring/swift/raring-security

« back to all changes in this revision

Viewing changes to test/unit/common/middleware/test_name_check.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2012-04-10 09:23:54 UTC
  • mfrom: (1.2.9)
  • Revision ID: package-import@ubuntu.com-20120410092354-5z06qt1jal4wtoxf
Tags: 1.4.8-0ubuntu1
* New upstream release.
* debian/patches/fix-ubuntu-unittests.patch: Refreshed. 
* debian/patches/fix-doc-no-network.patch: Dont access network when
  trying to build docs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (c) 2012 OpenStack, LLC.
 
2
#
 
3
# Licensed under the Apache License, Version 2.0 (the "License");
 
4
# you may not use this file except in compliance with the License.
 
5
# You may obtain a copy of the License at
 
6
#
 
7
#    http://www.apache.org/licenses/LICENSE-2.0
 
8
#
 
9
# Unless required by applicable law or agreed to in writing, software
 
10
# distributed under the License is distributed on an "AS IS" BASIS,
 
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 
12
# implied.
 
13
# See the License for the specific language governing permissions and
 
14
# limitations under the License.
 
15
 
 
16
'''
 
17
Unit tests for Name_check filter
 
18
 
 
19
Created on February 29, 2012
 
20
 
 
21
@author: eamonn-otoole
 
22
'''
 
23
 
 
24
import unittest
 
25
from webob import Request, Response
 
26
from swift.common.middleware import name_check
 
27
 
 
28
MAX_LENGTH = 255
 
29
FORBIDDEN_CHARS = '\'\"<>`'
 
30
 
 
31
 
 
32
class FakeApp(object):
 
33
 
 
34
    def __call__(self, env, start_response):
 
35
        return Response(body="OK")(env, start_response)
 
36
 
 
37
 
 
38
class TestNameCheckMiddleware(unittest.TestCase):
 
39
 
 
40
    def setUp(self):
 
41
        self.conf = {'maximum_length': MAX_LENGTH, 'forbidden_chars':
 
42
                     FORBIDDEN_CHARS}
 
43
        self.test_check = name_check.filter_factory(self.conf)(FakeApp())
 
44
 
 
45
    def test_valid_length_and_character(self):
 
46
        path = '/V1.0/' + 'c' * (MAX_LENGTH - 6)
 
47
        resp = Request.blank(path, environ={'REQUEST_METHOD': 'PUT'}
 
48
                             ).get_response(self.test_check)
 
49
        self.assertEquals(resp.body, 'OK')
 
50
 
 
51
    def test_invalid_character(self):
 
52
        for c in self.conf['forbidden_chars']:
 
53
            path = '/V1.0/1234' + c + '5'
 
54
            resp = Request.blank(path, environ={'REQUEST_METHOD': 'PUT'}
 
55
                             ).get_response(self.test_check)
 
56
            self.assertEquals(resp.body,
 
57
                    ("Object/Container name contains forbidden chars from %s"
 
58
                    % self.conf['forbidden_chars']))
 
59
            self.assertEquals(resp.status_int, 400)
 
60
 
 
61
    def test_invalid_length(self):
 
62
        path = '/V1.0/' + 'c' * (MAX_LENGTH - 5)
 
63
        resp = Request.blank(path, environ={'REQUEST_METHOD': 'PUT'}
 
64
                             ).get_response(self.test_check)
 
65
        self.assertEquals(resp.body,
 
66
                    ("Object/Container name longer than the allowed maximum %s"
 
67
                    % self.conf['maximum_length']))
 
68
        self.assertEquals(resp.status_int, 400)
 
69
 
 
70
 
 
71
if __name__ == '__main__':
 
72
    unittest.main()