Home modules.gotpike.org
Username: Password: [Create Account]
[Forgot Password?]

Modules

ADT
Database
GTK2
GUI
IP
PiJAX
Public
Sql
Stdio
Subversion
System
Tools
Xosd
lua
v4l2
wx

Recent Changes

Public.USB 1.0
Public.Parser.XML2 1.50
Public.ZeroMQ 1.1
Public.Template.Mustache 1.0
Public.Protocols.XMPP 1.4

Popular Downloads

Public.Parser.JSON2 1.0
Public.Parser.JSON 0.2
GTK2 2.23
Public.Web.FCGI 1.8
Public.Parser.XML2 1.48


Module Information
Public.USB
Viewing contents of Public_USB-1.0/testsuite.in

START_MARKER
// -*- pike -*-
// Pike USB Module Test Suite
// This file is preprocessed by m4 and then run by pike -x test_pike

// ============================================================================
// Module Structure Tests
// ============================================================================

// Verify that the module object exists
test_true(objectp(Public.USB))

// Verify that the USB class exists
test_true(programp(Public.USB.USB))

// Verify that the Device class exists
test_true(programp(Public.USB.Device))

// Verify that expected module functions exist
test_true(functionp(Public.USB.get_vendor_name))

// ============================================================================
// get_vendor_name() Functionality Tests
// ============================================================================

// Test return type for known vendor
test_true(stringp(Public.USB.get_vendor_name(0x046d)))

// Test return value for unknown vendor (should be 0)
test_eq(Public.USB.get_vendor_name(0xFFFF), 0)

// Test specific vendor lookups
test_eq(Public.USB.get_vendor_name(0x046d), [["Logitech, Inc."]])
test_eq(Public.USB.get_vendor_name(0x05ac), [["Apple, Inc."]])
test_eq(Public.USB.get_vendor_name(0x8086), "Intel Corp.")
test_eq(Public.USB.get_vendor_name(0x0bda), "Realtek Semiconductor Corp.")
test_eq(Public.USB.get_vendor_name(0x1d6b), "Linux Foundation")
test_eq(Public.USB.get_vendor_name(0x04b4), "Cypress Semiconductor Corp.")

// Test vendor lookup with small VID
test_true(stringp(Public.USB.get_vendor_name(0x0001)))

// Test boundary case (VID 0x0000)
test_true(stringp(Public.USB.get_vendor_name(0x0000)) || 
          Public.USB.get_vendor_name(0x0000) == 0)

// ============================================================================
// Caching Tests
// ============================================================================

// Verify that repeated lookups return identical results (tests caching)
test_do([[
  string first = Public.USB.get_vendor_name(0x046d);
  string second = Public.USB.get_vendor_name(0x046d);
  if (first != second) error("Cache inconsistency\n");
]])

// Verify that database loads successfully (at least one major vendor found)
test_true(stringp(Public.USB.get_vendor_name(0x046d)) ||
          stringp(Public.USB.get_vendor_name(0x05ac)) ||
          stringp(Public.USB.get_vendor_name(0x8086)))

// ============================================================================
// USB Class Tests (libusb-dependent)
// ============================================================================

// Test USB class instantiation (may fail if libusb not available)
test_any([[
  mixed err = catch {
    object usb = Public.USB.USB();
    return 1;
  };
  // If libusb is not available, expect an error
  // If it is available, should return 1
  // Either case is valid for this test
  return err ? 1 : 1;
]], 1)

// Test get_device_list returns an array
test_any([[
  mixed err = catch {
    object usb = Public.USB.USB();
    array devices = usb->get_device_list();
    return arrayp(devices) ? 1 : 0;
  };
  // If libusb not available, error is expected
  return err ? 1 : 1;
]], 1)

// Test USB speed constants exist (if libusb available)
test_any([[
  mixed err = catch {
    object usb = Public.USB.USB();
    // Check that speed constants exist
    return usb->SPEED_UNKNOWN >= 0 ? 1 : 0;
  };
  // If libusb not available, error is expected, return success
  return err ? 1 : 1;
]], 1)

// ============================================================================
// Device Class Tests
// ============================================================================

// Verify Device class exists (but should not be instantiated directly)
// Device objects should only be created by USB class methods
test_true(programp(Public.USB.Device))

