File: test_shared.cpp

package info (click to toggle)
libappimage 0.1.9%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,556 kB
  • sloc: ansic: 3,262; cpp: 1,049; sh: 40; makefile: 9
file content (94 lines) | stat: -rw-r--r-- 2,419 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <gtest/gtest.h>
#include <fstream>
#include <ftw.h>
#include <string>
#include <sstream>
#include <sys/stat.h>
#include <unistd.h>

#include "fixtures.h"

extern "C" {
    #include <appimage/appimage_shared.h>
}


using namespace std;


// most simple derivative class for better naming of the tests in this file
class LibAppImageSharedTest : public TestBase {};


static bool test_strcmp(char* a, char* b) {
    return strcmp(a, b) == 0;
}

TEST_F(LibAppImageSharedTest, test_appimage_hexlify) {
    {
        char bytesIn[] = "\x00\x01\x02\x03\x04\x05\x06\x07";
        char expectedHex[] = "0001020304050607";

        char* hexlified = appimage_hexlify(bytesIn, 8);
        EXPECT_PRED2(test_strcmp, hexlified, expectedHex);

        // cleanup
        free(hexlified);
    }
    {
        char bytesIn[] = "\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff";
        char expectedHex[] = "f8f9fafbfcfdfeff";

        char* hexlified = appimage_hexlify(bytesIn, 8);
        EXPECT_PRED2(test_strcmp, hexlified, expectedHex);

        // cleanup
        free(hexlified);
    }
}


bool isPowerOfTwo(int number) {
    return (number & (number - 1)) == 0;
}


TEST_F(LibAppImageSharedTest, test_appimage_get_elf_section_offset_and_length) {
    std::string appImagePath = std::string(TEST_DATA_DIR) + "/appimaged-i686.AppImage";

    unsigned long offset, length;

    ASSERT_TRUE(appimage_get_elf_section_offset_and_length(appImagePath.c_str(), ".upd_info", &offset, &length));

    EXPECT_GT(offset, 0);
    EXPECT_GT(length, 0);

    EXPECT_PRED1(isPowerOfTwo, length);
}


TEST_F(LibAppImageSharedTest, test_print_binary) {
    std::string appImagePath = std::string(TEST_DATA_DIR) + "/appimaged-i686.AppImage";

    unsigned long offset, length;

    ASSERT_TRUE(appimage_get_elf_section_offset_and_length(appImagePath.c_str(), ".upd_info", &offset, &length));

    EXPECT_EQ(appimage_print_binary(appImagePath.c_str(), offset, length), 0);
}


TEST_F(LibAppImageSharedTest, test_print_hex) {
    std::string appImagePath = std::string(TEST_DATA_DIR) + "/appimaged-i686.AppImage";

    unsigned long offset, length;

    ASSERT_TRUE(appimage_get_elf_section_offset_and_length(appImagePath.c_str(), ".sha256_sig", &offset, &length));

    EXPECT_EQ(appimage_print_hex(appImagePath.c_str(), offset, length), 0);
}

int main(int argc, char **argv) {
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}