forked from larsbs/id3v2lib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathset_test.c
257 lines (218 loc) · 6.57 KB
/
set_test.c
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
* This file is part of id3v2lib library
*
* Copyright (c) Lars Ruiz
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include "assertion_utils.h"
#include "id3v2lib.h"
#include "test_utils.h"
#include "set_test.h"
#define ORIGINAL_FILE "extra/file.mp3"
#define EDITED_FILE "extra/file_edited.mp3"
void edit_test()
{
// Clone the file to not having to modify the original
clone_file(ORIGINAL_FILE, EDITED_FILE);
ID3v2_Tag* tag = ID3v2_read_tag(EDITED_FILE);
if (tag == NULL)
{
tag = ID3v2_Tag_new_empty();
}
char* artist = ID3v2_to_unicode("Artist");
ID3v2_Tag_set_artist(tag, artist);
char* album = ID3v2_to_unicode("Album");
ID3v2_Tag_set_album(tag, album);
char* title = ID3v2_to_unicode("Title");
ID3v2_Tag_set_title(tag, title);
ID3v2_Tag_set_track(tag, "00");
char* album_artist = ID3v2_to_unicode("Album Artist");
ID3v2_Tag_set_album_artist(tag, album_artist);
char* genre = ID3v2_to_unicode("Genre");
ID3v2_Tag_set_genre(tag, genre);
ID3v2_Tag_set_year(tag, "0000");
ID3v2_Tag_set_disc_number(tag, "00");
char* composer = ID3v2_to_unicode("Writter");
ID3v2_Tag_set_composer(tag, composer);
char* comment = ID3v2_to_unicode("Comment");
char* short_desc = ID3v2_to_unicode("Short Description");
ID3v2_Tag_set_comment(tag, "eng", comment);
ID3v2_Tag_add_comment_frame(
tag,
&(ID3v2_CommentFrameInput){
.flags = "\0\0",
.language = "eng",
.short_description = short_desc,
.comment = comment,
}
);
FILE* album_cover_fp = fopen("extra/album_cover.png", "rb");
fseek(album_cover_fp, 0L, SEEK_END);
const int cover_file_size = ftell(album_cover_fp);
fseek(album_cover_fp, 0L, SEEK_SET);
char* picture_data = (char*) malloc(cover_file_size * sizeof(char));
fread(picture_data, 1, cover_file_size, album_cover_fp);
char* description = ID3v2_to_unicode("Description");
fclose(album_cover_fp);
ID3v2_Tag_set_album_cover(tag, ID3v2_MIME_TYPE_PNG, cover_file_size, picture_data);
ID3v2_Tag_add_apic_frame(
tag,
&(ID3v2_ApicFrameInput){
.flags = "\0\0",
.mime_type = ID3v2_MIME_TYPE_PNG,
.description = description,
.picture_type = ID3v2_PIC_TYPE_FRONT_COVER,
.data = picture_data,
.picture_size = cover_file_size,
}
);
// Write the edited tag
ID3v2_write_tag(EDITED_FILE, tag);
// Verify the written data
ID3v2_Tag* edited_tag = ID3v2_read_tag(EDITED_FILE);
assert(edited_tag != NULL);
assert_text_frame(
ID3v2_Tag_get_artist_frame(edited_tag),
&(ID3v2_TextFrameInput){
.id = ID3v2_ARTIST_FRAME_ID,
.flags = "\0\0",
.text = artist,
}
);
free(artist);
assert_text_frame(
ID3v2_Tag_get_album_frame(edited_tag),
&(ID3v2_TextFrameInput){
.id = ID3v2_ALBUM_FRAME_ID,
.flags = "\0\0",
.text = album,
}
);
free(album);
assert_text_frame(
ID3v2_Tag_get_title_frame(edited_tag),
&(ID3v2_TextFrameInput){
.id = ID3v2_TITLE_FRAME_ID,
.flags = "\0\0",
.text = title,
}
);
free(title);
assert_text_frame(
ID3v2_Tag_get_track_frame(edited_tag),
&(ID3v2_TextFrameInput){
.id = ID3v2_TRACK_FRAME_ID,
.flags = "\0\0",
.text = "00",
}
);
assert_text_frame(
ID3v2_Tag_get_album_artist_frame(edited_tag),
&(ID3v2_TextFrameInput){
.id = ID3v2_ALBUM_ARTIST_FRAME_ID,
.flags = "\0\0",
.text = album_artist,
}
);
free(album_artist);
assert_text_frame(
ID3v2_Tag_get_genre_frame(edited_tag),
&(ID3v2_TextFrameInput){
.id = ID3v2_GENRE_FRAME_ID,
.flags = "\0\0",
.text = genre,
}
);
free(genre);
assert_text_frame(
ID3v2_Tag_get_year_frame(edited_tag),
&(ID3v2_TextFrameInput){
.id = ID3v2_YEAR_FRAME_ID,
.flags = "\0\0",
.text = "0000",
}
);
assert_text_frame(
ID3v2_Tag_get_disc_number_frame(edited_tag),
&(ID3v2_TextFrameInput){
.id = ID3v2_DISC_NUMBER_FRAME_ID,
.flags = "\0\0",
.text = "00",
}
);
assert_text_frame(
ID3v2_Tag_get_composer_frame(edited_tag),
&(ID3v2_TextFrameInput){
.id = ID3v2_COMPOSER_FRAME_ID,
.flags = "\0\0",
.text = composer,
}
);
free(composer);
assert_comment_frame(
ID3v2_Tag_get_comment_frame(edited_tag),
&(ID3v2_CommentFrameInput){
.flags = "\0\0",
.language = "eng",
.short_description = EMPTY_UNICODE_STR,
.comment = comment,
}
);
ID3v2_FrameList* comments = ID3v2_Tag_get_comment_frames(edited_tag);
assert_comment_frame(
(ID3v2_CommentFrame*) comments->next->frame,
&(ID3v2_CommentFrameInput){
.flags = "\0\0",
.language = "eng",
.short_description = short_desc,
.comment = comment,
}
);
ID3v2_FrameList_unlink(comments);
free(comment);
free(short_desc);
assert_apic_frame(
ID3v2_Tag_get_album_cover_frame(edited_tag),
&(ID3v2_ApicFrameInput){
.flags = "\0\0",
.mime_type = ID3v2_MIME_TYPE_PNG,
.description = EMPTY_UNICODE_STR,
.picture_type = ID3v2_PIC_TYPE_FRONT_COVER,
.data = picture_data,
.picture_size = cover_file_size,
}
);
ID3v2_FrameList* apics = ID3v2_Tag_get_apic_frames(edited_tag);
assert_apic_frame(
(ID3v2_ApicFrame*) apics->next->frame,
&(ID3v2_ApicFrameInput){
.flags = "\0\0",
.mime_type = ID3v2_MIME_TYPE_PNG,
.description = description,
.picture_type = ID3v2_PIC_TYPE_FRONT_COVER,
.data = picture_data,
.picture_size = cover_file_size,
}
);
ID3v2_FrameList_unlink(apics);
free(picture_data);
free(description);
// Clean up
remove(EDITED_FILE);
ID3v2_Tag_free(tag);
ID3v2_Tag_free(edited_tag);
printf("SET TEST: OK\n");
}
void new_tag_test()
{
}
void set_test_main()
{
edit_test();
new_tag_test();
}