| // Copyright 2016 Google Inc. All Rights Reserved. |
| // Author: germuth@google.com (Aaron Germuth) |
| |
| #include <stdlib.h> |
| #include <stdio.h> |
| #include <fcntl.h> |
| #include <sys/stat.h> |
| #include "gtest/gtest.h" |
| |
| #define TEST_MAIN |
| #include "hmx_test_base.cc" |
| #include "hmx_upgrade_nvram.c" |
| |
| // Tests Constants |
| const unsigned char* writeData = (unsigned char*)"ABC123def"; |
| const unsigned int writeLen = 10; |
| |
| // Test Parameterization |
| const unsigned int offsets[] = {NVRAM_RO_OFFSET, NVRAM_RW_OFFSET, NVRAM_RWB_OFFSET}; |
| unsigned int offset; |
| |
| class HnvramFlashTest : public HnvramTest, |
| public ::testing::WithParamInterface<unsigned int> { |
| public: |
| HnvramFlashTest() {} |
| virtual ~HnvramFlashTest() {} |
| |
| virtual void SetUp() { |
| libupgrade_verbose = 0; |
| offset = GetParam(); |
| |
| HnvramTest::SetUp(); |
| } |
| |
| virtual void TearDown() { |
| HnvramTest::TearDown(); |
| offset = 0; |
| } |
| }; |
| |
| TEST_P(HnvramFlashTest, TestHnvramInit) { |
| EXPECT_STREQ("/dev/mtd/hnvram", HMX_UPGRADE_NVRAM_Get_Partition_Path()); |
| |
| HMX_UPGRADE_NVRAM_Init("/tmp/abctest123"); |
| EXPECT_STREQ("/tmp/abctest123", HMX_UPGRADE_NVRAM_Get_Partition_Path()); |
| |
| HMX_UPGRADE_NVRAM_Init(NULL); |
| EXPECT_STREQ("/dev/mtd/hnvram", HMX_UPGRADE_NVRAM_Get_Partition_Path()); |
| } |
| |
| TEST_P(HnvramFlashTest, TestFlashWrite) { |
| const unsigned char test[2] = "A"; |
| |
| // bad realpath |
| HMX_UPGRADE_NVRAM_Init("/fake/doesnotexist915"); |
| EXPECT_EQ(-1, HMX_UPGRADE_NVRAM_Write(0, test, 1)); |
| |
| // Can't open file (because its a directory) |
| HMX_UPGRADE_NVRAM_Init("/tmp"); |
| EXPECT_EQ(-1, HMX_UPGRADE_NVRAM_Write(0, test, 1)); |
| |
| // Can't flush device (BLKFLSBUF) |
| HMX_UPGRADE_NVRAM_Init("/dev/zero"); |
| EXPECT_EQ(-1, HMX_UPGRADE_NVRAM_Write(0, test, 1)); |
| |
| // should succeed without actually writing, if there would is no difference |
| // write zeros to zero'd file |
| HMX_UPGRADE_NVRAM_Init(hnvramFileName); |
| |
| // can check by comparing file mtime, before and after |
| struct stat statbuf; |
| EXPECT_NE(-1, stat(hnvramFileName, &statbuf)); |
| time_t before = statbuf.st_mtime; |
| |
| EXPECT_EQ(0, HMX_UPGRADE_NVRAM_Write(0, (unsigned char*)"\0\0\0", 3)); |
| unsigned char buff[255]; |
| HMX_UPGRADE_NVRAM_Read(0, buff, 10); |
| EXPECT_EQ(0, memcmp((char*)buff, "\0\0\0\0\0\0\0\0\0\0", 10)); |
| memset(buff, 0, sizeof(buff)); |
| |
| EXPECT_NE(-1, stat(hnvramFileName, &statbuf)); |
| time_t after = statbuf.st_mtime; |
| |
| EXPECT_EQ(before, after); |
| |
| // verify writes with read and HMX_Read |
| HMX_UPGRADE_NVRAM_Init(hnvramFileName); |
| int fp = open(hnvramFileName, O_RDONLY); |
| |
| EXPECT_EQ(0, HMX_UPGRADE_NVRAM_Write(offset, writeData, writeLen)); |
| |
| EXPECT_EQ(offset, lseek(fp, offset, SEEK_SET)); |
| EXPECT_EQ(writeLen, read(fp, buff, writeLen)); |
| EXPECT_EQ(0, memcmp((char*)buff, (char*)writeData, writeLen)); |
| memset(buff, 0, sizeof(buff)); |
| |
| EXPECT_EQ(0, HMX_UPGRADE_NVRAM_Read(offset, buff, writeLen)); |
| EXPECT_EQ(0, memcmp((char*)buff, (char*)writeData, writeLen)); |
| |
| close(fp); |
| } |
| |
| TEST_P(HnvramFlashTest, TestFlashRead) { |
| unsigned char read_buffer[10]; |
| // bad realpath |
| HMX_UPGRADE_NVRAM_Init("/fake/doesnotexist915"); |
| EXPECT_EQ(-1, HMX_UPGRADE_NVRAM_Read(0, read_buffer, 0)); |
| |
| // Can't open file (because its a directory) |
| HMX_UPGRADE_NVRAM_Init("/tmp"); |
| EXPECT_EQ(-1, HMX_UPGRADE_NVRAM_Read(0, read_buffer, 0)); |
| |
| // Can't read file because permissions |
| HMX_UPGRADE_NVRAM_Init("/proc/version"); |
| EXPECT_EQ(-1, HMX_UPGRADE_NVRAM_Read(0, read_buffer, 10000000)); |
| |
| // File should be completely empty |
| HMX_UPGRADE_NVRAM_Init(hnvramFileName); |
| unsigned char buff[255]; |
| |
| EXPECT_EQ(0, HMX_UPGRADE_NVRAM_Read(offset, buff, writeLen)); |
| EXPECT_EQ(0, memcmp((char*)buff, "\0\0\0\0\0\0\0\0\0\0", writeLen)); |
| memset((char*)buff, 0, sizeof(buff)); |
| |
| // Should be able to HMX_Read data from raw C writes |
| int fp = open(hnvramFileName, O_WRONLY); |
| EXPECT_EQ(offset, lseek(fp, offset, SEEK_SET)); |
| EXPECT_EQ(writeLen, write(fp, writeData, writeLen)); |
| close(fp); |
| |
| EXPECT_EQ(0, HMX_UPGRADE_NVRAM_Read(offset, buff, writeLen)); |
| EXPECT_EQ(0, memcmp((char*)buff, writeData, writeLen)); |
| } |
| |
| INSTANTIATE_TEST_CASE_P(TryAllOffsets, HnvramFlashTest, |
| ::testing::ValuesIn(offsets)); |
| |
| int main(int argc, char** argv) { |
| ::testing::InitGoogleTest(&argc, argv); |
| ::testing::AddGlobalTestEnvironment(new HnvramEnvironment()); |
| return RUN_ALL_TESTS(); |
| } |