wangzhibo
2026-08-16 ae6f40460dcd56af6c5f60ba52c883854c3bac55
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package nodomain.freeyourgadget.gadgetbridge.test;
 
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Assert;
import org.junit.Test;
 
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
 
import nodomain.freeyourgadget.gadgetbridge.util.GBZipFile;
import nodomain.freeyourgadget.gadgetbridge.util.ZipFileException;
 
public class GBZipFileTest extends TestBase {
    private static final String TEST_FILE_NAME = "manifest.json";
    private static final String TEST_NESTED_FILE_NAME = "directory/manifest.json";
    private static final String TEST_FILE_CONTENTS_1 = "{ \"mykey\": \"myvalue\", \"myarr\": [0, 1, 2, 3] }";
    private static final String TEST_FILE_CONTENTS_2 = "{\n" +
        "    \"manifest\": {\n" +
        "        \"application\": {\n" +
        "            \"bin_file\": \"pinetime-mcuboot-app-image-1.10.0.bin\",\n" +
        "            \"dat_file\": \"pinetime-mcuboot-app-image-1.10.0.dat\",\n" +
        "            \"init_packet_data\": {\n" +
        "                \"application_version\": 4294967295,\n" +
        "                \"device_revision\": 65535,\n" +
        "                \"device_type\": 82,\n" +
        "                \"firmware_crc16\": 21770,\n" +
        "                \"softdevice_req\": [\n" +
        "                    65534\n" +
        "                ]\n" +
        "            }\n" +
        "        },\n" +
        "        \"dfu_version\": 0.5\n" +
        "    }\n" +
        "}";
 
    @Test
    public void testZipSize1() throws IOException, ZipFileException {
        final String contents = TEST_FILE_CONTENTS_1;
 
        byte[] zipArchive = createZipArchive(TEST_FILE_NAME, contents);
 
        GBZipFile zipFile = new GBZipFile(zipArchive);
        String readContents = new String(zipFile.getFileFromZip(TEST_FILE_NAME));
 
        Assert.assertEquals(contents, readContents);
    }
 
    @Test
    public void testZipSize2() throws IOException, ZipFileException {
        final String contents = TEST_FILE_CONTENTS_2;
 
        byte[] zipArchive = createZipArchive(TEST_FILE_NAME, contents);
 
        GBZipFile zipFile = new GBZipFile(zipArchive);
        String readContents = new String(zipFile.getFileFromZip(TEST_FILE_NAME));
 
        Assert.assertEquals(contents, readContents);
    }
 
    @Test
    public void testZipSize3() throws IOException, ZipFileException, JSONException {
        String contents = makeLargeJsonObject(new JSONObject(TEST_FILE_CONTENTS_2), 4).toString(4);
 
        byte[] zipArchive = createZipArchive(TEST_FILE_NAME, contents);
 
        GBZipFile zipFile = new GBZipFile(zipArchive);
        String readContents = new String(zipFile.getFileFromZip(TEST_FILE_NAME));
 
        Assert.assertEquals(contents, readContents);
    }
 
    @Test
    public void testZipSize4() throws IOException, ZipFileException, JSONException {
        String contents = makeLargeJsonObject(new JSONObject(TEST_FILE_CONTENTS_2), 32).toString(4);
 
        byte[] zipArchive = createZipArchive(TEST_FILE_NAME, contents);
 
        GBZipFile zipFile = new GBZipFile(zipArchive);
        String readContents = new String(zipFile.getFileFromZip(TEST_FILE_NAME));
 
        Assert.assertEquals(contents, readContents);
    }
 
    @Test
    public void testZipFileInDir() throws IOException, ZipFileException {
        String contents = TEST_FILE_CONTENTS_1;
 
        byte[] zipArchive = createZipArchive(TEST_NESTED_FILE_NAME, contents);
 
        GBZipFile zipFile = new GBZipFile(zipArchive);
        String readContents = new String(zipFile.getFileFromZip(TEST_NESTED_FILE_NAME));
 
        Assert.assertEquals(contents, readContents);
    }
 
