-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathFileTest.cs
More file actions
157 lines (139 loc) · 5.41 KB
/
FileTest.cs
File metadata and controls
157 lines (139 loc) · 5.41 KB
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
using NUnit.Framework;
using System;
using System.Text;
using System.Threading.Tasks;
using LeanCloud;
using LeanCloud.Storage;
using System.Security.Cryptography;
using System.IO;
using LeanCloud.Common;
namespace Storage.Test {
public class FileTest : BaseTest {
static readonly string AvatarFilePath = "../../../../../assets/hello.png";
static readonly string APKFilePath = "../../../../../assets/test.apk";
static readonly string VideoFilePath = "../../../../../assets/video.mp4";
static readonly string US_APP_ID = "ldCRr8t23k3ydo7FxmJlKQmn-MdYXbMMI";
static readonly string US_APP_KEY = "GwQDHkmsQTSF2ZXWegzXio5F";
static readonly string US_SERVER = "https://ldcrr8t2.api.lncldglobal.com";
private LCFile video;
[Test]
[Order(0)]
public async Task SaveFromPath() {
LCFile avatar = new LCFile("avatar.png", AvatarFilePath);
await avatar.Save((count, total) => {
TestContext.WriteLine($"progress: {count}/{total}");
});
TestContext.WriteLine(avatar.ObjectId);
Assert.NotNull(avatar.ObjectId);
}
[Test]
[Order(1)]
public async Task SaveBigFileFromPath() {
video = new LCFile("video.mp4", VideoFilePath);
await video.Save((count, total) => {
TestContext.WriteLine($"progress: {count}/{total}");
});
TestContext.WriteLine(video.ObjectId);
Assert.NotNull(video.ObjectId);
}
[Test]
[Order(2)]
public async Task QueryFile() {
LCQuery<LCFile> query = LCFile.GetQuery();
LCFile file = await query.Get(video.ObjectId);
Assert.NotNull(file.Url);
TestContext.WriteLine(file.Url);
TestContext.WriteLine(file.GetThumbnailUrl(32, 32));
}
[Test]
[Order(3)]
public async Task SaveFromMemory() {
string text = "hello, world";
byte[] data = Encoding.UTF8.GetBytes(text);
LCFile file = new LCFile("text.txt", data);
file.PathPrefix = "gamesaves";
await file.Save();
TestContext.WriteLine(file.ObjectId);
Assert.NotNull(file.ObjectId);
Assert.True(file.Url.Contains("gamesaves"));
}
[Test]
[Order(4)]
public async Task SaveFromUrl() {
LCFile file = new LCFile("scene.jpg", new Uri("http://img95.699pic.com/photo/50015/9034.jpg_wh300.jpg"));
file.AddMetaData("size", 1024);
file.AddMetaData("width", 128);
file.AddMetaData("height", 256);
file.MimeType = "image/jpg";
await file.Save();
TestContext.WriteLine(file.ObjectId);
Assert.NotNull(file.ObjectId);
}
[Test]
[Order(5)]
public async Task Qiniu() {
LCFile file = new LCFile("test.apk", APKFilePath);
await file.Save();
TestContext.WriteLine(file.ObjectId);
Assert.NotNull(file.ObjectId);
}
[Test]
[Order(6)]
public async Task FileACL() {
LCUser user = await LCUser.LoginAnonymously();
LCFile file = new LCFile("avatar.png", AvatarFilePath);
LCACL acl = new LCACL();
acl.SetUserReadAccess(user, true);
file.ACL = acl;
await file.Save();
LCQuery<LCFile> query = LCFile.GetQuery();
LCFile avatar = await query.Get(file.ObjectId);
Assert.NotNull(avatar.ObjectId);
await LCUser.LoginAnonymously();
try {
LCFile forbiddenAvatar = await query.Get(file.ObjectId);
} catch (LCException e) {
Assert.AreEqual(e.Code, 403);
}
}
[Test]
[Order(10)]
public async Task AWS() {
LCApplication.Initialize(US_APP_ID, US_APP_KEY);
LCFile file = new LCFile("avatar.png", AvatarFilePath);
await file.Save((count, total) => {
TestContext.WriteLine($"progress: {count}/{total}");
});
TestContext.WriteLine(file.ObjectId);
Assert.NotNull(file.ObjectId);
}
[Test]
[Order(11)]
public async Task AWSBigFile() {
LCApplication.Initialize(US_APP_ID, US_APP_KEY, US_SERVER);
LCFile file = new LCFile("video.mp4", VideoFilePath);
await file.Save((count, total) => {
TestContext.WriteLine($"progress: {count}/{total}");
});
TestContext.WriteLine(file.ObjectId);
Assert.NotNull(file.ObjectId);
}
[Test]
[Order(12)]
public async Task CheckSum() {
LCFile avatar = new LCFile("avatar.png", AvatarFilePath);
await avatar.Save();
LCQuery<LCFile> query = new LCQuery<LCFile>(LCFile.CLASS_NAME);
LCFile file = await query.Get(avatar.ObjectId);
string sum = file.MetaData["_checksum"] as string;
using (MD5 md5 = MD5.Create()) {
using (FileStream fs = new FileStream(AvatarFilePath, FileMode.Open)) {
fs.Position = 0;
byte[] bytes = md5.ComputeHash(fs);
string hash = LCUtils.ToHex(bytes);
Assert.AreEqual(sum, hash);
}
}
}
}
}