summaryrefslogtreecommitdiff
path: root/testsuite/sanei/sanei_usb_test.c
blob: c2cc0c3620ba1538cf581f540f0ad2c97f86145b (plain)
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
#include "../../include/sane/config.h"

#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <math.h>
#include <stddef.h>
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_MKDIR
#include <sys/stat.h>
#include <sys/types.h>
#endif

#include <assert.h>

#define BACKEND_NAME	sanei_usb

#include "../../include/sane/sane.h"
#include "../../include/sane/sanei.h"
#include "../../include/sane/saneopts.h"

#include "../../include/sane/sanei_backend.h"
#include "../../include/sane/sanei_usb.h"

#include "../../include/_stdint.h"

/*
 * In order to avoid modifying sanei_usb.c to allow for unit tests
 * we include it so we can use its private variables and structures
 * and still test the code.
 */
#include "../../sanei/sanei_usb.c"


/** test sanei_usb_init()
 * calls sanei_usb_init
 * @param expected expected use count
 * @return 1 on success, else 0
 */
static int
test_init (int expected)
{
  /* initialize USB */
  printf ("%s starting ...\n", __func__);
  sanei_usb_init ();
  if (initialized == 0)
    {
      printf ("ERROR: sanei_usb not initialized!\n");
      return 0;
    }
  if (initialized != expected)
    {
      printf ("ERROR: incorrect use count, expected %d, got %d!\n", expected,
	      initialized);
      return 0;
    }

  printf ("sanei_usb initialized, use count is %d ...\n", initialized);
  printf ("%s success\n\n", __func__);
  return 1;
}

/** test sanei_usb_exit()
 * calls sanei_usb_exit
 * @param expected use count after exit call
 * @return 1 on success, else 0
 */
static int
test_exit (int expected)
{
  printf ("%s starting ...\n", __func__);

  /* end of USB use test */
  sanei_usb_exit ();
  if (initialized != expected)
    {
      printf ("ERROR: incorrect use count, expected %d, got %d!\n", expected,
	      initialized);
      return 0;
    }

  printf ("%s success\n\n", __func__);
  return 1;
}


/** count detected devices
 * count all detected devices and check it against expected value
 * @param expected detected count
 * @return 1 on success, else 0
 */
static int
count_detected (int expected)
{
  int num = 0;
  int i;

  for (i = 0; i < device_number; i++)
    {
      if (devices[i].missing == 0 && devices[i].devname != NULL)
	{
	  num++;
	}
    }
  if (num != expected)
    {
      printf ("ERROR: %d detected devices, expected %d!\n", num, expected);
      return 0;
    }
  printf ("%d devices still detected.\n", num);
  return 1;
}

/** create mock device
 * create a mock device entry
 * @param device device pointer to fill with mock data
 * @return nothing
 */
static void
create_mock_device (char *devname, device_list_type * device)
{
  memset (device, 0, sizeof (device_list_type));
  device->devname = strdup (devname);
  device->vendor = 0xdead;
  device->product = 0xbeef;
#if defined(HAVE_LIBUSB_LEGACY) || defined(HAVE_LIBUSB)
  device->method = sanei_usb_method_libusb;
#endif
#ifdef HAVE_USBCALLS
  device->method = sanei_usb_method_usbcalls;
#endif
#if !defined(HAVE_LIBUSB_LEGACY) && !defined(HAVE_LIBUSB) && !defined(HAVE_USBCALLS)
  device->method == sanei_usb_method_scanner_driver;
#endif
}

/** test store_device
 * test store_device for corner cases not covered by the
 * other regular use by sanei_usb_scan_devices
 * the startiing situation is that the mock device has never
 * put into device list.
 * @return 1 on success, else 0
 */
