/core/externals/update-engine/Core/KSCommandRunnerTest.m

http://macfuse.googlecode.com/ · Objective C · 114 lines · 81 code · 18 blank · 15 comment · 10 complexity · 910cedab49e5996231d848fb813f3c65 MD5 · raw file

  1. // Copyright 2008 Google Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import <SenTestingKit/SenTestingKit.h>
  15. #import "KSCommandRunner.h"
  16. @interface KSCommandRunnerTest : SenTestCase
  17. @end
  18. @implementation KSCommandRunnerTest
  19. - (void)testBasic {
  20. KSTaskCommandRunner *cmd = [KSTaskCommandRunner commandRunner];
  21. STAssertNotNil(cmd, nil);
  22. NSString *output = nil;
  23. int rc = 0;
  24. rc = [cmd runCommand:@"/bin/ls"
  25. withArgs:[NSArray arrayWithObjects:@"-F", @"/tmp", nil]
  26. environment:nil
  27. output:&output];
  28. STAssertTrue(rc == 0, nil);
  29. STAssertNotNil(output, nil);
  30. STAssertEqualObjects(output, @"/tmp@\n", nil);
  31. output = nil;
  32. rc = [cmd runCommand:@"/blah/foo/bar/baz/FAKE/PATH"
  33. withArgs:[NSArray arrayWithObjects:@"-F", @"/tmp", nil]
  34. environment:nil
  35. output:&output];
  36. STAssertTrue(rc != 0, nil);
  37. STAssertNil(output, nil);
  38. output = nil;
  39. rc = [cmd runCommand:@"/bin/ls"
  40. withArgs:[NSArray arrayWithObjects:@"-F", @"/tmp", nil]
  41. environment:nil
  42. output:nil];
  43. STAssertTrue(rc == 0, nil);
  44. STAssertNil(output, nil);
  45. output = nil;
  46. rc = [cmd runCommand:@"/bin/ls"
  47. withArgs:nil
  48. environment:nil
  49. output:&output];
  50. STAssertTrue(rc == 0, nil);
  51. STAssertNotNil(output, nil);
  52. output = nil;
  53. rc = [cmd runCommand:@"/usr/bin/env"
  54. withArgs:nil
  55. environment:[NSDictionary dictionaryWithObject:@"blah" forKey:@"KS_COMMAND_RUNNER_TEST"]
  56. output:&output];
  57. STAssertTrue(rc == 0, nil);
  58. STAssertNotNil(output, nil);
  59. NSRange r = [output rangeOfString:@"KS_COMMAND_RUNNER_TEST=blah"];
  60. STAssertTrue(r.location != NSNotFound, nil);
  61. output = nil;
  62. rc = [cmd runCommand:@"/bin/sh"
  63. withArgs:[NSArray arrayWithObjects:@"-c", @"exit 1", nil]
  64. environment:nil
  65. output:&output];
  66. STAssertTrue(rc == 1, nil);
  67. output = nil;
  68. rc = [cmd runCommand:nil
  69. withArgs:nil
  70. environment:nil
  71. output:&output];
  72. STAssertTrue(rc == 1, nil);
  73. }
  74. - (void)testError {
  75. KSTaskCommandRunner *cmd = [KSTaskCommandRunner commandRunner];
  76. // Successful command should have an empty stderror.
  77. int rc = 0;
  78. NSString *output;
  79. NSString *stderror;
  80. rc = [cmd runCommand:@"/bin/ls"
  81. withArgs:[NSArray arrayWithObjects:@"-F", @"/tmp", nil]
  82. environment:nil
  83. output:&output
  84. stdError:&stderror];
  85. STAssertEquals(rc, 0, nil);
  86. STAssertEquals([stderror length], 0u, nil);
  87. // Unsuccessful command should have stuff in the error
  88. rc = [cmd runCommand:@"/bin/ls"
  89. withArgs:[NSArray arrayWithObject:@"--fnordbork"]
  90. environment:nil
  91. output:&output
  92. stdError:&stderror];
  93. STAssertTrue(rc != 0, nil);
  94. STAssertFalse([stderror length] == 0, nil);
  95. }
  96. @end