1 /**
2 * @file
3 */
4
5 /*
6 Copyright (C) 2002-2013 UFO: Alien Invasion.
7
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
17 See the GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22
23 */
24
25 #include "test_webapi.h"
26 #include "test_shared.h"
27 #include "../client/web/web_main.h"
28 #include "../client/web/web_cgame.h"
29
30 #define FILEPATH "save/campaign/"
31 #define FILE "unittest1.savx"
32 #define FILENAME FILEPATH FILE
33 #define CGAME "test"
34 #define CATEGORY 0
35
36 static const char* user;
37 static const char* password;
38
39 /**
40 * The suite initialization function.
41 * Returns zero on success, non-zero otherwise.
42 */
InitSuite(void)43 static int InitSuite (void)
44 {
45 TEST_Init();
46
47 Com_ParseScripts(true);
48
49 WEB_InitStartup();
50
51 user = TEST_GetStringProperty("webapi-user");
52 if (user == nullptr)
53 user = "";
54
55 password = TEST_GetStringProperty("webapi-password");
56 if (password == nullptr)
57 password = "";
58
59 return 0;
60 }
61
62 /**
63 * The suite cleanup function.
64 * Returns zero on success, non-zero otherwise.
65 */
CleanSuite(void)66 static int CleanSuite (void)
67 {
68 TEST_Shutdown();
69 return 0;
70 }
71
testAuth(void)72 static void testAuth (void)
73 {
74 if (Q_strnull(user) || Q_strnull(password))
75 return;
76 CU_ASSERT_TRUE(WEB_Auth(user, password));
77 }
78
testFileManagement(void)79 static void testFileManagement (void)
80 {
81 if (Q_strnull(user) || Q_strnull(password))
82 return;
83
84 /* we can only upload files from within our user directory - so let's copy it there */
85 if (!FS_FileExists("%s/" FILENAME, FS_Gamedir())) {
86 byte* buf;
87 const int size = FS_LoadFile(FILENAME, &buf);
88 CU_ASSERT_TRUE_FATAL(size > 0);
89 CU_ASSERT_PTR_NOT_NULL_FATAL(buf);
90 FS_WriteFile(buf, size, FILENAME);
91 FS_FreeFile(buf);
92 }
93 CU_ASSERT_TRUE_FATAL(WEB_CGameUpload(CGAME, CATEGORY, FILENAME));
94 CU_ASSERT_TRUE_FATAL(WEB_CGameDownloadFromUser(CGAME, CATEGORY, FILE, web_userid->integer));
95 const int countAfterUpload = WEB_CGameListForUser(CGAME, CATEGORY, web_userid->integer);
96 CU_ASSERT_TRUE_FATAL(countAfterUpload != -1);
97 CU_ASSERT_TRUE_FATAL(countAfterUpload >= 1);
98 CU_ASSERT_TRUE(WEB_CGameDelete(CGAME, CATEGORY, FILE));
99 const int countAfterDelete = WEB_CGameListForUser(CGAME, CATEGORY, web_userid->integer);
100 CU_ASSERT_TRUE_FATAL(countAfterDelete != -1);
101 CU_ASSERT_EQUAL_FATAL(countAfterDelete, countAfterUpload - 1);
102 }
103
UFO_AddWebAPITests(void)104 int UFO_AddWebAPITests (void)
105 {
106 /* add a suite to the registry */
107 CU_pSuite DBufferSuite = CU_add_suite("WebAPITests", InitSuite, CleanSuite);
108 if (DBufferSuite == nullptr)
109 return CU_get_error();
110
111 /* add the tests to the suite */
112 if (CU_ADD_TEST(DBufferSuite, testAuth) == nullptr)
113 return CU_get_error();
114
115 if (CU_ADD_TEST(DBufferSuite, testFileManagement) == nullptr)
116 return CU_get_error();
117
118 return CUE_SUCCESS;
119 }
120