static int
test_store_device (void)
{
  int current_number;
  int expected;
  int i;
  int found;
  device_list_type mock;

  create_mock_device ("mock", &mock);

  /* first test store when there is no more room
   * to store device */
  current_number = device_number;
  device_number = MAX_DEVICES;
  /* give unused devices a name so strcmp() won't crash. */
  for (i = current_number; i < MAX_DEVICES; i++)
    devices[i].devname = "";

  store_device (mock);
  /* there should be no more devices */
  if (device_number > MAX_DEVICES)
    {
      printf ("ERROR: store past end of device list!\n");
      return 0;
    }
  /* walk device list to be sure mock device hasn't been stored */
  for (i = 0; i < MAX_DEVICES; i++)
    {
      if (devices[i].devname && !strcmp (devices[i].devname, mock.devname))
	{
	  printf
	    ("ERROR: device stored although there were no place for it!\n");
	  return 0;
	}
    }

  /* restore device_number */
  device_number = current_number;
  /* reset unused devnames to NULL */
  for (i = current_number; i < MAX_DEVICES; i++)
    devices[i].devname = NULL;
  expected = device_number + 1;

  /* store mock device */
  store_device (mock);
  found = 0;
  for (i = 0; i < MAX_DEVICES && !found; i++)
    {
      if (devices[i].devname && !strcmp (devices[i].devname, mock.devname))
	{
	  found = 1;
	}
    }
  if (device_number != expected || !found)
    {
      printf ("ERROR: mock device not stored !\n");
      return 0;
    }

  /* scan devices should mark it as missing, and device_number should decrease */
  sanei_usb_scan_devices ();
  found = 0;
  for (i = 0; i < MAX_DEVICES && !found; i++)
    {
      if (devices[i].devname
	  && devices[i].missing == 1
	  && !strcmp (devices[i].devname, mock.devname))
	{
	  found = 1;
	}
    }
  if (device_number != expected || !found)
    {
      printf ("ERROR: mock device still present !\n");
      return 0;
    }

  /* second scan devices should mark missing to 2 */
  sanei_usb_scan_devices ();
  found = 0;
  for (i = 0; i < MAX_DEVICES && !found; i++)
    {
      if (devices[i].devname
	  && devices[i].missing == 2
	  && !strcmp (devices[i].devname, mock.devname))
	{
	  found = 1;
	}
    }
  if (device_number != expected || !found)
    {
      printf ("ERROR: mock device slot not reusable !\n");
      return 0;
    }

  /* store mock device again, slot in devices should be reused
   * and device_number shouldn't change */
  create_mock_device ("mock2", &mock);
  store_device (mock);
  found = 0;
  for (i = 0; i < MAX_DEVICES && !found; i++)
    {
      if (devices[i].devname && !strcmp (devices[i].devname, mock.devname))
	{
	  found = 1;
	}
    }
  if (device_number != expected || !found)
    {
      printf ("ERROR: mock device not stored !\n");
      return 0;
    }

  /* last rescan to wipe mock device out */
  sanei_usb_scan_devices ();

  return 1;
}

/** return count of opened devices
 * @return count of opened devices
 */
static int
get_opened (void)
{
  int num = 0;
  int i;

  for (i = 0; i < device_number; i++)
    {
      if (devices[i].missing == 0 && devices[i].devname != NULL
	  && devices[i].open == SANE_TRUE)
	{
	  num++;
	}
    }
  return num;
}

/** count opened devices
 * count all opended devices and check it against expected value
 * @param expected use opened count
 * @return 1 on success, else 0
 */
static int
count_opened (int expected)
{
  int num = get_opened();

  if (num != expected)
    {
      printf ("ERROR: %d opened devices, expected %d!\n", num, expected);
      return 0;
    }
  printf ("%d devices still opened.\n", num);
  return 1;
}

/** open all devices
 * loop on all existing devices and open them
 * @param dn array to store opened device number
 * @param expected number of devices to be opened
 * @return 1 on success, else 0
 */
