blob: 4f3a035a312c1141233f3c7205272a809c841528 [file] [log] [blame]
root0d8b11f2025-05-15 14:10:43 +00001package cheattest;
2
Raver5ba779f2025-05-14 12:48:12 +00003import java.util.ArrayList;
4import java.util.Arrays;
5import java.util.Collection;
6import java.util.Date;
7import java.util.HashMap;
8import java.util.List;
9import java.util.Map;
10import java.util.UUID;
11import java.util.stream.Collectors;
12import java.util.stream.IntStream;
13
14import javax.persistence.EntityManager;
15import javax.persistence.EntityManagerFactory;
16import javax.persistence.Persistence;
17
18import org.apache.commons.lang3.tuple.Pair;
19import org.junit.jupiter.api.AfterAll;
20import org.junit.jupiter.api.Assertions;
21import org.junit.jupiter.api.BeforeAll;
22import org.junit.jupiter.api.DynamicTest;
23import org.junit.jupiter.api.TestFactory;
24
25import cheat.Cheat;
26import entity.Appeal;
27import entity.Seed;
28import entity.User;
29import entity.UserPT;
30import entity.config;
31
root0d8b11f2025-05-15 14:10:43 +000032public class cheatsystest {
Raver5ba779f2025-05-14 12:48:12 +000033 private static EntityManagerFactory emf;
34 private static EntityManager em;
35 private static Cheat cheat;
36 private static List<String> insertedAppealIds = new ArrayList<>();
37 private static List<String> insertedSeedIds = new ArrayList<>(); // 用于记录插入的假种子 ID
38
39 @BeforeAll
40 static void setup() throws Exception {
41 // 加载 MySQL 驱动
42 Class.forName("com.mysql.cj.jdbc.Driver");
43 Map<String, Object> props = new HashMap<>();
44 config cfg = new config();
45 String jdbcUrl = String.format(
46 "jdbc:mysql://%s/%s?useSSL=false&serverTimezone=UTC",
47 cfg.SqlURL, cfg.TestDatabase);
48 props.put("javax.persistence.jdbc.url", jdbcUrl);
49 props.put("javax.persistence.jdbc.user", cfg.SqlUsername);
50 props.put("javax.persistence.jdbc.password", cfg.SqlPassword);
51 props.put("javax.persistence.jdbc.driver", "com.mysql.cj.jdbc.Driver");
52 emf = Persistence.createEntityManagerFactory("myPersistenceUnit", props);
53 em = emf.createEntityManager();
54 cheat = new Cheat();
55 // 通过反射注入 entityManager
56 java.lang.reflect.Field f = Cheat.class.getDeclaredField("entityManager");
57 f.setAccessible(true);
58 f.set(cheat, em);
59 }
60
61 @AfterAll
62 static void teardown() {
63 // 清理测试过程中插入的 Appeal 数据
64 if (em != null && em.isOpen()) {
65 em.getTransaction().begin();
66 for (String appealId : insertedAppealIds) {
67 Appeal appeal = em.find(Appeal.class, appealId);
68 if (appeal != null) {
69 em.remove(appeal);
70 }
71 }
72 em.getTransaction().commit();
73 }
74
75 // 清理测试过程中插入的 Seed 数据
76 if (em != null && em.isOpen()) {
77 em.getTransaction().begin();
78 for (String seedId : insertedSeedIds) {
79 Seed seed = em.find(Seed.class, seedId);
80 if (seed != null) {
81 em.remove(seed);
82 }
83 }
84 em.getTransaction().commit();
85 }
86
87 // 清理测试过程中插入的 User 数据
88 if (em != null && em.isOpen()) {
89 em.getTransaction().begin();
90 List<String> insertedUserIds = em.createQuery("SELECT u.userid FROM User u WHERE u.accountstate = true", String.class)
91 .getResultList();
92 for (String userId : insertedUserIds) {
93 User user = em.find(User.class, userId);
94 if (user != null) {
95 em.remove(user);
96 }
97 }
98 em.getTransaction().commit();
99 }
100
101 // 关闭 EntityManager 和 EntityManagerFactory
102 if (em != null && em.isOpen()) em.close();
103 if (emf != null && emf.isOpen()) emf.close();
104 }
105
106 @TestFactory
107 Collection<DynamicTest> testAddAppeal() {
108 // 查询数据库中已存在的一个用户
109 String userId = em.createQuery("SELECT u.userid FROM User u", String.class)
110 .setMaxResults(1)
111 .getSingleResult();
112
113 return IntStream.range(0, 10)
114 .mapToObj(i -> DynamicTest.dynamicTest("AddAppeal test #" + i, () -> {
115 // 插入 Appeal
116 String appealId = UUID.randomUUID().toString();
117 Appeal appeal = new Appeal();
118 appeal.appealid = appealId;
119 appeal.appealuserid = userId;
120 appeal.content = "Test appeal content " + i;
121 appeal.fileURL = "http://example.com/file" + i;
122 appeal.status = 0;
123
124 em.getTransaction().begin();
125 boolean result = cheat.AddAppeal(appeal);
126 em.getTransaction().commit();
127
128 Assertions.assertTrue(result, "AddAppeal 应返回 true");
129
130 // 验证 Appeal 是否插入
131 Appeal fetched = em.find(Appeal.class, appealId);
132 Assertions.assertNotNull(fetched, "数据库应能查到新插入的 Appeal");
133 Assertions.assertEquals(userId, fetched.appealuserid);
134 Assertions.assertEquals("Test appeal content " + i, fetched.content);
135
136 // 清理
137 em.getTransaction().begin();
138 em.remove(fetched);
139 em.getTransaction().commit();
140 }))
141 .collect(Collectors.toList());
142 }
143
144 @TestFactory
145 Collection<DynamicTest> testGetAppeal() {
146 // 查询数据库中已存在的10个Appeal
147 List<Appeal> appeals = em.createQuery("SELECT a FROM Appeal a", Appeal.class)
148 .setMaxResults(10)
149 .getResultList();
150
151 List<DynamicTest> tests = new ArrayList<>();
152
153 // 前面用已有的Appeal测试能查到
154 for (int i = 0; i < Math.min(7, appeals.size()); i++) {
155 Appeal appeal = appeals.get(i);
156 tests.add(DynamicTest.dynamicTest("GetAppeal found test #" + i, () -> {
157 Appeal fetched = cheat.GetAppeal(appeal.appealid);
158 Assertions.assertNotNull(fetched, "GetAppeal 应返回非空");
159 Assertions.assertEquals(appeal.appealid, fetched.appealid);
160 Assertions.assertEquals(appeal.appealuserid, fetched.appealuserid);
161 Assertions.assertEquals(appeal.content, fetched.content);
162 }));
163 }
164
165 // 后3个用随机UUID测试查不到
166 for (int i = 7; i < 10; i++) {
167 String notExistId = UUID.randomUUID().toString();
168 tests.add(DynamicTest.dynamicTest("GetAppeal not found test #" + i, () -> {
169 Appeal fetched = cheat.GetAppeal(notExistId);
170 Assertions.assertNull(fetched, "GetAppeal 查询不存在的id应返回null");
171 }));
172 }
173
174 return tests;
175 }
176
177 @TestFactory
178 Collection<DynamicTest> testGetAppealList() {
179 // 查询数据库中已存在的一个用户
180 String userId = em.createQuery("SELECT u.userid FROM User u", String.class)
181 .setMaxResults(1)
182 .getSingleResult();
183 // 用于记录测试过程中插入的 Appeal ID
Raver5ba779f2025-05-14 12:48:12 +0000184
185 return IntStream.range(0, 10)
186 .mapToObj(i -> DynamicTest.dynamicTest("GetAppealList test #" + i, () -> {
187 // 插入一个新的 Appeal
188 String appealId = UUID.randomUUID().toString();
189 Appeal appeal = new Appeal();
190 appeal.appealid = appealId;
191 appeal.appealuserid = userId;
192 appeal.content = "GetAppealList test content " + i;
193 appeal.fileURL = "http://example.com/file" + i;
194 appeal.status = 0;
195
196 em.getTransaction().begin();
197 em.persist(appeal);
198 em.getTransaction().commit();
199
200 // 记录插入的 Appeal ID
201 insertedAppealIds.add(appealId);
202
203 // 调用 GetAppealList 并验证
204 Appeal[] appeals = cheat.GetAppealList();
205 Assertions.assertNotNull(appeals, "GetAppealList 应返回非空");
206
207 // 检查整个列表是否包含所有插入的数据
208 for (String id : insertedAppealIds) {
209 boolean found = Arrays.stream(appeals)
210 .anyMatch(a -> a.appealid.equals(id));
211 Assertions.assertTrue(found, "GetAppealList 应包含插入的 Appeal ID: " + id);
212 }
213 }))
214 .collect(Collectors.toList());
215 }
216
217 @TestFactory
218 Collection<DynamicTest> testHandleAppeal() {
219 // 查询数据库中已存在的一个用户
220 String userId = em.createQuery("SELECT u.userid FROM User u", String.class)
221 .setMaxResults(1)
222 .getSingleResult();
223
224 return IntStream.range(0, 10)
225 .mapToObj(i -> DynamicTest.dynamicTest("HandleAppeal test #" + i, () -> {
226 // 插入一个新的 Appeal
227 String appealId = UUID.randomUUID().toString();
228 Appeal appeal = new Appeal();
229 appeal.appealid = appealId;
230 appeal.appealuserid = userId;
231 appeal.content = "HandleAppeal test content " + i;
232 appeal.fileURL = "http://example.com/file" + i;
233 appeal.status = 0;
234
235 em.getTransaction().begin();
236 em.persist(appeal);
237 em.getTransaction().commit();
238
239 // 如果 newStatus 为 1,先将用户的 account_status 设置为 true
240 if (i % 2 == 0) { // 偶数索引对应 newStatus = 1
241 em.getTransaction().begin();
242 User user = em.find(User.class, userId);
243 Assertions.assertNotNull(user, "数据库应能查到相关用户");
244 user.accountstate = true; // 设置为 true
245 em.getTransaction().commit();
246 }
247
248 // 测试处理申诉
249 int newStatus = (i % 2 == 0) ? 1 : 2; // 偶数索引通过申诉,奇数索引拒绝申诉
250 boolean result = cheat.HandleAppeal(appealId, newStatus);
251 Assertions.assertTrue(result, "HandleAppeal 应返回 true");
252
253 // 验证 Appeal 状态是否更新
254 Appeal updatedAppeal = em.find(Appeal.class, appealId);
255 Assertions.assertNotNull(updatedAppeal, "数据库应能查到更新后的 Appeal");
256 Assertions.assertEquals(newStatus, updatedAppeal.status, "Appeal 的状态应被更新为: " + newStatus);
257
258 // 如果申诉通过,验证用户的 account_status 是否被设置为 false
259 if (newStatus == 1) {
260 User user = em.find(User.class, userId);
261 Assertions.assertNotNull(user, "数据库应能查到相关用户");
262 Assertions.assertFalse(user.accountstate, "通过申诉后用户的 account_status 应为 false");
263 }
264
265 // 清理测试数据
266 em.getTransaction().begin();
267 em.remove(updatedAppeal);
268 em.getTransaction().commit();
269 }))
270 .collect(Collectors.toList());
271 }
272
273 @TestFactory
274 Collection<DynamicTest> testGetFakeSeed() {
275 return IntStream.range(0, 10)
276 .mapToObj(i -> DynamicTest.dynamicTest("GetFakeSeed test #" + i, () -> {
277 // 插入一个新的假种子
278 String seedId = UUID.randomUUID().toString();
279 String userId = em.createQuery("SELECT u.userid FROM User u", String.class)
280 .setMaxResults(1)
281 .getSingleResult();
282
283 Seed seed = new Seed();
284 seed.seedid = seedId;
285 seed.seeduserid = userId;
286 seed.faketime = 100 + i; // 设置为大于 config.FakeTime 的值
287 seed.lastfakecheck = new Date();
288 seed.outurl = "http://example.com/fake" + i;
289 seed.title = "Fake Seed " + i;
290 seed.subtitle = "Subtitle " + i;
291 seed.seedsize = "100MB";
292 seed.seedtag = "test,fake";
293 seed.downloadtimes = 0;
294 seed.url = "http://example.com/seed" + i;
295
296 em.getTransaction().begin();
297 em.persist(seed);
298 em.getTransaction().commit();
299
300 // 记录插入的 Seed ID
301 insertedSeedIds.add(seedId);
302
303 // 调用 GetFakeSeed 并验证
304 Pair<String, String>[] fakeSeeds = cheat.GetFakeSeed();
305 Assertions.assertNotNull(fakeSeeds, "GetFakeSeed 应返回非空");
306
307 // 验证返回的假种子列表是否包含所有插入的假种子
308 for (String id : insertedSeedIds) {
309 boolean found = Arrays.stream(fakeSeeds)
310 .anyMatch(pair -> pair.getLeft().equals(id));
311 Assertions.assertTrue(found, "GetFakeSeed 应包含插入的假种子 ID: " + id);
312 }
313 }))
314 .collect(Collectors.toList());
315 }
316
317 @TestFactory
318 Collection<DynamicTest> testGetPunishedUserList() {
319 List<String> insertedUserIds = new ArrayList<>(); // 用于记录插入的用户 ID
320
321 return IntStream.range(0, 10)
322 .mapToObj(i -> DynamicTest.dynamicTest("GetPunishedUserList test #" + i, () -> {
323 // 插入一个新的用户,accountstate 设置为 true
324 String userId = UUID.randomUUID().toString();
325 User user = new User();
326 user.userid = userId;
327 user.email = "test" + i + "@example.com";
328 user.username = "TestUser" + i;
329 user.password = "password" + i;
330 user.sex = "M";
331 user.detectedCount = 0;
332 user.lastDetectedTime = new Date();
333 user.school = "TestSchool";
334 user.pictureurl = "http://example.com/avatar" + i;
335 user.profile = "Test profile " + i;
336 user.accountstate = true; // 设置为 true
337 user.invitetimes = 5;
338
339 // 创建并设置 UserPT 实例
340 UserPT userPT = new UserPT();
341 userPT.user = user; // 关联 User 实例
342 user.userPT = userPT;
343
344 em.getTransaction().begin();
345 em.persist(user);
346 em.persist(userPT); // 持久化 UserPT 实例
347 em.getTransaction().commit();
348
349 // 记录插入的用户 ID
350 insertedUserIds.add(userId);
351
352 // 调用 GetPunishedUserList 并验证
353 String[] punishedUsers = cheat.GetPunishedUserList();
354 Assertions.assertNotNull(punishedUsers, "GetPunishedUserList 应返回非空");
355
356 // 验证返回的用户列表是否包含所有插入的用户
357 for (String id : insertedUserIds) {
358 boolean found = Arrays.stream(punishedUsers).anyMatch(returnedId -> returnedId.equals(id));
359 Assertions.assertTrue(found, "GetPunishedUserList 应包含插入的用户 ID: " + id);
360 }
361 }))
362 .collect(Collectors.toList());
363 }
364
365 @TestFactory
366 Collection<DynamicTest> testPunishUser() {
367 List<String> insertedUserIds = new ArrayList<>(); // 用于记录插入的用户 ID
368
369 return IntStream.range(0, 10)
370 .mapToObj(i -> DynamicTest.dynamicTest("PunishUser test #" + i, () -> {
371 // 配置参数
372 int cheatTime = config.CheatTime;
373 int fakeTime = config.FakeTime;
374
375 // 插入用户 1(作弊用户)
376 String userId1 = UUID.randomUUID().toString();
377 User user1 = new User();
378 user1.userid = userId1;
379 user1.email = "cheater" + i + "_" + UUID.randomUUID().toString() + "@example.com"; // 确保唯一性
380 user1.username = "Cheater" + i;
381 user1.password = "password" + i;
382 user1.sex = "M";
383 user1.detectedCount = cheatTime + 1; // detectedCount 超过 cheatTime
384 user1.fakeDetectedCount = 0;
385 user1.lastDetectedTime = new Date();
386 user1.fakeLastDetectedTime = new Date();
387 user1.accountstate = false; // 初始状态为未封禁
388 user1.invitetimes = 5;
389
390 // 创建并设置 UserPT 实例
391 UserPT userPT1 = new UserPT();
392 userPT1.user = user1; // 关联 User 实例
393 user1.userPT = userPT1;
394
395 // 插入用户 2(非作弊用户)
396 String userId2 = UUID.randomUUID().toString();
397 User user2 = new User();
398 user2.userid = userId2;
399 user2.email = "normal" + i + "_" + UUID.randomUUID().toString() + "@example.com"; // 确保唯一性
400 user2.username = "NormalUser" + i;
401 user2.password = "password" + i;
402 user2.sex = "F";
403 user2.detectedCount = 0;
404 user2.fakeDetectedCount = fakeTime - 1; // fakeDetectedCount 未超过 fakeTime
405 user2.lastDetectedTime = new Date();
406 user2.fakeLastDetectedTime = new Date();
407 user2.accountstate = false; // 初始状态为未封禁
408 user2.invitetimes = 5;
409
410 // 创建并设置 UserPT 实例
411 UserPT userPT2 = new UserPT();
412 userPT2.user = user2; // 关联 User 实例
413 user2.userPT = userPT2;
414
415 em.getTransaction().begin();
416 em.persist(user1);
417 em.persist(userPT1); // 持久化 UserPT 实例
418 em.persist(user2);
419 em.persist(userPT2); // 持久化 UserPT 实例
420 em.getTransaction().commit();
421
422 // 记录插入的用户 ID
423 insertedUserIds.add(userId1);
424 insertedUserIds.add(userId2);
425
426 // 调用 PunishUser 方法
427 cheat.PunishUser();
428
429 // 验证用户 1 是否被封禁
430 User punishedUser1 = em.find(User.class, userId1);
431 Assertions.assertNotNull(punishedUser1, "数据库应能查到用户 1");
432 Assertions.assertTrue(punishedUser1.accountstate, "作弊用户应被封禁");
433
434 // 验证用户 2 是否未被封禁
435 User punishedUser2 = em.find(User.class, userId2);
436 Assertions.assertNotNull(punishedUser2, "数据库应能查到用户 2");
437 Assertions.assertFalse(punishedUser2.accountstate, "非作弊用户不应被封禁");
438 }))
439 .collect(Collectors.toList());
440 }
441
442 // @TestFactory
443 // Collection<DynamicTest> testDetectTrans() {
444 // List<String> insertedUserIds = new ArrayList<>(); // 用于记录插入的用户 ID
445 // List<String> insertedSeedIds = new ArrayList<>(); // 用于记录插入的种子 ID
446 // List<String> insertedTransIds = new ArrayList<>(); // 用于记录插入的传输记录 ID
447
448 // return IntStream.range(0, 10)
449 // .mapToObj(i -> DynamicTest.dynamicTest("DetectTrans test #" + i, () -> {
450 // // 插入上传用户
451 // String uploaderId = UUID.randomUUID().toString();
452 // User uploader = new User();
453 // uploader.userid = uploaderId;
454 // uploader.email = "uploader" + i + "_" + UUID.randomUUID().toString() + "@example.com"; // 确保唯一性
455 // uploader.username = "Uploader" + i;
456 // uploader.password = "password" + i;
457 // uploader.sex = "M";
458 // uploader.detectedCount = 0; // 初始 detectedCount 为 0
459 // uploader.fakeDetectedCount = 0;
460 // uploader.lastDetectedTime = new Date();
461 // uploader.fakeLastDetectedTime = new Date();
462 // uploader.accountstate = false;
463 // uploader.invitetimes = 5;
464
465 // // 创建并设置 UserPT 实例
466 // UserPT uploaderPT = new UserPT();
467 // uploaderPT.user = uploader; // 关联 User 实例
468 // uploader.userPT = uploaderPT;
469
470 // // 插入下载用户
471 // String downloaderId = UUID.randomUUID().toString();
472 // User downloader = new User();
473 // downloader.userid = downloaderId;
474 // downloader.email = "downloader" + i + "_" + UUID.randomUUID().toString() + "@example.com"; // 确保唯一性
475 // downloader.username = "Downloader" + i;
476 // downloader.password = "password" + i;
477 // downloader.sex = "F";
478 // downloader.detectedCount = 0;
479 // downloader.fakeDetectedCount = 0;
480 // downloader.lastDetectedTime = new Date();
481 // downloader.fakeLastDetectedTime = new Date();
482 // downloader.accountstate = false;
483 // downloader.invitetimes = 5;
484
485 // // 创建并设置 UserPT 实例
486 // UserPT downloaderPT = new UserPT();
487 // downloaderPT.user = downloader; // 关联 User 实例
488 // downloader.userPT = downloaderPT;
489
490 // em.getTransaction().begin();
491 // em.persist(uploader);
492 // em.persist(uploaderPT); // 持久化 UserPT 实例
493 // em.persist(downloader);
494 // em.persist(downloaderPT); // 持久化 UserPT 实例
495 // em.getTransaction().commit();
496 // insertedUserIds.add(uploaderId);
497 // insertedUserIds.add(downloaderId);
498
499 // // 插入种子
500 // String seedId = UUID.randomUUID().toString();
501 // Seed seed = new Seed();
502 // seed.seedid = seedId;
503 // seed.seeduserid = uploaderId;
504 // seed.faketime = 0;
505 // seed.lastfakecheck = new Date();
506 // seed.outurl = "http://example.com/seed" + i;
507 // seed.title = "Seed " + i;
508 // seed.subtitle = "Subtitle " + i;
509 // seed.seedsize = "100MB";
510 // seed.seedtag = "test";
511 // seed.downloadtimes = 0;
512 // seed.url = "http://example.com/seed" + i;
513
514 // em.getTransaction().begin();
515 // em.persist(seed);
516 // em.getTransaction().commit();
517 // insertedSeedIds.add(seedId);
518
519 // // 插入正常和异常的传输记录
520 // List<TransRecord> transRecords = new ArrayList<>();
521 // for (int j = 0; j < 15; j++) {
522 // TransRecord transRecord = new TransRecord();
523 // transRecord.taskid = UUID.randomUUID().toString();
524 // transRecord.uploaduserid = uploaderId;
525 // transRecord.downloaduserid = downloaderId; // 确保 downloader_id 存在于 User 表中
526 // transRecord.seedid = seedId; // 确保 seed_id 存在于 Seed 表中
527 // transRecord.upload = (j < 13) ? 100 : 1000; // 前 3 条为正常数据,后 2 条为异常数据
528 // transRecord.download = (j < 13) ? 90 : 10; // 异常数据的上传量远大于下载量
529 // transRecord.maxupload = 200;
530 // transRecord.maxdownload = 200;
531
532 // em.getTransaction().begin();
533 // em.persist(transRecord);
534 // em.getTransaction().commit();
535 // insertedTransIds.add(transRecord.taskid);
536 // transRecords.add(transRecord);
537 // }
538
539 // // 调用 DetectTrans 方法
540 // cheat.DetectTrans();
541
542 // // 验证下载用户的 detectedCount 是否增加
543 // User detectedDownloader = em.find(User.class, downloaderId);
544 // Assertions.assertNotNull(detectedDownloader, "数据库应能查到上传用户");
545 // Assertions.assertEquals(2, detectedDownloader.detectedCount, "上传用户的 detectedCount 应增加 2(对应 2 条异常数据)");
546 // }))
547 // .collect(Collectors.toList());
548 // }
root0d8b11f2025-05-15 14:10:43 +0000549}