    @Test
    public void testZipFilesUnorderedAccess() throws IOException, ZipFileException {
        String contents1 = TEST_FILE_CONTENTS_1;
        String contents2 = TEST_FILE_CONTENTS_2;
        String contents3 = "zbuMyWvIxeKgcWnsSYOd8CTLgjc9x7ti21OlLlGduMJVXlKc835WEUKJ3xR6GDA5d0tHSXnYxZkDlznFQVyueHhwYywsMO9PlkJqjOCA2Mn8uTuTliIKUNPBraFipOodb6rW31HdKLOd7gmniLF5mvdRPHOUKIXSMciqogOsZnvGXylMx6TegesGBWAeHFhTSQ5xXOrOEUsDHK78M3A0yFXzLE0XgwI90Tl87OHyWfE0y0yINv5PjxgGCLUB7mHYFpgPW1C5yyIkb2JA6CePE3hHv369khwmLumW7P9ErZhzdGgeskz6Os0p5HMrTFuySc0PWxsIfru1HldIH9TZTSMCbd91G5jCCikyx2zrzDKaasuQZyBGZcMjr1zcCLpPQiKT7ELSoUBCKhiFODxbFA06MC5bLXh2WvyP8W2kVxT2T4AnDX6pwf1BKs4nbHpAjvMmHrzlhQp7Q6VWBEiniY5M9QW4ExRcMGIBYXvY7vu5p";
 
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        final ZipOutputStream zipWriteStream = new ZipOutputStream(baos);
 
        writeFileToZip(contents1, "file1", zipWriteStream);
        writeFileToZip(contents2, "file2", zipWriteStream);
        writeFileToZip(contents3, "file3", zipWriteStream);
        zipWriteStream.close();
 
        GBZipFile zipFile = new GBZipFile(baos.toByteArray());
        String readContents2 = new String(zipFile.getFileFromZip("file2"));
        String readContents1 = new String(zipFile.getFileFromZip("file1"));
        String readContents3 = new String(zipFile.getFileFromZip("file3"));
 
        Assert.assertEquals(contents1, readContents1);
        Assert.assertEquals(contents2, readContents2);
        Assert.assertEquals(contents3, readContents3);
    }
 
    @Test
    public void testZipFilesFileExists() throws IOException, ZipFileException {
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        final ZipOutputStream zipWriteStream = new ZipOutputStream(baos);
 
        writeFileToZip(TEST_FILE_CONTENTS_1, "file1", zipWriteStream);
        writeFileToZip(TEST_FILE_CONTENTS_2, "file2", zipWriteStream);
        writeFileToZip("Hello, World!", "folder1/file3", zipWriteStream);
        zipWriteStream.close();
 
        final GBZipFile zipFile = new GBZipFile(baos.toByteArray());
        Assert.assertTrue(zipFile.fileExists("file2"));
        Assert.assertTrue(zipFile.fileExists("file1"));
        Assert.assertTrue(zipFile.fileExists("folder1/file3"));
        Assert.assertFalse(zipFile.fileExists("folder1"));
        Assert.assertFalse(zipFile.fileExists("file4"));
    }
 
    /**
     * Create a ZIP archive with a single text file.
     * The archive will not be saved to a file, it is kept in memory.
     *
     * @return the ZIP archive
     */
    private byte[] createZipArchive(String path, String fileContents) throws IOException {
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        final ZipOutputStream zipFile = new ZipOutputStream(baos);
 
        writeFileToZip(fileContents, path, zipFile);
        zipFile.close();
 
        return baos.toByteArray();
    }
 
    /**
     * Make a larger JSON object for testing purposes, based on a preexisting JSON object.
     */
    private JSONObject makeLargeJsonObject(JSONObject base, int repetitions) throws JSONException {
        JSONObject manifestObj = base.getJSONObject("manifest");
        JSONArray array = new JSONArray();
 
        for (int i = 0; i < repetitions; i++) {
            array.put(manifestObj);
        }
 
        return base.put("array", array);
    }
 
    /**
     * Write given data to file at given path into an already opened ZIP archive.
     * Allows to create an archive with multiple files.
     */
    private void writeFileToZip(String fileContents, String path, ZipOutputStream zipFile) throws IOException {
        byte[] data = fileContents.getBytes(StandardCharsets.UTF_8);
 
        ZipEntry zipEntry = new ZipEntry(path);
        zipFile.putNextEntry(zipEntry);
        zipFile.write(data, 0, fileContents.length());
        zipFile.closeEntry();
    }
}