2016-11-30 16:46:41 +00:00
|
|
|
#include "ipfs/blocks/block.h"
|
|
|
|
|
|
|
|
int test_blocks_new() {
|
2016-12-05 22:23:58 +00:00
|
|
|
const unsigned char* input = (const unsigned char*)"Hello, World!";
|
2016-11-30 16:46:41 +00:00
|
|
|
int retVal = 0;
|
2017-07-27 17:05:41 +00:00
|
|
|
struct Block* block = ipfs_block_new();
|
2017-07-20 20:03:49 +00:00
|
|
|
if (block == NULL)
|
2016-11-30 16:46:41 +00:00
|
|
|
return 0;
|
|
|
|
|
2016-12-14 17:07:43 +00:00
|
|
|
retVal = ipfs_blocks_block_add_data(input, strlen((const char*)input) + 1, block);
|
|
|
|
if (retVal == 0) {
|
2017-07-24 22:58:39 +00:00
|
|
|
ipfs_block_free(block);
|
2016-12-14 17:07:43 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-11-30 16:46:41 +00:00
|
|
|
// now examine the block
|
2016-12-14 17:07:43 +00:00
|
|
|
if (strcmp((const char*)block->data, (const char*)input) != 0) {
|
2017-07-24 22:58:39 +00:00
|
|
|
ipfs_block_free(block);
|
2016-11-30 16:46:41 +00:00
|
|
|
return 0;
|
2016-12-14 17:07:43 +00:00
|
|
|
}
|
2016-11-30 16:46:41 +00:00
|
|
|
|
2016-12-14 17:07:43 +00:00
|
|
|
if (block->data_length != strlen((const char*)input) + 1) {
|
2017-07-24 22:58:39 +00:00
|
|
|
ipfs_block_free(block);
|
2016-11-30 16:46:41 +00:00
|
|
|
return 0;
|
2016-12-14 17:07:43 +00:00
|
|
|
}
|
2016-11-30 16:46:41 +00:00
|
|
|
|
2017-09-04 18:33:56 +00:00
|
|
|
if (block->cid->codec != CID_DAG_PROTOBUF) {
|
2017-07-24 22:58:39 +00:00
|
|
|
ipfs_block_free(block);
|
2016-11-30 16:46:41 +00:00
|
|
|
return 0;
|
2016-12-14 17:07:43 +00:00
|
|
|
}
|
2016-11-30 16:46:41 +00:00
|
|
|
|
2016-12-14 17:07:43 +00:00
|
|
|
if (block->cid->version != 0) {
|
2017-07-24 22:58:39 +00:00
|
|
|
ipfs_block_free(block);
|
2016-11-30 16:46:41 +00:00
|
|
|
return 0;
|
2016-12-14 17:07:43 +00:00
|
|
|
}
|
2016-11-30 16:46:41 +00:00
|
|
|
|
2016-12-14 17:07:43 +00:00
|
|
|
if (block->cid->hash_length != 32) {
|
2017-07-24 22:58:39 +00:00
|
|
|
ipfs_block_free(block);
|
2016-11-30 16:46:41 +00:00
|
|
|
return 0;
|
2016-12-14 17:07:43 +00:00
|
|
|
}
|
2016-11-30 16:46:41 +00:00
|
|
|
|
|
|
|
unsigned char result_hash[32] = {33, 153, 66, 187, 124, 250, 87, 12, 12, 73, 43, 247, 175, 153, 10, 51, 192, 195, 218, 69, 220, 170, 105, 179, 195, 0, 203, 213, 172, 3, 244, 10 };
|
|
|
|
for(int i = 0; i < 32; i++) {
|
2016-12-14 17:07:43 +00:00
|
|
|
if (block->cid->hash[i] != result_hash[i]) {
|
2017-07-24 22:58:39 +00:00
|
|
|
ipfs_block_free(block);
|
2016-11-30 16:46:41 +00:00
|
|
|
return 0;
|
2016-12-14 17:07:43 +00:00
|
|
|
}
|
2016-11-30 16:46:41 +00:00
|
|
|
}
|
|
|
|
|
2017-07-24 22:58:39 +00:00
|
|
|
retVal = ipfs_block_free(block);
|
2016-11-30 16:46:41 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|