Skip to content

Commit 8546f26

Browse files
feat: add test for startup parameter
Test is startup parameter is working as expected. This is a work in progress
1 parent 9a99c00 commit 8546f26

File tree

6 files changed

+1363
-2
lines changed

6 files changed

+1363
-2
lines changed
Lines changed: 320 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,320 @@
1+
unit torrent_miscellaneous;
2+
3+
{
4+
Some generic routine
5+
6+
}
7+
{$mode objfpc}{$H+}
8+
9+
interface
10+
11+
uses
12+
Classes, SysUtils;
13+
14+
type
15+
//Updated torrent file trackers list order.
16+
TTrackerListOrder = (
17+
18+
// Console parameter: -U0
19+
// Insert new trackers list BEFORE, the original trackers list inside the torrent file.
20+
// And remove possible duplicated trackers from the ORIGINAL trackers list.
21+
tloInsertNewBeforeAndKeepNewIntact = 0,
22+
23+
// Console parameter: -U1
24+
// Insert new trackers list BEFORE, the original trackers list inside the torrent file.
25+
// And remove possible duplicated trackers from the NEW trackers list.
26+
tloInsertNewBeforeAndKeepOriginalIntact,
27+
28+
// Console parameter: -U2
29+
// Append new trackers list AFTER, the original trackers list inside the torrent file.
30+
// And remove possible duplicated trackers from the ORIGINAL trackers list.
31+
tloAppendNewAfterAndKeepNewIntact,
32+
33+
// Console parameter: -U3
34+
// Append new trackers list AFTER, the original trackers list inside the torrent file.
35+
// And remove possible duplicated trackers from the NEW trackers list.
36+
tloAppendNewAfterAndKeepOriginalIntact,
37+
38+
// Console parameter: -U4
39+
// Sort the trackers list by name.
40+
tloSort,
41+
42+
// Console parameter: -U5
43+
// Append new trackers list BEFORE, the original trackers list inside the torrent file.
44+
// Keep original tracker list 'of each individual torrent' unchanged and remove nothing.
45+
// Every torent may have diferent tracker list!
46+
tloInsertNewBeforeAndKeepOriginalIntactAndRemoveNothing,
47+
48+
// Console parameter: -U6
49+
// Append new trackers list AFTER, the original trackers list inside the torrent file.
50+
// Keep original tracker list 'of each individual torrent' unchanged and remove nothing.
51+
// Every torent may have diferent tracker list!
52+
tloAppendNewAfterAndKeepOriginalIntactAndRemoveNothing,
53+
54+
// Console parameter: -U7
55+
// Randomize the trackers list.
56+
tloRandomize
57+
58+
);
59+
60+
procedure RemoveTrackersFromList(RemoveList, UpdatedList: TStringList);
61+
procedure SanatizeTrackerList(StringList: TStringList);
62+
procedure RandomizeTrackerList(StringList: TStringList);
63+
procedure AddButIngnoreDuplicates(StringList: TStringList; const Str: UTF8String);
64+
function ByteSizeToBiggerSizeFormatStr(ByteSize: int64): string;
65+
function LoadTorrentViaDir(const Dir: UTF8String;
66+
TorrentFilesNameStringList: TStringList): boolean;
67+
68+
const
69+
VALID_TRACKERS_URL: array[0..4] of UTF8String =
70+
(
71+
'udp://',
72+
'http://',
73+
'https://',
74+
'ws://',
75+
'wss://'
76+
);
77+
78+
79+
implementation
80+
81+
uses LazUTF8, LazFileUtils;
82+
83+
procedure RemoveTrackersFromList(RemoveList, UpdatedList: TStringList);
84+
var
85+
TrackerStr: string;
86+
i: integer;
87+
begin
88+
//Remove the trackers that we do not want in the list
89+
for TrackerStr in RemoveList do
90+
begin
91+
//Find the tracker and remove it from the list.
92+
i := UpdatedList.IndexOf(UTF8Trim(TrackerStr));
93+
if i >= 0 then
94+
UpdatedList.Delete(i);
95+
end;
96+
end;
97+
98+
procedure SanatizeTrackerList(StringList: TStringList);
99+
var
100+
TrackerStr: UTF8String;
101+
i: integer;
102+
PositionSpace: PtrInt;
103+
begin
104+
//remove all empty space and comment after the URL
105+
106+
if StringList.Count > 0 then
107+
begin
108+
for i := 0 to StringList.Count - 1 do
109+
begin
110+
//process every line one by one
111+
TrackerStr := StringList[i];
112+
113+
//remove empty spaces at the begin/end of line
114+
TrackerStr := UTF8Trim(TrackerStr);
115+
116+
//find the first 'space' found in line
117+
PositionSpace := UTF8Pos(' ', TrackerStr);
118+
if PositionSpace > 0 then
119+
begin
120+
// There is a 'space' found
121+
// Remove everything after this 'space'
122+
TrackerStr := UTF8LeftStr(TrackerStr, PositionSpace - 1);
123+
end;
124+
125+
//write the modified string back
126+
StringList[i] := TrackerStr;
127+
end;
128+
end;
129+
end;
130+
131+
procedure RandomizeTrackerList(StringList: TStringList);
132+
var
133+
i: integer;
134+
begin
135+
//The order of the string list must be randomize
136+
if StringList.Count > 1 then
137+
begin
138+
Randomize;
139+
for i := 0 to StringList.Count - 1 do
140+
begin
141+
StringList.Exchange(i, Random(StringList.Count));
142+
end;
143+
end;
144+
end;
145+
146+
procedure AddButIngnoreDuplicates(StringList: TStringList; const Str: UTF8String);
147+
begin
148+
//Stringlist that are not sorted must use IndexOf to ignore Duplicates.
149+
if not StringList.Sorted then
150+
begin
151+
//not sorted version
152+
if StringList.IndexOf(Str) < 0 then
153+
begin
154+
StringList.add(Str);
155+
end;
156+
end
157+
else
158+
begin
159+
//sorted version
160+
StringList.add(Str);
161+
end;
162+
163+
end;
164+
165+
function ByteSizeToBiggerSizeFormatStr(ByteSize: int64): string;
166+
begin
167+
if ByteSize >= (1024 * 1024 * 1024) then
168+
Result := Format('%0.2f GiB', [ByteSize / (1024 * 1024 * 1024)])
169+
else
170+
if ByteSize >= (1024 * 1024) then
171+
Result := Format('%0.2f MiB', [ByteSize / (1024 * 1024)])
172+
else
173+
if ByteSize >= (1024) then
174+
Result := Format('%0.2f KiB', [ByteSize / 1024]);
175+
Result := Result + Format(' (%d Bytes)', [ByteSize]);
176+
end;
177+
178+
179+
function LoadTorrentViaDir(const Dir: UTF8String;
180+
TorrentFilesNameStringList: TStringList): boolean;
181+
var
182+
Info: TSearchRec;
183+
begin
184+
//place all the torrent file name in TorrentFilesNameStringList
185+
// TorrentFilesNameStringList := TStringList.Create;
186+
187+
if FindFirstUTF8(dir + PathDelim + '*.torrent', faAnyFile, Info) = 0 then
188+
begin
189+
//Read all the torrent files inside this dir.
190+
repeat
191+
TorrentFilesNameStringList.Add(UTF8Trim(dir + PathDelim + Info.Name));
192+
until FindNextUTF8(info) <> 0;
193+
end;
194+
FindCloseUTF8(Info);
195+
196+
Result := TorrentFilesNameStringList.Count > 0;
197+
198+
end;
199+
200+
201+
202+
function DecodeConsoleUpdateParameter(ConsoleUpdateParameter: UTF8String;
203+
var TrackerListOrder: TTrackerListOrder; LogStringList: TStringList = nil): boolean;
204+
var
205+
i: integer;
206+
begin
207+
//Decode the '-Ux' x is number [0..4]
208+
209+
//verify string content.
210+
Result := (Pos('-U', ConsoleUpdateParameter) = 1) and
211+
(length(ConsoleUpdateParameter) = 3);
212+
213+
if Result then
214+
begin
215+
//get the number
216+
Result := TryStrToInt(ConsoleUpdateParameter[3], i);
217+
if Result then
218+
begin
219+
//decode number [0..7]
220+
case i of
221+
0: TrackerListOrder := tloInsertNewBeforeAndKeepNewIntact;
222+
1: TrackerListOrder := tloInsertNewBeforeAndKeepOriginalIntact;
223+
2: TrackerListOrder := tloAppendNewAfterAndKeepNewIntact;
224+
3: TrackerListOrder := tloAppendNewAfterAndKeepOriginalIntact;
225+
4: TrackerListOrder := tloSort;
226+
5: TrackerListOrder := tloInsertNewBeforeAndKeepOriginalIntactAndRemoveNothing;
227+
6: TrackerListOrder := tloAppendNewAfterAndKeepOriginalIntactAndRemoveNothing;
228+
7: TrackerListOrder := tloRandomize;
229+
else
230+
begin
231+
//the number is out of range.
232+
Result := False;
233+
end;
234+
end;
235+
end;
236+
end;
237+
238+
if not Result then
239+
begin
240+
if assigned(LogStringList) then
241+
begin
242+
LogStringList.Add('ERROR: can not decode update parameter -U : ' +
243+
ConsoleUpdateParameter);
244+
end;
245+
246+
end;
247+
end;
248+
249+
250+
251+
252+
function ConsoleModeDecodeParameter(out FileNameOrDirStr: UTF8String;
253+
out CountParameter: integer; var TrackerListOrder: TTrackerListOrder;
254+
LogStringList: TStringList = nil): boolean;
255+
begin
256+
257+
// Console mode can be started with 2 parameter
258+
// Update methode: -U0 , -U1, -U2, -U3, -U4
259+
// String with a link to folder or to torrent file. 'C:\dir'
260+
261+
CountParameter := Paramcount;
262+
case CountParameter of
263+
0:
264+
begin
265+
if assigned(LogStringList) then
266+
begin
267+
LogStringList.Add('ERROR: There are no parameter detected.');
268+
end;
269+
Result := False;
270+
exit;
271+
end;
272+
1:
273+
begin
274+
//one parameter. Must be a link.
275+
FileNameOrDirStr := UTF8Trim(ParamStr(1));
276+
//Keep the same behaviour as the previeus software version.
277+
// FTrackerListOrderForUpdatedTorrent := tloSort;
278+
TrackerListOrder := tloSort;
279+
Result := True;
280+
end;
281+
2:
282+
begin
283+
//Two parameters. The user can select the update methode.
284+
//Check for '-U' contruction as first parameter
285+
if (Pos('-U', ParamStr(1)) = 1) then
286+
begin
287+
//Update parameter is the first parameter
288+
Result := DecodeConsoleUpdateParameter(ParamStr(1), TrackerListOrder,
289+
LogStringList);
290+
FileNameOrDirStr := UTF8Trim(ParamStr(2));
291+
end
292+
else
293+
begin
294+
//Update parameter is the second parameter
295+
Result := DecodeConsoleUpdateParameter(ParamStr(2), TrackerListOrder,
296+
LogStringList);
297+
FileNameOrDirStr := UTF8Trim(ParamStr(1));
298+
end;
299+
300+
end;
301+
else
302+
begin
303+
if assigned(LogStringList) then
304+
begin
305+
LogStringList.Add('ERROR: There can only be maximum of 2 parameter. Not: ' +
306+
IntToStr(ParamCount));
307+
end;
308+
end;
309+
Result := False;
310+
exit;
311+
end;
312+
313+
end;
314+
315+
316+
317+
318+
319+
320+
end.

