c-ipfs/test/cid/test_cid.h

109 lines
2.4 KiB
C
Raw Permalink Normal View History

2016-11-14 02:01:51 +00:00
#include <string.h>
#include "mh/hashes.h"
#include "mh/multihash.h"
#include "ipfs/cid/cid.h"
#include "ipfs/multibase/multibase.h"
#include "libp2p/crypto/sha256.h"
2016-11-14 02:01:51 +00:00
int test_cid_new_free() {
struct Cid* cid;
2016-11-14 02:01:51 +00:00
const unsigned char* hash = "ABC123";
int retVal = ipfs_cid_new(0, (unsigned char*)hash, strlen((char*)hash), CID_PROTOBUF, &cid);
2016-11-14 02:01:51 +00:00
if (retVal == 0)
return 0;
if (cid->version != 0)
2016-11-14 02:01:51 +00:00
return 0;
if (cid->codec != CID_PROTOBUF)
2016-11-14 02:01:51 +00:00
return 0;
if (cid->hash_length != strlen((char*)hash))
2016-11-14 02:01:51 +00:00
return 0;
if (strncmp((char*)cid->hash, (char*)hash, 6) != 0)
2016-11-14 02:01:51 +00:00
return 0;
return ipfs_cid_free(cid);
2016-11-14 02:01:51 +00:00
}
/***
* Test sending a multibase encoded multihash into cid_cast method
* that should return a Cid struct
*/
int test_cid_cast_multihash() {
// first, build a multihash
char* string_to_hash = "Hello, World!";
unsigned char hashed[32];
memset(hashed, 0, 32);
// hash the string
libp2p_crypto_hashing_sha256(string_to_hash, strlen(string_to_hash), hashed);
size_t multihash_size = mh_new_length(MH_H_SHA2_256, 32);
unsigned char multihash[multihash_size];
memset(multihash, 0, multihash_size);
unsigned char* ptr = multihash;
int retVal = mh_new(ptr, MH_H_SHA2_256, hashed, 32);
if (retVal < 0)
return 0;
// now call cast
struct Cid cid;
retVal = ipfs_cid_cast(multihash, multihash_size, &cid);
2016-11-14 02:01:51 +00:00
if (retVal == 0)
return 0;
// check results
if (cid.version != 0)
return 0;
if (cid.hash_length != 32)
return 0;
if (cid.codec != CID_PROTOBUF)
return 0;
if (strncmp(hashed, cid.hash, 32) != 0)
return 0;
return 1;
}
int test_cid_cast_non_multihash() {
2016-11-14 23:02:18 +00:00
// first, build a hash
char* string_to_hash = "Hello, World!";
unsigned char hashed[32];
memset(hashed, 0, 32);
// hash the string
libp2p_crypto_hashing_sha256(string_to_hash, strlen(string_to_hash), hashed);
// now make it a hash with a version and codec embedded in varints before the hash
size_t array_size = 34; // 32 for the hash, 2 for the 2 varints
unsigned char array[array_size];
memset(array, 0, array_size);
// first the version
array[0] = 0;
// then the codec
array[1] = CID_PROTOBUF;
// then the hash
memcpy(&array[2], hashed, 32);
2016-11-14 02:01:51 +00:00
// now call cast
2016-11-14 23:02:18 +00:00
struct Cid cid;
int retVal = ipfs_cid_cast(array, array_size, &cid);
2016-11-14 23:02:18 +00:00
if (retVal == 0)
return 0;
2016-11-14 02:01:51 +00:00
// check results
2016-11-14 23:02:18 +00:00
if (cid.version != 0)
return 0;
if (cid.hash_length != 32)
return 0;
if (cid.codec != CID_PROTOBUF)
return 0;
if (strncmp(hashed, cid.hash, 32) != 0)
return 0;
return 1;
2016-11-14 02:01:51 +00:00
}