static int
test_open_all (SANE_Int * dn, int expected)
{
  int opened = 0;
  int i;
  int last;
  SANE_Status status;

  /* loop on detected devices and open them */
  last = -1;
  for (i = 0; i < device_number; i++)
    {
      if (devices[i].missing == 0 && devices[i].devname != NULL)
	{
	  /* open device */
	  status = sanei_usb_open (devices[i].devname, dn + opened);
	  if (status == SANE_STATUS_GOOD)
	    {
	      opened++;
	      last = i;
	    }
	  else
	    {
              if (status == SANE_STATUS_ACCESS_DENIED ||
                  status == SANE_STATUS_DEVICE_BUSY)
                {
                  expected--;
                }
              else
                {
	          printf ("ERROR: couldn't open device %s!\n",
                          devices[i].devname);
	          return 0;
                }
	    }
	}
    }
  printf ("opened %d devices\n", opened);

  /* try to reopen an opened device when there is one */
  if (last >= 0)
    {
      status = sanei_usb_open (devices[last].devname, dn + opened);
      if (status == SANE_STATUS_GOOD)
	{
	  printf ("ERROR: unexpected success when opening %s twice!\n",
		  devices[last].devname);
	  return 0;
	}
    }

  /* there should be as many opened devices than detected devices */
  return count_opened (expected);
}

/** test opening invalid device
 * try to open an non existing device
 * @return 1 on success, else 0
 */
static int
test_open_invalid (void)
{
  SANE_Status status;
  SANE_Int dn;

  status = sanei_usb_open ("invalid device", &dn);
  if (status == SANE_STATUS_GOOD)
    {
      printf ("ERROR: unexpected success opening invalid device!\n");
      return 0;
    }
  return 1;
}

/** close all devices
 * loop on all opened devices and close them
 * @param dn array of opened device number
 * @param expected number of devices to be closed
 * @return 1 on success, else 0
 */
static int
test_close_all (SANE_Int * dn, int expected)
{
  int closed = 0;
  int i;

  /* loop on detected devices and open them */
  for (i = 0; i < expected; i++)
    {
      /* close device */
      sanei_usb_close (dn[i]);
      closed++;
    }
  printf ("closed %d devices\n", closed);

  /* there should be any more opened devices */
  return count_opened (0);
}


/** claim all open devices
 * loop on all opened devices and claim interface 0
 * @param dn array of opened device number
 * @param expected number of devices to be claimed
 * @return 1 on success, else 0
 */
static int
test_claim_all (SANE_Int * dn, int expected)
{
  int claimed = 0;
  int i;
  SANE_Status status;
  device_list_type mock;

  claimed = 0;
  for (i = 0; i < expected; i++)
    {
      status = sanei_usb_claim_interface (dn[i], devices[dn[i]].interface_nr);
      if (status != SANE_STATUS_GOOD)
	{
	  printf ("ERROR: couldn't claim interface 0 on device %d!\n", dn[i]);
	}
      else
	{
	  claimed++;
	}
    }
  if (claimed != expected)
    {
      printf ("ERROR: expected %d claimed interfaces, got %d!\n", expected,
	      claimed);
      return 0;
    }
  printf ("%d devices claimed...\n\n", claimed);

  /* try to claim invalid device entry */
  status = sanei_usb_claim_interface (device_number, 0);
  if (status == SANE_STATUS_GOOD)
    {
      printf ("ERROR: could claim interface 0 on invalid device!\n");
      return 0;
    }

  /* create a mock device and make it missing by rescanning */
  create_mock_device ("mock", &mock);
  store_device (mock);
  sanei_usb_scan_devices ();

  /* try to claim interface on missing device */
  status = sanei_usb_claim_interface (device_number - 1, 0);
  if (status == SANE_STATUS_GOOD)
    {
      printf ("ERROR: could claim interface 0 on invalid device!\n");
      return 0;
    }

  /* remove mock device */
  device_number--;
  free (devices[device_number].devname);
  devices[device_number].devname = NULL;

  return 1;
}


/** release all claimed devices
 * loop on all opened devices and claim interface 0
 * @param dn array of opened device number
 * @param expected number of devices to be claimed
 * @return 1 on success, else 0
 */
