aboutsummaryrefslogtreecommitdiffstats
path: root/librecrypt_next_algorithm.c
blob: 831f52e700bce781be12fd881a8295e42a7ec2e9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* See LICENSE file for copyright and license details. */
#include "common.h"
#ifndef TEST


extern inline char *librecrypt_next_algorithm(char **hash);


#else
# ifndef FUZZ


static void
testcase_1(void)
{
	char hash[] = ">a$b>c$d>e$f$";
	char *s = hash, *a;

	EXPECT((a = librecrypt_next_algorithm(&s)));
	EXPECT(s != NULL);
	EXPECT(!strcmp(a, ""));
	EXPECT(!strcmp(s, "a$b>c$d>e$f$"));

	EXPECT((a = librecrypt_next_algorithm(&s)));
	EXPECT(s != NULL);
	EXPECT(!strcmp(a, "a$b"));
	EXPECT(!strcmp(s, "c$d>e$f$"));

	EXPECT((a = librecrypt_next_algorithm(&s)));
	EXPECT(s != NULL);
	EXPECT(!strcmp(a, "c$d"));
	EXPECT(!strcmp(s, "e$f$"));

	EXPECT((a = librecrypt_next_algorithm(&s)));
	EXPECT(s == NULL); /* state is set to NULL when done */
	EXPECT(!strcmp(a, "e$f$"));

	/* Check librecrypt_next_algorithm when done returns NULL without changing state */
	EXPECT(librecrypt_next_algorithm(&s) == NULL);
	EXPECT(s == NULL);
}


static void
testcase_2(void)
{
	char hash[] = "a$b>c$d>e$f$>";
	char *s = hash, *a;

	EXPECT((a = librecrypt_next_algorithm(&s)));
	EXPECT(s != NULL);
	EXPECT(!strcmp(a, "a$b"));
	EXPECT(!strcmp(s, "c$d>e$f$>"));

	EXPECT((a = librecrypt_next_algorithm(&s)));
	EXPECT(s != NULL);
	EXPECT(!strcmp(a, "c$d"));
	EXPECT(!strcmp(s, "e$f$>"));

	EXPECT((a = librecrypt_next_algorithm(&s)));
	EXPECT(s != NULL);
	EXPECT(!strcmp(a, "e$f$"));
	EXPECT(!strcmp(s, ""));

	EXPECT((a = librecrypt_next_algorithm(&s)));
	EXPECT(s == NULL); /* state is set to NULL when done */
	EXPECT(!strcmp(a, ""));

	/* Check librecrypt_next_algorithm when done returns NULL without changing state */
	EXPECT(librecrypt_next_algorithm(&s) == NULL);
	EXPECT(s == NULL);
}


int
main(void)
{
	SET_UP_ALARM();
	INIT_RESOURCE_TEST();

	testcase_1();
	testcase_2();

	STOP_RESOURCE_TEST();
	return 0;
}


# else


int
LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
	char *hash, *r;
	size_t sum = 0u;
	hash = malloc(size + 1u);
	assert(hash);
	memcpy(hash, data, size);
	hash[size] = '\0';
	for (;;) {
		r = librecrypt_next_algorithm(&hash);
		if (!r)
			break;
		sum += strlen(r) + 1u;
	}
	EXPECT(sum == size + 1u);
	return 0;
}


# endif
#endif