summaryrefslogtreecommitdiffstats
path: root/test/auth.c
blob: 40e2678a2640e6dec7528c2aa7d45bda56df6bb7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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;
}