aboutsummaryrefslogtreecommitdiffstats
path: root/test/ut.c
blob: 8a5fd91dc214b8fb008b1307902305073d3f31e5 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include "contrib/fctx/fct.h"
#include <stdlib.h>
#include <errno.h>

#include <stdlib.h>

#include "lib/dmsdos.h"
#include "lib/lib_interface.h"

#define SECSZ 512

static int create_image(int fd) {
	FILE* src = popen("cat data/DRVSPACE.001.xz | unxz", "r");
	if (!src) {
		perror("unable to open data source: ");
		return -1;
	}

	FILE* dst = fdopen(fd, "r+");
	if (!dst) {
		perror("fdopen error: ");
		fclose(src);
		return -1;
	}

	char buf[1024];
	size_t bytes;
	while (0 < (bytes = fread(buf, 1, sizeof(buf), src)))
		fwrite(buf, 1, bytes, dst);

	if (fclose(dst) == -1) {
		perror("fclose error: ");
		return -1;
	}

	if (pclose(src) == -1) {
		perror("pclose error: ");
		return -1;
	}

	return 0;
}

static int prepare_env(char* template) {
	int temp_fd = mkstemp(template); 
	if (temp_fd < 0) {
		perror("mkstemp failed: ");
		return -1;
	}

//	if (unlink(template) < 0) {
//		perror("unlink failed: ");
//		return -1;
//	}

	if (create_image(temp_fd) < 0) {
		return -1;
	}

	return temp_fd;
}

#define PREPARE_ENV \
	char template[] = "/tmp/dmsdos_ut_XXXXXX"; 	\
	do {					\
		int fd = prepare_env(template);	\
		fct_chk(fd > 0);		\
	} while (0)				\


static int check_short_filename(const unsigned char* data, short offset,
	const char* expected)
{
	char filename[9] = {0};
	int j = 0;
	while (j < 8 && data[offset + j] != ' ') {
		filename[j] = data[offset + j];
		j++;
	}

	return strcmp(filename, expected) == 0;
}

FCT_BGN()
{
	FCT_SUITE_BGN(RW)
	{
		FCT_TEST_BGN(UpdateDirEntry)
		{
			PREPARE_ENV;
			struct super_block* sb = open_cvf(template, 1);
			fct_chk(sb);
			Dblsb* dblsb = MSDOS_SB(sb)->private_data;
			fct_chk(dblsb);
			size_t sz = dblsb->s_sectperclust * SECSZ;
                        unsigned char* data = malloc(sz);
			fct_chk(data);

			// Read directory
    			int i = dmsdos_read_cluster(sb, data, 6);
			fct_chk(i > 0);

			fct_chk(check_short_filename(data, 96, "TESTFI~1"));

			data[96] = 'X';

			// Write new cluster
			dmsdos_write_cluster(sb, data, sz, 6, 0, UC_NORMAL); 

			memset(data, 0, sz);

			// Read directory again
    			i = dmsdos_read_cluster(sb, data, 6);
			fct_chk(i > 0);
			fct_chk(check_short_filename(data, 96, "XESTFI~1"));

			close_cvf(sb);

			sb = open_cvf(template, 1);
			fct_chk(sb);
			dblsb = MSDOS_SB(sb)->private_data;
			fct_chk(dblsb);

			// Read directory again
    			i = dmsdos_read_cluster(sb, data, 6);
			fct_chk(i > 0);
			fct_chk(check_short_filename(data, 96, "XESTFI~1"));
		}
		FCT_TEST_END();
	}
	FCT_SUITE_END();
}
FCT_END();