static int
test_release_all (SANE_Int * dn, int expected)
{
  int released = 0;
  int i;
  SANE_Status status;
  device_list_type mock;

  released = 0;
  for (i = 0; i < expected; i++)
    {
      status =
	sanei_usb_release_interface (dn[i], devices[dn[i]].interface_nr);
      if (status != SANE_STATUS_GOOD)
	{
	  printf ("ERROR: couldn't release interface 0 on device %d!\n",
		  dn[i]);
	}
      else
	{
	  released++;
	}
    }
  if (released != expected)
    {
      printf ("ERROR: expected %d released interfaces, got %d!\n", expected,
	      released);
      return 0;
    }
  printf ("%d devices released...\n\n", released);

  /* try to release invalid device entry */
  status = sanei_usb_release_interface (device_number, 0);
  if (status == SANE_STATUS_GOOD)
    {
      printf ("ERROR: could release interface 0 on invalid device!\n");
      return 0;
    }

  /* create a mock device and make it missing by rescanning */
  create_mock_device ("mock", &mock);
  store_device (mock);
  sanei_usb_scan_devices ();

  /* try to claim interface on missing device */
  status = sanei_usb_release_interface (device_number - 1, 0);
  if (status == SANE_STATUS_GOOD)
    {
      printf ("ERROR: could release interface 0 on invalid device!\n");
      return 0;
    }

  /* remove mock device */
  device_number--;
  free (devices[device_number].devname);
  devices[device_number].devname = NULL;

  return 1;
}

/** get id for all devices names
 * loop on all existing devices and get vendor
 * and product id by name.
 * @param expected count
 * @return 1 on success, else 0
 */
static int
test_vendor_by_devname (void)
{
  int i;
  SANE_Status status;
  SANE_Word vendor, product;
  device_list_type mock;

  /* loop on detected devices and open them */
  for (i = 0; i < device_number; i++)
    {
      if (devices[i].missing == 0 && devices[i].devname != NULL)
	{
	  /* get device id */
	  status = sanei_usb_get_vendor_product_byname (devices[i].devname,
							&vendor, &product);
	  if (status != SANE_STATUS_GOOD)
	    {
	      printf ("ERROR: couldn't query device %s!\n",
		      devices[i].devname);
	      return 0;
	    }
	  if (vendor == 0 || product == 0)
	    {
	      printf ("ERROR: incomplete device id for %s!\n",
		      devices[i].devname);
	      return 0;
	    }
	  printf ("%s is %04x:%04x\n", devices[i].devname, vendor, product);
	}
    }

  /* add mock device */
  create_mock_device ("mock", &mock);
  store_device (mock);
  status = sanei_usb_get_vendor_product_byname ("mock", &vendor, &product);
  if (status != SANE_STATUS_GOOD)
    {
      printf ("ERROR: getting vendor for mock devname!\n");
      return 0;
    }
  if (vendor != mock.vendor || product != mock.product)
    {
      printf ("ERROR: wrong vendor/product for mock devname!\n");
      return 0;
    }
  /* remove mock device */
  device_number--;
  free (devices[device_number].devname);
  devices[device_number].devname = NULL;

  /* try go get id for an invalid devname */
  status = sanei_usb_get_vendor_product_byname ("invalid devname",
						&vendor, &product);
  if (status == SANE_STATUS_GOOD)
    {
      printf ("ERROR: unexpected success getting id for invalid devname!\n");
      return 0;
    }

  printf ("\n");
  return 1;
}

/** get vendor for all devices id
 * loop on all existing devices and get vendor
 * and product id.
 * @param expected count
 * @return 1 on success, else 0
 */
