-
Notifications
You must be signed in to change notification settings - Fork 257
/
io_test.py
181 lines (155 loc) · 4.43 KB
/
io_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# -*- coding: utf-8 -*-
import os
import unittest
import string
import random
import urllib
try:
import zlib as binascii
except ImportError:
import binascii
import cStringIO
from qiniu import conf
from qiniu import rs
from qiniu import io
conf.ACCESS_KEY = os.getenv("QINIU_ACCESS_KEY")
conf.SECRET_KEY = os.getenv("QINIU_SECRET_KEY")
bucket_name = os.getenv("QINIU_TEST_BUCKET")
policy = rs.PutPolicy(bucket_name)
extra = io.PutExtra()
extra.mime_type = "text/plain"
extra.params = {'x:a':'a'}
def r(length):
lib = string.ascii_uppercase
return ''.join([random.choice(lib) for i in range(0, length)])
class TestUp(unittest.TestCase):
def test(self):
def test_put():
key = "test_%s" % r(9)
params = "op=3"
data = "hello bubby!"
extra.check_crc = 2
extra.crc32 = binascii.crc32(data) & 0xFFFFFFFF
ret, err = io.put(policy.token(), key, data, extra)
assert err is None
assert ret['key'] == key
def test_put_same_crc():
key = "test_%s" % r(9)
data = "hello bubby!"
extra.check_crc = 2
ret, err = io.put(policy.token(), key, data, extra)
assert err is None
assert ret['key'] == key
def test_put_no_key():
data = r(100)
extra.check_crc = 0
ret, err = io.put(policy.token(), key=None, data=data, extra=extra)
assert err is None
assert ret['hash'] == ret['key']
def test_put_quote_key():
data = r(100)
key = 'a\\b\\c"你好' + r(9)
ret, err = io.put(policy.token(), key, data)
print err
assert err is None
assert ret['key'].encode('utf8') == key
data = r(100)
key = u'a\\b\\c"你好' + r(9)
ret, err = io.put(policy.token(), key, data)
assert err is None
assert ret['key'] == key
def test_put_unicode1():
key = "test_%s" % r(9) + '你好'
data = key
ret, err = io.put(policy.token(), key, data, extra)
assert err is None
assert ret[u'key'].endswith(u'你好')
def test_put_unicode2():
key = "test_%s" % r(9) + '你好'
data = key
data = data.decode('utf8')
ret, err = io.put(policy.token(), key, data)
assert err is None
assert ret[u'key'].endswith(u'你好')
def test_put_unicode3():
key = "test_%s" % r(9) + '你好'
data = key
key = key.decode('utf8')
ret, err = io.put(policy.token(), key, data)
assert err is None
assert ret[u'key'].endswith(u'你好')
def test_put_unicode4():
key = "test_%s" % r(9) + '你好'
data = key
key = key.decode('utf8')
data = data.decode('utf8')
ret, err = io.put(policy.token(), key, data)
assert err is None
assert ret[u'key'].endswith(u'你好')
def test_put_StringIO():
key = "test_%s" % r(9)
data = cStringIO.StringIO('hello buddy!')
ret, err = io.put(policy.token(), key, data)
assert err is None
assert ret['key'] == key
def test_put_urlopen():
key = "test_%s" % r(9)
data = urllib.urlopen('http://cheneya.qiniudn.com/hello_jpg')
ret, err = io.put(policy.token(), key, data)
assert err is None
assert ret['key'] == key
def test_put_no_length():
class test_reader(object):
def __init__(self):
self.data = 'abc'
self.pos = 0
def read(self, n=None):
if n is None or n < 0:
newpos = len(self.data)
else:
newpos = min(self.pos+n, len(self.data))
r = self.data[self.pos: newpos]
self.pos = newpos
return r
key = "test_%s" % r(9)
data = test_reader()
extra.check_crc = 2
extra.crc32 = binascii.crc32('abc') & 0xFFFFFFFF
ret, err = io.put(policy.token(), key, data, extra)
assert err is None
assert ret['key'] == key
test_put()
test_put_same_crc()
test_put_no_key()
test_put_quote_key()
test_put_unicode1()
test_put_unicode2()
test_put_unicode3()
test_put_unicode4()
test_put_StringIO()
test_put_urlopen()
test_put_no_length()
def test_put_file(self):
localfile = "%s" % __file__
key = "test_%s" % r(9)
extra.check_crc = 1
ret, err = io.put_file(policy.token(), key, localfile, extra)
assert err is None
assert ret['key'] == key
def test_put_crc_fail(self):
key = "test_%s" % r(9)
data = "hello bubby!"
extra.check_crc = 2
extra.crc32 = "wrong crc32"
ret, err = io.put(policy.token(), key, data, extra)
assert err is not None
class Test_get_file_crc32(unittest.TestCase):
def test_get_file_crc32(self):
file_path = '%s' % __file__
data = None
with open(file_path) as f:
data = f.read()
io._BLOCK_SIZE = 4
assert binascii.crc32(data) & 0xFFFFFFFF == io._get_file_crc32(file_path)
if __name__ == "__main__":
unittest.main()