1() = evalfile ("./test.sl");
2require ("chksum");
3
4private variable Chksum_Map = Assoc_Type[];
5
6private define add_entry (str, md5, sha1)
7{
8   Chksum_Map[str] = struct
9     {
10	md5 = md5,
11	sha1 = sha1,
12     };
13}
14
15add_entry ("",
16	   "d41d8cd98f00b204e9800998ecf8427e",
17	   "da39a3ee5e6b4b0d3255bfef95601890afd80709"
18	  );
19
20add_entry ("1",
21	   "c4ca4238a0b923820dcc509a6f75849b",
22	   "356a192b7913b04c54574d18c28d46e6395428ab"
23	  );
24
25add_entry ("Four score and seven years ago",
26	   "8bc88284b17081c54df4daa4576251f7",
27	   "0e6089220e01abfc69188c555f1a37201d2fa37f"
28	  );
29
30add_entry ("Four score and seven years ago our fathers brought forth on this continent a new nation, conceived in liberty, and dedicated to the proposition that all men are created equal.",
31	   "73168f4191456bc526791a83c064997b",
32	   "3eabb94199e5347c55ae914ef75803ff0970d9e4"
33	  );
34
35private define test_accumulate (name, str, chksum)
36{
37   str = typecast (str, BString_Type); %  test bstrings too
38   variable n = bstrlen (str);
39   _for (1, n, 1)
40     {
41	variable i0 = ();
42	variable s0 = str[[0:i0-1]];
43	variable s1 = str[[i0:]];
44	variable m = bstrlen (s1);
45	_for (1, m, 1)
46	  {
47	     variable i1 = ();
48	     variable s10 = s1[[0:i1-1]];
49	     variable s11 = s1[[i1:]];
50	     variable c = chksum_new (name);
51	     c.accumulate (s0);
52	     c.accumulate (s10);
53	     c.accumulate (s11);
54	     if (chksum != c.close ())
55	       failed ("Failed to compute %s for the partition '%s', '%s', '%s'", name, s0, s10, s11);
56	  }
57     }
58}
59
60private define test_chksum_file (func, data)
61{
62   variable tmpfile = sprintf ("/tmp/test_chksum_%d_%d", getpid(), _time());
63   variable fp = fopen (tmpfile, "wb");
64   if (fp == NULL)
65     return;
66   () = fwrite (data, fp);
67   () = fclose (fp);
68   variable s = (@func)(tmpfile);
69   () = remove (tmpfile);
70   return s;
71}
72
73private define test_module (module_name)
74{
75   testing_module (module_name);
76
77   foreach (assoc_get_keys (Chksum_Map))
78     {
79	variable key = ();
80	variable s = Chksum_Map[key];
81
82	variable md5 = md5sum (key);
83	if (md5 != s.md5)
84	  failed ("MD5 failure for %s, got %s instead of %s", key, md5, s.md5);
85
86	test_accumulate ("md5", key, md5);
87
88	md5 = test_chksum_file (&md5sum_file, key);
89	if (md5 != s.md5)
90	  failed ("md5sum_file failed: got %s, expected %s", md5, s.md5);
91
92	variable sha1 = sha1sum (key);
93	if (sha1 != s.sha1)
94	  failed ("SHA1 failure for %s, got %s instead of %s", key, sha1, s.sha1);
95
96	test_accumulate ("sha1", key, sha1);
97
98	sha1 = test_chksum_file (&sha1sum_file, key);
99	if (sha1 != s.sha1)
100	  failed ("sha1sum_file failed: got %s, expected %s", sha1, s.sha1);
101     }
102}
103
104define slsh_main ()
105{
106   test_module ("chksum");
107   end_test ();
108}
109
110