/NMSSHTests/NMSSHConfigTests.m

https://github.com/Lejdborg/NMSSH · Objective C · 612 lines · 406 code · 113 blank · 93 comment · 0 complexity · c68db864ab0adfccf3a9beed9e479671 MD5 · raw file

  1. //
  2. // NMSSHConfigTests.m
  3. // NMSSH
  4. //
  5. // Created by George Nachman on 5/8/14.
  6. //
  7. //
  8. #import <XCTest/XCTest.h>
  9. #import "NMSSHConfig.h"
  10. #import "NMSSHHostConfig.h"
  11. @interface NMSSHConfigTests : XCTestCase
  12. @end
  13. @implementation NMSSHConfigTests
  14. /**
  15. Tests that an empty config file is ok
  16. */
  17. - (void)testEmptyConfig {
  18. NSString *contents = @"";
  19. NMSSHConfig *config = [[NMSSHConfig alloc] initWithString:contents];
  20. NSArray *hostConfigs = config.hostConfigs;
  21. XCTAssertEqual([hostConfigs count], 0, @"Wrong number of configs read");
  22. }
  23. /**
  24. Tests that a config file with all supported keywords is properly read.
  25. */
  26. - (void)testAllKeywords {
  27. NSString *contents =
  28. @"Host pattern\n"
  29. @" Hostname hostname\n"
  30. @" User user\n"
  31. @" Port 1234\n"
  32. @" IdentityFile id_file\n";
  33. NMSSHConfig *config = [[NMSSHConfig alloc] initWithString:contents];
  34. NSArray *hostConfigs = config.hostConfigs;
  35. XCTAssertEqual([hostConfigs count], 1, @"Wrong number of configs read");
  36. NMSSHHostConfig *hostConfig = hostConfigs[0];
  37. XCTAssertEqualObjects(hostConfig.hostPatterns, @[ @"pattern" ], @"Patterns don't match");
  38. XCTAssertEqualObjects(hostConfig.hostname, @"hostname", @"Hostnames don't match");
  39. XCTAssertEqualObjects(hostConfig.user, @"user", @"Users don't match");
  40. XCTAssertEqualObjects(hostConfig.port, @1234, @"Port doesn't match");
  41. XCTAssertEqualObjects(hostConfig.identityFiles, @[ @"id_file" ], @"Identity files don't match");
  42. }
  43. /**
  44. Tests that comments are ignored.
  45. */
  46. - (void)testCommentsIgnored {
  47. NSString *contents =
  48. @"# Comment\n"
  49. @"Host pattern\n"
  50. @"# Comment\n"
  51. @" Hostname hostname\n"
  52. @"# Comment\n"
  53. @" Port 1234\n"
  54. @"# Comment\n"
  55. @" IdentityFile id_file\n"
  56. @"# Comment\n";
  57. NMSSHConfig *config = [[NMSSHConfig alloc] initWithString:contents];
  58. NSArray *hostConfigs = config.hostConfigs;
  59. XCTAssertEqual([hostConfigs count], 1, @"Wrong number of configs read");
  60. NMSSHHostConfig *hostConfig = hostConfigs[0];
  61. XCTAssertEqualObjects(hostConfig.hostPatterns, @[ @"pattern" ], @"Patterns don't match");
  62. XCTAssertEqualObjects(hostConfig.hostname, @"hostname", @"Hostnames don't match");
  63. XCTAssertEqualObjects(hostConfig.port, @1234, @"Port doesn't match");
  64. XCTAssertEqualObjects(hostConfig.identityFiles, @[ @"id_file" ], @"Identity files don't match");
  65. }
  66. /**
  67. Tests that empty lines are ignored.
  68. */
  69. - (void)testEmptyLinesIgnored {
  70. NSString *contents =
  71. @"\n"
  72. @"Host pattern\n"
  73. @"\n"
  74. @" Hostname hostname\n"
  75. @"\n"
  76. @" Port 1234\n"
  77. @"\n"
  78. @" IdentityFile id_file\n"
  79. @"\n";
  80. NMSSHConfig *config = [[NMSSHConfig alloc] initWithString:contents];
  81. NSArray *hostConfigs = config.hostConfigs;
  82. XCTAssertEqual([hostConfigs count], 1, @"Wrong number of configs read");
  83. NMSSHHostConfig *hostConfig = hostConfigs[0];
  84. XCTAssertEqualObjects(hostConfig.hostPatterns, @[ @"pattern" ], @"Patterns don't match");
  85. XCTAssertEqualObjects(hostConfig.hostname, @"hostname", @"Hostnames don't match");
  86. XCTAssertEqualObjects(hostConfig.port, @1234, @"Port doesn't match");
  87. XCTAssertEqualObjects(hostConfig.identityFiles, @[ @"id_file" ], @"Identity files don't match");
  88. }
  89. /**
  90. Tests that unknown keywords are ignored.
  91. */
  92. - (void)testIgnoreUnknownKeywords {
  93. NSString *contents =
  94. @"Host pattern\n"
  95. @" Hostname hostname\n"
  96. @" Port 1234\n"
  97. @" jfkldsajfdkl fjdkslafjdl fdjkla fjdslkf asdl\n"
  98. @" IdentityFile id_file\n";
  99. NMSSHConfig *config = [[NMSSHConfig alloc] initWithString:contents];
  100. NSArray *hostConfigs = config.hostConfigs;
  101. XCTAssertEqual([hostConfigs count], 1, @"Wrong number of configs read");
  102. NMSSHHostConfig *hostConfig = hostConfigs[0];
  103. XCTAssertEqualObjects(hostConfig.hostPatterns, @[ @"pattern" ], @"Patterns don't match");
  104. XCTAssertEqualObjects(hostConfig.hostname, @"hostname", @"Hostnames don't match");
  105. XCTAssertEqualObjects(hostConfig.port, @1234, @"Port doesn't match");
  106. XCTAssertEqualObjects(hostConfig.identityFiles, @[ @"id_file" ], @"Identity files don't match");
  107. }
  108. /**
  109. Tests that a malformed port line doesn't break parsing
  110. */
  111. - (void)testMalformedPort {
  112. NSString *contents =
  113. @"Host pattern\n"
  114. @" Hostname hostname\n"
  115. @" Port\n"
  116. @" IdentityFile id_file\n";
  117. NMSSHConfig *config = [[NMSSHConfig alloc] initWithString:contents];
  118. NSArray *hostConfigs = config.hostConfigs;
  119. XCTAssertEqual([hostConfigs count], 1, @"Wrong number of configs read");
  120. NMSSHHostConfig *hostConfig = hostConfigs[0];
  121. XCTAssertEqualObjects(hostConfig.hostPatterns, @[ @"pattern" ], @"Patterns don't match");
  122. XCTAssertEqualObjects(hostConfig.hostname, @"hostname", @"Hostnames don't match");
  123. XCTAssertNil(hostConfig.port, @"Port should be nil");
  124. XCTAssertEqualObjects(hostConfig.identityFiles, @[ @"id_file" ], @"Identity files don't match");
  125. }
  126. /**
  127. Tests that multiple patterns are parsed properly
  128. */
  129. - (void)testMultiplePatterns {
  130. NSString *contents =
  131. @"Host pattern1 pattern2\n";
  132. NMSSHConfig *config = [[NMSSHConfig alloc] initWithString:contents];
  133. NSArray *hostConfigs = config.hostConfigs;
  134. XCTAssertEqual([hostConfigs count], 1, @"Wrong number of configs read");
  135. NMSSHHostConfig *hostConfig = hostConfigs[0];
  136. NSArray *expected = @[ @"pattern1", @"pattern2" ];
  137. XCTAssertEqualObjects(hostConfig.hostPatterns, expected, @"Patterns don't match");
  138. }
  139. /**
  140. Tests that quoted patterns are parsed properly
  141. */
  142. - (void)testQuotedPatterns {
  143. NSString *contents =
  144. @"Host pattern1 \"a quoted pattern\" pattern2 \"foo bar\" \"baz\"\n";
  145. NMSSHConfig *config = [[NMSSHConfig alloc] initWithString:contents];
  146. NSArray *hostConfigs = config.hostConfigs;
  147. XCTAssertEqual([hostConfigs count], 1, @"Wrong number of configs read");
  148. NMSSHHostConfig *hostConfig = hostConfigs[0];
  149. NSArray *expected = @[ @"pattern1", @"a quoted pattern", @"pattern2", @"foo bar", @"baz" ];
  150. XCTAssertEqualObjects(hostConfig.hostPatterns, expected, @"Patterns don't match");
  151. }
  152. /**
  153. Tests that an unterminated quoted patterns are ignored.
  154. */
  155. - (void)testUnterminatedQuotation {
  156. NSString *contents =
  157. @"Host pattern1 \"unterminated quotation\n";
  158. NMSSHConfig *config = [[NMSSHConfig alloc] initWithString:contents];
  159. NSArray *hostConfigs = config.hostConfigs;
  160. XCTAssertEqual([hostConfigs count], 1, @"Wrong number of configs read");
  161. NMSSHHostConfig *hostConfig = hostConfigs[0];
  162. NSArray *expected = @[ @"pattern1" ];
  163. XCTAssertEqualObjects(hostConfig.hostPatterns, expected, @"Patterns don't match");
  164. }
  165. /**
  166. Tests that multiple identity file commands are respected.
  167. */
  168. - (void)testMultipleIdentityFile {
  169. NSString *contents =
  170. @"Host pattern\n"
  171. @" Hostname hostname\n"
  172. @" Port 1234\n"
  173. @" IdentityFile id_file1\n"
  174. @" IdentityFile id_file2\n";
  175. NMSSHConfig *config = [[NMSSHConfig alloc] initWithString:contents];
  176. NSArray *hostConfigs = config.hostConfigs;
  177. XCTAssertEqual([hostConfigs count], 1, @"Wrong number of configs read");
  178. NMSSHHostConfig *hostConfig = hostConfigs[0];
  179. NSArray *expected = @[ @"id_file1", @"id_file2" ];
  180. XCTAssertEqualObjects(hostConfig.identityFiles, expected, @"Identity files don't match");
  181. }
  182. /**
  183. Tests that trailing and midline spaces are ignored
  184. */
  185. - (void)testExtraSpaces {
  186. NSString *contents =
  187. @" Host pattern \"quoted pattern\" \" \" \n"
  188. @" Hostname hostname \n";
  189. NMSSHConfig *config = [[NMSSHConfig alloc] initWithString:contents];
  190. NSArray *hostConfigs = config.hostConfigs;
  191. XCTAssertEqual([hostConfigs count], 1, @"Wrong number of configs read");
  192. NMSSHHostConfig *hostConfig = hostConfigs[0];
  193. NSArray *expected = @[ @"pattern", @"quoted pattern", @" " ];
  194. XCTAssertEqualObjects(hostConfig.hostPatterns, expected, @"Patterns don't match");
  195. XCTAssertEqualObjects(hostConfig.hostname, @"hostname", @"Hostnames don't match");
  196. }
  197. /**
  198. Tests multiple hosts
  199. */
  200. - (void)testMultipleHosts {
  201. NSString *contents =
  202. @"Host pattern1\n"
  203. @" Hostname hostname1\n"
  204. @" Port 1\n"
  205. @" IdentityFile id_file1\n"
  206. @"Host pattern2\n"
  207. @" Hostname hostname2\n"
  208. @" Port 2\n"
  209. @" IdentityFile id_file2\n";
  210. NMSSHConfig *config = [[NMSSHConfig alloc] initWithString:contents];
  211. NSArray *hostConfigs = config.hostConfigs;
  212. XCTAssertEqual([hostConfigs count], 2, @"Wrong number of configs read");
  213. NMSSHHostConfig *hostConfig = hostConfigs[0];
  214. XCTAssertEqualObjects(hostConfig.hostPatterns, @[ @"pattern1" ], @"Patterns don't match");
  215. XCTAssertEqualObjects(hostConfig.hostname, @"hostname1", @"Hostnames don't match");
  216. XCTAssertEqualObjects(hostConfig.port, @1, @"Port doesn't match");
  217. XCTAssertEqualObjects(hostConfig.identityFiles, @[ @"id_file1" ],
  218. @"Identity files don't match");
  219. hostConfig = hostConfigs[1];
  220. XCTAssertEqualObjects(hostConfig.hostPatterns, @[ @"pattern2" ], @"Patterns don't match");
  221. XCTAssertEqualObjects(hostConfig.hostname, @"hostname2", @"Hostnames don't match");
  222. XCTAssertEqualObjects(hostConfig.port, @2, @"Port doesn't match");
  223. XCTAssertEqualObjects(hostConfig.identityFiles, @[ @"id_file2" ],
  224. @"Identity files don't match");
  225. }
  226. // -----------------------------------------------------------------------------
  227. #pragma mark - TEST MATCHING
  228. // -----------------------------------------------------------------------------
  229. /**
  230. Test matching a simple pattern
  231. */
  232. - (void)testSimplestPossiblePattern {
  233. NSString *contents =
  234. @"Host pattern1\n"
  235. @" Hostname hostname1\n";
  236. NMSSHConfig *config = [[NMSSHConfig alloc] initWithString:contents];
  237. NMSSHHostConfig *hostConfig = [config hostConfigForHost:@"pattern1"];
  238. XCTAssertEqualObjects(hostConfig.hostname, @"hostname1", @"Match failed");
  239. }
  240. /**
  241. Test that a simple pattern fails when it ought to.
  242. */
  243. - (void)testSimplestPossiblePatternNoMatch {
  244. NSString *contents =
  245. @"Host pattern1\n"
  246. @" Hostname hostname1\n";
  247. NMSSHConfig *config = [[NMSSHConfig alloc] initWithString:contents];
  248. NMSSHHostConfig *hostConfig = [config hostConfigForHost:@"pattern2"];
  249. XCTAssertNil(hostConfig, @"Match should have failed but didn't");
  250. }
  251. /**
  252. Test that a pattern list of simple patterns works.
  253. */
  254. - (void)testSimplePatternList {
  255. NSString *contents =
  256. @"Host pattern1,pattern2\n"
  257. @" Hostname hostname1\n";
  258. NMSSHConfig *config = [[NMSSHConfig alloc] initWithString:contents];
  259. NMSSHHostConfig *hostConfig = [config hostConfigForHost:@"pattern1"];
  260. XCTAssertEqualObjects(hostConfig.hostname, @"hostname1", @"Match failed");
  261. hostConfig = [config hostConfigForHost:@"pattern2"];
  262. XCTAssertEqualObjects(hostConfig.hostname, @"hostname1", @"Match failed");
  263. hostConfig = [config hostConfigForHost:@"pattern3"];
  264. XCTAssertNil(hostConfig, @"Match should have failed but didn't");
  265. }
  266. /**
  267. Test that a question mark wildcard works
  268. */
  269. - (void)testSingleCharWildcard {
  270. NSString *contents =
  271. @"Host pattern?\n"
  272. @" Hostname hostname1\n";
  273. NMSSHConfig *config = [[NMSSHConfig alloc] initWithString:contents];
  274. NMSSHHostConfig *hostConfig = [config hostConfigForHost:@"pattern1"];
  275. XCTAssertEqualObjects(hostConfig.hostname, @"hostname1", @"Match failed");
  276. hostConfig = [config hostConfigForHost:@"pattern2"];
  277. XCTAssertEqualObjects(hostConfig.hostname, @"hostname1", @"Match failed");
  278. hostConfig = [config hostConfigForHost:@"Xattern3"];
  279. XCTAssertNil(hostConfig, @"Match should have failed but didn't");
  280. }
  281. /**
  282. Test that a lone star matches everything
  283. */
  284. - (void)testLoneStarMatchesAll {
  285. NSString *contents =
  286. @"Host *\n"
  287. @" Hostname hostname1\n";
  288. NMSSHConfig *config = [[NMSSHConfig alloc] initWithString:contents];
  289. NMSSHHostConfig *hostConfig = [config hostConfigForHost:@"pattern1"];
  290. XCTAssertEqualObjects(hostConfig.hostname, @"hostname1", @"Match failed");
  291. hostConfig = [config hostConfigForHost:@"pattern2"];
  292. XCTAssertEqualObjects(hostConfig.hostname, @"hostname1", @"Match failed");
  293. hostConfig = [config hostConfigForHost:@""];
  294. XCTAssertEqualObjects(hostConfig.hostname, @"hostname1", @"Match failed");
  295. }
  296. /**
  297. Test that a star suffix matches all suffixes
  298. */
  299. - (void)testStarSuffix {
  300. NSString *contents =
  301. @"Host a*\n"
  302. @" Hostname hostname1\n";
  303. NMSSHConfig *config = [[NMSSHConfig alloc] initWithString:contents];
  304. NMSSHHostConfig *hostConfig = [config hostConfigForHost:@"abcdef"];
  305. XCTAssertEqualObjects(hostConfig.hostname, @"hostname1", @"Match failed");
  306. hostConfig = [config hostConfigForHost:@"a"];
  307. XCTAssertEqualObjects(hostConfig.hostname, @"hostname1", @"Match failed");
  308. hostConfig = [config hostConfigForHost:@""];
  309. XCTAssertNil(hostConfig, @"Match should have failed but didn't");
  310. }
  311. /**
  312. Test that a midline star works
  313. */
  314. - (void)testMidlineStar {
  315. NSString *contents =
  316. @"Host abc*xyz\n"
  317. @" Hostname hostname1\n";
  318. NMSSHConfig *config = [[NMSSHConfig alloc] initWithString:contents];
  319. NMSSHHostConfig *hostConfig = [config hostConfigForHost:@"abcxyz"];
  320. XCTAssertEqualObjects(hostConfig.hostname, @"hostname1", @"Match failed");
  321. hostConfig = [config hostConfigForHost:@"abc123xyz"];
  322. XCTAssertEqualObjects(hostConfig.hostname, @"hostname1", @"Match failed");
  323. hostConfig = [config hostConfigForHost:@"abc"];
  324. XCTAssertNil(hostConfig, @"Match should have failed but didn't");
  325. hostConfig = [config hostConfigForHost:@"xyz"];
  326. XCTAssertNil(hostConfig, @"Match should have failed but didn't");
  327. hostConfig = [config hostConfigForHost:@"abxyz"];
  328. XCTAssertNil(hostConfig, @"Match should have failed but didn't");
  329. hostConfig = [config hostConfigForHost:@"abcyz"];
  330. XCTAssertNil(hostConfig, @"Match should have failed but didn't");
  331. hostConfig = [config hostConfigForHost:@"abcabc"];
  332. XCTAssertNil(hostConfig, @"Match should have failed but didn't");
  333. }
  334. /**
  335. Test that a star prefix works
  336. */
  337. - (void)testLeadingStar {
  338. NSString *contents =
  339. @"Host *xyz\n"
  340. @" Hostname hostname1\n";
  341. NMSSHConfig *config = [[NMSSHConfig alloc] initWithString:contents];
  342. NMSSHHostConfig *hostConfig = [config hostConfigForHost:@"xyz"];
  343. XCTAssertEqualObjects(hostConfig.hostname, @"hostname1", @"Match failed");
  344. hostConfig = [config hostConfigForHost:@"123xyz"];
  345. XCTAssertEqualObjects(hostConfig.hostname, @"hostname1", @"Match failed");
  346. hostConfig = [config hostConfigForHost:@"xyz"];
  347. XCTAssertEqualObjects(hostConfig.hostname, @"hostname1", @"Match failed");
  348. hostConfig = [config hostConfigForHost:@"abc"];
  349. XCTAssertNil(hostConfig, @"Match should have failed but didn't");
  350. hostConfig = [config hostConfigForHost:@""];
  351. XCTAssertNil(hostConfig, @"Match should have failed but didn't");
  352. }
  353. /**
  354. Test that multiple disjoint stars work.
  355. */
  356. - (void)testMultipleDisjointStars {
  357. NSString *contents =
  358. @"Host a*b*c\n"
  359. @" Hostname hostname1\n";
  360. NMSSHConfig *config = [[NMSSHConfig alloc] initWithString:contents];
  361. NMSSHHostConfig *hostConfig = [config hostConfigForHost:@"a12b34c"];
  362. XCTAssertEqualObjects(hostConfig.hostname, @"hostname1", @"Match failed");
  363. hostConfig = [config hostConfigForHost:@"abc"];
  364. XCTAssertEqualObjects(hostConfig.hostname, @"hostname1", @"Match failed");
  365. hostConfig = [config hostConfigForHost:@"abc1"];
  366. XCTAssertNil(hostConfig, @"Match should have failed but didn't");
  367. hostConfig = [config hostConfigForHost:@""];
  368. XCTAssertNil(hostConfig, @"Match should have failed but didn't");
  369. }
  370. /**
  371. Test that two stars in a row work
  372. */
  373. - (void)testConsecutiveStars {
  374. NSString *contents =
  375. @"Host a**z\n"
  376. @" Hostname hostname1\n";
  377. NMSSHConfig *config = [[NMSSHConfig alloc] initWithString:contents];
  378. NMSSHHostConfig *hostConfig = [config hostConfigForHost:@"abcz"];
  379. XCTAssertEqualObjects(hostConfig.hostname, @"hostname1", @"Match failed");
  380. hostConfig = [config hostConfigForHost:@"abz"];
  381. XCTAssertEqualObjects(hostConfig.hostname, @"hostname1", @"Match failed");
  382. hostConfig = [config hostConfigForHost:@"az"];
  383. XCTAssertEqualObjects(hostConfig.hostname, @"hostname1", @"Match failed");
  384. hostConfig = [config hostConfigForHost:@"a"];
  385. XCTAssertNil(hostConfig, @"Match should have failed but didn't");
  386. hostConfig = [config hostConfigForHost:@""];
  387. XCTAssertNil(hostConfig, @"Match should have failed but didn't");
  388. }
  389. /**
  390. Test that a star followed by a question mark works
  391. */
  392. - (void)testStarQuestionMark {
  393. NSString *contents =
  394. @"Host a*?z\n"
  395. @" Hostname hostname1\n";
  396. NMSSHConfig *config = [[NMSSHConfig alloc] initWithString:contents];
  397. NMSSHHostConfig *hostConfig = [config hostConfigForHost:@"abcz"];
  398. XCTAssertEqualObjects(hostConfig.hostname, @"hostname1", @"Match failed");
  399. hostConfig = [config hostConfigForHost:@"abz"];
  400. XCTAssertEqualObjects(hostConfig.hostname, @"hostname1", @"Match failed");
  401. hostConfig = [config hostConfigForHost:@"az"];
  402. XCTAssertNil(hostConfig, @"Match should have failed but didn't");
  403. hostConfig = [config hostConfigForHost:@"a"];
  404. XCTAssertNil(hostConfig, @"Match should have failed but didn't");
  405. hostConfig = [config hostConfigForHost:@""];
  406. XCTAssertNil(hostConfig, @"Match should have failed but didn't");
  407. }
  408. /**
  409. Test a host with multiple pattern lists
  410. */
  411. - (void)testMultiplePatternLists {
  412. NSString *contents =
  413. @"Host pattern1,pattern2 pattern3,pattern4\n"
  414. @" Hostname hostname1\n";
  415. NMSSHConfig *config = [[NMSSHConfig alloc] initWithString:contents];
  416. NMSSHHostConfig *hostConfig = [config hostConfigForHost:@"pattern1"];
  417. XCTAssertEqualObjects(hostConfig.hostname, @"hostname1", @"Match failed");
  418. hostConfig = [config hostConfigForHost:@"pattern2"];
  419. XCTAssertEqualObjects(hostConfig.hostname, @"hostname1", @"Match failed");
  420. hostConfig = [config hostConfigForHost:@"pattern3"];
  421. XCTAssertEqualObjects(hostConfig.hostname, @"hostname1", @"Match failed");
  422. hostConfig = [config hostConfigForHost:@"pattern4"];
  423. XCTAssertEqualObjects(hostConfig.hostname, @"hostname1", @"Match failed");
  424. }
  425. /**
  426. Test negation alone
  427. */
  428. - (void)testNegationAlone {
  429. NSString *contents =
  430. @"Host !pattern1\n"
  431. @" Hostname hostname1\n";
  432. NMSSHConfig *config = [[NMSSHConfig alloc] initWithString:contents];
  433. NMSSHHostConfig *hostConfig = [config hostConfigForHost:@"pattern1"];
  434. XCTAssertNil(hostConfig, @"Match should have failed but didn't");
  435. hostConfig = [config hostConfigForHost:@"pattern2"];
  436. XCTAssertNil(hostConfig, @"Match should have failed but didn't");
  437. }
  438. /**
  439. Test negation in combination with a matchable pattern
  440. */
  441. - (void)testNegationPlusMatchablePattern {
  442. NSString *contents =
  443. @"Host !*x* a*\n"
  444. @" Hostname hostname1\n";
  445. NMSSHConfig *config = [[NMSSHConfig alloc] initWithString:contents];
  446. NMSSHHostConfig *hostConfig = [config hostConfigForHost:@"axy"];
  447. XCTAssertNil(hostConfig, @"Match should have failed but didn't");
  448. hostConfig = [config hostConfigForHost:@"abc"];
  449. XCTAssertEqualObjects(hostConfig.hostname, @"hostname1", @"Match failed");
  450. hostConfig = [config hostConfigForHost:@"b"];
  451. XCTAssertNil(hostConfig, @"Match should have failed but didn't");
  452. }
  453. /**
  454. Test two rules where the first negates and the second matches.
  455. */
  456. - (void)testTwoRulesWhereFirstNegatesAndSecondMatches {
  457. NSString *contents =
  458. @"Host !*x* a*\n"
  459. @" Hostname hostname1\n"
  460. @"Host *z\n"
  461. @" Hostname hostname2\n";
  462. NMSSHConfig *config = [[NMSSHConfig alloc] initWithString:contents];
  463. NMSSHHostConfig *hostConfig = [config hostConfigForHost:@"axy"];
  464. XCTAssertNil(hostConfig, @"Match should have failed but didn't");
  465. hostConfig = [config hostConfigForHost:@"abc"];
  466. XCTAssertEqualObjects(hostConfig.hostname, @"hostname1", @"Match failed");
  467. hostConfig = [config hostConfigForHost:@"axz"];
  468. XCTAssertEqualObjects(hostConfig.hostname, @"hostname2", @"Match failed");
  469. hostConfig = [config hostConfigForHost:@"xz"];
  470. XCTAssertEqualObjects(hostConfig.hostname, @"hostname2", @"Match failed");
  471. hostConfig = [config hostConfigForHost:@"z"];
  472. XCTAssertEqualObjects(hostConfig.hostname, @"hostname2", @"Match failed");
  473. hostConfig = [config hostConfigForHost:@"b"];
  474. XCTAssertNil(hostConfig, @"Match should have failed but didn't");
  475. }
  476. /**
  477. Test two rules that both match. They should be merged.
  478. */
  479. - (void)testMergeTwoMatchingRules {
  480. NSString *contents =
  481. @"Host *\n"
  482. @" Hostname hostname1\n"
  483. @" Port 1\n"
  484. @" IdentityFile id_file1\n"
  485. @"Host *\n"
  486. @" Hostname hostname2\n"
  487. @" Port 2\n"
  488. @" User user\n"
  489. @" IdentityFile id_file2\n";
  490. NMSSHConfig *config = [[NMSSHConfig alloc] initWithString:contents];
  491. NMSSHHostConfig *hostConfig = [config hostConfigForHost:@"hostname"];
  492. XCTAssertEqualObjects(hostConfig.hostname, @"hostname1", @"Hostnames don't match");
  493. XCTAssertEqualObjects(hostConfig.port, @1, @"Port doesn't match");
  494. XCTAssertEqualObjects(hostConfig.user, @"user", @"Users doesn't match");
  495. NSArray *expected = @[ @"id_file1", @"id_file2" ];
  496. XCTAssertEqualObjects(hostConfig.identityFiles, expected,
  497. @"Identity files don't match");
  498. }
  499. @end