|
| 1 | +/* |
| 2 | + * Copyright (c) 2015, Parse, LLC. All rights reserved. |
| 3 | + * |
| 4 | + * You are hereby granted a non-exclusive, worldwide, royalty-free license to use, |
| 5 | + * copy, modify, and distribute this software in source code or binary form for use |
| 6 | + * in connection with the web services and APIs provided by Parse. |
| 7 | + * |
| 8 | + * As with any software that integrates with the Parse platform, your use of |
| 9 | + * this software is subject to the Parse Terms of Service |
| 10 | + * [https://www.parse.com/about/terms]. This copyright notice shall be |
| 11 | + * included in all copies or substantial portions of the software. |
| 12 | + * |
| 13 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
| 15 | + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 16 | + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 17 | + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 18 | + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 19 | + * |
| 20 | + */ |
| 21 | + |
| 22 | +#include <stdio.h> |
| 23 | +#include <stdlib.h> |
| 24 | +#include <string.h> |
| 25 | +#include <sys/select.h> |
| 26 | +#include <parse.h> |
| 27 | +#include <time.h> |
| 28 | + |
| 29 | +#include "simplejson.h" |
| 30 | + |
| 31 | +int totalTests = 0; |
| 32 | +int successfullTests = 0; |
| 33 | +int failedTests = 0; |
| 34 | +unsigned long run; |
| 35 | + |
| 36 | +void logSummary() { |
| 37 | + printf("Tests run: %4i\n", totalTests); |
| 38 | + printf("Tests succeded: %4i\n", successfullTests); |
| 39 | + printf("Tests failed: %4i\n", failedTests); |
| 40 | +} |
| 41 | + |
| 42 | +void logResults(int success, int abort, const char* test, const char* error) { |
| 43 | + totalTests++; |
| 44 | + if (success) { |
| 45 | + successfullTests++; |
| 46 | + printf("[SUCCESS] %s.\n", test); |
| 47 | + } else { |
| 48 | + failedTests++; |
| 49 | + printf("[FAILURE] %s: %s\n", test, error); |
| 50 | + if (abort) { |
| 51 | + logSummary(); |
| 52 | + exit (-1); |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +int isHex(char c) { |
| 58 | + if (c >= 'a' && c <= 'z') return 1; |
| 59 | + if (c >= '0' && c <= '9') return 1; |
| 60 | + return 0; |
| 61 | +} |
| 62 | + |
| 63 | +int verifyInstallationId(const char* id) { |
| 64 | + int i; |
| 65 | + if (strlen(id) != 36) return 0; |
| 66 | + for (i = 0 ; i < 36; i++) { |
| 67 | + if (i == 8 || i == 13 || i == 18 || i == 23) { |
| 68 | + if (id[i] != '-') return 0; |
| 69 | + } else { |
| 70 | + if (!isHex(id[i])) return 0; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + return 1; |
| 75 | +} |
| 76 | + |
| 77 | +char* cachedRunResult = NULL; |
| 78 | + |
| 79 | +static void clearCachedResults() { |
| 80 | + if (cachedRunResult) free (cachedRunResult); |
| 81 | + cachedRunResult = NULL; |
| 82 | +} |
| 83 | + |
| 84 | +static void callback(ParseClient client, int error, int httpStatus, const char* httpResponseBody) { |
| 85 | + if (error == 0) { |
| 86 | + cachedRunResult = strdup(httpResponseBody); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +int pushCounter = 0; |
| 91 | + |
| 92 | +static void pushCallback(ParseClient client, int error, const char *buffer) { |
| 93 | + if (error == 0 && buffer != NULL) { |
| 94 | + char data[64] = {0}; |
| 95 | + char id[16] = {0}; |
| 96 | + char orig[16] = {0}; |
| 97 | + snprintf(orig, sizeof(orig), "%ld", run); |
| 98 | + if (simpleJsonProcessor(buffer, "data", data, sizeof(data))) { |
| 99 | + if (simpleJsonProcessor(data, "id", id, sizeof(id))) { |
| 100 | + if (strncmp(id, orig, sizeof(id)) == 0) { |
| 101 | + pushCounter++; |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +int main(int argc, char *argv[]) { |
| 109 | + |
| 110 | + // CREATE UNIQUE IDENTIFIER FOR THE RUN |
| 111 | + |
| 112 | + run = time(NULL); |
| 113 | + char classPathOne[128] = {0}; |
| 114 | + char classPathTwo[128] = {0}; |
| 115 | + char classPathThree[128] = {0}; |
| 116 | + char objectId[11] = {0}; |
| 117 | + char path[256] = {0}; |
| 118 | + |
| 119 | + snprintf(classPathOne, sizeof(classPathOne), "/1/classes/TestObjectOne%ld", run); |
| 120 | + |
| 121 | + |
| 122 | + // TEST INITIALIZATION |
| 123 | + ParseClient client = parseInitialize(YOUR_APP_IP, YOUR_CLIENT_KEY); |
| 124 | + logResults(client != NULL, 1, "parseInitialize call", "failed to start parse"); |
| 125 | + |
| 126 | + |
| 127 | + // TEST GENERATION OF INSTALLATION ID |
| 128 | + |
| 129 | + const char* id = parseGetInstallationId(client); |
| 130 | + logResults(id == NULL, 0, "parseGetInstallationId call", "remove .parse-embedded from home directory"); |
| 131 | + |
| 132 | + parseSendRequest(client, "GET", "/1/classes/testObjectFake/1111111", NULL, NULL); |
| 133 | + |
| 134 | + id = parseGetInstallationId(client); |
| 135 | + logResults(id != NULL, 1, "parseGetInstallationId call", "did not create the installation id properly"); |
| 136 | + |
| 137 | + logResults(verifyInstallationId(id), 0, "installation id generated correctly", "installation id is not correct"); |
| 138 | + |
| 139 | + // TEST CREATING AND FETCHING OBJECTS ON/FROM THE SERVER |
| 140 | + |
| 141 | + clearCachedResults(); |
| 142 | + |
| 143 | + parseSendRequest(client, "POST", classPathOne, "{\"test\":\"object1\", \"value\":1}", callback); |
| 144 | + logResults(cachedRunResult != NULL, 0, "create test object 1", "creating object failed"); |
| 145 | + memset(objectId, 0, sizeof(objectId)); |
| 146 | + logResults(simpleJsonProcessor(cachedRunResult, "objectId", objectId, sizeof(objectId)), 0, "object 1 created", "could not create an object"); |
| 147 | + |
| 148 | + clearCachedResults(); |
| 149 | + |
| 150 | + memset(path, 0, sizeof(path)); |
| 151 | + snprintf(path, sizeof(path), "%s/%s", classPathOne, objectId); |
| 152 | + parseSendRequest(client, "GET", classPathOne, NULL, callback); |
| 153 | + logResults(cachedRunResult != NULL, 0, "fetch test object 1", "fetching object failed"); |
| 154 | + |
| 155 | + clearCachedResults(); |
| 156 | + |
| 157 | + parseSendRequest(client, "POST", classPathOne, "{\"test\":\"object1\", \"value\":2}", callback); |
| 158 | + logResults(cachedRunResult != NULL, 0, "create test object 2", "creating object failed"); |
| 159 | + memset(objectId, 0, sizeof(objectId)); |
| 160 | + logResults(simpleJsonProcessor(cachedRunResult, "objectId", objectId, sizeof(objectId)), 0, "object 2 created", "could not create an object"); |
| 161 | + |
| 162 | + clearCachedResults(); |
| 163 | + |
| 164 | + memset(path, 0, sizeof(path)); |
| 165 | + snprintf(path, sizeof(path), "%s/%s", classPathOne, objectId); |
| 166 | + parseSendRequest(client, "GET", classPathOne, NULL, callback); |
| 167 | + logResults(cachedRunResult != NULL, 0, "fetch test object 2", "fetching object failed"); |
| 168 | + |
| 169 | + clearCachedResults(); |
| 170 | + |
| 171 | + parseSendRequest(client, "POST", classPathOne, "{\"test\":\"object1\", \"value\":3}", callback); |
| 172 | + logResults(cachedRunResult != NULL, 0, "create test object 3", "creating object failed"); |
| 173 | + char objectIdKeepAround[11] = {0}; |
| 174 | + logResults(simpleJsonProcessor(cachedRunResult, "objectId", objectIdKeepAround, sizeof(objectIdKeepAround)), 0, "object 3 created", "could not create an object"); |
| 175 | + |
| 176 | + clearCachedResults(); |
| 177 | + |
| 178 | + memset(path, 0, sizeof(path)); |
| 179 | + snprintf(path, sizeof(path), "%s/%s", classPathOne, objectIdKeepAround); |
| 180 | + parseSendRequest(client, "GET", classPathOne, NULL, callback); |
| 181 | + logResults(cachedRunResult != NULL, 0, "fetch test object 3", "fetching object failed"); |
| 182 | + |
| 183 | + clearCachedResults(); |
| 184 | + |
| 185 | + parseSendRequest(client, "POST", classPathOne, "{\"test\":\"object1\", \"value\":2}", callback); |
| 186 | + logResults(cachedRunResult != NULL, 0, "create test object 4", "creating object failed"); |
| 187 | + memset(objectId, 0, sizeof(objectId)); |
| 188 | + logResults(simpleJsonProcessor(cachedRunResult, "objectId", objectId, sizeof(objectId)), 0, "object 4 created", "could not create an object"); |
| 189 | + |
| 190 | + clearCachedResults(); |
| 191 | + |
| 192 | + memset(path, 0, sizeof(path)); |
| 193 | + snprintf(path, sizeof(path), "%s/%s", classPathOne, objectId); |
| 194 | + parseSendRequest(client, "GET", classPathOne, NULL, callback); |
| 195 | + logResults(cachedRunResult != NULL, 0, "fetch test object 4", "fetching object failed"); |
| 196 | + |
| 197 | + // TEST QUERIES |
| 198 | + |
| 199 | + clearCachedResults(); |
| 200 | + |
| 201 | + parseSendRequest(client, "GET", classPathOne, "where={\"value\":2}", callback); |
| 202 | + logResults(cachedRunResult != NULL, 0, "query objects", "querying objects failed"); |
| 203 | + |
| 204 | + const char* results = "{\"results\":[{\""; |
| 205 | + int cmp = !strncmp(cachedRunResult, results, strlen(results)); |
| 206 | + char* location1 = strstr(cachedRunResult, "objectId"); |
| 207 | + char* location2 = strstr(location1 + 1, "objectId"); |
| 208 | + char* location3 = strstr(location2 + 1, "objectId"); |
| 209 | + logResults(cmp && location1 && location2 && !location3, 0, "query value", "query results not valid"); |
| 210 | + |
| 211 | + |
| 212 | + // TEST OBJECT MODIFICATION |
| 213 | + |
| 214 | + clearCachedResults(); |
| 215 | + |
| 216 | + memset(path, 0, sizeof(path)); |
| 217 | + snprintf(path, sizeof(path), "%s/%s", classPathOne, objectIdKeepAround); |
| 218 | + |
| 219 | + parseSendRequest(client, "PUT", path, "{\"test\":\"object1\", \"value\":2}", callback); |
| 220 | + logResults(cachedRunResult != NULL, 0, "modify test object 3", "modifying object failed"); |
| 221 | + |
| 222 | + clearCachedResults(); |
| 223 | + |
| 224 | + parseSendRequest(client, "GET", classPathOne, "where={\"value\":2}", callback); |
| 225 | + logResults(cachedRunResult != NULL, 0, "query objects after modification", "querying objects failed"); |
| 226 | + |
| 227 | + results = "{\"results\":[{\""; |
| 228 | + cmp = !strncmp(cachedRunResult, results, strlen(results)); |
| 229 | + location1 = strstr(cachedRunResult, "objectId"); |
| 230 | + location2 = strstr(location1 + 1, "objectId"); |
| 231 | + location3 = strstr(location2 + 1, "objectId"); |
| 232 | + logResults(cmp && location1 && location2 && location3, 0, "query value after modifying an object", "query results not valid"); |
| 233 | + |
| 234 | + // TEST DELETING AN OBJECT |
| 235 | + |
| 236 | + clearCachedResults(); |
| 237 | + |
| 238 | + memset(path, 0, sizeof(path)); |
| 239 | + snprintf(path, sizeof(path), "%s/%s", classPathOne, objectIdKeepAround); |
| 240 | + |
| 241 | + parseSendRequest(client, "DELETE", path, NULL, callback); |
| 242 | + logResults(cachedRunResult != NULL, 0, "delete test object 3", "deleting object failed"); |
| 243 | + |
| 244 | + clearCachedResults(); |
| 245 | + |
| 246 | + parseSendRequest(client, "GET", classPathOne, "where={\"value\":2}", callback); |
| 247 | + logResults(cachedRunResult != NULL, 0, "query objects after delete", "querying objects failed"); |
| 248 | + |
| 249 | + results = "{\"results\":[{\""; |
| 250 | + cmp = !strncmp(cachedRunResult, results, strlen(results)); |
| 251 | + location1 = strstr(cachedRunResult, "objectId"); |
| 252 | + location2 = strstr(location1 + 1, "objectId"); |
| 253 | + location3 = strstr(location2 + 1, "objectId"); |
| 254 | + logResults(cmp && location1 && location2 && !location3, 0, "query value after deleting an object", "query results not valid"); |
| 255 | + |
| 256 | + // TEST PUSH |
| 257 | + |
| 258 | + parseSetPushCallback(client, pushCallback); |
| 259 | + parseStartPushService(client); |
| 260 | + int socket = parseGetPushSocket(client); |
| 261 | + |
| 262 | + int loopCount = 20; |
| 263 | + |
| 264 | + |
| 265 | + printf("[!!!!!!!] Run ./push.sh %ld\n", run); |
| 266 | + |
| 267 | + while(loopCount--) { |
| 268 | + struct timeval tv; |
| 269 | + fd_set receive, send, error; |
| 270 | + |
| 271 | + tv.tv_sec = 10; |
| 272 | + tv.tv_usec= 0; |
| 273 | + FD_ZERO(&receive); |
| 274 | + FD_ZERO(&send); |
| 275 | + FD_ZERO(&error); |
| 276 | + FD_SET(socket, &error); |
| 277 | + FD_SET(socket, &receive); |
| 278 | + select(socket + 1, &receive, &send, &error, &tv); |
| 279 | + |
| 280 | + parseProcessNextPushNotification(client); |
| 281 | + |
| 282 | + if (pushCounter == 10) loopCount = loopCount > 2 ? 2 : loopCount; |
| 283 | + } |
| 284 | + |
| 285 | + logResults(pushCounter == 10, 0, "receive push notifications", "did not receive the push notifications correctly"); |
| 286 | + |
| 287 | + logSummary(); |
| 288 | + return 0; |
| 289 | +} |
0 commit comments