// Test Device methods exist when devices are available
test_any([[
  mixed err = catch {
    object usb = Public.USB.USB();
    array devices = usb->get_device_list();
    if (sizeof(devices) > 0) {
      object dev = devices[0];
      return functionp(dev->open) && 
             functionp(dev->close) &&
             functionp(dev->get_bus_number) &&
             functionp(dev->get_device_address) &&
             functionp(dev->get_speed) &&
             functionp(dev->get_descriptor) &&
             functionp(dev->kernel_driver_active) &&
             functionp(dev->detach_kernel_driver) &&
             functionp(dev->attach_kernel_driver) &&
             functionp(dev->claim_interface) &&
             functionp(dev->release_interface);
    }
    return 1;  // No devices available, test passes
  };
  return err ? 1 : 1;
]], 1)

// Test open/close cycle if devices available
test_do([[
  mixed err = catch {
    object usb = Public.USB.USB();
    array devices = usb->get_device_list();
    if (sizeof(devices) > 0) {
      object dev = devices[0];
      // Try to open (may fail due to permissions)
      mixed open_err = catch { dev->open(); };
      if (!open_err) {
        // If open succeeded, test close
        dev->close();
        // Test double close (should be safe)
        dev->close();
      }
    }
  };
  // Test passes regardless - we're just checking it doesn't crash
]])

// Test kernel driver methods if device available and open
test_do([[
  mixed err = catch {
    object usb = Public.USB.USB();
    array devices = usb->get_device_list();
    if (sizeof(devices) > 0) {
      object dev = devices[0];
      mixed open_err = catch { dev->open(); };
      if (!open_err) {
        // Just verify the method is callable
        // May fail due to permissions or platform differences
        mixed check_err = catch { 
          int active = dev->kernel_driver_active(0); 
        };
        dev->close();
      }
    }
  };
  // Test passes regardless - just checking it's callable
]])

// ============================================================================
// Backend Integration Tests
// ============================================================================

// Test enable_backend_events() method exists and is callable
test_true(functionp(Public.USB.USB()->enable_backend_events))

// Test enabling backend events (should not crash)
test_do([[
  object usb = Public.USB.USB();
  usb->enable_backend_events(1);
  // Should not crash or error
]])

// Test disabling backend events (should not crash)
test_do([[
  object usb = Public.USB.USB();
  usb->enable_backend_events(1);
  usb->enable_backend_events(0);
  // Should not crash or error
]])

// Test multiple USB instances with backend events
test_do([[
  object usb1 = Public.USB.USB();
  object usb2 = Public.USB.USB();
  object usb3 = Public.USB.USB();
  
  // Enable on all three
  usb1->enable_backend_events(1);
  usb2->enable_backend_events(1);
  usb3->enable_backend_events(1);
  
  // Disable on middle one
  usb2->enable_backend_events(0);
  
  // Should not crash - registry should handle multiple contexts
]])

// Test cleanup - enable then destroy object
test_do([[
  {
    object usb = Public.USB.USB();
    usb->enable_backend_events(1);
    // usb goes out of scope here, should cleanup properly
  }
  // If we get here without crash, cleanup worked
]])

// Test re-enabling after disable
test_do([[
  object usb = Public.USB.USB();
  usb->enable_backend_events(1);
  usb->enable_backend_events(0);
  usb->enable_backend_events(1);
  usb->enable_backend_events(0);
  // Should handle enable/disable cycling
]])

// Test backend events with device operations
test_do([[
  mixed err = catch {
    object usb = Public.USB.USB();
    usb->enable_backend_events(1);
    array devices = usb->get_device_list();
    // Just verify backend doesn't interfere with normal operations
  };
  // Test passes - backend should not break normal functionality
]])

// Test that backend events default to disabled
test_do([[
  object usb = Public.USB.USB();
  // Default should be disabled - this test just verifies
  // creating a USB object doesn't auto-enable backend
  // (No direct way to query state, but no crash = pass)
]])

// ============================================================================
// Configuration Descriptor Tests
// ============================================================================

// Test get_config_descriptor() method exists and is callable
test_do([[
  object usb = Public.USB.USB();
  array devices = usb->get_device_list();
  if (sizeof(devices) > 0) {
    mixed err = catch {
      mapping config = devices[0]->get_config_descriptor(0);
      // If we get here without error, function works
    };
    // Test passes - just verifying it's callable
  }
]])

END_MARKER


gotpike.org | Copyright © 2004 - 2019 | Pike is a trademark of Department of Computer and Information Science, Linköping University