From a30f19cffc71e9fe3ea0600f77e0e07001df46be Mon Sep 17 00:00:00 2001 From: Tomas Mlcoch Date: Sat, 1 Jun 2013 13:11:59 +0200 Subject: [PATCH] checksum: New module + test + all checksum related operations switched to the new module. --- CMakeLists.txt | 1 + README.md | 1 + src/CMakeLists.txt | 3 + src/checksum.c | 273 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/checksum.h | 107 ++++++++++++++++++++ src/cmd_parser.c | 16 +-- src/cmd_parser.h | 2 +- src/constants.h | 9 -- src/createrepo_c.c | 2 +- src/createrepo_c.h | 2 +- src/error.h | 2 + src/misc.c | 123 ----------------------- src/misc.h | 19 ---- src/parsepkg.c | 52 ++-------- src/parsepkg.h | 2 +- src/repomd.c | 44 ++------ src/repomd.h | 1 + tests/CMakeLists.txt | 4 + tests/fixtures.h | 10 ++ tests/test_checksum.c | 143 ++++++++++++++++++++++++++ tests/test_misc.c | 107 ++++---------------- 21 files changed, 588 insertions(+), 335 deletions(-) create mode 100644 src/checksum.c create mode 100644 src/checksum.h create mode 100644 tests/test_checksum.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 007e2ff..1d5465e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,6 +30,7 @@ find_package(GTHREAD2 REQUIRED) find_package(Libmagic REQUIRED) find_package(LibXml2 REQUIRED) find_package(LZMA REQUIRED) +find_package(OpenSSL REQUIRED) find_package(Sqlite3 REQUIRED) find_package(ZLIB REQUIRED) diff --git a/README.md b/README.md index e7b3236..ec1860f 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ Package build requires - Pkg name in Fedora/Ubuntu: * libxml2 (http://xmlsoft.org/) - libxml2-devel/libxml2-dev * python (http://python.org/) - python2-devel/libpython2.7-dev * rpm (http://www.rpm.org/) - rpm-devel/librpm-dev +* openssl (http://www.openssl.org/) - openssl-devel/libssl-dev * sqlite3 (https://sqlite.org/) - sqlite-devel/libsqlite3-dev * xz (http://tukaani.org/xz/) - xz-devel/liblzma-dev * zlib (http://www.zlib.net/) - zlib-devel/zlib1g-dev diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a06fbd6..52c999b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,4 +1,5 @@ SET (createrepo_c_SRCS + checksum.c compression_wrapper.c error.c load_metadata.c @@ -20,6 +21,7 @@ SET (createrepo_c_SRCS xml_parser_primary.c) SET(headers + checksum.h compression_wrapper.h constants.h createrepo_c.h @@ -45,6 +47,7 @@ TARGET_LINK_LIBRARIES(libcreaterepo_c ${GLIB2_LIBRARIES}) TARGET_LINK_LIBRARIES(libcreaterepo_c ${Libmagic_LIBRARY}) TARGET_LINK_LIBRARIES(libcreaterepo_c ${LIBXML2_LIBRARIES}) TARGET_LINK_LIBRARIES(libcreaterepo_c ${LZMA_LIBRARIES}) +TARGET_LINK_LIBRARIES(libcreaterepo_c ${OPENSSL_LIBRARIES}) TARGET_LINK_LIBRARIES(libcreaterepo_c ${RPMDB_LIBRARY}) TARGET_LINK_LIBRARIES(libcreaterepo_c ${SQLITE3_LIBRARIES}) TARGET_LINK_LIBRARIES(libcreaterepo_c ${ZLIB_LIBRARY}) diff --git a/src/checksum.c b/src/checksum.c new file mode 100644 index 0000000..925d4b8 --- /dev/null +++ b/src/checksum.c @@ -0,0 +1,273 @@ +/* createrepo_c - Library of routines for manipulation with repodata + * Copyright (C) 2013 Tomas Mlcoch + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, + * USA. + */ + +#include +#include +#include +#include +#include +#include +#include +#include "error.h" +#include "checksum.h" + +#define MAX_CHECKSUM_NAME_LEN 7 +#define BUFFER_SIZE 2048 + +struct _cr_ChecksumCtx { + EVP_MD_CTX *ctx; + cr_ChecksumType type; +}; + +cr_ChecksumType +cr_checksum_type(const char *name) +{ + size_t len; + char name_lower[MAX_CHECKSUM_NAME_LEN+1]; + + if (!name) + return CR_CHECKSUM_UNKNOWN; + + len = strlen(name); + if (len > MAX_CHECKSUM_NAME_LEN) + return CR_CHECKSUM_UNKNOWN; + + for (size_t x = 0; x <= len; x++) + name_lower[x] = tolower(name[x]); + + if (!strncmp(name_lower, "md", 2)) { + // MD* family + if (name_lower[2] == '2') + return CR_CHECKSUM_MD2; + else if (name_lower[2] == '5') + return CR_CHECKSUM_MD5; + } else if (!strncmp(name_lower, "sha", 3)) { + // SHA* family + char *sha_type = name_lower + 3; + if (!strcmp(sha_type, "")) + return CR_CHECKSUM_SHA1; + else if (!strcmp(sha_type, "1")) + return CR_CHECKSUM_SHA1; + else if (!strcmp(sha_type, "224")) + return CR_CHECKSUM_SHA224; + else if (!strcmp(sha_type, "256")) + return CR_CHECKSUM_SHA256; + else if (!strcmp(sha_type, "384")) + return CR_CHECKSUM_SHA384; + else if (!strcmp(sha_type, "512")) + return CR_CHECKSUM_SHA512; + } + + return CR_CHECKSUM_UNKNOWN; +} + +const char * +cr_checksum_name_str(cr_ChecksumType type) +{ + switch (type) { + case CR_CHECKSUM_UNKNOWN: + return "Unknown checksum"; +// case CR_CHECKSUM_MD2: +// return "md2"; + case CR_CHECKSUM_MD5: + return "md5"; + case CR_CHECKSUM_SHA: + return "sha"; + case CR_CHECKSUM_SHA1: + return "sha1"; + case CR_CHECKSUM_SHA224: + return "sha224"; + case CR_CHECKSUM_SHA256: + return "sha256"; + case CR_CHECKSUM_SHA384: + return "sha384"; + case CR_CHECKSUM_SHA512: + return "sha512"; + } + return NULL; +} + +char * +cr_checksum_file(const char *filename, + cr_ChecksumType type, + GError **err) +{ + FILE *f; + int rc; + unsigned int len; + ssize_t readed; + char buf[BUFFER_SIZE]; + unsigned char raw_checksum[EVP_MAX_MD_SIZE]; + char *checksum; + EVP_MD_CTX *ctx; + const EVP_MD *ctx_type; + + switch (type) { + //case CR_CHECKSUM_MD2: ctx_type = EVP_md2(); break; + case CR_CHECKSUM_MD5: ctx_type = EVP_md5(); break; + case CR_CHECKSUM_SHA: ctx_type = EVP_sha(); break; + case CR_CHECKSUM_SHA1: ctx_type = EVP_sha1(); break; + case CR_CHECKSUM_SHA224: ctx_type = EVP_sha224(); break; + case CR_CHECKSUM_SHA256: ctx_type = EVP_sha256(); break; + case CR_CHECKSUM_SHA384: ctx_type = EVP_sha384(); break; + case CR_CHECKSUM_SHA512: ctx_type = EVP_sha512(); break; + case CR_CHECKSUM_UNKNOWN: + default: + g_set_error(err, CR_CHECKSUM_ERROR, CRE_UNKNOWNCHECKSUMTYPE, + "Unknown checksum type"); + return NULL; + } + + f = fopen(filename, "rb"); + if (!f) { + g_set_error(err, CR_CHECKSUM_ERROR, CRE_IO, + "Cannot open a file: %s", strerror(errno)); + return NULL; + } + + ctx = EVP_MD_CTX_create(); + rc = EVP_DigestInit_ex(ctx, ctx_type, NULL); + if (!rc) { + g_set_error(err, CR_CHECKSUM_ERROR, CRE_OPENSSL, + "EVP_DigestInit_ex() failed"); + EVP_MD_CTX_destroy(ctx); + fclose(f); + return NULL; + } + + while ((readed = fread(buf, 1, BUFFER_SIZE, f)) == BUFFER_SIZE) + EVP_DigestUpdate(ctx, buf, readed); + + if (feof(f)) { + EVP_DigestUpdate(ctx, buf, readed); + } else { + g_set_error(err, CR_CHECKSUM_ERROR, CRE_IO, + "Error while reading a file: %s", strerror(errno)); + EVP_MD_CTX_destroy(ctx); + fclose(f); + return NULL; + } + + fclose(f); + + EVP_DigestFinal_ex(ctx, raw_checksum, &len); + EVP_MD_CTX_destroy(ctx); + checksum = g_malloc0(sizeof(char) * (len * 2 + 1)); + for (size_t x = 0; x < len; x++) + sprintf(checksum+(x*2), "%02x", raw_checksum[x]); + + return checksum; +} + +cr_ChecksumCtx * +cr_checksum_new(cr_ChecksumType type, GError **err) +{ + int rc; + EVP_MD_CTX *ctx; + const EVP_MD *ctx_type; + cr_ChecksumCtx *cr_ctx; + + assert(!err || *err == NULL); + + switch (type) { + //case CR_CHECKSUM_MD2: ctx_type = EVP_md2(); break; + case CR_CHECKSUM_MD5: ctx_type = EVP_md5(); break; + case CR_CHECKSUM_SHA: ctx_type = EVP_sha(); break; + case CR_CHECKSUM_SHA1: ctx_type = EVP_sha1(); break; + case CR_CHECKSUM_SHA224: ctx_type = EVP_sha224(); break; + case CR_CHECKSUM_SHA256: ctx_type = EVP_sha256(); break; + case CR_CHECKSUM_SHA384: ctx_type = EVP_sha384(); break; + case CR_CHECKSUM_SHA512: ctx_type = EVP_sha512(); break; + case CR_CHECKSUM_UNKNOWN: + default: + g_set_error(err, CR_CHECKSUM_ERROR, CRE_UNKNOWNCHECKSUMTYPE, + "Unknown checksum type"); + return NULL; + } + + ctx = EVP_MD_CTX_create(); + if (!ctx) { + g_set_error(err, CR_CHECKSUM_ERROR, CRE_OPENSSL, + "EVP_MD_CTX_create() failed"); + return NULL; + } + + if (!EVP_DigestInit_ex(ctx, ctx_type, NULL)) { + g_set_error(err, CR_CHECKSUM_ERROR, CRE_OPENSSL, + "EVP_DigestInit_ex() failed"); + EVP_MD_CTX_destroy(ctx); + return NULL; + } + + cr_ctx = g_malloc0(sizeof(cr_ChecksumCtx)); + cr_ctx->ctx = ctx; + cr_ctx->type = type; + + return cr_ctx; +} + +int +cr_checksum_update(cr_ChecksumCtx *ctx, + const void *buf, + size_t len, + GError **err) +{ + assert(ctx); + assert(!err || *err == NULL); + + if (len == 0) + return CRE_OK; + + if (EVP_DigestUpdate(ctx->ctx, buf, len)) { + g_set_error(err, CR_CHECKSUM_ERROR, CRE_OPENSSL, + "EVP_DigestInit_ex() failed"); + return CRE_OPENSSL; + } + + return CRE_OK; +} + +char * +cr_checksum_final(cr_ChecksumCtx *ctx, GError **err) +{ + unsigned int len; + unsigned char raw_checksum[EVP_MAX_MD_SIZE]; + char *checksum; + + assert(ctx); + assert(!err || *err == NULL); + + if (!EVP_DigestFinal_ex(ctx->ctx, raw_checksum, &len)) { + g_set_error(err, CR_CHECKSUM_ERROR, CRE_OPENSSL, + "EVP_DigestFinal_ex() failed"); + EVP_MD_CTX_destroy(ctx->ctx); + g_free(ctx); + return NULL; + } + + EVP_MD_CTX_destroy(ctx->ctx); + + checksum = g_malloc0(sizeof(char) * (len * 2 + 1)); + for (size_t x = 0; x < len; x++) + sprintf(checksum+(x*2), "%02x", raw_checksum[x]); + + g_free(ctx); + + return checksum; +} diff --git a/src/checksum.h b/src/checksum.h new file mode 100644 index 0000000..4916372 --- /dev/null +++ b/src/checksum.h @@ -0,0 +1,107 @@ +/* createrepo_c - Library of routines for manipulation with repodata + * Copyright (C) 2013 Tomas Mlcoch + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, + * USA. + */ + +#ifndef __C_CREATEREPOLIB_CHECKSUM_H__ +#define __C_CREATEREPOLIB_CHECKSUM_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +/** \defgroup checksum API for checksum calculation. + * \addtogroup checksum + * @{ + */ + +typedef struct _cr_ChecksumCtx cr_ChecksumCtx; + +/** + * Enum of supported checksum types. + */ +typedef enum { + CR_CHECKSUM_UNKNOWN, /*!< Unknown checksum */ + CR_CHECKSUM_MD2, /*!< MD2 checksum */ + CR_CHECKSUM_MD5, /*!< MD5 checksum */ + CR_CHECKSUM_SHA, /*!< SHA checksum */ + CR_CHECKSUM_SHA1, /*!< SHA1 checksum */ + CR_CHECKSUM_SHA224, /*!< SHA224 checksum */ + CR_CHECKSUM_SHA256, /*!< SHA256 checksum */ + CR_CHECKSUM_SHA384, /*!< SHA384 checksum */ + CR_CHECKSUM_SHA512, /*!< SHA512 checksum */ + CR_CHECKSUM_SENTINEL, /*!< sentinel of the list */ +} cr_ChecksumType; + +/** Return checksum name. + * @param type checksum type + * @return constant null terminated string with checksum name + * or NULL on error + */ +const char *cr_checksum_name_str(cr_ChecksumType type); + +/** Return checksum type. + * @param name checksum name + * @return checksum type + */ +cr_ChecksumType cr_checksum_type(const char *name); + +/** Compute file checksum. + * @param filename filename + * @param type type of checksum + * @param err GError ** + * @return malloced null terminated string with checksum + * or NULL on error + */ +char *cr_checksum_file(const char *filename, + cr_ChecksumType type, + GError **err); + +/** Create new checksum context. + * @param type Checksum algorithm of the new checksum context. + * @param err GError ** + * @return cr_ChecksumCtx or NULL on error + */ +cr_ChecksumCtx *cr_checksum_new(cr_ChecksumType type, GError **err); + +/** Feeds data into the checksum. + * @param ctx Checksum context. + * @param buf Pointer to the data. + * @param len Length of the data. + * @param err GError ** + * @return cr_Error code. + */ +int cr_checksum_update(cr_ChecksumCtx *ctx, + const void *buf, + size_t len, + GError **err); + +/** Finalize checksum calculation, return checksum string and frees + * all checksum context resources. + * @param ctx Checksum context. + * @param err GError ** + * @return Checksum string or NULL on error. + */ +char *cr_checksum_final(cr_ChecksumCtx *ctx, GError **err); + +/** @} */ + +#ifdef __cplusplus +} +#endif + +#endif /* __C_CREATEREPOLIB_XML_PARSER_H__ */ diff --git a/src/cmd_parser.c b/src/cmd_parser.c index f26bb3d..a153900 100644 --- a/src/cmd_parser.c +++ b/src/cmd_parser.c @@ -176,21 +176,13 @@ check_arguments(struct CmdOptions *options) // Check and set checksum type if (options->checksum) { - GString *checksum_str = g_string_ascii_down(g_string_new(options->checksum)); - if (!strcmp(checksum_str->str, "sha256")) { - options->checksum_type = CR_CHECKSUM_SHA256; - } else if (!strcmp(checksum_str->str, "sha1")) { - options->checksum_type = CR_CHECKSUM_SHA1; - } else if (!strcmp(checksum_str->str, "sha")) { - options->checksum_type = CR_CHECKSUM_SHA1; - } else if (!strcmp(checksum_str->str, "md5")) { - options->checksum_type = CR_CHECKSUM_MD5; - } else { - g_string_free(checksum_str, TRUE); + cr_ChecksumType type; + type = cr_checksum_type(options->checksum); + if (type == CR_CHECKSUM_UNKNOWN) { g_critical("Unknown/Unsupported checksum type \"%s\"", options->checksum); return FALSE; } - g_string_free(checksum_str, TRUE); + options->checksum_type = type; } // Check and set compression type diff --git a/src/cmd_parser.h b/src/cmd_parser.h index b353655..bb18c19 100644 --- a/src/cmd_parser.h +++ b/src/cmd_parser.h @@ -21,7 +21,7 @@ #define __C_CREATEREPOLIB_CMD_PARSER_H__ #include -#include "constants.h" +#include "checksum.h" #include "compression_wrapper.h" diff --git a/src/constants.h b/src/constants.h index f66f987..a9d502f 100644 --- a/src/constants.h +++ b/src/constants.h @@ -29,15 +29,6 @@ extern "C" { * @{ */ -/** - * Checksum type. - */ -typedef enum { - CR_CHECKSUM_MD5, /*!< MD5 checksum */ - CR_CHECKSUM_SHA1, /*!< SHA1 checksum */ - CR_CHECKSUM_SHA256, /*!< SHA256 checksum */ - CR_CHECKSUM_SENTINEL, /*!< sentinel of the list */ -} cr_ChecksumType; /** @} */ diff --git a/src/createrepo_c.c b/src/createrepo_c.c index e7f7c01..f5bfb1a 100644 --- a/src/createrepo_c.c +++ b/src/createrepo_c.c @@ -29,7 +29,7 @@ #include #include "cmd_parser.h" #include "compression_wrapper.h" -#include "constants.h" +#include "checksum.h" #include "error.h" #include "load_metadata.h" #include "locate_metadata.h" diff --git a/src/createrepo_c.h b/src/createrepo_c.h index 4c2a6fc..8c3f212 100644 --- a/src/createrepo_c.h +++ b/src/createrepo_c.h @@ -40,8 +40,8 @@ extern "C" { */ #include +#include "checksum.h" #include "compression_wrapper.h" -#include "constants.h" #include "error.h" #include "load_metadata.h" #include "locate_metadata.h" diff --git a/src/error.h b/src/error.h index 194f50a..534ad39 100644 --- a/src/error.h +++ b/src/error.h @@ -70,6 +70,8 @@ typedef enum { (21) Bzip2 library related error */ CRE_XZ, /*!< (22) Xz (lzma) library related error */ + CRE_OPENSSL, /*!< + (23) OpenSSL library related error */ } cr_Error; /** Converts cr_Error return code to error string. diff --git a/src/misc.c b/src/misc.c index 89b4953..f63e30d 100644 --- a/src/misc.c +++ b/src/misc.c @@ -33,7 +33,6 @@ #include #include "error.h" #include "logging.h" -#include "constants.h" #include "misc.h" #define BUFFER_SIZE 4096 @@ -218,103 +217,6 @@ cr_is_primary(const char *filename) } */ - -char * -cr_compute_file_checksum(const char *filename, - cr_ChecksumType type, - GError **err) -{ - GChecksumType gchecksumtype; - - assert(filename); - assert(!err || *err == NULL); - - // Check if file exists and if it is a regular file (not a directory) - - if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) { - g_debug("%s: File %s doesn't exists or not a regular file", - __func__, filename); - g_set_error(err, CR_CHECKSUM_ERROR, CRE_NOFILE, - "File %s doesn't exists or not a regular file", filename); - return NULL; - } - - - // Convert our checksum type into glib type - - switch (type) { - case CR_CHECKSUM_MD5: - gchecksumtype = G_CHECKSUM_MD5; - break; - case CR_CHECKSUM_SHA1: - gchecksumtype = G_CHECKSUM_SHA1; - break; - case CR_CHECKSUM_SHA256: - gchecksumtype = G_CHECKSUM_SHA256; - break; - default: - g_debug("%s: Unknown checksum type", __func__); - g_set_error(err, CR_CHECKSUM_ERROR, CRE_UNKNOWNCHECKSUMTYPE, - "Unknown checksum type: %d", type); - return NULL; - }; - - - // Open file and initialize checksum structure - - FILE *fp = fopen(filename, "rb"); - if (!fp) { - g_critical("%s: Cannot open %s (%s)", __func__, filename, - strerror(errno)); - g_set_error(err, CR_CHECKSUM_ERROR, CRE_IO, - "Cannot open %s: %s", filename, strerror(errno)); - return NULL; - } - - - // Calculate checksum - - GChecksum *checksum = g_checksum_new(gchecksumtype); - unsigned char buffer[BUFFER_SIZE]; - - while (1) { - size_t input_len; - input_len = fread((void *) buffer, sizeof(unsigned char), - BUFFER_SIZE, fp); - g_checksum_update(checksum, (const guchar *) buffer, input_len); - if (input_len < BUFFER_SIZE) { - break; - } - } - - if (ferror(fp)) { - g_set_error(err, CR_CHECKSUM_ERROR, CRE_IO, - "fread call faied: %s", strerror(errno)); - fclose(fp); - g_checksum_free(checksum); - return NULL; - } - - fclose(fp); - - - // Get checksum - - char *checksum_str = g_strdup(g_checksum_get_string(checksum)); - g_checksum_free(checksum); - - if (!checksum_str) { - g_critical("%s: Cannot get checksum %s (low memory?)", __func__, - filename); - g_set_error(err, CR_CHECKSUM_ERROR, CRE_MEMORY, - "Cannot calculate checksum (low memory?)"); - } - - return checksum_str; -} - - - #define VAL_LEN 4 // Len of numeric values in rpm struct cr_HeaderRangeStruct @@ -394,31 +296,6 @@ cr_get_header_byte_range(const char *filename) return results; } - -const char * -cr_checksum_name_str(cr_ChecksumType type) -{ - char *name = NULL; - - switch (type) { - case CR_CHECKSUM_MD5: - name = "md5"; - break; - case CR_CHECKSUM_SHA1: - name = "sha"; - break; - case CR_CHECKSUM_SHA256: - name = "sha256"; - break; - default: - g_debug("%s: Unknown checksum (%d)", __func__, type); - break; - } - - return name; -} - - char * cr_get_filename(const char *filepath) { diff --git a/src/misc.h b/src/misc.h index d3cbd60..823f511 100644 --- a/src/misc.h +++ b/src/misc.h @@ -28,7 +28,6 @@ extern "C" { #include #include #include "compression_wrapper.h" -#include "constants.h" /** \defgroup misc Miscellaneous useful functions and macros. * \addtogroup misc @@ -103,17 +102,6 @@ static inline int cr_is_primary(const char *filename) { return 0; }; -/** Compute file checksum. - * @param filename filename - * @param type type of checksum - * @param err GError ** - * @return malloced null terminated string with checksum - * or NULL on error - */ -char *cr_compute_file_checksum(const char *filename, - cr_ChecksumType type, - GError **err); - /** Header range */ struct cr_HeaderRangeStruct { @@ -127,13 +115,6 @@ struct cr_HeaderRangeStruct { */ struct cr_HeaderRangeStruct cr_get_header_byte_range(const char *filename); -/** Return checksum name. - * @param type checksum type - * @return constant null terminated string with checksum name - * or NULL on error - */ -const char *cr_checksum_name_str(cr_ChecksumType type); - /** Return pointer to the rest of string after last '/'. * (e.g. for "/foo/bar" returns "bar") * @param filepath path diff --git a/src/parsepkg.c b/src/parsepkg.c index 89f7fa9..f319338 100644 --- a/src/parsepkg.c +++ b/src/parsepkg.c @@ -31,10 +31,9 @@ #include #include "logging.h" #include "error.h" -#include "constants.h" #include "parsehdr.h" #include "misc.h" -#include "parsehdr.h" +#include "checksum.h" volatile short cr_initialized = 0; rpmts cr_ts = NULL; @@ -126,24 +125,7 @@ cr_package_from_rpm(const char *filename, assert(filename); assert(!err || *err == NULL); - // Set checksum type - - switch (checksum_type) { - case CR_CHECKSUM_MD5: - checksum_type_str = "md5"; - break; - case CR_CHECKSUM_SHA1: - checksum_type_str = "sha1"; - break; - case CR_CHECKSUM_SHA256: - checksum_type_str = "sha256"; - break; - default: - g_warning("%s: Unknown checksum type", __func__); - g_set_error(err, CR_PARSEPKG_ERROR, CRE_UNKNOWNCHECKSUMTYPE, - "Unknown/Unsupported checksum type: %d", checksum_type); - return NULL; - }; + checksum_type_str = cr_checksum_name_str(checksum_type); // Read header @@ -179,10 +161,10 @@ cr_package_from_rpm(const char *filename, // Compute checksum - char *checksum = cr_compute_file_checksum(filename, checksum_type, &tmp_err); + char *checksum = cr_checksum_file(filename, checksum_type, &tmp_err); if (tmp_err) { g_propagate_prefixed_error(err, tmp_err, - "Error while checksum calculation:"); + "Error while checksum calculation: "); headerFree(hdr); return NULL; } @@ -232,28 +214,10 @@ cr_xml_from_rpm(const char *filename, result.filelists = NULL; result.other = NULL; + checksum_type_str = cr_checksum_name_str(checksum_type); - // Set checksum type - - switch (checksum_type) { - case CR_CHECKSUM_MD5: - checksum_type_str = "md5"; - break; - case CR_CHECKSUM_SHA1: - checksum_type_str = "sha1"; - break; - case CR_CHECKSUM_SHA256: - checksum_type_str = "sha256"; - break; - default: - g_warning("%s: Unknown checksum type", __func__); - g_set_error(err, CR_PARSEPKG_ERROR, CRE_UNKNOWNCHECKSUMTYPE, - "Unknown/Unsupported checksum type: %d", checksum_type); - return result; - }; - - // Read header + // Read header Header hdr; read_header(filename, &hdr, &tmp_err); @@ -286,10 +250,10 @@ cr_xml_from_rpm(const char *filename, // Compute checksum - char *checksum = cr_compute_file_checksum(filename, checksum_type, NULL); + char *checksum = cr_checksum_file(filename, checksum_type, &tmp_err); if (tmp_err) { g_propagate_prefixed_error(err, tmp_err, - "Error while checksum calculation:"); + "Error while checksum calculation: "); headerFree(hdr); return result; } diff --git a/src/parsepkg.h b/src/parsepkg.h index 5c7d4d6..998bd99 100644 --- a/src/parsepkg.h +++ b/src/parsepkg.h @@ -25,7 +25,7 @@ extern "C" { #endif #include -#include "constants.h" +#include "checksum.h" #include "package.h" /** \defgroup parsepkg Package parser API. diff --git a/src/repomd.c b/src/repomd.c index bd30753..5e9bd84 100644 --- a/src/repomd.c +++ b/src/repomd.c @@ -29,6 +29,7 @@ #include "error.h" #include "logging.h" #include "misc.h" +#include "checksum.h" #include "repomd.h" #include "compression_wrapper.h" @@ -134,34 +135,13 @@ cr_get_compressed_content_stat(const char *filename, } - // Create checksum structure - - GChecksumType gchecksumtype; - switch (checksum_type) { - case CR_CHECKSUM_MD5: - gchecksumtype = G_CHECKSUM_MD5; - break; - case CR_CHECKSUM_SHA1: - gchecksumtype = G_CHECKSUM_SHA1; - break; - case CR_CHECKSUM_SHA256: - gchecksumtype = G_CHECKSUM_SHA256; - break; - default: - g_critical("%s: Unknown checksum type", __func__); - g_set_error(err, CR_REPOMD_RECORD_ERROR, CRE_UNKNOWNCHECKSUMTYPE, - "Unknown checksum type: %d", checksum_type); - return NULL; - }; - - // Read compressed file and calculate checksum and size - GChecksum *checksum = g_checksum_new(gchecksumtype); - if (!checksum) { + cr_ChecksumCtx *checksum = cr_checksum_new(checksum_type, &tmp_err); + if (tmp_err) { g_critical("%s: g_checksum_new() failed", __func__); - g_set_error(err, CR_REPOMD_RECORD_ERROR, CRE_UNKNOWNCHECKSUMTYPE, - "g_checksum_new() failed - Unknown checksum type"); + g_propagate_prefixed_error(err, tmp_err, + "Error while checksum calculation: "); return NULL; } @@ -179,7 +159,7 @@ cr_get_compressed_content_stat(const char *filename, filename); break; } - g_checksum_update (checksum, buffer, readed); + cr_checksum_update(checksum, buffer, readed, NULL); size += readed; } while (readed == BUFFER_SIZE); @@ -190,17 +170,13 @@ cr_get_compressed_content_stat(const char *filename, contentStat* result = g_malloc(sizeof(contentStat)); if (result) { - result->checksum = g_strdup(g_checksum_get_string(checksum)); + result->checksum = cr_checksum_final(checksum, NULL); result->size = size; } else { g_set_error(err, CR_REPOMD_RECORD_ERROR, CRE_MEMORY, "Cannot allocate memory"); } - - // Clean up - - g_checksum_free(checksum); cr_close(cwfile, NULL); return result; @@ -246,7 +222,7 @@ cr_repomd_record_fill(cr_RepomdRecord md, if (!md->checksum_type || !md->checksum) { gchar *chksum; - chksum = cr_compute_file_checksum(path, checksum_t, &tmp_err); + chksum = cr_checksum_file(path, checksum_t, &tmp_err); if (tmp_err) { int code = tmp_err->code; g_propagate_prefixed_error(err, tmp_err, @@ -441,7 +417,7 @@ cr_repomd_record_compress_and_fill(cr_RepomdRecord record, // Compute checksums - checksum = cr_compute_file_checksum(path, checksum_type, &tmp_err); + checksum = cr_checksum_file(path, checksum_type, &tmp_err); if (tmp_err) { int code = tmp_err->code; g_propagate_prefixed_error(err, tmp_err, @@ -449,7 +425,7 @@ cr_repomd_record_compress_and_fill(cr_RepomdRecord record, return code; } - cchecksum = cr_compute_file_checksum(cpath, checksum_type, &tmp_err); + cchecksum = cr_checksum_file(cpath, checksum_type, &tmp_err); if (tmp_err) { int code = tmp_err->code; g_propagate_prefixed_error(err, tmp_err, diff --git a/src/repomd.h b/src/repomd.h index 41a06d8..3dd6f1e 100644 --- a/src/repomd.h +++ b/src/repomd.h @@ -24,6 +24,7 @@ extern "C" { #endif +#include "checksum.h" #include "compression_wrapper.h" #include "package.h" diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 08136e5..576e1d7 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,3 +1,7 @@ +ADD_EXECUTABLE(test_checksum test_checksum.c) +TARGET_LINK_LIBRARIES(test_checksum libcreaterepo_c ${GLIB2_LIBRARIES}) +ADD_DEPENDENCIES(tests test_checksum) + ADD_EXECUTABLE(test_compression_wrapper test_compression_wrapper.c) TARGET_LINK_LIBRARIES(test_compression_wrapper libcreaterepo_c ${GLIB2_LIBRARIES}) ADD_DEPENDENCIES(tests test_compression_wrapper) diff --git a/tests/fixtures.h b/tests/fixtures.h index c048114..49edf1e 100644 --- a/tests/fixtures.h +++ b/tests/fixtures.h @@ -61,4 +61,14 @@ #define TEST_MRF_UE_OTH_01 TEST_MODIFIED_REPO_FILES_PATH"unknown_element_01-other.xml" #define TEST_MRF_UE_OTH_02 TEST_MODIFIED_REPO_FILES_PATH"unknown_element_02-other.xml" +// Test files + +#define TEST_EMPTY_FILE TEST_FILES_PATH"empty_file" +#define TEST_TEXT_FILE TEST_FILES_PATH"text_file" +#define TEST_BINARY_FILE TEST_FILES_PATH"binary_file" + +// Other + +#define NON_EXIST_FILE "/tmp/foobarfile.which.should.not.exists" + #endif diff --git a/tests/test_checksum.c b/tests/test_checksum.c new file mode 100644 index 0000000..93e6d13 --- /dev/null +++ b/tests/test_checksum.c @@ -0,0 +1,143 @@ +/* createrepo_c - Library of routines for manipulation with repodata + * Copyright (C) 2012 Tomas Mlcoch + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, + * USA. + */ + +#include +#include +#include +#include +#include +#include "fixtures.h" +#include "createrepo/checksum.h" + +static void +test_cr_checksum_file(void) +{ + char *checksum; + GError *tmp_err = NULL; + + checksum = cr_checksum_file(TEST_EMPTY_FILE, CR_CHECKSUM_MD5, NULL); + g_assert_cmpstr(checksum, ==, "d41d8cd98f00b204e9800998ecf8427e"); + g_free(checksum); + checksum = cr_checksum_file(TEST_EMPTY_FILE, CR_CHECKSUM_SHA1, NULL); + g_assert_cmpstr(checksum, ==, "da39a3ee5e6b4b0d3255bfef95601890afd80709"); + g_free(checksum); + checksum = cr_checksum_file(TEST_EMPTY_FILE, CR_CHECKSUM_SHA256, NULL); + g_assert_cmpstr(checksum, ==, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649" + "b934ca495991b7852b855"); + g_free(checksum); + checksum = cr_checksum_file(TEST_EMPTY_FILE, CR_CHECKSUM_SHA512, NULL); + g_assert_cmpstr(checksum, ==, "cf83e1357eefb8bdf1542850d66d8007d620e4050b5" + "715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd4741" + "7a81a538327af927da3e"); + g_free(checksum); + + checksum = cr_checksum_file(TEST_TEXT_FILE, CR_CHECKSUM_MD5, &tmp_err); + g_assert_cmpstr(checksum, ==, "d6d4da5c15f8fe7570ce6ab6b3503916"); + g_assert(!tmp_err); + g_free(checksum); + checksum = cr_checksum_file(TEST_TEXT_FILE, CR_CHECKSUM_SHA1, &tmp_err); + g_assert_cmpstr(checksum, ==, "da048ee8fabfbef1b3d6d3f5a4be20029eecec77"); + g_assert(!tmp_err); + g_free(checksum); + checksum = cr_checksum_file(TEST_TEXT_FILE, CR_CHECKSUM_SHA256, &tmp_err); + g_assert_cmpstr(checksum, ==, "2f395bdfa2750978965e4781ddf224c89646c7d7a15" + "69b7ebb023b170f7bd8bb"); + g_assert(!tmp_err); + g_free(checksum); + checksum = cr_checksum_file(TEST_TEXT_FILE, CR_CHECKSUM_SHA512, &tmp_err); + g_assert_cmpstr(checksum, ==, "6ef7c2fd003614033aab59a65164c897fd150cfa855" + "1f2dd66828cc7a4d16afc3a35890f342eeaa424c1270fa8bbb4b792875b9deb34" + "cd78ab9ded1c360de45c"); + g_assert(!tmp_err); + g_free(checksum); + + checksum = cr_checksum_file(TEST_BINARY_FILE, CR_CHECKSUM_MD5, NULL); + g_assert_cmpstr(checksum, ==, "4f8b033d7a402927a20c9328fc0e0f46"); + g_free(checksum); + checksum = cr_checksum_file(TEST_BINARY_FILE, CR_CHECKSUM_SHA1, NULL); + g_assert_cmpstr(checksum, ==, "3539fb660a41846352ac4fa9076d168a3c77070b"); + g_free(checksum); + checksum = cr_checksum_file(TEST_BINARY_FILE, CR_CHECKSUM_SHA256, NULL); + g_assert_cmpstr(checksum, ==, "bf68e32ad78cea8287be0f35b74fa3fecd0eaa91770" + "b48f1a7282b015d6d883e"); + g_free(checksum); + checksum = cr_checksum_file(TEST_BINARY_FILE, CR_CHECKSUM_SHA512, NULL); + g_assert_cmpstr(checksum, ==, "339877a8ce6cdb2df62f3f76c005cac4f50144197bd" + "095cec21056d6ddde570fe5b16e3f1cd077ece799d5dd23dc6c9c1afed018384d" + "840bd97233c320e60dfa"); + g_free(checksum); + + // Corner cases + + checksum = cr_checksum_file(TEST_BINARY_FILE, 244, &tmp_err); + g_assert(!checksum); + g_assert(tmp_err); + g_error_free(tmp_err); + tmp_err = NULL; + + checksum = cr_checksum_file(NON_EXIST_FILE, CR_CHECKSUM_MD5, &tmp_err); + g_assert(!checksum); + g_assert(tmp_err); + g_error_free(tmp_err); + tmp_err = NULL; +} + + +static void +test_cr_checksum_name_str(void) +{ + const char *checksum_name; + + checksum_name = cr_checksum_name_str(CR_CHECKSUM_MD5); + g_assert_cmpstr(checksum_name, ==, "md5"); + + checksum_name = cr_checksum_name_str(CR_CHECKSUM_SHA); + g_assert_cmpstr(checksum_name, ==, "sha"); + + checksum_name = cr_checksum_name_str(CR_CHECKSUM_SHA1); + g_assert_cmpstr(checksum_name, ==, "sha1"); + + checksum_name = cr_checksum_name_str(CR_CHECKSUM_SHA224); + g_assert_cmpstr(checksum_name, ==, "sha224"); + + checksum_name = cr_checksum_name_str(CR_CHECKSUM_SHA256); + g_assert_cmpstr(checksum_name, ==, "sha256"); + + checksum_name = cr_checksum_name_str(CR_CHECKSUM_SHA384); + g_assert_cmpstr(checksum_name, ==, "sha384"); + + checksum_name = cr_checksum_name_str(CR_CHECKSUM_SHA512); + g_assert_cmpstr(checksum_name, ==, "sha512"); + + checksum_name = cr_checksum_name_str(244); + g_assert_cmpstr(checksum_name, ==, NULL); +} + +int +main(int argc, char *argv[]) +{ + g_test_init(&argc, &argv, NULL); + + g_test_add_func("/checksum/test_cr_checksum_file", + test_cr_checksum_file); + g_test_add_func("/checksum/test_cr_checksum_name_str", + test_cr_checksum_name_str); + + return g_test_run(); +} diff --git a/tests/test_misc.c b/tests/test_misc.c index 28b2561..6bda53c 100644 --- a/tests/test_misc.c +++ b/tests/test_misc.c @@ -23,13 +23,9 @@ #include #include #include "fixtures.h" +#include "createrepo/checksum.h" #include "createrepo/misc.h" - -#define EMPTY_FILE TEST_FILES_PATH"empty_file" -#define TEXT_FILE TEST_FILES_PATH"text_file" -#define BINARY_FILE TEST_FILES_PATH"binary_file" - #define PACKAGE_01 TEST_PACKAGES_PATH"super_kernel-6.0.1-2.x86_64.rpm" #define PACKAGE_01_HEADER_START 280 #define PACKAGE_01_HEADER_END 2637 @@ -38,9 +34,6 @@ #define PACKAGE_02_HEADER_START 280 #define PACKAGE_02_HEADER_END 2057 -#define TMP_DIR_PATTERN "/tmp/createrepo_test_XXXXXX" -#define NON_EXIST_FILE "/tmp/foobarfile.which.should.not.exists" - #define VALID_URL_01 "http://google.com/index.html" #define URL_FILENAME_01 "index.html" #define INVALID_URL "htp://foo.bar" @@ -360,51 +353,6 @@ test_cr_is_primary(void) static void -test_cr_compute_file_checksum(void) -{ - char *checksum; - - checksum = cr_compute_file_checksum(EMPTY_FILE, CR_CHECKSUM_MD5, NULL); - g_assert_cmpstr(checksum, ==, "d41d8cd98f00b204e9800998ecf8427e"); - g_free(checksum); - checksum = cr_compute_file_checksum(EMPTY_FILE, CR_CHECKSUM_SHA1, NULL); - g_assert_cmpstr(checksum, ==, "da39a3ee5e6b4b0d3255bfef95601890afd80709"); - g_free(checksum); - checksum = cr_compute_file_checksum(EMPTY_FILE, CR_CHECKSUM_SHA256, NULL); - g_assert_cmpstr(checksum, ==, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"); - g_free(checksum); - - checksum = cr_compute_file_checksum(TEXT_FILE, CR_CHECKSUM_MD5, NULL); - g_assert_cmpstr(checksum, ==, "d6d4da5c15f8fe7570ce6ab6b3503916"); - g_free(checksum); - checksum = cr_compute_file_checksum(TEXT_FILE, CR_CHECKSUM_SHA1, NULL); - g_assert_cmpstr(checksum, ==, "da048ee8fabfbef1b3d6d3f5a4be20029eecec77"); - g_free(checksum); - checksum = cr_compute_file_checksum(TEXT_FILE, CR_CHECKSUM_SHA256, NULL); - g_assert_cmpstr(checksum, ==, "2f395bdfa2750978965e4781ddf224c89646c7d7a1569b7ebb023b170f7bd8bb"); - g_free(checksum); - - checksum = cr_compute_file_checksum(BINARY_FILE, CR_CHECKSUM_MD5, NULL); - g_assert_cmpstr(checksum, ==, "4f8b033d7a402927a20c9328fc0e0f46"); - g_free(checksum); - checksum = cr_compute_file_checksum(BINARY_FILE, CR_CHECKSUM_SHA1, NULL); - g_assert_cmpstr(checksum, ==, "3539fb660a41846352ac4fa9076d168a3c77070b"); - g_free(checksum); - checksum = cr_compute_file_checksum(BINARY_FILE, CR_CHECKSUM_SHA256, NULL); - g_assert_cmpstr(checksum, ==, "bf68e32ad78cea8287be0f35b74fa3fecd0eaa91770b48f1a7282b015d6d883e"); - g_free(checksum); - - // Corner cases - - checksum = cr_compute_file_checksum(BINARY_FILE, 244, NULL); - g_assert(!checksum); - - checksum = cr_compute_file_checksum(NON_EXIST_FILE, CR_CHECKSUM_MD5, NULL); - g_assert(!checksum); -} - - -static void test_cr_get_header_byte_range(void) { struct cr_HeaderRangeStruct hdr_range; @@ -424,25 +372,6 @@ test_cr_get_header_byte_range(void) static void -test_cr_checksum_name_str(void) -{ - const char *checksum_name; - - checksum_name = cr_checksum_name_str(CR_CHECKSUM_MD5); - g_assert_cmpstr(checksum_name, ==, "md5"); - - checksum_name = cr_checksum_name_str(CR_CHECKSUM_SHA1); - g_assert_cmpstr(checksum_name, ==, "sha"); - - checksum_name = cr_checksum_name_str(CR_CHECKSUM_SHA256); - g_assert_cmpstr(checksum_name, ==, "sha256"); - - checksum_name = cr_checksum_name_str(244); - g_assert_cmpstr(checksum_name, ==, NULL); -} - - -static void test_cr_get_filename(void) { char *filename; @@ -485,7 +414,7 @@ static void copyfiletest_setup(Copyfiletest *copyfiletest, gconstpointer test_data) { CR_UNUSED(test_data); - copyfiletest->tmp_dir = g_strdup(TMP_DIR_PATTERN); + copyfiletest->tmp_dir = g_strdup(TMPDIR_TEMPLATE); mkdtemp(copyfiletest->tmp_dir); copyfiletest->dst_file = g_strconcat(copyfiletest->tmp_dir, "/", DST_FILE, NULL); } @@ -510,10 +439,10 @@ copyfiletest_test_empty_file(Copyfiletest *copyfiletest, gconstpointer test_data char *checksum; g_assert(!g_file_test(copyfiletest->dst_file, G_FILE_TEST_EXISTS)); - ret = cr_copy_file(EMPTY_FILE, copyfiletest->dst_file); + ret = cr_copy_file(TEST_EMPTY_FILE, copyfiletest->dst_file); g_assert_cmpint(ret, ==, CR_COPY_OK); g_assert(g_file_test(copyfiletest->dst_file, G_FILE_TEST_EXISTS|G_FILE_TEST_IS_REGULAR)); - checksum = cr_compute_file_checksum(copyfiletest->dst_file, CR_CHECKSUM_SHA256, NULL); + checksum = cr_checksum_file(copyfiletest->dst_file, CR_CHECKSUM_SHA256, NULL); g_assert_cmpstr(checksum, ==, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"); g_free(checksum); } @@ -527,10 +456,10 @@ copyfiletest_test_text_file(Copyfiletest *copyfiletest, gconstpointer test_data) char *checksum; g_assert(!g_file_test(copyfiletest->dst_file, G_FILE_TEST_EXISTS)); - ret = cr_copy_file(TEXT_FILE, copyfiletest->dst_file); + ret = cr_copy_file(TEST_TEXT_FILE, copyfiletest->dst_file); g_assert_cmpint(ret, ==, CR_COPY_OK); g_assert(g_file_test(copyfiletest->dst_file, G_FILE_TEST_EXISTS|G_FILE_TEST_IS_REGULAR)); - checksum = cr_compute_file_checksum(copyfiletest->dst_file, CR_CHECKSUM_SHA256, NULL); + checksum = cr_checksum_file(copyfiletest->dst_file, CR_CHECKSUM_SHA256, NULL); g_assert_cmpstr(checksum, ==, "2f395bdfa2750978965e4781ddf224c89646c7d7a1569b7ebb023b170f7bd8bb"); g_free(checksum); } @@ -544,10 +473,10 @@ copyfiletest_test_binary_file(Copyfiletest *copyfiletest, gconstpointer test_dat char *checksum; g_assert(!g_file_test(copyfiletest->dst_file, G_FILE_TEST_EXISTS)); - ret = cr_copy_file(BINARY_FILE, copyfiletest->dst_file); + ret = cr_copy_file(TEST_BINARY_FILE, copyfiletest->dst_file); g_assert_cmpint(ret, ==, CR_COPY_OK); g_assert(g_file_test(copyfiletest->dst_file, G_FILE_TEST_EXISTS|G_FILE_TEST_IS_REGULAR)); - checksum = cr_compute_file_checksum(copyfiletest->dst_file, CR_CHECKSUM_SHA256, NULL); + checksum = cr_checksum_file(copyfiletest->dst_file, CR_CHECKSUM_SHA256, NULL); g_assert_cmpstr(checksum, ==, "bf68e32ad78cea8287be0f35b74fa3fecd0eaa91770b48f1a7282b015d6d883e"); g_free(checksum); } @@ -561,17 +490,17 @@ copyfiletest_test_rewrite(Copyfiletest *copyfiletest, gconstpointer test_data) char *checksum; g_assert(!g_file_test(copyfiletest->dst_file, G_FILE_TEST_EXISTS)); - ret = cr_copy_file(BINARY_FILE, copyfiletest->dst_file); + ret = cr_copy_file(TEST_BINARY_FILE, copyfiletest->dst_file); g_assert_cmpint(ret, ==, CR_COPY_OK); g_assert(g_file_test(copyfiletest->dst_file, G_FILE_TEST_EXISTS|G_FILE_TEST_IS_REGULAR)); - checksum = cr_compute_file_checksum(copyfiletest->dst_file, CR_CHECKSUM_SHA256, NULL); + checksum = cr_checksum_file(copyfiletest->dst_file, CR_CHECKSUM_SHA256, NULL); g_assert_cmpstr(checksum, ==, "bf68e32ad78cea8287be0f35b74fa3fecd0eaa91770b48f1a7282b015d6d883e"); g_free(checksum); - ret = cr_copy_file(TEXT_FILE, copyfiletest->dst_file); + ret = cr_copy_file(TEST_TEXT_FILE, copyfiletest->dst_file); g_assert_cmpint(ret, ==, CR_COPY_OK); g_assert(g_file_test(copyfiletest->dst_file, G_FILE_TEST_EXISTS|G_FILE_TEST_IS_REGULAR)); - checksum = cr_compute_file_checksum(copyfiletest->dst_file, CR_CHECKSUM_SHA256, NULL); + checksum = cr_checksum_file(copyfiletest->dst_file, CR_CHECKSUM_SHA256, NULL); g_assert_cmpstr(checksum, ==, "2f395bdfa2750978965e4781ddf224c89646c7d7a1569b7ebb023b170f7bd8bb"); g_free(checksum); } @@ -598,10 +527,10 @@ compressfile_test_text_file(Copyfiletest *copyfiletest, gconstpointer test_data) char *checksum; g_assert(!g_file_test(copyfiletest->dst_file, G_FILE_TEST_EXISTS)); - ret = cr_compress_file(TEXT_FILE, copyfiletest->dst_file, CR_CW_GZ_COMPRESSION); + ret = cr_compress_file(TEST_TEXT_FILE, copyfiletest->dst_file, CR_CW_GZ_COMPRESSION); g_assert_cmpint(ret, ==, CR_COPY_OK); g_assert(g_file_test(copyfiletest->dst_file, G_FILE_TEST_EXISTS|G_FILE_TEST_IS_REGULAR)); - checksum = cr_compute_file_checksum(copyfiletest->dst_file, CR_CHECKSUM_SHA256, NULL); + checksum = cr_checksum_file(copyfiletest->dst_file, CR_CHECKSUM_SHA256, NULL); g_assert_cmpstr(checksum, ==, "8909fde88a5747d800fd2562b0f22945f014aa7df64cf1c15c7933ae54b72ab6"); g_free(checksum); } @@ -668,10 +597,10 @@ test_cr_better_copy_file_local(Copyfiletest *copyfiletest, gconstpointer test_da char *checksum; g_assert(!g_file_test(copyfiletest->dst_file, G_FILE_TEST_EXISTS)); - ret = cr_better_copy_file(BINARY_FILE, copyfiletest->dst_file); + ret = cr_better_copy_file(TEST_BINARY_FILE, copyfiletest->dst_file); g_assert_cmpint(ret, ==, CR_COPY_OK); g_assert(g_file_test(copyfiletest->dst_file, G_FILE_TEST_EXISTS|G_FILE_TEST_IS_REGULAR)); - checksum = cr_compute_file_checksum(copyfiletest->dst_file, CR_CHECKSUM_SHA256, NULL); + checksum = cr_checksum_file(copyfiletest->dst_file, CR_CHECKSUM_SHA256, NULL); g_assert_cmpstr(checksum, ==, "bf68e32ad78cea8287be0f35b74fa3fecd0eaa91770b48f1a7282b015d6d883e"); g_free(checksum); } @@ -699,7 +628,7 @@ test_cr_remove_dir(void) char *subdir01, *subdir02, *subsubdir011, *subsubsubdir0111; gchar *tmp_file_1, *tmp_file_2, *tmp_file_3; - tmp_dir = g_strdup(TMP_DIR_PATTERN); + tmp_dir = g_strdup(TMPDIR_TEMPLATE); g_assert(mkdtemp(tmp_dir)); subdir01 = g_strconcat(tmp_dir, "/subdir01", NULL); @@ -1057,9 +986,7 @@ main(int argc, char *argv[]) g_test_add_func("/misc/test_cr_str_to_evr", test_cr_str_to_evr); g_test_add_func("/misc/test_cr_str_to_evr_with_chunk", test_cr_str_to_evr_with_chunk); g_test_add_func("/misc/test_cr_is_primary", test_cr_is_primary); - g_test_add_func("/misc/test_cr_compute_file_checksum", test_cr_compute_file_checksum); g_test_add_func("/misc/test_cr_get_header_byte_range", test_cr_get_header_byte_range); - g_test_add_func("/misc/test_cr_checksum_name_str", test_cr_checksum_name_str); g_test_add_func("/misc/test_cr_get_filename", test_cr_get_filename); g_test_add("/misc/copyfiletest_test_empty_file", Copyfiletest, NULL, copyfiletest_setup, copyfiletest_test_empty_file, copyfiletest_teardown); g_test_add("/misc/copyfiletest_test_text_file", Copyfiletest, NULL, copyfiletest_setup, copyfiletest_test_text_file, copyfiletest_teardown); -- 2.7.4