Home | History | Annotate | Download | only in imageburner
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include <gmock/gmock.h>
      6 #include <gtest/gtest.h>
      7 
      8 #include "base/memory/scoped_ptr.h"
      9 #include "chrome/browser/chromeos/imageburner/burn_manager.h"
     10 
     11 namespace chromeos {
     12 namespace imageburner {
     13 
     14 using ::testing::_;
     15 using ::testing::AnyNumber;
     16 using ::testing::InSequence;
     17 
     18 const std::string kConfigFileWithNoHwidProperty =
     19     "name=some_name\n"
     20     "version=version\n"
     21     "filesize=1000\n"
     22     "url=http://image.bin.zip\n";
     23 
     24 const std::string kConfigFileWithNoNameProperty =
     25     "version=version\n"
     26     "filesize=2000\n"
     27     "url=http://some_image.bin.zip\n";
     28 
     29 const std::string kConfigFileWithNoNewLineAtEnd =
     30     "name=some_name\n"
     31     "version=version\n"
     32     "filesize=1000\n"
     33     "hwid=some_hwid\n"
     34     "url=http://image.bin.zip";
     35 
     36 const std::string kSampleConfigFile =
     37     "version=aaa\n"
     38     "hwid=block_no_name\n"
     39     "url=aaa\n"
     40     "\n"
     41     "name=some_name1\n"
     42     "version=version1\n"
     43     "hwid=hwid11\n"
     44     "hwid=hwid12\n"
     45     "hwid=hwid13\n"
     46     "\n"
     47     "filesize=1000\n"
     48     "url=http://image1.bin.zip\n"
     49     "file=url\n"
     50     "name=some_name2\n"
     51     "version=version2\n"
     52     "hwid=hwid21\n"
     53     "hwid=hwid22\n"
     54     "hwid=hwid23\n"
     55     "\n"
     56     "filesize=1200\n"
     57     "url=http://image2.bin.zip\n"
     58     "file=file2"
     59     "\n"
     60     "name=some_name3\n"
     61     "version=version3\n"
     62     "hwid=hwid31\n"
     63     "\n"
     64     "filesize=3\n"
     65     "url=http://image3.bin.zip\n"
     66     "file=file3"
     67     "\n"
     68     "name=some_block_with_no_hwid\n"
     69     "url=some_url\n"
     70     "\n"
     71     "name=some_name_invalid_block\n"  // Good line.
     72     "version=version \n"  // Trailing whitespace.
     73     "hwid=hwid41=q\n"  // Extra =.
     74     "hwid=hwid42\n"
     75     "hwid=  \n"  // Blank property value.
     76     "=\n"
     77     "filesize=\n"  // Empty property value.
     78     "url\n"  // No =.
     79     "   =something\n"
     80     "name=another_block_with_no_hwid\n"
     81     "version=version\n";
     82 
     83 TEST(BurnManagerTest, ConfigFileTest) {
     84   scoped_ptr<ConfigFile> cf(new ConfigFile());
     85   EXPECT_TRUE(cf->empty());
     86 
     87   cf.reset(new ConfigFile(""));
     88   EXPECT_TRUE(cf->empty());
     89 
     90   cf.reset(new ConfigFile(kConfigFileWithNoNameProperty));
     91   EXPECT_TRUE(cf->empty());
     92 
     93   cf.reset(new ConfigFile(kConfigFileWithNoHwidProperty));
     94   EXPECT_TRUE(cf->empty());
     95 
     96   cf.reset(new ConfigFile(kConfigFileWithNoNewLineAtEnd));
     97   EXPECT_FALSE(cf->empty());
     98   EXPECT_EQ(1u, cf->size());
     99   EXPECT_EQ("http://image.bin.zip", cf->GetProperty("url", "some_hwid"));
    100   EXPECT_EQ("some_name", cf->GetProperty("name", "some_hwid"));
    101 
    102   cf.reset(new ConfigFile(kSampleConfigFile));
    103   EXPECT_FALSE(cf->empty());
    104 
    105   EXPECT_EQ(4u, cf->size());
    106 
    107   EXPECT_EQ("", cf->GetProperty("version", "block_no_name"));
    108 
    109   EXPECT_EQ("some_name1", cf->GetProperty("name", "hwid11"));
    110   EXPECT_EQ("version1", cf->GetProperty("version", "hwid12"));
    111   EXPECT_EQ("", cf->GetProperty("filesize", "hwid1_non_existent"));
    112   EXPECT_EQ("http://image1.bin.zip", cf->GetProperty("url", "hwid13"));
    113   EXPECT_EQ("", cf->GetProperty("hwid", "hwid11"));
    114   EXPECT_EQ("", cf->GetProperty("", "hwid12"));
    115   EXPECT_EQ("", cf->GetProperty("name", ""));
    116   EXPECT_EQ("", cf->GetProperty("some_name", "hwid11"));
    117   EXPECT_EQ("url", cf->GetProperty("file", "hwid11"));
    118 
    119   EXPECT_EQ("http://image2.bin.zip", cf->GetProperty("url", "hwid21"));
    120   EXPECT_EQ("some_name2", cf->GetProperty("name", "hwid23"));
    121 
    122   EXPECT_EQ("http://image3.bin.zip", cf->GetProperty("url", "hwid31"));
    123   EXPECT_EQ("some_name3", cf->GetProperty("name", "hwid31"));
    124 
    125   EXPECT_EQ("some_name_invalid_block", cf->GetProperty("name", "hwid42"));
    126   // TODO(tbarzic): make this pass.
    127   // EXPECT_EQ("version", cf->GetProperty("version", "hwid42"));
    128   EXPECT_EQ("", cf->GetProperty("filesize", "hwid42"));
    129   EXPECT_EQ("", cf->GetProperty("url", "hwid42"));
    130   // TODO(tbarzic): make this pass.
    131   // EXPECT_EQ("", cf->GetProperty("  ", "hwid42"));
    132   EXPECT_EQ("", cf->GetProperty("name", "hwid41"));
    133 }
    134 
    135 class MockStateMachineObserver : public StateMachine::Observer {
    136  public:
    137   MOCK_METHOD1(OnBurnStateChanged, void(StateMachine::State));
    138   MOCK_METHOD1(OnError, void(int));
    139 };
    140 
    141 TEST(BurnManagerTest, StateMachineNormalWorkflow) {
    142   scoped_ptr<StateMachine> state_machine(new StateMachine());
    143   EXPECT_EQ(StateMachine::INITIAL, state_machine->state());
    144 
    145   MockStateMachineObserver observer;
    146   state_machine->AddObserver(&observer);
    147   EXPECT_CALL(observer, OnBurnStateChanged(StateMachine::DOWNLOADING))
    148     .Times(1)
    149     .RetiresOnSaturation();
    150 
    151   EXPECT_CALL(observer, OnBurnStateChanged(StateMachine::BURNING))
    152     .Times(1)
    153     .RetiresOnSaturation();
    154 
    155   EXPECT_CALL(observer, OnBurnStateChanged(StateMachine::INITIAL))
    156     .Times(1)
    157     .RetiresOnSaturation();
    158 
    159   EXPECT_FALSE(state_machine->download_started());
    160   EXPECT_FALSE(state_machine->download_finished());
    161   EXPECT_TRUE(state_machine->new_burn_posible());
    162 
    163   state_machine->OnDownloadStarted();
    164 
    165   EXPECT_EQ(StateMachine::DOWNLOADING, state_machine->state());
    166   EXPECT_TRUE(state_machine->download_started());
    167   EXPECT_FALSE(state_machine->download_finished());
    168   EXPECT_FALSE(state_machine->new_burn_posible());
    169 
    170   state_machine->OnDownloadFinished();
    171 
    172   // TODO(tbarzic): make this pass.
    173   // EXPECT_EQ(StateMachine::INITIAL, state_machine->state());
    174   EXPECT_TRUE(state_machine->download_started());
    175   EXPECT_TRUE(state_machine->download_finished());
    176   EXPECT_FALSE(state_machine->new_burn_posible());
    177 
    178   state_machine->OnBurnStarted();
    179 
    180   EXPECT_EQ(StateMachine::BURNING, state_machine->state());
    181   EXPECT_TRUE(state_machine->download_started());
    182   EXPECT_TRUE(state_machine->download_finished());
    183   EXPECT_FALSE(state_machine->new_burn_posible());
    184 
    185   state_machine->OnSuccess();
    186 
    187   EXPECT_EQ(StateMachine::INITIAL, state_machine->state());
    188   EXPECT_TRUE(state_machine->download_started());
    189   EXPECT_TRUE(state_machine->download_finished());
    190   EXPECT_TRUE(state_machine->new_burn_posible());
    191 }
    192 
    193 TEST(BurnManagerTest, StateMachineError) {
    194   scoped_ptr<StateMachine> state_machine(new StateMachine());
    195 
    196   MockStateMachineObserver observer;
    197   // We don't want state change to INITIAL due to error to be reported to
    198   // observers. We use OnError for that.
    199   EXPECT_CALL(observer, OnBurnStateChanged(_))
    200       .Times(AnyNumber());
    201   EXPECT_CALL(observer, OnBurnStateChanged(StateMachine::INITIAL))
    202       .Times(0);
    203   {
    204     InSequence error_calls;
    205     EXPECT_CALL(observer, OnError(1234))
    206         .Times(1);
    207     EXPECT_CALL(observer, OnError(4321))
    208         .Times(1);
    209     EXPECT_CALL(observer, OnError(0))
    210         .Times(1);
    211   }
    212   state_machine->AddObserver(&observer);
    213 
    214   state_machine->OnDownloadStarted();
    215 
    216   state_machine->OnError(1234);
    217 
    218   // If called before download finished, download flags should be reset.
    219   EXPECT_FALSE(state_machine->download_started());
    220   EXPECT_EQ(state_machine->state(), StateMachine::INITIAL);
    221   EXPECT_TRUE(state_machine->new_burn_posible());
    222 
    223   state_machine->OnDownloadStarted();
    224   state_machine->OnDownloadFinished();
    225 
    226   state_machine->OnError(4321);
    227 
    228   // If called after download finished, download flags should not be changed.
    229   EXPECT_TRUE(state_machine->download_started());
    230   EXPECT_TRUE(state_machine->download_finished());
    231   EXPECT_EQ(state_machine->state(), StateMachine::INITIAL);
    232   EXPECT_TRUE(state_machine->new_burn_posible());
    233 
    234   state_machine->OnBurnStarted();
    235   state_machine->OnError(0);
    236 
    237   EXPECT_EQ(state_machine->state(), StateMachine::INITIAL);
    238   EXPECT_TRUE(state_machine->new_burn_posible());
    239 }
    240 
    241 TEST(BurnManagerTest, StateMachineObservers) {
    242   scoped_ptr<StateMachine> state_machine(new StateMachine());
    243 
    244   MockStateMachineObserver observer1, observer2;
    245 
    246   EXPECT_CALL(observer1, OnBurnStateChanged(_))
    247       .Times(0);
    248   EXPECT_CALL(observer2, OnBurnStateChanged(_))
    249       .Times(0);
    250   EXPECT_CALL(observer1, OnError(_))
    251       .Times(0);
    252   EXPECT_CALL(observer2, OnError(_))
    253       .Times(0);
    254 
    255   state_machine->OnDownloadStarted();
    256   state_machine->OnError(1);
    257 
    258   state_machine->AddObserver(&observer1);
    259   state_machine->AddObserver(&observer2);
    260   EXPECT_CALL(observer1, OnBurnStateChanged(_))
    261       .Times(1);
    262   EXPECT_CALL(observer2, OnBurnStateChanged(_))
    263       .Times(1);
    264   EXPECT_CALL(observer1, OnError(_))
    265       .Times(1);
    266   EXPECT_CALL(observer2, OnError(_))
    267       .Times(1);
    268 
    269   state_machine->OnDownloadStarted();
    270   state_machine->OnError(1);
    271 
    272   state_machine->RemoveObserver(&observer1);
    273   EXPECT_CALL(observer1, OnBurnStateChanged(_))
    274       .Times(0);
    275   EXPECT_CALL(observer2, OnBurnStateChanged(_))
    276       .Times(1);
    277   EXPECT_CALL(observer1, OnError(_))
    278       .Times(0);
    279   EXPECT_CALL(observer2, OnError(_))
    280       .Times(1);
    281   state_machine->OnDownloadStarted();
    282   state_machine->OnError(1);
    283 }
    284 
    285 }  // namespace imageburner
    286 }  // namespace chromeos
    287