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

http://macfuse.googlecode.com/ · Objective C · 199 lines · 145 code · 37 blank · 17 comment · 2 complexity · a321c1c7adac5abb2e391f35f009f888 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 "KSActionProcessor.h"
  16. #import "KSActionPipe.h"
  17. #import "KSSilentUpdateAction.h"
  18. #import "KSUpdateAction.h"
  19. #import "KSUpdateInfo.h"
  20. #import "KSUpdateEngine.h"
  21. #import "KSCommandRunner.h"
  22. @interface KSSilentUpdateActionTest : SenTestCase
  23. @end
  24. static NSString *const kTicketStorePath = @"/tmp/KSSilentUpdateActionTest.ticketstore";
  25. @implementation KSSilentUpdateActionTest
  26. - (void)setUp {
  27. [@"" writeToFile:kTicketStorePath atomically:YES];
  28. [KSUpdateEngine setDefaultTicketStorePath:kTicketStorePath];
  29. }
  30. - (void)tearDown {
  31. [[NSFileManager defaultManager] removeFileAtPath:kTicketStorePath handler:nil];
  32. [KSUpdateEngine setDefaultTicketStorePath:nil];
  33. }
  34. // KSUpdateEngineDelegate protocol method
  35. - (NSArray *)engine:(KSUpdateEngine *)engine
  36. shouldSilentlyUpdateProducts:(NSArray *)products {
  37. GTMLoggerInfo(@"products=%@", products);
  38. return [products filteredArrayUsingPredicate:
  39. [NSPredicate predicateWithFormat:
  40. @"%K like 'allow*'", kServerProductID]];
  41. }
  42. // KSUpdateEngineDelegate protocol method
  43. - (id<KSCommandRunner>)commandRunnerForEngine:(KSUpdateEngine *)engine {
  44. return [KSTaskCommandRunner commandRunner];
  45. }
  46. - (void)loopUntilDone:(KSActionProcessor *)processor {
  47. int count = 50;
  48. while ([processor isProcessing] && (count > 0)) {
  49. NSDate *quick = [NSDate dateWithTimeIntervalSinceNow:0.2];
  50. [[NSRunLoop currentRunLoop] runUntilDate:quick];
  51. count--;
  52. }
  53. STAssertFalse([processor isProcessing], nil);
  54. }
  55. - (void)testCreation {
  56. KSSilentUpdateAction *action = [KSSilentUpdateAction actionWithEngine:nil];
  57. STAssertNil(action, nil);
  58. action = [[[KSSilentUpdateAction alloc] init] autorelease];
  59. STAssertNil(action, nil);
  60. action = [[[KSSilentUpdateAction alloc] initWithEngine:nil] autorelease];
  61. STAssertNil(action, nil);
  62. KSUpdateEngine *engine = [KSUpdateEngine engineWithDelegate:self];
  63. action = [KSSilentUpdateAction actionWithEngine:engine];
  64. STAssertNotNil(action, nil);
  65. STAssertFalse([action isRunning], nil);
  66. // For the sake of code coverage, let's call this method even though we don't
  67. // really have a good way to test the functionality.
  68. [action terminateAction];
  69. }
  70. - (void)testPrompting {
  71. KSUpdateEngine *engine = [KSUpdateEngine engineWithDelegate:self];
  72. STAssertNotNil(engine, nil);
  73. KSSilentUpdateAction *action = [KSSilentUpdateAction actionWithEngine:engine];
  74. STAssertNotNil(action, nil);
  75. NSArray *availableProducts =
  76. [[NSArray alloc] initWithObjects:
  77. [NSDictionary dictionaryWithObjectsAndKeys:
  78. @"allow1", kServerProductID,
  79. [NSURL URLWithString:@"a://b"], kServerCodebaseURL,
  80. [NSNumber numberWithInt:1], kServerCodeSize,
  81. @"zzz", kServerCodeHash,
  82. @"a://b", kServerMoreInfoURLString,
  83. nil],
  84. [NSDictionary dictionaryWithObjectsAndKeys:
  85. @"allow2", kServerProductID,
  86. [NSURL URLWithString:@"a://b"], kServerCodebaseURL,
  87. [NSNumber numberWithInt:2], kServerCodeSize,
  88. @"xxx", kServerCodeHash,
  89. @"a://b", kServerMoreInfoURLString,
  90. nil],
  91. [NSDictionary dictionaryWithObjectsAndKeys:
  92. @"allow3", kServerProductID,
  93. [NSURL URLWithString:@"a://b"], kServerCodebaseURL,
  94. [NSNumber numberWithInt:3], kServerCodeSize,
  95. @"yyy", kServerCodeHash,
  96. @"a://b", kServerMoreInfoURLString,
  97. nil],
  98. [NSDictionary dictionaryWithObjectsAndKeys:
  99. @"deny1", kServerProductID,
  100. [NSURL URLWithString:@"a://b"], kServerCodebaseURL,
  101. [NSNumber numberWithInt:3], kServerCodeSize,
  102. @"vvv", kServerCodeHash,
  103. @"a://b", kServerMoreInfoURLString,
  104. nil],
  105. nil];
  106. KSActionPipe *pipe = [KSActionPipe pipe];
  107. [pipe setContents:availableProducts];
  108. [action setInPipe:pipe];
  109. KSActionProcessor *ap = [[[KSActionProcessor alloc] init] autorelease];
  110. [ap enqueueAction:action];
  111. [ap startProcessing];
  112. [self loopUntilDone:ap];
  113. STAssertFalse([ap isProcessing], nil);
  114. STAssertEquals([action subActionsProcessed], 3, nil);
  115. }
  116. - (void)testNegativeFiltering {
  117. KSUpdateEngine *engine = [KSUpdateEngine engineWithDelegate:self];
  118. STAssertNotNil(engine, nil);
  119. KSSilentUpdateAction *action = [KSSilentUpdateAction actionWithEngine:engine];
  120. STAssertNotNil(action, nil);
  121. NSArray *availableProducts =
  122. [[NSArray alloc] initWithObjects:
  123. [NSDictionary dictionaryWithObjectsAndKeys:
  124. @"deny1", kServerProductID,
  125. [NSURL URLWithString:@"a://b"], kServerCodebaseURL,
  126. [NSNumber numberWithInt:1], kServerCodeSize,
  127. @"vvv", kServerCodeHash,
  128. @"a://b", kServerMoreInfoURLString,
  129. nil],
  130. [NSDictionary dictionaryWithObjectsAndKeys:
  131. @"deny2", kServerProductID,
  132. [NSURL URLWithString:@"a://b"], kServerCodebaseURL,
  133. [NSNumber numberWithInt:2], kServerCodeSize,
  134. @"qqq", kServerCodeHash,
  135. @"a://b", kServerMoreInfoURLString,
  136. nil],
  137. nil];
  138. KSActionPipe *pipe = [KSActionPipe pipe];
  139. [pipe setContents:availableProducts];
  140. [action setInPipe:pipe];
  141. KSActionProcessor *ap = [[[KSActionProcessor alloc] init] autorelease];
  142. [ap enqueueAction:action];
  143. [ap startProcessing];
  144. [self loopUntilDone:ap];
  145. STAssertFalse([ap isProcessing], nil);
  146. STAssertEquals([action subActionsProcessed], 0, nil);
  147. }
  148. - (void)testNoUpdates {
  149. KSUpdateEngine *engine = [KSUpdateEngine engineWithDelegate:self];
  150. STAssertNotNil(engine, nil);
  151. KSSilentUpdateAction *action = [KSSilentUpdateAction actionWithEngine:engine];
  152. STAssertNotNil(action, nil);
  153. KSActionPipe *pipe = [KSActionPipe pipe];
  154. [action setInPipe:pipe]; // This pipe is empty
  155. KSActionProcessor *ap = [[[KSActionProcessor alloc] init] autorelease];
  156. [ap enqueueAction:action];
  157. [ap startProcessing];
  158. [self loopUntilDone:ap];
  159. STAssertFalse([ap isProcessing], nil);
  160. STAssertEqualObjects([[action outPipe] contents], nil, nil);
  161. }
  162. @end