blob: 962593567baa06db3d8ea1bf9e5536da5961ee8e [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
184 // List<String> insertedAppealIds = new ArrayList<>();
185 List<String> insertedAppealIds = new ArrayList<>();
186
187 return IntStream.range(0, 10)
188 .mapToObj(i -> DynamicTest.dynamicTest("GetAppealList test #" + i, () -> {
189 // 插入一个新的 Appeal
190 String appealId = UUID.randomUUID().toString();
191 Appeal appeal = new Appeal();
192 appeal.appealid = appealId;
193 appeal.appealuserid = userId;
194 appeal.content = "GetAppealList test content " + i;
195 appeal.fileURL = "http://example.com/file" + i;
196 appeal.status = 0;
197
198 em.getTransaction().begin();
199 em.persist(appeal);
200 em.getTransaction().commit();
201
202 // 记录插入的 Appeal ID
203 insertedAppealIds.add(appealId);
204
205 // 调用 GetAppealList 并验证
206 Appeal[] appeals = cheat.GetAppealList();
207 Assertions.assertNotNull(appeals, "GetAppealList 应返回非空");
208
209 // 检查整个列表是否包含所有插入的数据
210 for (String id : insertedAppealIds) {
211 boolean found = Arrays.stream(appeals)
212 .anyMatch(a -> a.appealid.equals(id));
213 Assertions.assertTrue(found, "GetAppealList 应包含插入的 Appeal ID: " + id);
214 }
215 }))
216 .collect(Collectors.toList());
217 }
218
219 @TestFactory
220 Collection<DynamicTest> testHandleAppeal() {
221 // 查询数据库中已存在的一个用户
222 String userId = em.createQuery("SELECT u.userid FROM User u", String.class)
223 .setMaxResults(1)
224 .getSingleResult();
225
226 return IntStream.range(0, 10)
227 .mapToObj(i -> DynamicTest.dynamicTest("HandleAppeal test #" + i, () -> {
228 // 插入一个新的 Appeal
229 String appealId = UUID.randomUUID().toString();
230 Appeal appeal = new Appeal();
231 appeal.appealid = appealId;
232 appeal.appealuserid = userId;
233 appeal.content = "HandleAppeal test content " + i;
234 appeal.fileURL = "http://example.com/file" + i;
235 appeal.status = 0;
236
237 em.getTransaction().begin();
238 em.persist(appeal);
239 em.getTransaction().commit();
240
241 // 如果 newStatus 为 1,先将用户的 account_status 设置为 true
242 if (i % 2 == 0) { // 偶数索引对应 newStatus = 1
243 em.getTransaction().begin();
244 User user = em.find(User.class, userId);
245 Assertions.assertNotNull(user, "数据库应能查到相关用户");
246 user.accountstate = true; // 设置为 true
247 em.getTransaction().commit();
248 }
249
250 // 测试处理申诉
251 int newStatus = (i % 2 == 0) ? 1 : 2; // 偶数索引通过申诉,奇数索引拒绝申诉
252 boolean result = cheat.HandleAppeal(appealId, newStatus);
253 Assertions.assertTrue(result, "HandleAppeal 应返回 true");
254
255 // 验证 Appeal 状态是否更新
256 Appeal updatedAppeal = em.find(Appeal.class, appealId);
257 Assertions.assertNotNull(updatedAppeal, "数据库应能查到更新后的 Appeal");
258 Assertions.assertEquals(newStatus, updatedAppeal.status, "Appeal 的状态应被更新为: " + newStatus);
259
260 // 如果申诉通过,验证用户的 account_status 是否被设置为 false
261 if (newStatus == 1) {
262 User user = em.find(User.class, userId);
263 Assertions.assertNotNull(user, "数据库应能查到相关用户");
264 Assertions.assertFalse(user.accountstate, "通过申诉后用户的 account_status 应为 false");
265 }
266
267 // 清理测试数据
268 em.getTransaction().begin();
269 em.remove(updatedAppeal);
270 em.getTransaction().commit();
271 }))
272 .collect(Collectors.toList());
273 }
274
275 @TestFactory
276 Collection<DynamicTest> testGetFakeSeed() {
277 return IntStream.range(0, 10)
278 .mapToObj(i -> DynamicTest.dynamicTest("GetFakeSeed test #" + i, () -> {
279 // 插入一个新的假种子
280 String seedId = UUID.randomUUID().toString();
281 String userId = em.createQuery("SELECT u.userid FROM User u", String.class)
282 .setMaxResults(1)
283 .getSingleResult();
284
285 Seed seed = new Seed();
286 seed.seedid = seedId;
287 seed.seeduserid = userId;
288 seed.faketime = 100 + i; // 设置为大于 config.FakeTime 的值
289 seed.lastfakecheck = new Date();
290 seed.outurl = "http://example.com/fake" + i;
291 seed.title = "Fake Seed " + i;
292 seed.subtitle = "Subtitle " + i;
293 seed.seedsize = "100MB";
294 seed.seedtag = "test,fake";
295 seed.downloadtimes = 0;
296 seed.url = "http://example.com/seed" + i;
297
298 em.getTransaction().begin();
299 em.persist(seed);
300 em.getTransaction().commit();
301
302 // 记录插入的 Seed ID
303 insertedSeedIds.add(seedId);
304
305 // 调用 GetFakeSeed 并验证
306 Pair<String, String>[] fakeSeeds = cheat.GetFakeSeed();
307 Assertions.assertNotNull(fakeSeeds, "GetFakeSeed 应返回非空");
308
309 // 验证返回的假种子列表是否包含所有插入的假种子
310 for (String id : insertedSeedIds) {
311 boolean found = Arrays.stream(fakeSeeds)
312 .anyMatch(pair -> pair.getLeft().equals(id));
313 Assertions.assertTrue(found, "GetFakeSeed 应包含插入的假种子 ID: " + id);
314 }
315 }))
316 .collect(Collectors.toList());
317 }
318
319 @TestFactory
320 Collection<DynamicTest> testGetPunishedUserList() {
321 List<String> insertedUserIds = new ArrayList<>(); // 用于记录插入的用户 ID
322
323 return IntStream.range(0, 10)
324 .mapToObj(i -> DynamicTest.dynamicTest("GetPunishedUserList test #" + i, () -> {
325 // 插入一个新的用户,accountstate 设置为 true
326 String userId = UUID.randomUUID().toString();
327 User user = new User();
328 user.userid = userId;
329 user.email = "test" + i + "@example.com";
330 user.username = "TestUser" + i;
331 user.password = "password" + i;
332 user.sex = "M";
333 user.detectedCount = 0;
334 user.lastDetectedTime = new Date();
335 user.school = "TestSchool";
336 user.pictureurl = "http://example.com/avatar" + i;
337 user.profile = "Test profile " + i;
338 user.accountstate = true; // 设置为 true
339 user.invitetimes = 5;
340
341 // 创建并设置 UserPT 实例
342 UserPT userPT = new UserPT();
343 userPT.user = user; // 关联 User 实例
344 user.userPT = userPT;
345
346 em.getTransaction().begin();
347 em.persist(user);
348 em.persist(userPT); // 持久化 UserPT 实例
349 em.getTransaction().commit();
350
351 // 记录插入的用户 ID
352 insertedUserIds.add(userId);
353
354 // 调用 GetPunishedUserList 并验证
355 String[] punishedUsers = cheat.GetPunishedUserList();
356 Assertions.assertNotNull(punishedUsers, "GetPunishedUserList 应返回非空");
357
358 // 验证返回的用户列表是否包含所有插入的用户
359 for (String id : insertedUserIds) {
360 boolean found = Arrays.stream(punishedUsers).anyMatch(returnedId -> returnedId.equals(id));
361 Assertions.assertTrue(found, "GetPunishedUserList 应包含插入的用户 ID: " + id);
362 }
363 }))
364 .collect(Collectors.toList());
365 }
366
367 @TestFactory
368 Collection<DynamicTest> testPunishUser() {
369 List<String> insertedUserIds = new ArrayList<>(); // 用于记录插入的用户 ID
370
371 return IntStream.range(0, 10)
372 .mapToObj(i -> DynamicTest.dynamicTest("PunishUser test #" + i, () -> {
373 // 配置参数
374 int cheatTime = config.CheatTime;
375 int fakeTime = config.FakeTime;
376
377 // 插入用户 1(作弊用户)
378 String userId1 = UUID.randomUUID().toString();
379 User user1 = new User();
380 user1.userid = userId1;
381 user1.email = "cheater" + i + "_" + UUID.randomUUID().toString() + "@example.com"; // 确保唯一性
382 user1.username = "Cheater" + i;
383 user1.password = "password" + i;
384 user1.sex = "M";
385 user1.detectedCount = cheatTime + 1; // detectedCount 超过 cheatTime
386 user1.fakeDetectedCount = 0;
387 user1.lastDetectedTime = new Date();
388 user1.fakeLastDetectedTime = new Date();
389 user1.accountstate = false; // 初始状态为未封禁
390 user1.invitetimes = 5;
391
392 // 创建并设置 UserPT 实例
393 UserPT userPT1 = new UserPT();
394 userPT1.user = user1; // 关联 User 实例
395 user1.userPT = userPT1;
396
397 // 插入用户 2(非作弊用户)
398 String userId2 = UUID.randomUUID().toString();
399 User user2 = new User();
400 user2.userid = userId2;
401 user2.email = "normal" + i + "_" + UUID.randomUUID().toString() + "@example.com"; // 确保唯一性
402 user2.username = "NormalUser" + i;
403 user2.password = "password" + i;
404 user2.sex = "F";
405 user2.detectedCount = 0;
406 user2.fakeDetectedCount = fakeTime - 1; // fakeDetectedCount 未超过 fakeTime
407 user2.lastDetectedTime = new Date();
408 user2.fakeLastDetectedTime = new Date();
409 user2.accountstate = false; // 初始状态为未封禁
410 user2.invitetimes = 5;
411
412 // 创建并设置 UserPT 实例
413 UserPT userPT2 = new UserPT();
414 userPT2.user = user2; // 关联 User 实例
415 user2.userPT = userPT2;
416
417 em.getTransaction().begin();
418 em.persist(user1);
419 em.persist(userPT1); // 持久化 UserPT 实例
420 em.persist(user2);
421 em.persist(userPT2); // 持久化 UserPT 实例
422 em.getTransaction().commit();
423
424 // 记录插入的用户 ID
425 insertedUserIds.add(userId1);
426 insertedUserIds.add(userId2);
427
428 // 调用 PunishUser 方法
429 cheat.PunishUser();
430
431 // 验证用户 1 是否被封禁
432 User punishedUser1 = em.find(User.class, userId1);
433 Assertions.assertNotNull(punishedUser1, "数据库应能查到用户 1");
434 Assertions.assertTrue(punishedUser1.accountstate, "作弊用户应被封禁");
435
436 // 验证用户 2 是否未被封禁
437 User punishedUser2 = em.find(User.class, userId2);
438 Assertions.assertNotNull(punishedUser2, "数据库应能查到用户 2");
439 Assertions.assertFalse(punishedUser2.accountstate, "非作弊用户不应被封禁");
440 }))
441 .collect(Collectors.toList());
442 }
443
444 // @TestFactory
445 // Collection<DynamicTest> testDetectTrans() {
446 // List<String> insertedUserIds = new ArrayList<>(); // 用于记录插入的用户 ID
447 // List<String> insertedSeedIds = new ArrayList<>(); // 用于记录插入的种子 ID
448 // List<String> insertedTransIds = new ArrayList<>(); // 用于记录插入的传输记录 ID
449
450 // return IntStream.range(0, 10)
451 // .mapToObj(i -> DynamicTest.dynamicTest("DetectTrans test #" + i, () -> {
452 // // 插入上传用户
453 // String uploaderId = UUID.randomUUID().toString();
454 // User uploader = new User();
455 // uploader.userid = uploaderId;
456 // uploader.email = "uploader" + i + "_" + UUID.randomUUID().toString() + "@example.com"; // 确保唯一性
457 // uploader.username = "Uploader" + i;
458 // uploader.password = "password" + i;
459 // uploader.sex = "M";
460 // uploader.detectedCount = 0; // 初始 detectedCount 为 0
461 // uploader.fakeDetectedCount = 0;
462 // uploader.lastDetectedTime = new Date();
463 // uploader.fakeLastDetectedTime = new Date();
464 // uploader.accountstate = false;
465 // uploader.invitetimes = 5;
466
467 // // 创建并设置 UserPT 实例
468 // UserPT uploaderPT = new UserPT();
469 // uploaderPT.user = uploader; // 关联 User 实例
470 // uploader.userPT = uploaderPT;
471
472 // // 插入下载用户
473 // String downloaderId = UUID.randomUUID().toString();
474 // User downloader = new User();
475 // downloader.userid = downloaderId;
476 // downloader.email = "downloader" + i + "_" + UUID.randomUUID().toString() + "@example.com"; // 确保唯一性
477 // downloader.username = "Downloader" + i;
478 // downloader.password = "password" + i;
479 // downloader.sex = "F";
480 // downloader.detectedCount = 0;
481 // downloader.fakeDetectedCount = 0;
482 // downloader.lastDetectedTime = new Date();
483 // downloader.fakeLastDetectedTime = new Date();
484 // downloader.accountstate = false;
485 // downloader.invitetimes = 5;
486
487 // // 创建并设置 UserPT 实例
488 // UserPT downloaderPT = new UserPT();
489 // downloaderPT.user = downloader; // 关联 User 实例
490 // downloader.userPT = downloaderPT;
491
492 // em.getTransaction().begin();
493 // em.persist(uploader);
494 // em.persist(uploaderPT); // 持久化 UserPT 实例
495 // em.persist(downloader);
496 // em.persist(downloaderPT); // 持久化 UserPT 实例
497 // em.getTransaction().commit();
498 // insertedUserIds.add(uploaderId);
499 // insertedUserIds.add(downloaderId);
500
501 // // 插入种子
502 // String seedId = UUID.randomUUID().toString();
503 // Seed seed = new Seed();
504 // seed.seedid = seedId;
505 // seed.seeduserid = uploaderId;
506 // seed.faketime = 0;
507 // seed.lastfakecheck = new Date();
508 // seed.outurl = "http://example.com/seed" + i;
509 // seed.title = "Seed " + i;
510 // seed.subtitle = "Subtitle " + i;
511 // seed.seedsize = "100MB";
512 // seed.seedtag = "test";
513 // seed.downloadtimes = 0;
514 // seed.url = "http://example.com/seed" + i;
515
516 // em.getTransaction().begin();
517 // em.persist(seed);
518 // em.getTransaction().commit();
519 // insertedSeedIds.add(seedId);
520
521 // // 插入正常和异常的传输记录
522 // List<TransRecord> transRecords = new ArrayList<>();
523 // for (int j = 0; j < 15; j++) {
524 // TransRecord transRecord = new TransRecord();
525 // transRecord.taskid = UUID.randomUUID().toString();
526 // transRecord.uploaduserid = uploaderId;
527 // transRecord.downloaduserid = downloaderId; // 确保 downloader_id 存在于 User 表中
528 // transRecord.seedid = seedId; // 确保 seed_id 存在于 Seed 表中
529 // transRecord.upload = (j < 13) ? 100 : 1000; // 前 3 条为正常数据,后 2 条为异常数据
530 // transRecord.download = (j < 13) ? 90 : 10; // 异常数据的上传量远大于下载量
531 // transRecord.maxupload = 200;
532 // transRecord.maxdownload = 200;
533
534 // em.getTransaction().begin();
535 // em.persist(transRecord);
536 // em.getTransaction().commit();
537 // insertedTransIds.add(transRecord.taskid);
538 // transRecords.add(transRecord);
539 // }
540
541 // // 调用 DetectTrans 方法
542 // cheat.DetectTrans();
543
544 // // 验证下载用户的 detectedCount 是否增加
545 // User detectedDownloader = em.find(User.class, downloaderId);
546 // Assertions.assertNotNull(detectedDownloader, "数据库应能查到上传用户");
547 // Assertions.assertEquals(2, detectedDownloader.detectedCount, "上传用户的 detectedCount 应增加 2(对应 2 条异常数据)");
548 // }))
549 // .collect(Collectors.toList());
550 // }
root0d8b11f2025-05-15 14:10:43 +0000551}