diff options
Diffstat (limited to 'test')
| -rw-r--r-- | test/auth.c | 144 |
1 files changed, 144 insertions, 0 deletions
diff --git a/test/auth.c b/test/auth.c new file mode 100644 index 0000000..40e2678 --- /dev/null +++ b/test/auth.c @@ -0,0 +1,144 @@ +#include <assert.h> +#include <stdio.h> +#include <transport.h> +#include <auth.h> +#include <auth.h> + +#include "api.h" + +struct ExpectedRequest { + bool ignore; + char *url; + char *token; + char *reqBody; + + struct SizedBlock retBody; + enum ReturnCode rc; +}; + +struct ExpectedRequest *nextResult = NULL; + +enum ReturnCode transport_request(char *url, char *token, char *reqBody, struct SizedBlock *retBody) { + assert(nextResult != NULL); + assert(strcmp(nextResult->url, url) == 0); + if(nextResult->reqBody == NULL) { + assert(nextResult->reqBody == NULL); + } else { + assert(reqBody != NULL); + assert(strcmp(nextResult->reqBody, reqBody) == 0); + } + + if(nextResult->token == NULL) { + assert(nextResult->token == NULL); + } else { + assert(token != NULL); + assert(strcmp(nextResult->token, token) == 0); + } + + + memcpy(retBody, &nextResult->retBody, sizeof(struct SizedBlock)); + + enum ReturnCode rc = nextResult->rc; + nextResult++; + return rc; +} + +#define STATIC_SIZED_BLOCK(str) {.size = sizeof(str), .memory = strdup(str)} + +int main() { + nextResult = (struct ExpectedRequest[]) { + { + .url = "https://webapp-prod.cloud.remarkable.engineering/token/json/2/device/new", + .reqBody = "{\"code\": \"123456\",\"DeviceDesc\": \"librm\",\"DeviceId\": \"5E35708B-59B6-4715-A5D8-576C741D8276\"}", + .retBody = STATIC_SIZED_BLOCK("SomeDeviceToken"), + .rc = RC_SUCCESS, + }, + { + .url = "https://webapp-prod.cloud.remarkable.engineering/token/json/2/user/new", + .token = "SomeDeviceToken", + .reqBody = NULL, + .retBody = STATIC_SIZED_BLOCK("SomeUserToken"), + .rc = RC_SUCCESS, + }, + { + .url = "https://internal.cloud.remarkable.com/sync/v4/root", + .token = "SomeUserToken", + .reqBody = NULL, + .retBody = STATIC_SIZED_BLOCK("{\"hash\": \"0000000000000000000000000000000000000000000000000000000000000000\", \"generation\": 0}"), + .rc = RC_SUCCESS, + }, + { + .url = "https://internal.cloud.remarkable.com/sync/v4/files/0000000000000000000000000000000000000000000000000000000000000000", + .token = "SomeUserToken", + .reqBody = NULL, + .retBody = STATIC_SIZED_BLOCK( + "3\n" + "0000000000000000000000000000000000000000000000000000000000000001:CollectionType:someId:1:0\n" + ), + .rc = RC_SUCCESS, + }, + { + .url = "https://internal.cloud.remarkable.com/sync/v4/files/0000000000000000000000000000000000000000000000000000000000000001", + .token = "SomeUserToken", + .reqBody = NULL, + .retBody = STATIC_SIZED_BLOCK( + "3\n" + "0000000000000000000000000000000000000000000000000000000000000002:DocumentType:someFileId:0:16\n" + ), + .rc = RC_SUCCESS, + }, + }; + + { + enum ReturnCode rc; + + char *code = "123456"; + + struct RMDevice device = { + .id = "5E35708B-59B6-4715-A5D8-576C741D8276" + }; + rc = rm_device_open(&device, code); + assert(rc == RC_SUCCESS); + + struct RMDir dir = {}; + rc = rm_dir_root(&device, &dir); + assert(rc == RC_SUCCESS); + + struct RMDEntry dentry = {}; + { + rc = rm_dir_read(&dir, &dentry); + assert(rc == RC_NEXT); + assert(memcmp(dentry.hash, "0000000000000000000000000000000000000000000000000000000000000001", sizeof(dentry.hash)) == 0); + assert(strncmp(dentry.type, "CollectionType", 14) == 0); + assert(strncmp(dentry.id, "someId", 14) == 0); + assert(dentry.n == 1); + assert(dentry.size == 0); + + rc = rm_dir_read(&dir, NULL); + assert(rc == RC_SUCCESS); + } + + { + struct RMDir nestedDir = {}; + rm_dir_open(&dentry, &nestedDir); + assert(rc == RC_SUCCESS); + + rc = rm_dir_read(&nestedDir, &dentry); + assert(rc == RC_NEXT); + assert(memcmp(dentry.hash, "0000000000000000000000000000000000000000000000000000000000000002", sizeof(dentry.hash)) == 0); + assert(strncmp(dentry.type, "DocumentType", 12) == 0); + assert(strncmp(dentry.id, "someFileId", 10) == 0); + assert(dentry.n == 0); + assert(dentry.size == 16); + + rm_dir_close(&nestedDir); + } + + rm_dir_close(&dir); + rm_device_close(&device); + + // @COMPLETE We currently don't check that all the requests fired + } + + return 0; +} |