source/project/unit_test/tracker_editor_test.lpi

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
<PackageName Value="FCL"/>
3737
</Item2>
3838
</RequiredPackages>
39-
<Units Count="3">
39+
<Units Count="8">
4040
<Unit0>
4141
<Filename Value="tracker_editor_test.lpr"/>
4242
<IsPartOfProject Value="True"/>
@@ -49,6 +49,26 @@
4949
<Filename Value="..\..\code\newtrackon.pas"/>
5050
<IsPartOfProject Value="True"/>
5151
</Unit2>
52+
<Unit3>
53+
<Filename Value="..\..\test\test_start_up_parameter.pas"/>
54+
<IsPartOfProject Value="True"/>
55+
</Unit3>
56+
<Unit4>
57+
<Filename Value="..\..\code\torrent_miscellaneous.pas"/>
58+
<IsPartOfProject Value="True"/>
59+
</Unit4>
60+
<Unit5>
61+
<Filename Value="..\..\test\test_miscellaneous.pas"/>
62+
<IsPartOfProject Value="True"/>
63+
</Unit5>
64+
<Unit6>
65+
<Filename Value="..\..\code\ngosang_trackerslist.pas"/>
66+
<IsPartOfProject Value="True"/>
67+
</Unit6>
68+
<Unit7>
69+
<Filename Value="..\..\test\test_ngosang_trackers_list.pas"/>
70+
<IsPartOfProject Value="True"/>
71+
</Unit7>
5272
</Units>
5373
</ProjectOptions>
5474
<CompilerOptions>

source/project/unit_test/tracker_editor_test.lpr

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
uses
66
Classes, consoletestrunner, fpcunit,
7-
fpcunitreport, test_newtrackon;
7+
fpcunitreport, test_newtrackon, test_ngosang_trackers_list, test_start_up_parameter,
8+
torrent_miscellaneous, test_miscellaneous, ngosang_trackerslist;
89

910
type
1011

0 commit comments

Comments
 (0)