static int
test_vendor_by_id (void)
{
  int i;
  SANE_Status status;
  SANE_Word vendor, product;
  device_list_type mock;

  /* loop on detected devices and open them */
  for (i = 0; i < device_number; i++)
    {
      if (devices[i].missing == 0 && devices[i].devname != NULL)
	{
	  /* get device id */
	  status = sanei_usb_get_vendor_product (i, &vendor, &product);
	  if (status != SANE_STATUS_GOOD)
	    {
	      printf ("ERROR: couldn't query device %d!\n", i);
	      return 0;
	    }
	  if (vendor == 0 || product == 0)
	    {
	      printf ("ERROR: incomplete device id for %d!\n", i);
	      return 0;
	    }
	  printf ("%d is %04x:%04x\n", i, vendor, product);
	}
    }

  /* add mock device */
  create_mock_device ("mock", &mock);
  store_device (mock);
  status =
    sanei_usb_get_vendor_product (device_number - 1, &vendor, &product);
  if (status != SANE_STATUS_GOOD)
    {
      printf ("ERROR: getting vendor for mock devname!\n");
      return 0;
    }
  if (vendor != mock.vendor || product != mock.product)
    {
      printf ("ERROR: wrong vendor/product for mock devname!\n");
      return 0;
    }
  /* remove mock device */
  device_number--;
  free (devices[device_number].devname);
  devices[device_number].devname = NULL;

  /* try go get id for an invalid id */
  status =
    sanei_usb_get_vendor_product (device_number + 1, &vendor, &product);
  if (status == SANE_STATUS_GOOD)
    {
      printf
	("ERROR: unexpected success getting vendor for invalid devname!\n");
      return 0;
    }

  printf ("\n");
  return 1;
}

/** test timeout functions : libusb only
 * @return 1 on success, else 0
 */
static int
test_timeout (void)
{
#if defined(HAVE_LIBUSB_LEGACY) || defined(HAVE_LIBUSB)
  int timeout = libusb_timeout;

  sanei_usb_set_timeout (5000);
  if (libusb_timeout != 5000)
    {
      printf ("ERROR: failed to set timeout\n");
      return 1;
    }
  sanei_usb_set_timeout (timeout);
#endif
  return 1;
}

/** test device scanning
 * call sanei_usb_scan_devices, since it has no return code, no real
 * assert can be done, but at least we can test it doesn't break
 * other functions or don't leak memory
 * @return always 1
 */
static int
test_scan_devices (int detected, int opened)
{
  int rc;

  printf ("rescanning for devices ...\n");
  sanei_usb_scan_devices ();
  rc = count_detected (detected);
  if (!rc)
    {
      printf ("ERROR: scanning devices change detected count!\n");
      return 0;
    }
  rc = count_opened (opened);
  if (!rc)
    {
      printf ("ERROR: scanning devices change opened count!\n");
      return 0;
    }
  printf ("\n");
  return 1;
}


/**
 * flag for dummy attach
 */
static int dummy_flag;

/**
 * expected device name during attach
 */
static char *expected_device;

/** dummy attach function
 * dummy attach function
 * @return resturn SANE_STATUS_GOOD
 */
static SANE_Status
dummy_attach (const char *dev)
{
  dummy_flag = (strcmp (expected_device, dev) == 0);
  if (dummy_flag)
    {
      printf ("success attaching to %s...\n", dev);
    }
  else
    {
      printf ("failed attaching to %s...\n", dev);
    }
  return SANE_STATUS_GOOD;
}

/** test attaching usb device
 * create a mock device and attach to it, checking
 * if it is ok
 * @return 1 on success, else 0
 */
static int
test_attach (void)
{
  device_list_type mock;

  /* add mock device and try ot attach to it */
  dummy_flag = 0;
  create_mock_device ("mock", &mock);
  expected_device = mock.devname;
  store_device (mock);
  sanei_usb_attach_matching_devices ("usb 0xdead 0xbeef", dummy_attach);

  /* flag must be set */
  if (dummy_flag != 1)
    {
      printf ("ERROR: couldn't attach to 'usb xdead 0xbeef' device!\n");
      return 0;
    }

  /* attach by devname */
  dummy_flag = 0;
  sanei_usb_attach_matching_devices (mock.devname, dummy_attach);
  /* flag must be set */
  if (dummy_flag != 1)
    {
      printf ("ERROR: couldn't attach to 'mock' device!\n");
      return 0;
    }

  /* attach to bogus device */
  dummy_flag = 0;
  sanei_usb_attach_matching_devices ("usb 0x0001 0x0001", dummy_attach);

  /* flag must not be set */
  if (dummy_flag != 0)
    {
      printf ("ERROR: shouldn't be attached to bogus device!\n");
      return 0;
    }

  /* attach by bogus devname */
  sanei_usb_attach_matching_devices ("bogus", dummy_attach);

  /* flag must not be set */
  if (dummy_flag != 0)
    {
      printf ("ERROR: shouldn't be attached to bogus device!\n");
      return 0;
    }

  /* remove mock device */
  device_number--;
  free (devices[device_number].devname);
  devices[device_number].devname = NULL;
  dummy_flag = 0;

  return 1;
}

