@@ -90,7 +90,7 @@ START_TEST(test_checksum_fd)
9090}
9191END_TEST
9292
93- START_TEST (test_cached_checksum )
93+ START_TEST (test_cached_checksum_matches )
9494{
9595 FILE * f ;
9696 int fd , ret ;
@@ -105,7 +105,7 @@ START_TEST(test_cached_checksum)
105105 gchar * checksum_key = g_strconcat (XATTR_CHKSUM_PREFIX , "sha256" , NULL );
106106 gchar * mtime_str = NULL ;
107107
108- filename = lr_pathconcat (test_globals .tmpdir , "/test_checksum " , NULL );
108+ filename = lr_pathconcat (test_globals .tmpdir , "/test_checksum_matches " , NULL );
109109 f = fopen (filename , "w" );
110110 fail_if (f == NULL );
111111 fwrite ("foo\nbar\n" , 1 , 8 , f );
@@ -178,6 +178,67 @@ START_TEST(test_cached_checksum)
178178}
179179END_TEST
180180
181+ START_TEST (test_cached_checksum_value )
182+ {
183+ FILE * f ;
184+ int fd , ret ;
185+ gboolean checksum_ret , matches ;
186+ ssize_t attr_ret ;
187+ char * filename ;
188+ static char * expected = "d78931fcf2660108eec0d6674ecb4e02401b5256a6b5ee82527766ef6d198c67" ;
189+ struct stat st ;
190+ char buf [256 ];
191+ GError * tmp_err = NULL ;
192+ gchar * timestamp_key = g_strconcat (XATTR_CHKSUM_PREFIX , "mtime" , NULL );
193+ gchar * checksum_key = g_strconcat (XATTR_CHKSUM_PREFIX , "sha256" , NULL );
194+ gchar * mtime_str = NULL ;
195+ gchar * calculated = NULL ;
196+
197+ filename = lr_pathconcat (test_globals .tmpdir , "/test_checksum_value" , NULL );
198+ f = fopen (filename , "w" );
199+ fail_if (f == NULL );
200+ fwrite ("foo\nbar\n" , 1 , 8 , f );
201+ fclose (f );
202+
203+ // Assert no cached checksum exists
204+ attr_ret = GETXATTR (filename , timestamp_key , & buf , sizeof (buf )- 1 );
205+ fail_if (attr_ret != -1 ); // Cached timestamp should not exists
206+ attr_ret = GETXATTR (filename , checksum_key , & buf , sizeof (buf )- 1 );
207+ fail_if (attr_ret != -1 ); // Cached checksum should not exists
208+
209+ // Calculate checksum
210+ fd = open (filename , O_RDONLY );
211+ fail_if (fd < 0 );
212+ checksum_ret = lr_checksum_fd_compare (LR_CHECKSUM_SHA256 ,
213+ fd ,
214+ "" ,
215+ 1 ,
216+ & matches ,
217+ & calculated ,
218+ & tmp_err );
219+ fail_if (tmp_err );
220+ fail_if (!checksum_ret );
221+ // We pass in an empty string for expected, so we must not match.
222+ fail_if (matches );
223+ close (fd );
224+ fail_if (strcmp (calculated , expected ));
225+
226+ // Assert no cached checksum exists
227+ // This assumes issue #235 is unresolved. Once it is, this code
228+ // should fail and the test will need updated.
229+ attr_ret = GETXATTR (filename , timestamp_key , & buf , sizeof (buf )- 1 );
230+ fail_if (attr_ret != -1 ); // Cached timestamp should not exists
231+ attr_ret = GETXATTR (filename , checksum_key , & buf , sizeof (buf )- 1 );
232+ fail_if (attr_ret != -1 ); // Cached checksum should not exists
233+
234+ lr_free (calculated );
235+ lr_free (filename );
236+ lr_free (timestamp_key );
237+ lr_free (checksum_key );
238+ lr_free (mtime_str );
239+ }
240+ END_TEST
241+
181242START_TEST (test_cached_checksum_clear )
182243{
183244 FILE * f ;
@@ -242,7 +303,8 @@ checksum_suite(void)
242303 Suite * s = suite_create ("checksum" );
243304 TCase * tc = tcase_create ("Main" );
244305 tcase_add_test (tc , test_checksum_fd );
245- tcase_add_test (tc , test_cached_checksum );
306+ tcase_add_test (tc , test_cached_checksum_matches );
307+ tcase_add_test (tc , test_cached_checksum_value );
246308 tcase_add_test (tc , test_cached_checksum_clear );
247309 suite_add_tcase (s , tc );
248310 return s ;
0 commit comments