| // Copyright 2016 Google Inc. All Rights Reserved. |
| // Author: germuth@google.com (Aaron Germuth) |
| |
| // Humax Hnvram Base Testing Class |
| |
| #include <stdlib.h> |
| #include <stdio.h> |
| #include <fcntl.h> |
| #include "gtest/gtest.h" |
| |
| #define TEST_MAIN |
| |
| // Tests can change this variable to temporarily write to another file |
| // Value restored to staticFileName at end of test |
| const char* hnvramFileName; |
| const char* staticFileName; |
| const int hnvramFileSize = 2048000; |
| |
| // Helper Testing Methods |
| int hnvram_empty() { |
| unsigned char buff[hnvramFileSize + 1]; |
| int fp = open(hnvramFileName, O_RDONLY); |
| EXPECT_GT(fp, 0); |
| EXPECT_EQ(hnvramFileSize, read(fp, buff, hnvramFileSize)); |
| EXPECT_EQ(0, close(fp)); |
| |
| for(int i = 0; i < hnvramFileSize; i++) { |
| if (buff[i] != '\0') { |
| return 0; |
| } |
| } |
| return 1; |
| } |
| |
| int delete_hnvram_file() { |
| char cmd[30]; |
| sprintf(cmd, "rm -f %s", hnvramFileName); |
| int ret = system(cmd); |
| if (ret != 0) { |
| fprintf(stderr, "Failed to delete hnvram partition file: %s\n", |
| hnvramFileName); |
| } |
| return ret; |
| } |
| |
| // Base testing class |
| class HnvramTest : public ::testing::Test { |
| public: |
| HnvramTest() {} |
| virtual ~HnvramTest() {} |
| |
| virtual void SetUp() { |
| hnvramFileName = staticFileName; |
| |
| // Create new blank hnvram partition |
| char cmd[255]; |
| sprintf(cmd, "dd if=/dev/zero of=%s bs=1024 count=2KB >& /dev/null", |
| hnvramFileName); |
| int ret = system(cmd); |
| |
| if (ret != 0) { |
| fprintf(stderr, "Failed to create hnvram partition file: %s\n", |
| hnvramFileName); |
| } |
| |
| EXPECT_TRUE(hnvram_empty()); |
| } |
| |
| virtual void TearDown() { |
| delete_hnvram_file(); |
| } |
| }; |
| |
| class HnvramEnvironment : public ::testing::Environment { |
| public: |
| virtual ~HnvramEnvironment() {} |
| virtual void SetUp() { |
| // Get Unique File Name |
| // use mktemp instead of mkstemp, since we don't want to open file yet |
| char fileName[255] = "/tmp/hnvram-test-XXXXXX"; |
| mktemp(fileName); |
| if (fileName[0] == '\0') { |
| fprintf(stderr, "Failed to create unique file name\n"); |
| } |
| staticFileName = strdup(fileName); |
| } |
| |
| virtual void TearDown() { |
| // Just incase test case was interrupted, delete file here too |
| delete_hnvram_file(); |
| } |
| }; |