FOSSology  4.4.0
Open Source License Compliance by Open Source Software
test_Checksum.c
Go to the documentation of this file.
1 /*
2  SPDX-FileCopyrightText: © 2012 Hewlett-Packard Development Company, L.P.
3 
4  SPDX-License-Identifier: GPL-2.0-only
5 */
6 #include "run_tests.h"
7 
20 {
21  uint64_t Num = 15;
22  int Digits = 0;
23  Digits = CountDigits(Num);
24  //printf("%d has %d digits\n",Num,Digits);
25  FO_ASSERT_EQUAL(Digits, 2);
26 }
27 
35 {
36  Cksum *SumTest;
37  FILE *Fin;
38  Filename = "../testdata/test.zip";
39  char Fuid[1024];
40  int i;
41 
42  memset(Fuid,0,sizeof(Fuid));
43  Fin = fopen(Filename,"rb");
44  if (Fin)
45  {
46  SumTest = SumComputeFile(Fin);
47  if (SumTest)
48  {
49  for(i=0; i<20; i++) { sprintf(Fuid+0+i*2,"%02X",SumTest->SHA1digest[i]); }
50  Fuid[40]='.';
51  for(i=0; i<16; i++) { sprintf(Fuid+41+i*2,"%02X",SumTest->MD5digest[i]); }
52  Fuid[73]='.';
53  snprintf(Fuid+74,sizeof(Fuid)-74,"%Lu",(long long unsigned int)SumTest->DataLen);
54  //printf("%s +++++++++\n",Fuid);
55  FO_ASSERT_STRING_EQUAL(Fuid, "5CBBD4E0487601E9160A5C887E5C0C1E6541B3DE.5234FC4D5F9786A51B2206B9DEEACA68.825");
56  FO_ASSERT_EQUAL((int)SumTest->DataLen, 825);
57  free(SumTest);
58  }
59  fclose(Fin);
60  }
61 }
62 
71 {
72  Cksum *SumTest;
73  FILE *Fin;
74  Filename = "../testdata/test.zip";
75  char *Fuid = NULL;
76 
77  Fin = fopen(Filename,"rb");
78  if (Fin)
79  {
80  SumTest = SumComputeFile(Fin);
81  if (SumTest)
82  {
83  Fuid = SumToString(SumTest);
84  FO_ASSERT_STRING_EQUAL(Fuid, "5CBBD4E0487601E9160A5C887E5C0C1E6541B3DE.5234FC4D5F9786A51B2206B9DEEACA68.825");
85  free(SumTest);
86  }
87  fclose(Fin);
88  }
89 }
90 /* ************************************************************************** */
91 /* **** cunit test cases **************************************************** */
92 /* ************************************************************************** */
93 
94 CU_TestInfo Checksum_testcases[] =
95 {
96  {"Checksum function CountDigits:", testCountDigits},
97  {"Checksum function SumComputeFile:", testSumComputeFile},
98  {"Checksum function SumToString:", testSumToString},
99  CU_TEST_INFO_NULL
100 };
char * SumToString(Cksum *Sum)
Return string representing a Cksum. NOTE: The calling function must free() the string!
Definition: checksum.c:237
int CountDigits(uint64_t Num)
Count how many digits are in a number.
Definition: checksum.c:97
Cksum * SumComputeFile(FILE *Fin)
Compute the checksum, allocate and return a string containing the sum value.
Definition: checksum.c:115
Store check sum of a file.
Definition: checksum.h:33
uint8_t SHA1digest[20]
SHA1 digest of the file.
Definition: checksum.h:35
uint64_t DataLen
Size of the file.
Definition: checksum.h:36
uint8_t MD5digest[16]
MD5 digest of the file.
Definition: checksum.h:34
CU_TestInfo Checksum_testcases[]
Checksum test cases.
Definition: test_Checksum.c:94
void testSumToString()
test function SumToString
Definition: test_Checksum.c:70
void testCountDigits()
test function CountDigits
Definition: test_Checksum.c:19
void testSumComputeFile()
test function SumComputeFile
Definition: test_Checksum.c:34
char * Filename
Filename.
Definition: run_tests.c:17