| 1 | #include "NetworkUpdater.h"
|
|---|
| 2 |
|
|---|
| 3 | #include <string>
|
|---|
| 4 | #include <iostream>
|
|---|
| 5 | #include <cassert>
|
|---|
| 6 | #include <cstdio>
|
|---|
| 7 |
|
|---|
| 8 | #ifdef TEXT
|
|---|
| 9 | # define _T(x) TEXT(x)
|
|---|
| 10 | #else
|
|---|
| 11 | # define _T(x) x
|
|---|
| 12 | typedef char TCHAR;
|
|---|
| 13 | #endif
|
|---|
| 14 |
|
|---|
| 15 | #define CURRENT_VERSION 200
|
|---|
| 16 | #define CONTEXT_URL _T("http://www.lunaport.net/test/updates.xml")
|
|---|
| 17 |
|
|---|
| 18 | void Test_NU_StartSearch() {
|
|---|
| 19 | NU_Option valid_option;
|
|---|
| 20 | memset(&valid_option, 0, sizeof(valid_option));
|
|---|
| 21 | valid_option.url = CONTEXT_URL;
|
|---|
| 22 |
|
|---|
| 23 | assert(!NU_StartSearch(NULL));
|
|---|
| 24 |
|
|---|
| 25 | {
|
|---|
| 26 | NU_NetworkUpdater object = {0};
|
|---|
| 27 | assert(!NU_StartSearch(&object));
|
|---|
| 28 | }
|
|---|
| 29 |
|
|---|
| 30 | {
|
|---|
| 31 | NU_NetworkUpdater object;
|
|---|
| 32 | assert(NU_Create(&object, &valid_option));
|
|---|
| 33 | assert(NU_StartSearch(&object));
|
|---|
| 34 | assert(NU_Destroy(&object));
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | {
|
|---|
| 38 | NU_NetworkUpdater object1, object2;
|
|---|
| 39 | assert(NU_Create(&object1, &valid_option));
|
|---|
| 40 | assert(NU_StartSearch(&object1));
|
|---|
| 41 | object2 = object1;
|
|---|
| 42 | assert(NU_Destroy(&object2));
|
|---|
| 43 | assert(!NU_StartSearch(&object1));
|
|---|
| 44 | assert(object1.error = NU_ERROR_INVALID_OPTION);
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | {
|
|---|
| 48 | NU_NetworkUpdater object1, object2;
|
|---|
| 49 | assert(NU_Create(&object1, &valid_option));
|
|---|
| 50 | object2 = object1;
|
|---|
| 51 | assert(NU_StartSearch(&object2));
|
|---|
| 52 | assert(NU_Destroy(&object2));
|
|---|
| 53 | assert(!NU_StartSearch(&object1));
|
|---|
| 54 | assert(object1.error = NU_ERROR_INVALID_OPTION);
|
|---|
| 55 | assert(!NU_StartSearch(&object2));
|
|---|
| 56 | assert(object2.error = NU_ERROR_INVALID_OPTION);
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | {
|
|---|
| 60 | NU_NetworkUpdater object, object2;
|
|---|
| 61 | NU_Option o = {0};
|
|---|
| 62 | o.url = _T("http://12345.67890.09876.54321");
|
|---|
| 63 | assert(NU_Create(&object, &o));
|
|---|
| 64 | assert(NU_StartSearch(&object));
|
|---|
| 65 | do {
|
|---|
| 66 | NU_GetStatus(&object);
|
|---|
| 67 | } while (object.pending);
|
|---|
| 68 | assert(object.error == NU_ERROR_NETWORK);
|
|---|
| 69 | assert(!NU_GetStatus(&object));
|
|---|
| 70 | assert(object.error == NU_ERROR_NETWORK);
|
|---|
| 71 | object2 = object;
|
|---|
| 72 | assert(!NU_GetStatus(&object2));
|
|---|
| 73 | assert(object2.error == NU_ERROR_NETWORK);
|
|---|
| 74 | assert(NU_Destroy(&object));
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | {
|
|---|
| 78 | NU_NetworkUpdater object;
|
|---|
| 79 | NU_Option o = {0};
|
|---|
| 80 | o.url = _T("http://example.com");
|
|---|
| 81 | assert(NU_Create(&object, &o));
|
|---|
| 82 | assert(NU_StartSearch(&object));
|
|---|
| 83 | do {
|
|---|
| 84 | NU_GetStatus(&object);
|
|---|
| 85 | } while (object.pending);
|
|---|
| 86 | assert(object.error == NU_ERROR_INVALID_CONFIG);
|
|---|
| 87 | assert(NU_Destroy(&object));
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | {
|
|---|
| 91 | NU_NetworkUpdater object;
|
|---|
| 92 | assert(NU_Create(&object, &valid_option));
|
|---|
| 93 | assert(NU_StartSearch(&object));
|
|---|
| 94 | do {
|
|---|
| 95 | assert(NU_GetStatus(&object));
|
|---|
| 96 | } while (object.pending);
|
|---|
| 97 | assert(NU_Destroy(&object));
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | void Test_NU_Create() {
|
|---|
| 103 | TCHAR url[] = _T("http://aaaaaaaaa");
|
|---|
| 104 | NU_Option o = {0};
|
|---|
| 105 | assert(!NU_Create(NULL, &o));
|
|---|
| 106 |
|
|---|
| 107 | {
|
|---|
| 108 | NU_NetworkUpdater object;
|
|---|
| 109 | assert(!NU_Create(&object, NULL));
|
|---|
| 110 | assert(object.error == NU_ERROR_INVALID_OPTION);
|
|---|
| 111 | assert(!NU_Create(&object, &o));
|
|---|
| 112 | assert(object.error == NU_ERROR_INVALID_OPTION);
|
|---|
| 113 | o.url = url;
|
|---|
| 114 | assert(NU_Create(&object, &o));
|
|---|
| 115 | assert(NU_Destroy(&object));
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | {
|
|---|
| 119 | NU_NetworkUpdater* s = (NU_NetworkUpdater*)calloc(sizeof(NU_NetworkUpdater), 1);
|
|---|
| 120 | assert(!NU_StartSearch(s));
|
|---|
| 121 | assert(s->error == NU_ERROR_INVALID_UPDATER);
|
|---|
| 122 | assert(!NU_StartUpdate(s));
|
|---|
| 123 | assert(s->error == NU_ERROR_INVALID_UPDATER);
|
|---|
| 124 | assert(!NU_Cancel(s));
|
|---|
| 125 | assert(s->error == NU_ERROR_INVALID_UPDATER);
|
|---|
| 126 | assert(!NU_Destroy(s));
|
|---|
| 127 | assert(s->error == NU_ERROR_INVALID_UPDATER);
|
|---|
| 128 | free(s);
|
|---|
| 129 | }
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | int main(int argc, char** argv) {
|
|---|
| 133 | NU_NetworkUpdater updater;
|
|---|
| 134 | NU_Option option;
|
|---|
| 135 | int result = 1;
|
|---|
| 136 |
|
|---|
| 137 | using namespace std;
|
|---|
| 138 |
|
|---|
| 139 | /* ���炩�̈��n���ꂽ�ꍇ�A�����\�����ďI�� */
|
|---|
| 140 | if (argc > 1) {
|
|---|
| 141 | int i = 0;
|
|---|
| 142 | for (i = 0; i < argc; ++i) {
|
|---|
| 143 | printf("argv[%d]=%s\n", i, argv[i]);
|
|---|
| 144 | }
|
|---|
| 145 | getchar();
|
|---|
| 146 | return 0;
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | /*
|
|---|
| 150 | Test_NU_Create();
|
|---|
| 151 | Test_NU_StartSearch();
|
|---|
| 152 | InternalTest();
|
|---|
| 153 | */
|
|---|
| 154 |
|
|---|
| 155 | /* NU_NetworkUpdater�̍\���̂����/
|
|---|
| 156 | memset(&option, 0, sizeof(option));
|
|---|
| 157 | option.current_revision = 123;
|
|---|
| 158 | option.url = _T("http://www.lunaport.net/test/updates.xml");
|
|---|
| 159 | if (!NU_Create(&updater, &option)) {
|
|---|
| 160 | fprintf(stderr, "error1: %d\n", updater.error);
|
|---|
| 161 | goto clean_up;
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | /* �X�V�v���O������� */
|
|---|
| 165 | if (!NU_StartSearch(&updater)) {
|
|---|
| 166 | fprintf(stderr, "error2: %d\n", updater.error);
|
|---|
| 167 | goto clean_up;
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | do { /* �X�V�v���O��������ۂ܂ł������� */
|
|---|
| 171 | if (!NU_GetStatus(&updater)) {
|
|---|
| 172 | fprintf(stderr, "error3: %d\n", updater.error);
|
|---|
| 173 | goto clean_up;
|
|---|
| 174 | }
|
|---|
| 175 | printf("NU_StartSearch(): "
|
|---|
| 176 | "list.downloaded=%dbytes, description.downloaded=%dbytes\n",
|
|---|
| 177 | updater.list.downloaded,
|
|---|
| 178 | updater.description.downloaded
|
|---|
| 179 | );
|
|---|
| 180 | } while (updater.pending);
|
|---|
| 181 |
|
|---|
| 182 | if (updater.error) {
|
|---|
| 183 | fprintf(stderr, "error4: %d\n", updater.error);
|
|---|
| 184 | goto clean_up;
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | /* �X�V�v���O��������������ꍇ�͏I�� */
|
|---|
| 188 | if (!updater.found) {
|
|---|
| 189 | result = 0;
|
|---|
| 190 | printf("not found\n");
|
|---|
| 191 | goto clean_up;
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 | printf("revision %d found\n", updater.new_revision);
|
|---|
| 195 |
|
|---|
| 196 | /* �A�b�v�f�[�g�J�n */
|
|---|
| 197 | if (!NU_StartUpdate(&updater)) {
|
|---|
| 198 | fprintf(stderr, "error5: %d\n", updater.error);
|
|---|
| 199 | goto clean_up;
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | do { /* �X�V�v���O�����̃_�E�����[�h�ƓW�J���I�������ő҂�*/
|
|---|
| 203 | if (!NU_GetStatus(&updater)) {
|
|---|
| 204 | fprintf(stderr, "error6: %d\n", updater.error);
|
|---|
| 205 | goto clean_up;
|
|---|
| 206 | }
|
|---|
| 207 | printf("NU_StartUpdate(): "
|
|---|
| 208 | "archive.downloaded=%dbytes\n",
|
|---|
| 209 | updater.archive.downloaded
|
|---|
| 210 | );
|
|---|
| 211 | } while (updater.pending);
|
|---|
| 212 |
|
|---|
| 213 | if (updater.error) {
|
|---|
| 214 | fprintf(stderr, "error7: %d\n", updater.error);
|
|---|
| 215 | goto clean_up;
|
|---|
| 216 | }
|
|---|
| 217 |
|
|---|
| 218 | printf("update ready\n");
|
|---|
| 219 |
|
|---|
| 220 | result = 0;
|
|---|
| 221 | clean_up:
|
|---|
| 222 | NU_Destroy(&updater);
|
|---|
| 223 |
|
|---|
| 224 | return result;
|
|---|
| 225 | }
|
|---|
| 226 |
|
|---|