int
main (int __sane_unused__ argc, char **argv)
{
  int detected, opened, i;
  SANE_Int dn[MAX_DEVICES];

#ifdef HAVE_LIBUSB_LEGACY
  printf ("\n%s built with old libusb\n\n", argv[0]);
#endif
#ifdef HAVE_LIBUSB
  printf ("\n%s built with libusb-1.0\n\n", argv[0]);
#endif
#ifdef HAVE_USBCALLS
  printf ("\n%s built with usbcalls\n\n", argv[0]);
#endif
#if !defined(HAVE_LIBUSB_LEGACY) && !defined(HAVE_LIBUSB) && !defined(HAVE_USBCALLS)
  printf ("\n%s relying on deprecated scanner kernel module\n", argv[0]);
#endif

  /* start sanei_usb */
  assert (test_init (1));

  /* test timeout function */
  assert (test_timeout ());

  /* count available devices */
  detected = 0;
  for (i = 0; i < device_number; i++)
    {
      if (devices[i].missing == 0 && devices[i].devname != NULL)
	{
	  detected++;
	}
    }
  printf ("%d devices found.\n", detected);

  /* rescan devices : detected count shouldn't change */
  assert (test_scan_devices (detected, 0));

  /* test corner cases with mock device */
  assert (test_store_device ());

  /* get vendor/product id for all available devices devname */
  assert (test_vendor_by_devname ());

  /* get vendor/product id for all available devices id */
  assert (test_vendor_by_id ());

  /* open all available devices */
  assert (test_open_all (dn, detected));

  opened = get_opened();

  /* rescan devices : detected and opened count shouldn't change */
  assert (test_scan_devices (detected, opened));

  /* try to open an inexisting device */
  assert (test_open_invalid ());

  /* increase sanei _sub use count */
  assert (test_init (2));

  /* there should be still as many detected devices */
  assert (count_detected (detected));

  /* there should be still as many opened devices */
  assert (count_opened (opened));

  assert (test_exit (1));

  /* there should be still as many opened devices */
  assert (count_opened (opened));

  /* count devices again , sanei_usb_exit() shouldn't have
   * change the count */
  assert (count_detected (detected));

  /* claim all available devices */
  assert (test_claim_all (dn, opened));

  /* then release them all */
  assert (test_release_all (dn, opened));

  /* close all opened devices */
  assert (test_close_all (dn, opened));

  /* check there is no opened device */
  assert (count_opened (0));

  /* finally free resources */
  assert (test_exit (0));

  /* check there is no more devices */
  assert (count_detected (0));

  /* test attach matching device with a mock */
  assert (test_attach ());

  /* try to call sanei_usb_exit() when it not initialized */
  assert (test_exit (0));

  /* scan devices when sanei usb is not initialized */
  assert (test_scan_devices (0, 0));

  /* we re start use of sanei usb so we check we have left it
   * it he correct state after "closing" it. */
  printf ("\n============================================================\n");
  printf ("restart use of sanei usb after having freed all resources...\n\n");
  assert (test_init (1));

  /* we should have the same initial count of detected devices */
  assert (count_detected (detected));

  /* finally free resources */
  assert (test_exit (0));

  /* all the tests are OK ! */
  return 0;
}

/* vim: set sw=2 cino=>2se-1sn-1s{s^-1st0(0u0 smarttab expandtab: */