FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
test_Checksum.c
Go to the documentation of this file.
1 /*********************************************************************
2 Copyright (C) 2012 Hewlett-Packard Development Company, L.P.
3 
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 version 2 as published by the Free Software Foundation.
7 
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12 
13 You should have received a copy of the GNU General Public License along
14 with this program; if not, write to the Free Software Foundation, Inc.,
15 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 *********************************************************************/
17 #include "run_tests.h"
18 
31 {
32  uint64_t Num = 15;
33  int Digits = 0;
34  Digits = CountDigits(Num);
35  //printf("%d has %d digits\n",Num,Digits);
36  FO_ASSERT_EQUAL(Digits, 2);
37 }
38 
46 {
47  Cksum *SumTest;
48  FILE *Fin;
49  Filename = "../testdata/test.zip";
50  char Fuid[1024];
51  int i;
52 
53  memset(Fuid,0,sizeof(Fuid));
54  Fin = fopen(Filename,"rb");
55  if (Fin)
56  {
57  SumTest = SumComputeFile(Fin);
58  if (SumTest)
59  {
60  for(i=0; i<20; i++) { sprintf(Fuid+0+i*2,"%02X",SumTest->SHA1digest[i]); }
61  Fuid[40]='.';
62  for(i=0; i<16; i++) { sprintf(Fuid+41+i*2,"%02X",SumTest->MD5digest[i]); }
63  Fuid[73]='.';
64  snprintf(Fuid+74,sizeof(Fuid)-74,"%Lu",(long long unsigned int)SumTest->DataLen);
65  //printf("%s +++++++++\n",Fuid);
66  FO_ASSERT_STRING_EQUAL(Fuid, "5CBBD4E0487601E9160A5C887E5C0C1E6541B3DE.5234FC4D5F9786A51B2206B9DEEACA68.825");
67  FO_ASSERT_EQUAL((int)SumTest->DataLen, 825);
68  free(SumTest);
69  }
70  fclose(Fin);
71  }
72 }
73 
82 {
83  Cksum *SumTest;
84  FILE *Fin;
85  Filename = "../testdata/test.zip";
86  char *Fuid = NULL;
87 
88  Fin = fopen(Filename,"rb");
89  if (Fin)
90  {
91  SumTest = SumComputeFile(Fin);
92  if (SumTest)
93  {
94  Fuid = SumToString(SumTest);
95  FO_ASSERT_STRING_EQUAL(Fuid, "5CBBD4E0487601E9160A5C887E5C0C1E6541B3DE.5234FC4D5F9786A51B2206B9DEEACA68.825");
96  free(SumTest);
97  }
98  fclose(Fin);
99  }
100 }
101 /* ************************************************************************** */
102 /* **** cunit test cases **************************************************** */
103 /* ************************************************************************** */
104 
105 CU_TestInfo Checksum_testcases[] =
106 {
107  {"Checksum function CountDigits:", testCountDigits},
108  {"Checksum function SumComputeFile:", testSumComputeFile},
109  {"Checksum function SumToString:", testSumToString},
110  CU_TEST_INFO_NULL
111 };
Cksum * SumComputeFile(FILE *Fin)
Compute the checksum, allocate and return a string containing the sum value.
Definition: checksum.c:127
CU_TestInfo Checksum_testcases[]
Checksum test cases.
uint8_t SHA1digest[20]
SHA1 digest of the file.
Definition: checksum.h:42
int CountDigits(uint64_t Num)
Count how many digits are in a number.
Definition: checksum.c:109
uint8_t MD5digest[16]
MD5 digest of the file.
Definition: checksum.h:41
void testCountDigits()
test function CountDigits
Definition: test_Checksum.c:30
Store check sum of a file.
Definition: checksum.h:39
uint64_t DataLen
Size of the file.
Definition: checksum.h:43
char * SumToString(Cksum *Sum)
Return string representing a Cksum. NOTE: The calling function must free() the string! ...
Definition: checksum.c:249
char * Filename
Filename.
Definition: run_tests.c:28
void testSumComputeFile()
test function SumComputeFile
Definition: test_Checksum.c:45
void testSumToString()
test function SumToString
Definition: test_Checksum.c:81