1 /*
2 * Copyright 2020, Haiku, Inc.
3 * Distributed under the terms of the MIT License.
4 */
5
6 #include "FileTest.h"
7
8 #include <stdlib.h>
9
10 #include <File.h>
11 #include <FileRequest.h>
12 #include <Url.h>
13 #include <UrlProtocolRoster.h>
14 #include <UrlProtocolListener.h>
15
16 #include <ThreadedTestCaller.h>
17 #include <TestUtils.h>
18
19 #include "TestServer.h"
20
21
22 using namespace BPrivate::Network;
23
24
25 class StopTestListener : public BDataIO {
26 public:
StopTestListener(BUrlRequest * request=NULL)27 StopTestListener(BUrlRequest* request = NULL)
28 {
29 SetRequest(request);
30 }
31
Write(const void *,size_t size)32 ssize_t Write(const void*, size_t size)
33 {
34 fRequest->Stop();
35 return size;
36 }
37
SetRequest(BUrlRequest * request)38 void SetRequest(BUrlRequest* request)
39 {
40 fRequest = request;
41 }
42
43 private:
44 BUrlRequest* fRequest;
45 };
46
47
48 void
StopTest()49 FileTest::StopTest()
50 {
51 StopTestListener listener;
52 char tmpl[] = "/tmp/filestoptestXXXXXX";
53 int fd = mkstemp(tmpl);
54 CHK(fd != -1);
55 close(fd);
56
57 BFile file(tmpl, O_WRONLY | O_CREAT);
58 CHK(file.InitCheck() == B_OK);
59 BString content;
60 /* number chosen to be larger than BFileRequest buffer, adjust as needed */
61 content.Append('f', 40960);
62
63 CHK(file.WriteExactly(content.String(), content.Length()) == B_OK);
64
65 BUrl url("file://");
66 url.SetPath(tmpl);
67 BUrlRequest *request = BUrlProtocolRoster::MakeRequest(url, &listener);
68 CHK(request != NULL);
69
70 listener.SetRequest(request);
71 thread_id thr = request->Run();
72 status_t dummy;
73 wait_for_thread(thr, &dummy);
74
75 CHK(request->Status() == B_INTERRUPTED);
76
77 delete request;
78
79 request = BUrlProtocolRoster::MakeRequest("file:///", &listener);
80 CHK(request != NULL);
81
82 listener.SetRequest(request);
83 thr = request->Run();
84 wait_for_thread(thr, &dummy);
85
86 CHK(request->Status() == B_INTERRUPTED);
87
88 delete request;
89 }
90
91
92 /* static */ void
AddTests(BTestSuite & parent)93 FileTest::AddTests(BTestSuite& parent)
94 {
95 CppUnit::TestSuite *suite = new CppUnit::TestSuite("FileTest");
96
97 suite->addTest(new CppUnit::TestCaller<FileTest>("FileTest: Stop requests",
98 &FileTest::StopTest));
99
100 parent.addTest("FileTest", suite);
101 }
102