ObjFW  Diff

Differences From Artifact [b3f4ed0fcd]:

To Artifact [404c597141]:


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
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







+

-
+
+

-
+
+

-
-
+
+

-




















-
-
+
+


-
+




-
-
-
-
+
-
-
-






-
+

-
-
+

-
+
-
-
-
+

-
-
-
+
+

-
-
+
+





-
+
-
-

 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#include "config.h"

#include <errno.h>
#include <string.h>

#import "TestsAppDelegate.h"
#import "ObjFW.h"
#import "ObjFWTest.h"

static OFString *const module = @"OFUNIXStreamSocket";
@interface OFUNIXStreamSocketTests: OTTestCase
@end

@implementation TestsAppDelegate (OFUNIXStreamSocketTests)
- (void)UNIXStreamSocketTests
@implementation OFUNIXStreamSocketTests
- (void)testUNIXStreamSocket
{
	void *pool = objc_autoreleasePoolPush();
	OFString *path;
	OFUNIXStreamSocket *sockClient, *sockServer, *sockAccepted;
	char buffer[5];

#if defined(OF_HAVE_FILES) && !defined(OF_IOS)
	path = [[OFSystemInfo temporaryDirectoryIRI]
	    IRIByAppendingPathComponent: [[OFUUID UUID] UUIDString]]
	    .fileSystemRepresentation;
#else
	/*
	 * We can have sockets, including UNIX sockets, while file support is
	 * disabled.
	 *
	 * We also use this code path for iOS, as the temporaryDirectory:RI is
	 * too long on the iOS simulator.
	 */
	path = [OFString stringWithFormat: @"/tmp/%@",
					   [[OFUUID UUID] UUIDString]];
#endif

	TEST(@"+[socket]", (sockClient = [OFUNIXStreamSocket socket]) &&
	    (sockServer = [OFUNIXStreamSocket socket]))
	sockClient = [OFUNIXStreamSocket socket];
	sockServer = [OFUNIXStreamSocket socket];

	@try {
		TEST(@"-[bindToPath:]", R([sockServer bindToPath: path]))
		[sockServer bindToPath: path];
	} @catch (OFBindSocketFailedException *e) {
		switch (e.errNo) {
		case EAFNOSUPPORT:
		case EPERM:
			[OFStdOut setForegroundColor: [OFColor lime]];
			[OFStdOut writeLine:
			    @"\r[OFUNIXStreamSocket] -[bindToPath:]: "
			    @"UNIX stream sockets unsupported, skipping tests"];
			OTSkip(@"UNIX stream sockets unsupported");

			objc_autoreleasePoolPop(pool);
			return;
		default:
			@throw e;
		}
	}

	@try {
		TEST(@"-[listen]", R([sockServer listen]))
		[sockServer listen];

		TEST(@"-[connectToPath:]",
		    R([sockClient connectToPath: path]))
		[sockClient connectToPath: path];

		TEST(@"-[accept]", (sockAccepted = [sockServer accept]))
		sockAccepted = [sockServer accept];

		TEST(@"-[writeBuffer:length:]",
		    R([sockAccepted writeBuffer: "Hello" length: 5]))
		[sockAccepted writeBuffer: "Hello" length: 5];

		TEST(@"-[readIntoBuffer:length:]",
		    [sockClient readIntoBuffer: buffer length: 5] == 5 &&
		    memcmp(buffer, "Hello", 5) == 0)
		OTAssertEqual([sockClient readIntoBuffer: buffer length: 5], 5);
		OTAssertEqual(memcmp(buffer, "Hello", 5), 0);

		TEST(@"-[remoteAddress]", OFSocketAddressUNIXPath(
		    sockAccepted.remoteAddress).length == 0)
		OTAssertEqual(OFSocketAddressUNIXPath(
		    sockAccepted.remoteAddress).length, 0);
	} @finally {
#ifdef OF_HAVE_FILES
		[[OFFileManager defaultManager] removeItemAtPath: path];
#endif
	}

}
	objc_autoreleasePoolPop(pool);
}
@end