commit 5a4db9725c4d5dd098083c90db453aa0b55f490a
parent b64fb1cd9e9d23f3622ba121f4aec795782ad0c2
Author: lash <dev@holbrook.no>
Date: Fri, 26 Apr 2024 20:54:14 +0100
IMplement test for transport pack
Diffstat:
5 files changed, 112 insertions(+), 31 deletions(-)
diff --git a/src/chunk.h b/src/chunk.h
@@ -8,7 +8,7 @@
#endif
struct kee_chunk_t {
- char *data;
+ char data[KEE_TRANSPORT_CHUNK_MAX_SIZE];
size_t data_len;
size_t crsr;
short cardinality;
diff --git a/src/tests/Makefile b/src/tests/Makefile
@@ -24,6 +24,7 @@ test_run:
./test_ledger
./test_dn
./test_sign
+ ./test_transport
test: all test_run
diff --git a/src/tests/transport.c b/src/tests/transport.c
@@ -0,0 +1,73 @@
+#include <string.h>
+
+#include "transport.h"
+
+
+int test_raw() {
+ int r;
+ size_t c;
+ struct kee_transport_t h;
+ char out[1024];
+
+ r = kee_transport_single(&h, KEE_TRANSPORT_RAW, 1, 1024);
+ if (r) {
+ return 1;
+ }
+ r = kee_transport_write(&h, "foo", 4);
+ if (r) {
+ return 1;
+ }
+ c = 1024;
+ r = kee_transport_next(&h, out, &c);
+ if (r) {
+ return 1;
+ }
+ if (out[0] != 1) {
+ return 1;
+ }
+ if (strcmp("foo", ((char*)out)+1)) {
+ return 1;
+ }
+
+ return 0;
+}
+
+int test_pack() {
+ int r;
+ size_t c;
+ struct kee_transport_t h;
+ char out[1024];
+
+ r = kee_transport_single(&h, KEE_TRANSPORT_BASE64, 1, 1024);
+ if (r) {
+ return 1;
+ }
+ r = kee_transport_write(&h, "foo", 4);
+ if (r) {
+ return 1;
+ }
+ c = 1024;
+ r = kee_transport_next(&h, out, &c);
+ if (r) {
+ return 1;
+ }
+ if (strcmp("eNpjTMvPZwAAA80BRg==", (char*)out)) {
+ return 1;
+ }
+
+ return 0;
+}
+
+int main() {
+ int r;
+
+ r = test_raw();
+ if (r) {
+ return 1;
+ }
+ r = test_pack();
+ if (r) {
+ return 1;
+ }
+
+}
diff --git a/src/transport.c b/src/transport.c
@@ -115,6 +115,10 @@ int pack(char *in, size_t in_len, char *out, size_t *out_len) {
free(buf);
return ERR_FAIL;
}
+ if (*(out+*out_len-1) == 0x0a) {
+ *(out+*out_len-1) = 0;
+ (*out_len)--;
+ }
free(buf);
return ERR_OK;
@@ -143,70 +147,68 @@ int unpack(char *in, size_t in_len, char *out, size_t *out_len) {
}
/// \todo implement checksum
-int kee_transport_single(struct kee_transport_t *trans, char cmd, size_t data_len) {
+int kee_transport_single(struct kee_transport_t *trans, enum kee_transport_mode_e mode, char cmd, size_t data_len) {
- memset(trans, 0, sizeof(struct kee_transport_header_t));
+ memset(trans, 0, sizeof(struct kee_transport_t));
if (cmd >= KEE_N_CMD) {
return ERR_INVALID_CMD;
}
- trans->chunk.data_len(in_len + 1);
- trans->chunk.data = malloc(trans->chunk.data_len);
- trans->cmd = trans->chunk.data;
+ trans->chunker.data_len = data_len + 1;
+ trans->cmd = (char*)trans->chunker.data;
+ trans->mode = mode;
+ *(trans->cmd) = cmd;
+ trans->chunker.crsr = 1;
return ERR_OK;
}
int kee_transport_write(struct kee_transport_t *trans, const char *in, size_t in_len) {
- if (trans.state) {
+ if (trans->state) {
return ERR_FAIL;
}
- memcpy(trans->chunk.data + trans->chunk.crsr, in, in_len);
- trans->chunk.crsr += in_len;
+ memcpy(trans->chunker.data + trans->chunker.crsr, in, in_len);
+ trans->chunker.crsr += in_len;
return ERR_OK;
}
/// \todo consider pass validation function
/// \todo implement chunking
-int kee_transport_next(struct kee_transport_t *trans, enum kee_transport_mode_e mode, char *out, size_t *out_len) {
+int kee_transport_next(struct kee_transport_t *trans, char *out, size_t *out_len) {
int r;
size_t l;
+ size_t remaining;
- if (trans.state) {
- if (trans.state > 1) {
+ if (trans->state) {
+ if (trans->state > 1) {
return ERR_FAIL;
}
- else {
- trans->chunk.data_len = trans->chunk.crsr;
- trans->chunk.crsr = 0;
+ } else {
+ trans->chunker.data_len = trans->chunker.crsr;
+ trans->chunker.crsr = 0;
trans->state = 1;
}
- if (trans->remaining < KEE_TRANSPORT_CHUNK_SIZE - 1) {
- l = trans_remaining;
+ remaining = trans->chunker.data_len - trans->chunker.crsr;
+ if (remaining < KEE_TRANSPORT_CHUNK_MAX_SIZE - 1) {
+ l = remaining;
} else {
return 1; // unimplemented
//l = KEE_TRANSPORT_CHUNK_SIZE;
}
- *(trans->buf) = trans->cmd;
- memcpy(trans->buf + 1, trans->data, l - 1);
- switch (mode) {
+ switch (trans->mode) {
case KEE_TRANSPORT_BASE64:
- r = pack_encode(trans->data, trans->data_len + 1, out, out_len);
+ r = pack(trans->chunker.data, l, out, out_len);
if (r) {
- retrun ERR_FAIL;
+ return ERR_FAIL;
}
break;
case KEE_TRANSPORT_RAW:
- memcpy(out, trans->data, trans->data_len + 1);
+ memcpy(out, trans->chunker.data, l);
break;
default:
- retrun ERR_FAIL;
+ return ERR_FAIL;
}
return 0;
}
-
-void kee_transport_header_free() {
- free(trans->buf);
-}
diff --git a/src/transport.h b/src/transport.h
@@ -3,10 +3,12 @@
#include <stddef.h>
+#include "chunk.h"
+
enum kee_transport_mode_e {
KEE_TRANSPORT_RAW,
KEE_TRANSPORT_BASE64,
-}
+};
enum kee_cmd_e { // max number 31
KEE_CMD_ID = 0,
@@ -20,7 +22,8 @@ enum kee_cmd_e { // max number 31
#define KEE_CMD_SIGN_REQUEST 64
#define KEE_CMD_CHUNKED 128
-struct kee_transport_header_t {
+struct kee_transport_t {
+ enum kee_transport_mode_e mode;
char *cmd;
struct kee_chunk_t chunker;
short checksum;
@@ -53,6 +56,8 @@ int pack(char *in, size_t in_len, char *out, size_t *out_len);
*/
int unpack(char *in, size_t in_len, char *out, size_t *out_len);
-int kee_transport_single(enum kee_cmd_e cmd, int total);
+int kee_transport_single(struct kee_transport_t *trans, enum kee_transport_mode_e mode, char cmd, size_t data_len);
+int kee_transport_write(struct kee_transport_t *trans, const char *in, size_t in_len);
+int kee_transport_next(struct kee_transport_t *trans, char *out, size_t *out_len);
#endif // _KEE_TRANSPORT_H