diff options
Diffstat (limited to 'tests/unit/test_stratum_client.c')
| -rw-r--r-- | tests/unit/test_stratum_client.c | 125 |
1 files changed, 125 insertions, 0 deletions
diff --git a/tests/unit/test_stratum_client.c b/tests/unit/test_stratum_client.c new file mode 100644 index 0000000..302fcba --- /dev/null +++ b/tests/unit/test_stratum_client.c | |||
| @@ -0,0 +1,125 @@ | |||
| 1 | #include "test_framework.h" | ||
| 2 | #include "../../components/tollgate_core/src/tollgate_core_stratum_client.h" | ||
| 3 | #include <cjson/cJSON.h> | ||
| 4 | #include <string.h> | ||
| 5 | #include <stdio.h> | ||
| 6 | #include <stdlib.h> | ||
| 7 | |||
| 8 | int main(void) | ||
| 9 | { | ||
| 10 | printf("=== test_stratum_client ===\n"); | ||
| 11 | |||
| 12 | printf("\n--- hex_to_bytes ---\n"); | ||
| 13 | { | ||
| 14 | const char *hex = "0a1b2c3d"; | ||
| 15 | uint8_t out[4] = {0}; | ||
| 16 | tollgate_core_stratum_hex_to_bytes(hex, out, 4); | ||
| 17 | ASSERT_EQ_INT(0x0a, (int)out[0], "byte 0"); | ||
| 18 | ASSERT_EQ_INT(0x1b, (int)out[1], "byte 1"); | ||
| 19 | ASSERT_EQ_INT(0x2c, (int)out[2], "byte 2"); | ||
| 20 | ASSERT_EQ_INT(0x3d, (int)out[3], "byte 3"); | ||
| 21 | } | ||
| 22 | |||
| 23 | printf("\n--- hex_to_bytes zeros ---\n"); | ||
| 24 | { | ||
| 25 | const char *hex = "00000000"; | ||
| 26 | uint8_t out[4] = {0xff, 0xff, 0xff, 0xff}; | ||
| 27 | tollgate_core_stratum_hex_to_bytes(hex, out, 4); | ||
| 28 | ASSERT_EQ_INT(0, (int)out[0], "zero byte 0"); | ||
| 29 | ASSERT_EQ_INT(0, (int)out[1], "zero byte 1"); | ||
| 30 | } | ||
| 31 | |||
| 32 | printf("\n--- parse_notify valid ---\n"); | ||
| 33 | { | ||
| 34 | cJSON *params = cJSON_Parse("[\"123\",\"0000000000000000000000000000000000000000000000000000000000000000\"," | ||
| 35 | "\"\"" | ||
| 36 | ",\"\"" | ||
| 37 | ",\"\"" | ||
| 38 | ",\"20000000\"" | ||
| 39 | ",\"1705e3c4\"" | ||
| 40 | ",\"65a3b2c1\"]"); | ||
| 41 | tollgate_stratum_job_t job = {0}; | ||
| 42 | uint32_t nbits = 0; | ||
| 43 | bool ok = tollgate_core_stratum_parse_notify(params, &job, &nbits); | ||
| 44 | ASSERT(ok, "parse_notify returns true"); | ||
| 45 | ASSERT_EQ_INT(123, (int)job.job_id, "job_id parsed"); | ||
| 46 | ASSERT(job.valid, "job is valid"); | ||
| 47 | ASSERT_EQ_INT(0x20000000, (int)job.version, "version parsed"); | ||
| 48 | ASSERT_EQ_INT(0x1705e3c4, (int)job.nbits, "nbits parsed"); | ||
| 49 | ASSERT_EQ_INT(0x65a3b2c1, (int)job.ntime, "ntime parsed"); | ||
| 50 | ASSERT_EQ_INT(0x1705e3c4, (int)nbits, "out_nbits set"); | ||
| 51 | ASSERT_EQ_INT(32, job.target_len, "target_len is 32"); | ||
| 52 | cJSON_Delete(params); | ||
| 53 | } | ||
| 54 | |||
| 55 | printf("\n--- parse_notify too few params ---\n"); | ||
| 56 | { | ||
| 57 | cJSON *params = cJSON_Parse("[\"123\",\"abcd\"]"); | ||
| 58 | tollgate_stratum_job_t job = {0}; | ||
| 59 | bool ok = tollgate_core_stratum_parse_notify(params, &job, NULL); | ||
| 60 | ASSERT(!ok, "parse_notify returns false for insufficient params"); | ||
| 61 | cJSON_Delete(params); | ||
| 62 | } | ||
| 63 | |||
| 64 | printf("\n--- parse_notify NULL ---\n"); | ||
| 65 | { | ||
| 66 | tollgate_stratum_job_t job = {0}; | ||
| 67 | bool ok = tollgate_core_stratum_parse_notify(NULL, &job, NULL); | ||
| 68 | ASSERT(!ok, "parse_notify returns false for NULL"); | ||
| 69 | } | ||
| 70 | |||
| 71 | printf("\n--- parse_difficulty valid ---\n"); | ||
| 72 | { | ||
| 73 | cJSON *params = cJSON_Parse("[512]"); | ||
| 74 | uint64_t diff = 0; | ||
| 75 | bool ok = tollgate_core_stratum_parse_difficulty(params, &diff); | ||
| 76 | ASSERT(ok, "parse_difficulty returns true"); | ||
| 77 | ASSERT_EQ_UINT64(512, (unsigned long long)diff, "difficulty parsed"); | ||
| 78 | cJSON_Delete(params); | ||
| 79 | } | ||
| 80 | |||
| 81 | printf("\n--- parse_difficulty invalid ---\n"); | ||
| 82 | { | ||
| 83 | cJSON *params = cJSON_Parse("[]"); | ||
| 84 | uint64_t diff = 0; | ||
| 85 | bool ok = tollgate_core_stratum_parse_difficulty(params, &diff); | ||
| 86 | ASSERT(!ok, "parse_difficulty returns false for empty array"); | ||
| 87 | cJSON_Delete(params); | ||
| 88 | } | ||
| 89 | |||
| 90 | printf("\n--- build_subscribe ---\n"); | ||
| 91 | { | ||
| 92 | char buf[256]; | ||
| 93 | int len = tollgate_core_stratum_build_subscribe(buf, sizeof(buf), 1); | ||
| 94 | ASSERT(len > 0, "build_subscribe returns positive length"); | ||
| 95 | ASSERT(strstr(buf, "mining.subscribe") != NULL, "contains method"); | ||
| 96 | ASSERT(strstr(buf, "\"id\":1") != NULL, "contains id"); | ||
| 97 | ASSERT(strstr(buf, "TollGate/1.0") != NULL, "contains user agent"); | ||
| 98 | ASSERT(buf[len - 1] == '\n', "ends with newline"); | ||
| 99 | } | ||
| 100 | |||
| 101 | printf("\n--- build_authorize ---\n"); | ||
| 102 | { | ||
| 103 | char buf[512]; | ||
| 104 | int len = tollgate_core_stratum_build_authorize(buf, sizeof(buf), 2, "user1", "pass1"); | ||
| 105 | ASSERT(len > 0, "build_authorize returns positive length"); | ||
| 106 | ASSERT(strstr(buf, "mining.authorize") != NULL, "contains method"); | ||
| 107 | ASSERT(strstr(buf, "user1") != NULL, "contains user"); | ||
| 108 | ASSERT(strstr(buf, "pass1") != NULL, "contains pass"); | ||
| 109 | } | ||
| 110 | |||
| 111 | printf("\n--- build_submit ---\n"); | ||
| 112 | { | ||
| 113 | char buf[512]; | ||
| 114 | int len = tollgate_core_stratum_build_submit(buf, sizeof(buf), 3, "worker1", | ||
| 115 | 100, 0x65a3b2c1, 0x12345678, 0x20000000); | ||
| 116 | ASSERT(len > 0, "build_submit returns positive length"); | ||
| 117 | ASSERT(strstr(buf, "mining.submit") != NULL, "contains method"); | ||
| 118 | ASSERT(strstr(buf, "worker1") != NULL, "contains worker"); | ||
| 119 | ASSERT(strstr(buf, "100") != NULL, "contains job_id"); | ||
| 120 | ASSERT(strstr(buf, "65a3b2c1") != NULL, "contains ntime"); | ||
| 121 | ASSERT(strstr(buf, "12345678") != NULL, "contains nonce"); | ||
| 122 | } | ||
| 123 | |||
| 124 | TEST_SUMMARY(); | ||
| 125 | } | ||